netplay

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checkpoint

type Checkpoint struct {
	Frame       uint32
	State       []byte
	Crc32       uint32
	LocalInput  uint8
	RemoteInput uint8
}

type Game

type Game struct {
	LocalJoy      *input.Joystick
	RemoteJoy     *input.Joystick
	DisasmEnabled bool
	// contains filtered or unexported fields
}

Game is a network play state manager. It keeps track of the inputs from both players and makes sure their state is synchronized.

func NewGame

func NewGame(bus *console.Bus, audio *ui.AudioOut) *Game

func (*Game) Checkpoint

func (g *Game) Checkpoint() *Checkpoint

Checkpoint returns the current checkpoint where both players are in sync. The returned value should not be modified and is only valid within the current frame.

func (*Game) Frame

func (g *Game) Frame() uint32

Frame returns the current frame number.

func (*Game) FrameDrift

func (g *Game) FrameDrift() int

func (*Game) Gen

func (g *Game) Gen() uint32

Gen returns the current generation number. It is incremented every time the game is reset.

func (*Game) HandleLocalInput

func (g *Game) HandleLocalInput(buttons uint8)

HandleLocalInput adds records and applies the input from the local player. Since the remote player is behind, it assumes that it just keeps pressing the same buttons until it catches up.

func (*Game) HandleRemoteInput

func (g *Game) HandleRemoteInput(buttons uint8, frame uint32)

HandleRemoteInput adds the input from the remote player.

func (*Game) Init

func (g *Game) Init(cp *Checkpoint)

func (*Game) Reset

func (g *Game) Reset()

func (*Game) RunFrame

func (g *Game) RunFrame(startTime time.Time)

RunFrame runs a single frame of the game.

func (*Game) SetRTT

func (g *Game) SetRTT(t time.Duration)

func (*Game) SleepFrames

func (g *Game) SleepFrames(n uint32)

func (*Game) Sleeping

func (g *Game) Sleeping() bool

Sleeping returns true if the game is currently sleeping to let the remote player catch up.

type Message

type Message struct {
	Type       MsgType
	Frame      uint32
	Generation uint32
	Payload    []byte
}

func (*Message) Decode

func (m *Message) Decode(data []byte) error

func (*Message) Encode

func (m *Message) Encode() ([]byte, error)

type MsgType

type MsgType = uint8
const (
	MsgTypeReset MsgType = iota + 1
	MsgTypeWait
	MsgTypeInput
	MsgTypePing
	MsgTypePong
	MsgTypeBye
)

type Netplay

type Netplay struct {
	// contains filtered or unexported fields
}

func Connect

func Connect(game *Game, addr string, protocol Protocol) (*Netplay, net.Addr, error)

func Listen

func Listen(game *Game, addr string, protocol Protocol) (*Netplay, net.Addr, error)

func (*Netplay) HandleMessages

func (np *Netplay) HandleMessages()

HandleMessages handles incoming messages from the remote player.

func (*Netplay) RemotePing

func (np *Netplay) RemotePing() int64

RemotePing returns the ping time to the remote peer in milliseconds.

func (*Netplay) RunFrame

func (np *Netplay) RunFrame(startTime time.Time)

RunFrame progresses the game by one frame.

func (*Netplay) SendButtons

func (np *Netplay) SendButtons(buttons uint8)

SendButtons sends the local input to the remote player. Should be called every frame.

func (*Netplay) SendBye

func (np *Netplay) SendBye()

SendBye sends a bye message to the remote player when the game is over.

func (*Netplay) SendInitialState

func (np *Netplay) SendInitialState()

SendInitialState is used by the server to send the initial state to the client.

func (*Netplay) SendPing

func (np *Netplay) SendPing()

func (*Netplay) SendReset

func (np *Netplay) SendReset()

SendReset restarts the game on both sides.

func (*Netplay) SendResync

func (np *Netplay) SendResync()

func (*Netplay) SendWait

func (np *Netplay) SendWait(frames uint32)

func (*Netplay) ShouldExit

func (np *Netplay) ShouldExit() bool

ShouldExit indicates whether the game loop should exit.

type Protocol

type Protocol int
const (
	TCP Protocol = iota
	UDP
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL