examples/2048: Add comments

This commit is contained in:
Hajime Hoshi 2017-06-04 16:04:51 +09:00
parent 17d6ea16de
commit ffc6be0c10

View File

@ -18,6 +18,7 @@ package twenty48
import ( import (
"errors" "errors"
"fmt"
"image/color" "image/color"
"math/rand" "math/rand"
"sort" "sort"
@ -27,12 +28,14 @@ import (
"github.com/hajimehoshi/ebiten/examples/common" "github.com/hajimehoshi/ebiten/examples/common"
) )
// TileData represents a tile information like a value and a position.
type TileData struct { type TileData struct {
value int value int
x int x int
y int y int
} }
// Tile represents a tile infomation including TileData and animation states.
type Tile struct { type Tile struct {
current TileData current TileData
next TileData next TileData
@ -41,6 +44,7 @@ type Tile struct {
poppingCount int poppingCount int
} }
// NewTile creates a new Tile object.
func NewTile(value int, x, y int) *Tile { func NewTile(value int, x, y int) *Tile {
return &Tile{ return &Tile{
current: TileData{ current: TileData{
@ -52,22 +56,27 @@ func NewTile(value int, x, y int) *Tile {
} }
} }
// Value returns the current value.
func (t *Tile) Value() int { func (t *Tile) Value() int {
return t.current.value return t.current.value
} }
// Pos returns the current position.
func (t *Tile) Pos() (int, int) { func (t *Tile) Pos() (int, int) {
return t.current.x, t.current.y return t.current.x, t.current.y
} }
// NextValue returns the next value.
func (t *Tile) NextValue() int { func (t *Tile) NextValue() int {
return t.next.value return t.next.value
} }
// NextPos returns the next position.
func (t *Tile) NextPos() (int, int) { func (t *Tile) NextPos() (int, int) {
return t.next.x, t.next.y return t.next.x, t.next.y
} }
// IsMoving returns a boolean value indicating if the tile is animating.
func (t *Tile) IsMoving() bool { func (t *Tile) IsMoving() bool {
return 0 < t.movingCount return 0 < t.movingCount
} }
@ -121,6 +130,8 @@ const (
maxPoppingCount = 6 maxPoppingCount = 6
) )
// MoveTiles moves tiles in the given tiles map if possible.
// MoveTiles returns true if there are movable tiles, otherwise false.
func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool { func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool {
vx, vy := dir.Vector() vx, vy := dir.Vector()
tx := []int{} tx := []int{}
@ -289,6 +300,7 @@ func init() {
tileImage.Fill(color.White) tileImage.Fill(color.White)
} }
// Draw draws the current tile to the given boardImage.
func (t *Tile) Draw(boardImage *ebiten.Image) { func (t *Tile) Draw(boardImage *ebiten.Image) {
i, j := t.current.x, t.current.y i, j := t.current.x, t.current.y
ni, nj := t.next.x, t.next.y ni, nj := t.next.x, t.next.y