From 828595010094e313cc4f9232d6306ebb41e9fb19 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 4 Jun 2017 18:00:47 +0900 Subject: [PATCH] examples/2048: Add comments --- examples/2048/2048/board.go | 6 ++++++ examples/2048/2048/game.go | 4 ++++ examples/2048/2048/input.go | 8 ++++++++ examples/2048/2048/tile.go | 3 ++- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/examples/2048/2048/board.go b/examples/2048/2048/board.go index 46ae56f74..4254550f0 100644 --- a/examples/2048/2048/board.go +++ b/examples/2048/2048/board.go @@ -26,12 +26,14 @@ var taskTerminated = errors.New("twenty48: task terminated") type task func() error +// Board represents the game board. type Board struct { size int tiles map[*Tile]struct{} tasks []task } +// NewBoard generates a new Board with giving a size. func NewBoard(size int) (*Board, error) { b := &Board{ size: size, @@ -49,6 +51,7 @@ func (b *Board) tileAt(x, y int) *Tile { return tileAt(b.tiles, x, y) } +// Update updates the board state. func (b *Board) Update(input *Input) error { for t := range b.tiles { if err := t.Update(); err != nil { @@ -72,6 +75,7 @@ func (b *Board) Update(input *Input) error { return nil } +// Move enqueues tile moving tasks. func (b *Board) Move(dir Dir) error { for t := range b.tiles { t.stopAnimation() @@ -110,12 +114,14 @@ func (b *Board) Move(dir Dir) error { return nil } +// Size returns the board size. func (b *Board) Size() (int, int) { x := b.size*tileSize + (b.size+1)*tileMargin y := x return x, y } +// Draw draws the board to the given boardImage. func (b *Board) Draw(boardImage *ebiten.Image) { boardImage.Fill(frameColor) for j := 0; j < b.size; j++ { diff --git a/examples/2048/2048/game.go b/examples/2048/2048/game.go index e42569566..28825e9c6 100644 --- a/examples/2048/2048/game.go +++ b/examples/2048/2048/game.go @@ -33,12 +33,14 @@ const ( boardSize = 4 ) +// Game represents a game state. type Game struct { input *Input board *Board boardImage *ebiten.Image } +// NewGame generates a new Game object. func NewGame() (*Game, error) { g := &Game{ input: NewInput(), @@ -51,6 +53,7 @@ func NewGame() (*Game, error) { return g, nil } +// Update updates the current game state. func (g *Game) Update() error { g.input.Update() if err := g.board.Update(g.input); err != nil { @@ -59,6 +62,7 @@ func (g *Game) Update() error { return nil } +// Draw draws the current game to the given screen. func (g *Game) Draw(screen *ebiten.Image) { if g.boardImage == nil { w, h := g.board.Size() diff --git a/examples/2048/2048/input.go b/examples/2048/2048/input.go index 552d714b1..6473e7381 100644 --- a/examples/2048/2048/input.go +++ b/examples/2048/2048/input.go @@ -20,6 +20,7 @@ import ( "github.com/hajimehoshi/ebiten" ) +// Dir represents a direction. type Dir int const ( @@ -29,6 +30,7 @@ const ( DirLeft ) +// String returns a string representing the direction. func (d Dir) String() string { switch d { case DirUp: @@ -43,6 +45,7 @@ func (d Dir) String() string { panic("not reach") } +// Vector returns a [-1, 1] value for each axis. func (d Dir) Vector() (x, y int) { switch d { case DirUp: @@ -57,10 +60,12 @@ func (d Dir) Vector() (x, y int) { panic("not reach") } +// Input represents the current key states. type Input struct { keyState map[ebiten.Key]int } +// NewInput generates a new Input object. func NewInput() *Input { return &Input{ keyState: map[ebiten.Key]int{}, @@ -76,6 +81,7 @@ var ( } ) +// Update updates the current input states. func (i *Input) Update() { for k := range dirKeys { if ebiten.IsKeyPressed(k) { @@ -86,6 +92,8 @@ func (i *Input) Update() { } } +// Dir returns a currenly pressed direction. +// Dir returns false if no direction key is pressed. func (i *Input) Dir() (Dir, bool) { for k, d := range dirKeys { if i.keyState[k] == 1 { diff --git a/examples/2048/2048/tile.go b/examples/2048/2048/tile.go index 893b8ed34..35cd3bdd4 100644 --- a/examples/2048/2048/tile.go +++ b/examples/2048/2048/tile.go @@ -131,7 +131,7 @@ const ( ) // MoveTiles moves tiles in the given tiles map if possible. -// MoveTiles returns true if there are movable tiles, otherwise false. +// MoveTiles returns true if there are tiles that are to move, otherwise false. func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool { vx, vy := dir.Vector() tx := []int{} @@ -244,6 +244,7 @@ func addRandomTile(tiles map[*Tile]struct{}, size int) error { return nil } +// Update updates the tile's animation states. func (t *Tile) Update() error { switch { case 0 < t.movingCount: