examples/2048: Add comments

This commit is contained in:
Hajime Hoshi 2017-06-04 18:00:47 +09:00
parent 4b449bbc75
commit 8285950100
4 changed files with 20 additions and 1 deletions

View File

@ -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++ {

View File

@ -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()

View File

@ -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 {

View File

@ -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: