-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame-state.go
168 lines (140 loc) · 4.29 KB
/
game-state.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
package main
import (
"log"
"math/rand"
"sync"
)
type GameState struct {
Player Player
Pipes map[string]*PipeSet
PollRate string
DebugMode bool
Points int
BackgroundOffset int
BackgroundGroundOffset int
pipe_hor_offset int
pipe_vert_offset int
pipe_starting_pos int
pipe_variation int
pipe_count int
in_point_collider bool
mut sync.Mutex
}
func (s *GameState) getFurthestPipe() *PipeSet {
furthest := &PipeSet{}
for _, pipe := range s.Pipes {
if pipe.X > furthest.X {
furthest = pipe
}
}
return furthest
}
func (s *GameState) genInitialPipes() {
num_pipes := s.pipe_count
for i := 1; i < num_pipes+1; i++ {
vert_level := rand.Intn(s.pipe_variation)
new_pipe := PipeSet{
Y: vert_level,
BottomY: vert_level + 300,
X: i * (s.pipe_starting_pos + s.pipe_hor_offset),
ID: genID(12),
Visible: true,
Width: 255,
TopPieceHeight: 135,
Height: 5000,
}
new_pipe.Height += new_pipe.TopPieceHeight
new_pipe.TopCollider.X = float32(new_pipe.X)
new_pipe.BottomCollider.X = float32(new_pipe.X)
new_pipe.PointCollider.X = float32(new_pipe.X)
new_pipe.BottomCollider.Y = float32(new_pipe.BottomY)
new_pipe.TopCollider.Y = float32(new_pipe.Y)
new_pipe.TopCollider.Width = float32(new_pipe.Width / 4)
new_pipe.BottomCollider.Width = float32(new_pipe.Width / 4)
new_pipe.TopCollider.Height = float32(new_pipe.Height)
new_pipe.BottomCollider.Height = float32(new_pipe.Height)
on_point_collected := func(name string) {
s.Points++
}
new_pipe.PointCollider.OnLeave = on_point_collected
log.Printf("pipe ID: %s Top Collider: %+v", new_pipe.ID, new_pipe.TopCollider)
log.Printf("pipe ID: %s Bottom Collider: %+v", new_pipe.ID, new_pipe.BottomCollider)
log.Printf("pipe ID: %s Point Collider: %+v", new_pipe.ID, new_pipe.PointCollider)
s.Pipes[new_pipe.ID] = &new_pipe
}
}
func (s *GameState) isColliding() bool {
for _, pipe := range s.Pipes {
if pipe.BottomCollider.isColliding(&s.Player.Collider) ||
pipe.TopCollider.isColliding(&s.Player.Collider) {
return true
}
pipe.PointCollider.isColliding(&s.Player.Collider)
}
return false
}
func (s *GameState) update() {
s.mut.Lock()
defer s.mut.Unlock()
if !s.Player.Dead && s.Player.Started {
s.BackgroundOffset -= 1
s.BackgroundGroundOffset -= 15
for key := range s.Pipes {
vert_level := rand.Intn(s.pipe_variation)
gap_level := rand.Intn(100)
bottom_y_offset := vert_level + 150 + gap_level
new_pipe := s.Pipes[key]
new_pipe.X -= 15
if new_pipe.X < -100 {
// If it goes past the screen then send it to the back
new_pipe.Visible = false
new_pipe.Y = vert_level
new_pipe.BottomY = bottom_y_offset
furthest_pipe := s.getFurthestPipe()
new_pipe.X = furthest_pipe.X + (s.pipe_hor_offset * 2)
// If it's outside the screen then we don't show it
} else if new_pipe.X < 1500 && new_pipe.X > 0 {
new_pipe.Visible = true
}
new_pipe.TopCollider.X = float32(new_pipe.X)
new_pipe.TopCollider.Y = float32(new_pipe.Y - 5110) // Not even I know how I got this value
new_pipe.PointCollider.X = float32(new_pipe.X)
new_pipe.PointCollider.Y = float32(new_pipe.Y)
new_pipe.PointCollider.Width = float32(new_pipe.Width / 4)
new_pipe.PointCollider.Height = float32(new_pipe.BottomY - new_pipe.Y)
new_pipe.BottomCollider.X = float32(new_pipe.X)
new_pipe.BottomCollider.Y = float32(new_pipe.BottomY)
s.Pipes[key] = new_pipe
}
}
if s.isColliding() {
s.Player.Dead = true
}
}
func newGameState() *GameState {
game_state := GameState{
Player: Player{
Y: 300,
X: 100,
Width: 50,
Height: 32,
},
DebugMode: false,
Pipes: map[string]*PipeSet{},
PollRate: "35ms",
pipe_vert_offset: 400,
pipe_count: 4,
pipe_variation: 250,
pipe_hor_offset: 300,
pipe_starting_pos: 300,
}
log.Printf("%+v", &game_state.Player)
game_state.Player.Collider = BoundingBox{
X: game_state.Player.X,
Y: game_state.Player.Y,
Width: float32(game_state.Player.Width),
Height: float32(game_state.Player.Height),
}
game_state.genInitialPipes()
return &game_state
}