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 type task func() error
// Board represents the game board.
type Board struct { type Board struct {
size int size int
tiles map[*Tile]struct{} tiles map[*Tile]struct{}
tasks []task tasks []task
} }
// NewBoard generates a new Board with giving a size.
func NewBoard(size int) (*Board, error) { func NewBoard(size int) (*Board, error) {
b := &Board{ b := &Board{
size: size, size: size,
@ -49,6 +51,7 @@ func (b *Board) tileAt(x, y int) *Tile {
return tileAt(b.tiles, x, y) return tileAt(b.tiles, x, y)
} }
// Update updates the board state.
func (b *Board) Update(input *Input) error { func (b *Board) Update(input *Input) error {
for t := range b.tiles { for t := range b.tiles {
if err := t.Update(); err != nil { if err := t.Update(); err != nil {
@ -72,6 +75,7 @@ func (b *Board) Update(input *Input) error {
return nil return nil
} }
// Move enqueues tile moving tasks.
func (b *Board) Move(dir Dir) error { func (b *Board) Move(dir Dir) error {
for t := range b.tiles { for t := range b.tiles {
t.stopAnimation() t.stopAnimation()
@ -110,12 +114,14 @@ func (b *Board) Move(dir Dir) error {
return nil return nil
} }
// Size returns the board size.
func (b *Board) Size() (int, int) { func (b *Board) Size() (int, int) {
x := b.size*tileSize + (b.size+1)*tileMargin x := b.size*tileSize + (b.size+1)*tileMargin
y := x y := x
return x, y return x, y
} }
// Draw draws the board to the given boardImage.
func (b *Board) Draw(boardImage *ebiten.Image) { func (b *Board) Draw(boardImage *ebiten.Image) {
boardImage.Fill(frameColor) boardImage.Fill(frameColor)
for j := 0; j < b.size; j++ { for j := 0; j < b.size; j++ {

View File

@ -33,12 +33,14 @@ const (
boardSize = 4 boardSize = 4
) )
// Game represents a game state.
type Game struct { type Game struct {
input *Input input *Input
board *Board board *Board
boardImage *ebiten.Image boardImage *ebiten.Image
} }
// NewGame generates a new Game object.
func NewGame() (*Game, error) { func NewGame() (*Game, error) {
g := &Game{ g := &Game{
input: NewInput(), input: NewInput(),
@ -51,6 +53,7 @@ func NewGame() (*Game, error) {
return g, nil return g, nil
} }
// Update updates the current game state.
func (g *Game) Update() error { func (g *Game) Update() error {
g.input.Update() g.input.Update()
if err := g.board.Update(g.input); err != nil { if err := g.board.Update(g.input); err != nil {
@ -59,6 +62,7 @@ func (g *Game) Update() error {
return nil return nil
} }
// Draw draws the current game to the given screen.
func (g *Game) Draw(screen *ebiten.Image) { func (g *Game) Draw(screen *ebiten.Image) {
if g.boardImage == nil { if g.boardImage == nil {
w, h := g.board.Size() w, h := g.board.Size()

View File

@ -20,6 +20,7 @@ import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
) )
// Dir represents a direction.
type Dir int type Dir int
const ( const (
@ -29,6 +30,7 @@ const (
DirLeft DirLeft
) )
// String returns a string representing the direction.
func (d Dir) String() string { func (d Dir) String() string {
switch d { switch d {
case DirUp: case DirUp:
@ -43,6 +45,7 @@ func (d Dir) String() string {
panic("not reach") panic("not reach")
} }
// Vector returns a [-1, 1] value for each axis.
func (d Dir) Vector() (x, y int) { func (d Dir) Vector() (x, y int) {
switch d { switch d {
case DirUp: case DirUp:
@ -57,10 +60,12 @@ func (d Dir) Vector() (x, y int) {
panic("not reach") panic("not reach")
} }
// Input represents the current key states.
type Input struct { type Input struct {
keyState map[ebiten.Key]int keyState map[ebiten.Key]int
} }
// NewInput generates a new Input object.
func NewInput() *Input { func NewInput() *Input {
return &Input{ return &Input{
keyState: map[ebiten.Key]int{}, keyState: map[ebiten.Key]int{},
@ -76,6 +81,7 @@ var (
} }
) )
// Update updates the current input states.
func (i *Input) Update() { func (i *Input) Update() {
for k := range dirKeys { for k := range dirKeys {
if ebiten.IsKeyPressed(k) { 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) { func (i *Input) Dir() (Dir, bool) {
for k, d := range dirKeys { for k, d := range dirKeys {
if i.keyState[k] == 1 { if i.keyState[k] == 1 {

View File

@ -131,7 +131,7 @@ const (
) )
// MoveTiles moves tiles in the given tiles map if possible. // 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 { func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool {
vx, vy := dir.Vector() vx, vy := dir.Vector()
tx := []int{} tx := []int{}
@ -244,6 +244,7 @@ func addRandomTile(tiles map[*Tile]struct{}, size int) error {
return nil return nil
} }
// Update updates the tile's animation states.
func (t *Tile) Update() error { func (t *Tile) Update() error {
switch { switch {
case 0 < t.movingCount: case 0 < t.movingCount: