Skip to content

Commit

Permalink
Draw objects with Drawable interface rather than directly
Browse files Browse the repository at this point in the history
  • Loading branch information
z-riley committed Oct 12, 2024
1 parent a5afa25 commit 2a0b035
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 60 deletions.
12 changes: 6 additions & 6 deletions common/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,17 @@ func (a *Arena) Destroy() {
}

// Draw draws the arena.
func (a *Arena) Draw(win *turdgl.Window) {
win.DrawBackground(a.background)
func (a *Arena) Draw(buf *turdgl.FrameBuffer) {
a.background.Draw(buf)

for i := range numTiles {
for j := range numTiles {
win.DrawBackground(a.bgTiles[j][i])
a.bgTiles[j][i].Draw(buf)
}
}

for _, t := range a.tiles {
win.DrawForeground(t.tb)
t.tb.Draw(buf)
}
}

Expand Down Expand Up @@ -189,8 +189,8 @@ func (a *Arena) SetLose() {
})
}

// Animate animates the arena to match the given game state.
func (a *Arena) Animate(game backend.Game) {
// Update animates the arena to match the given game state.
func (a *Arena) Update(game backend.Game) {
defer func() {
// Update the local state upon exit
a.latestState.Grid.Tiles = game.Grid.Tiles
Expand Down
6 changes: 3 additions & 3 deletions common/textbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func NewGameTextBox(width, height float64, pos turdgl.Vec, colour color.RGBA) *G
}

// Draw draws the UI box to the window.
func (g *GameUIBox) Draw(win *turdgl.Window) {
win.Draw(g.body)
win.Draw(g.heading)
func (g *GameUIBox) Draw(buf *turdgl.FrameBuffer) {
g.body.Draw(buf)
g.heading.Draw(buf)
}

// SetHeading sets the heading text of the UI box.
Expand Down
57 changes: 26 additions & 31 deletions screen/multiplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ func (s *MultiplayerScreen) Deinit() {

// Update updates and draws the singleplayer screen.
func (s *MultiplayerScreen) Update() {
// Check for win or lose
switch s.backend.Grid.Outcome() {
case grid.None:
s.backgroundColour = common.BackgroundColour
case grid.Win:
s.backgroundColour = common.BackgroundColourWin
case grid.Lose:
s.backgroundColour = common.BackgroundColourLose
}

s.win.SetBackground(s.backgroundColour)

// Handle user inputs from user. Only 1 input must be sent per update cycle,
Expand All @@ -226,44 +236,29 @@ func (s *MultiplayerScreen) Update() {
// No user input; continue
}

// Draw UI widgets
s.win.Draw(s.title)
s.win.Draw(s.guide)
s.win.Draw(s.opponentGuide)

s.win.Draw(s.menu)
s.menu.Update(s.win)

s.score.Draw(s.win)
s.score.SetBody(fmt.Sprint(s.backend.Score.CurrentScore()))

s.opponentScore.Draw(s.win)
s.opponentScore.SetBody(fmt.Sprint(s.opponentBackend.Score.CurrentScore()))

s.win.Draw(s.newGame)
s.newGame.Update(s.win)

s.timer.SetText(s.backend.Timer.Time.String())
s.win.Draw(s.timer)

// Draw arena of tiles
s.arena.Animate(*s.backend)
s.arena.Draw(s.win)

// Draw opponent's arena
s.opponentArena.Animate(*s.opponentBackend)
s.opponentArena.Draw(s.win)

// Check for win or lose
switch s.backend.Grid.Outcome() {
case grid.None:
s.backgroundColour = common.BackgroundColour
case grid.Win:
s.backgroundColour = common.BackgroundColourWin
case grid.Lose:
s.backgroundColour = common.BackgroundColourLose
s.arena.Update(*s.backend)
s.opponentArena.Update(*s.opponentBackend)

for _, d := range []turdgl.Drawable{
s.title,
s.guide,
s.opponentGuide,
s.menu,
s.score,
s.opponentScore,
s.newGame,
s.timer,
s.arena,
s.opponentArena,
} {
s.win.Draw(d)
}

}

// sendGameUpdate sends the local game state to the opponent.
Expand Down
47 changes: 27 additions & 20 deletions screen/singleplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,28 @@ func (s *SingleplayerScreen) Update() {
// Update updates and draws the singleplayer screen in a normal state.
func (s *SingleplayerScreen) updateNormal(game backend.Game) {
s.win.SetBackground(common.BackgroundColour)
s.arena.SetNormal()

s.score.SetBody(fmt.Sprint(game.Score.Current))
s.menu.Update(s.win)
s.highScore.SetBody(fmt.Sprint(game.Score.High))
s.newGame.Update(s.win)
s.timer.SetText(game.Timer.Time.String())
s.newGame.Update(s.win)

s.win.Draw(s.logo2048)
s.score.Draw(s.win)
s.highScore.Draw(s.win)
s.win.Draw(s.menu) // TODO: convert these to turdgl.Drawable interface (s.menu.Draw(s.win.Framebuffer))
s.win.Draw(s.newGame)
s.win.Draw(s.guide)
s.win.Draw(s.timer)

s.arena.Draw(s.win)
s.arena.Animate(game)
s.arena.SetNormal()
s.arena.Update(game)

for _, d := range []turdgl.Drawable{
s.logo2048,
s.score,
s.highScore,
s.menu,
s.newGame,
s.guide,
s.timer,
s.arena,
} {
s.win.Draw(d)
}
}

// updateWin updates and draws the singleplayer screen in a winning state.
Expand All @@ -267,12 +271,15 @@ func (s *SingleplayerScreen) updateLose(game backend.Game) {

s.menu.Update(s.win)
s.newGame.Update(s.win)

s.win.Draw(s.heading)
s.win.Draw(s.loseDialog)
s.win.Draw(s.menu)
s.win.Draw(s.newGame)

s.arena.Draw(s.win)
s.arena.Animate(game)
s.arena.Update(game)

for _, d := range []turdgl.Drawable{
s.heading,
s.loseDialog,
s.menu,
s.newGame,
s.arena,
} {
s.win.Draw(d)
}
}

0 comments on commit 2a0b035

Please sign in to comment.