mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
examples/2048: Refactoring
This commit is contained in:
parent
b59649336a
commit
c44f2819af
@ -84,8 +84,14 @@ func tileAt(tiles map[*Tile]struct{}, x, y int) *Tile {
|
|||||||
func nextTileAt(tiles map[*Tile]struct{}, x, y int) *Tile {
|
func nextTileAt(tiles map[*Tile]struct{}, x, y int) *Tile {
|
||||||
var result *Tile
|
var result *Tile
|
||||||
for t := range tiles {
|
for t := range tiles {
|
||||||
if t.next.x != x || t.next.y != y || t.next.value == 0 {
|
if 0 < t.animationCount {
|
||||||
continue
|
if t.next.x != x || t.next.y != y || t.next.value == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if t.current.x != x || t.current.y != y {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if result != nil {
|
if result != nil {
|
||||||
panic("not reach")
|
panic("not reach")
|
||||||
@ -142,10 +148,10 @@ func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool {
|
|||||||
moved = true
|
moved = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if t.current.value != tt.next.value {
|
if t.current.value != tt.current.value {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if tt.current.value != tt.next.value {
|
if 0 < tt.animationCount && tt.current.value != tt.next.value {
|
||||||
// already merged
|
// already merged
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -157,14 +163,18 @@ func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool {
|
|||||||
next := TileData{}
|
next := TileData{}
|
||||||
next.value = t.current.value
|
next.value = t.current.value
|
||||||
if tt := nextTileAt(tiles, ii, jj); tt != t && tt != nil {
|
if tt := nextTileAt(tiles, ii, jj); tt != t && tt != nil {
|
||||||
next.value = t.current.value + tt.next.value
|
next.value = t.current.value + tt.current.value
|
||||||
tt.next.value = 0
|
tt.next.value = 0
|
||||||
|
tt.next.x = ii
|
||||||
|
tt.next.y = jj
|
||||||
tt.animationCount = maxAnimationCount
|
tt.animationCount = maxAnimationCount
|
||||||
}
|
}
|
||||||
next.x = ii
|
next.x = ii
|
||||||
next.y = jj
|
next.y = jj
|
||||||
t.next = next
|
if t.current != next {
|
||||||
t.animationCount = maxAnimationCount
|
t.next = next
|
||||||
|
t.animationCount = maxAnimationCount
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !moved {
|
if !moved {
|
||||||
|
@ -42,11 +42,15 @@ func tilesToCells(tiles map[*Tile]struct{}, size int) ([]int, []int) {
|
|||||||
for t := range tiles {
|
for t := range tiles {
|
||||||
x, y := t.Pos()
|
x, y := t.Pos()
|
||||||
cells[x+y*size] = t.Value()
|
cells[x+y*size] = t.Value()
|
||||||
if t.NextValue() == 0 {
|
if t.IsAnimating() {
|
||||||
continue
|
if t.NextValue() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
nx, ny := t.NextPos()
|
||||||
|
nextCells[nx+ny*size] = t.NextValue()
|
||||||
|
} else {
|
||||||
|
nextCells[x+y*size] = t.Value()
|
||||||
}
|
}
|
||||||
nx, ny := t.NextPos()
|
|
||||||
nextCells[nx+ny*size] = t.NextValue()
|
|
||||||
}
|
}
|
||||||
return cells, nextCells
|
return cells, nextCells
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user