-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
63 lines (50 loc) · 1.01 KB
/
game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"time"
"fmt"
)
type Game struct {
snakeHead *Snake
grid *Grid
moveTimer *time.Timer
}
func newGame(grid *Grid, snake *Snake) *Game {
g := Game{
snakeHead: snake,
grid: grid,
moveTimer: time.NewTimer(time.Duration(time.Second)),
}
g.moveTimer.Stop()
return &g
}
func (g *Game) move() {
// reset the timer
g.resetMoveTimer()
x, y := g.snakeHead.Position();
// leave the body behind
body := newSnake()
body.state = snakeBody
body.decay = g.snakeHead.decay
body.SetPosition(x, y)
g.grid.Cell(x, y).s = body
// ???
xNew, yNew := g.grid.PositionAdjacent(x, y, g.snakeHead.direction)
nextCell := g.grid.Cell(xNew, yNew)
if (nextCell == nil) {
fmt.Println("WTF")
}
// switch nextCell.(type) {
// case nil:
g.snakeHead.SetPosition(xNew, yNew)
nextCell.s = g.snakeHead
// default:
// fmt.Println("WTF")
// }
}
func (g *Game) start() {
// start the timers and shit
g.resetMoveTimer()
}
func (g *Game) resetMoveTimer() {
g.moveTimer.Reset(time.Duration(time.Second))
}