mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +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 {
|
||||
var result *Tile
|
||||
for t := range tiles {
|
||||
if t.next.x != x || t.next.y != y || t.next.value == 0 {
|
||||
continue
|
||||
if 0 < t.animationCount {
|
||||
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 {
|
||||
panic("not reach")
|
||||
@ -142,10 +148,10 @@ func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool {
|
||||
moved = true
|
||||
continue
|
||||
}
|
||||
if t.current.value != tt.next.value {
|
||||
if t.current.value != tt.current.value {
|
||||
break
|
||||
}
|
||||
if tt.current.value != tt.next.value {
|
||||
if 0 < tt.animationCount && tt.current.value != tt.next.value {
|
||||
// already merged
|
||||
break
|
||||
}
|
||||
@ -157,14 +163,18 @@ func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool {
|
||||
next := TileData{}
|
||||
next.value = t.current.value
|
||||
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.x = ii
|
||||
tt.next.y = jj
|
||||
tt.animationCount = maxAnimationCount
|
||||
}
|
||||
next.x = ii
|
||||
next.y = jj
|
||||
t.next = next
|
||||
t.animationCount = maxAnimationCount
|
||||
if t.current != next {
|
||||
t.next = next
|
||||
t.animationCount = maxAnimationCount
|
||||
}
|
||||
}
|
||||
}
|
||||
if !moved {
|
||||
|
@ -42,11 +42,15 @@ func tilesToCells(tiles map[*Tile]struct{}, size int) ([]int, []int) {
|
||||
for t := range tiles {
|
||||
x, y := t.Pos()
|
||||
cells[x+y*size] = t.Value()
|
||||
if t.NextValue() == 0 {
|
||||
continue
|
||||
if t.IsAnimating() {
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user