diff --git a/game/player.go b/component/player.go similarity index 52% rename from game/player.go rename to component/player.go index 2001ea9..feaa9cb 100644 --- a/game/player.go +++ b/component/player.go @@ -1,6 +1,9 @@ -package game +package component -import "image/color" +import ( + "fmt" + "image/color" +) type Player struct { X float64 @@ -9,7 +12,11 @@ type Player struct { PlayerNum int } -func (p *Player) clone() Player { +func (p *Player) String() string { + return fmt.Sprintf("Player %d: X:%f Y:%f Color: %s", p.PlayerNum, p.X, p.Y, p.Color) +} + +func (p *Player) Clone() Player { result := Player{} result.X = p.X result.Y = p.Y diff --git a/game/game.go b/game/game.go index 42d9610..5cb597b 100644 --- a/game/game.go +++ b/game/game.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" + "code.rocketnine.space/tslocum/boxbrawl/component" "code.rocketnine.space/tslocum/boxbrawl/entity" "code.rocketnine.space/tslocum/boxbrawl/system" "code.rocketnine.space/tslocum/boxbrawl/world" @@ -22,30 +23,31 @@ var backend ggpo.Backend var currentPlayer = 1 type Game struct { - Players []Player + Players []component.Player } var addedGame bool func NewGame() (*Game, error) { - var player1 = Player{ + var player1 = component.Player{ X: 50, Y: 50, Color: color.RGBA{255, 0, 0, 255}, PlayerNum: 1, } - var player2 = Player{ + var player2 = component.Player{ X: 150, Y: 50, Color: color.RGBA{0, 0, 255, 255}, PlayerNum: 2, } g := &Game{ - Players: []Player{player1, player2}, + Players: []component.Player{player1, player2}, } if !addedGame { entity.NewOnceEntity() + gohan.AddSystem(&system.PlayerSystem{}) gohan.AddSystem(&system.UISystem{}) addedGame = true } @@ -57,9 +59,9 @@ func (g *Game) clone() (result *Game) { result = &Game{} *result = *g - result.Players = make([]Player, len(g.Players)) + result.Players = make([]component.Player, len(g.Players)) for i := range g.Players { - result.Players[i] = g.Players[i].clone() + result.Players[i] = g.Players[i].Clone() } return } @@ -138,11 +140,17 @@ func (g *Game) Update() error { panic(err) } g.RunFrame() + + g.updatePlayerState() // TODO only after advanceframe? } return gohan.Update() } +func (g *Game) updatePlayerState() { + world.Player1, world.Player2 = g.Players[0], g.Players[1] +} + func (g *Game) Draw(screen *ebiten.Image) { err := gohan.Draw(screen) if err != nil { @@ -217,7 +225,12 @@ func (g *Game) RunFrame() { } func (g *Game) AdvanceFrame(inputs []InputBits, disconnectFlags int) { - log.Println("ADVANCE FRAME") + if world.ConnectPromptVisible { + // We are connected now. + + world.ConnectPromptVisible = false + log.Println("Connected successfully") + } g.UpdateByInputs(inputs) err := backend.AdvanceFrame(uint32(g.Checksum())) diff --git a/game/session.go b/game/session.go index d7e0c6b..c4568aa 100644 --- a/game/session.go +++ b/game/session.go @@ -63,10 +63,6 @@ func (g *Game) String() string { return fmt.Sprintf("%s : %s ", g.Players[0].String(), g.Players[1].String()) } -func (p *Player) String() string { - return fmt.Sprintf("Player %d: X:%f Y:%f Color: %s", p.PlayerNum, p.X, p.Y, p.Color) -} - func (g *GameSession) AdvanceFrame(flags int) { fmt.Println("Advancing frame from callback. ") var discconectFlags int diff --git a/system/player.go b/system/player.go new file mode 100644 index 0000000..7e742bd --- /dev/null +++ b/system/player.go @@ -0,0 +1,53 @@ +package system + +import ( + "image" + + "code.rocketnine.space/tslocum/boxbrawl/component" + "code.rocketnine.space/tslocum/boxbrawl/world" + "code.rocketnine.space/tslocum/gohan" + "github.com/hajimehoshi/ebiten/v2" +) + +type PlayerSystem struct { + *component.Once + + initialized bool + debugImg *ebiten.Image +} + +func (s *PlayerSystem) initialize() { +} + +func (s *PlayerSystem) Update(e gohan.Entity) error { + if !s.initialized { + s.initialize() + } + + if world.ConnectPromptVisible { + return nil + } + + return nil +} + +func (s *PlayerSystem) Draw(e gohan.Entity, screen *ebiten.Image) error { + if world.ConnectPromptVisible { + return nil + } + + size := 20 + var p *component.Player + for i := 0; i < 2; i++ { + if i == 0 { + p = &world.Player1 + } else { + p = &world.Player2 + } + + r := image.Rect(int(p.X), int(p.Y), int(p.X)+size, int(p.Y)+size) + screen.SubImage(r).(*ebiten.Image).Fill(p.Color) + } + + return nil +} diff --git a/world/world.go b/world/world.go index a389ca8..1d83ce8 100644 --- a/world/world.go +++ b/world/world.go @@ -1,6 +1,10 @@ package world -const TPS = 144 +import ( + "code.rocketnine.space/tslocum/boxbrawl/component" +) + +const TPS = 60 const ( DefaultScreenWidth = 1280 @@ -21,4 +25,7 @@ var ( Debug = 1 WASM bool + + Player1 component.Player + Player2 component.Player )