-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
188 lines (142 loc) · 3.27 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// the main loop and input handling
package main
import (
"fmt"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"math"
"math/rand"
"time"
)
const GameWidth float64 = 320
const GameHeight float64 = 240
const MaxLevels int = 0
//ebiten sets this
const FPS = 60
type GameState struct {
snake Snake
input InputHandler
coroutine func(game *GameState, screen *ebiten.Image)
coroutineStart int //the tick at which the coroutine started
//its assumed that 60 ticks = 1s
chef Chef
edibles []Edible
ended bool
frozen bool
level int
ticker int
tick int
score int
running bool
endMessage string
}
func (game *GameState) AdvanceLevel(screen *ebiten.Image) {
//yeah. this should be an enum
game.frozen = true
game.running = false
game.coroutine = DoLevelAnimation
game.coroutineStart = game.tick
go func() {
time.Sleep(time.Second * 3)
game.snake.ResetToStartSize()
game.NextLevel()
game.running = true
game.frozen = false
}()
}
func (game *GameState) End(msg string) {
game.ended = true
game.running = false
game.endMessage = msg
}
func (game *GameState) Main(screen *ebiten.Image) error {
game.tick += 1
switch {
case game.ended:
ebitenutil.DebugPrint(screen, game.endMessage)
case game.running:
game.Update(screen)
case game.frozen:
if game.coroutine != nil {
game.coroutine(game, screen)
}
default:
ebitenutil.DebugPrint(screen, "Advanced Snek? (Y/n)")
game.input.WaitForOption(game)
}
return nil
}
func (game *GameState) NextLevel() {
game.level += 1
//this is the kind of thing where no coersion at all
//starts to look silly
game.level = int(
math.Min(
float64(MaxLevels),
float64(game.level)))
}
func (game *GameState) Start() {
game.chef.Start()
game.snake.Start(Vector2{GameWidth / 2, GameHeight / 2})
game.running = true
}
func (game *GameState) Update(screen *ebiten.Image) error {
if game.tick%game.ticker != 0 {
err := Draw(*game, screen)
return err
}
if game.snake.advanced && game.snake.size == maxSize {
game.AdvanceLevel(screen)
return nil
}
//handled in input.go
err := game.input.ProcessInput(game)
if err != nil {
return err
}
//for now, will think of another way to do this later
if game.edibles[0] == nil {
game.edibles[0] = game.chef.MakeEdibleForLevel(game.level)
}
//handled in snake.go
game.snake.Update()
if game.snake.EatingTail() {
//end with score message
game.End(fmt.Sprintf("Game Over! Score: %v", game.score))
}
for i := 0; i < len(game.edibles); i++ {
edible := game.edibles[i]
if edible == nil {
continue
}
//this might be in a collision handler that tracks
//all edibles, but for now this is easiest
if game.snake.IsColliding(edible) {
game.snake.Eat(edible)
game.score += 1
game.edibles[i] = nil
}
}
//handled in draw.go, this is not a pointer fn
//because it can't change state
err = Draw(*game, screen)
if err != nil {
return err
}
return nil
}
func main() {
game := GameState{
snake: Snake{direction: Vector2{1, 0}, speed: 2},
input: InputHandler{},
chef: Chef{},
edibles: make([]Edible, 10),
ticker: 1}
rand.Seed(time.Now().Unix())
ebiten.Run(
game.Main,
int(GameWidth),
int(GameHeight),
2,
"Go Go Snek!")
}