This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
107 lines (92 loc) · 1.94 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
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
package game
import (
"fmt"
"github.com/tanema/amore-examples/asteroids/game/phys"
"github.com/tanema/amore/audio"
"github.com/tanema/amore/gfx"
"github.com/tanema/amore/keyboard"
)
var debug bool
type GameObject interface {
Update(dt float32)
Draw()
Destroy(force bool)
}
const (
cellSize float32 = 60
)
var (
score = 0
world *phys.World
objects []GameObject
bomb, _ = audio.NewSource("../test-all/assets/audio/bomb.wav", true)
lazer, _ = audio.NewSource("audio/lazer.wav", true)
player *Player
gameOver = false
screenWidth float32
screenHeight float32
)
func New() {
keyboard.OnKeyUp = keyup
screenWidth = gfx.GetWidth()
screenHeight = gfx.GetHeight()
reset()
}
func reset() {
gameOver = false
score = 0
world = phys.NewWorld(screenWidth, screenHeight, cellSize)
player = newPlayer()
objects = []GameObject{
player,
newAsteroid(),
newAsteroid(),
newAsteroid(),
newAsteroid(),
newAsteroid(),
newAsteroid(),
newAsteroid(),
}
}
func keyup(key keyboard.Key) {
if key == keyboard.KeyTab {
debug = !debug
}
if key == keyboard.KeyReturn {
reset()
}
}
func Update(dt float32) {
for _, object := range objects {
object.Update(dt)
}
gameOver = gameOver || len(objects) == 1
}
func addObject(object GameObject) {
objects = append(objects, object)
}
func removeObject(object GameObject) {
for i, other := range objects {
if object == other {
objects = append(objects[:i], objects[i+1:]...)
return
}
}
}
func Draw() {
if debug {
world.DrawGrid()
gfx.Print(fmt.Sprintf("objects: %v", len(objects)), 0, 15)
}
for _, object := range objects {
object.Draw()
}
gfx.Print(fmt.Sprintf("Score: %v", score), screenWidth-100, 0)
if gameOver {
if len(objects) == 1 && objects[0] == player {
gfx.Print("Game Over. You Won.", screenWidth/2-175, screenHeight/2, 0, 2, 3)
} else {
gfx.Print("Game Over. You Lost.", screenWidth/2-175, screenHeight/2, 0, 2, 2)
}
}
}