-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.go
194 lines (135 loc) · 4.85 KB
/
level.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
189
190
191
192
193
194
package engine
import (
"image"
"image/color"
"image/draw"
)
// Level is a struct that defines a single level of a game
type Level struct {
BackgroundColour color.RGBA
Gravity float64
GameObjects []*GameObject
Game *Game
PaintOffset Vector
BeforePaint BeforePaint
}
// Repaint redraws the entire level for a new game
func (level *Level) Repaint(stage *image.RGBA) {
// Figure out where all the floor objects are
level.AssignFloors()
// Figure out which objects are colliding
level.CalculateCollisions()
// Paint the background color
draw.Draw(stage, stage.Bounds(), &image.Uniform{level.BackgroundColour}, image.ZP, draw.Src)
// Update each game object
for _, gameObject := range level.GameObjects {
// Skip hidden objects
if gameObject.IsHidden == true {
continue
}
gameObject.Level = level
gameObject.RecalculatePosition(level.Gravity)
if gameObject.Direction == DirLeft {
gameObject.IsFlipped = true
} else if gameObject.Direction == DirRight {
gameObject.IsFlipped = false
}
// 0 is at the bottom, so flip the Y axis to paint correctly
invertedY := level.Game.Height - int(gameObject.Position.Y) - gameObject.Height()
paintY := invertedY + int(level.PaintOffset.Y)
paintX := int(gameObject.Position.X) - int(level.PaintOffset.X)
gameObject.CurrentSprite().AddToCanvas(stage, paintX, paintY, gameObject.IsFlipped)
}
}
// AssignFloors iterates through all objects in the level and defines which
// object beneath them (if any) should be considered their 'floor' object,
// setting its top edge as the lowest point that the object can fall
func (level *Level) AssignFloors() {
floorXCoords := map[int][]*GameObject{}
// Make a map of each object's possible X positions
for _, gameObject := range level.GameObjects {
// Skip hidden, non-interactive and non-floor objects
if gameObject.IsHidden == true || gameObject.IsInteractive == false || gameObject.IsFloor == false {
continue
}
for i := 0; i < gameObject.Width(); i++ {
xPos := i + int(gameObject.Position.X)
floorXCoords[xPos] = append(floorXCoords[xPos], gameObject)
}
}
// Find the objects that sit beneath every other object
for _, gameObject := range level.GameObjects {
// Skip objects that float or are non-interactive
if gameObject.Mass == 0 || gameObject.IsInteractive == false {
continue
}
highestFloorObject := float64(0 - gameObject.Height())
for i := 0; i < gameObject.Width(); i++ {
xPos := i + int(gameObject.Position.X)
if floorObjects, ok := floorXCoords[xPos]; ok {
// Find the one that is highest while still being lower than
// the object itself
for _, floorObject := range floorObjects {
floorObjectTop := (floorObject.Position.Y + float64(floorObject.Height()))
if floorObjectTop <= gameObject.Position.Y {
if floorObjectTop > highestFloorObject {
highestFloorObject = floorObjectTop
}
}
}
}
}
gameObject.FloorY = highestFloorObject
}
}
// CalculateCollisions iterates via all objects in the level and defines which
// objects (if any) intersect them
func (level *Level) CalculateCollisions() {
xCoords := map[int][]*GameObject{}
// Make a map of each object's possible x positions
for _, gameObject := range level.GameObjects {
// Skip hidden of each object's possible X positions
if gameObject.IsHidden == true || gameObject.IsInteractive == false {
continue
}
for i := 0; i < gameObject.Width(); i++ {
xPos := i + int(gameObject.Position.X)
xCoords[xPos] = append(xCoords[xPos], gameObject)
}
}
// Find objects that also intersect on the Y axis
for _, gameObject := range level.GameObjects {
intersections := map[*GameObject]bool{}
gameObjectYmin := gameObject.Position.Y
gameObjectYmax := gameObjectYmin + float64(gameObject.Height())
for i := 0; i < gameObject.Width(); i++ {
xPos := i + int(gameObject.Position.X)
if intersectingObjects, ok := xCoords[xPos]; ok {
for _, intersectingObject := range intersectingObjects {
// Ignore the object itself
if intersectingObject == gameObject {
continue
}
// Skip the object if it has already been stored
if _, ok := intersections[intersectingObject]; ok {
continue
}
intersectingObjectYMin := intersectingObject.Position.Y
intersectingObjectYMax := intersectingObjectYMin + float64(intersectingObject.Height())
if (gameObjectYmin >= intersectingObjectYMax || gameObjectYmax <= intersectingObjectYMin) == false {
intersections[intersectingObject] = true
}
}
}
}
// Let the game know that there have been collisions
if len(intersections) > 0 {
for collidingObject := range intersections {
gameObject.CollisionHandler(gameObject, Collision{
GameObject: collidingObject,
Edge: gameObject.GetCollisionEdge(collidingObject),
})
}
}
}
}