-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemspatial.go
296 lines (271 loc) · 8.5 KB
/
systemspatial.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package zelduh
import (
"math/rand"
"github.com/faiface/pixel"
)
type spatialEntity struct {
ID EntityID
*ComponentMovement
*ComponentSpatial
*ComponentDash
TotalMoves int
MoveCounter int
}
// SystemSpatial is a custom system
type SystemSpatial struct {
Rand *rand.Rand
player spatialEntity
sword spatialEntity
arrow spatialEntity
enemies []*spatialEntity
moveableObstacles []*spatialEntity
}
// AddEntity adds an entity to the system
func (s *SystemSpatial) AddEntity(entity Entity) {
r := spatialEntity{
ID: entity.ID(),
ComponentSpatial: entity.ComponentSpatial,
ComponentMovement: entity.ComponentMovement,
}
switch entity.Category {
case CategoryPlayer:
r.ComponentDash = entity.ComponentDash
s.player = r
case CategorySword:
s.sword = r
case CategoryArrow:
s.arrow = r
case CategoryMovableObstacle:
s.moveableObstacles = append(s.moveableObstacles, &r)
case CategoryEnemy:
s.enemies = append(s.enemies, &r)
}
}
// Remove removes the entity from the system
func (s *SystemSpatial) Remove(category EntityCategory, id EntityID) {
switch category {
case CategoryEnemy:
for i := len(s.enemies) - 1; i >= 0; i-- {
enemy := s.enemies[i]
if enemy.ID == id {
s.enemies = append(s.enemies[:i], s.enemies[i+1:]...)
}
}
}
}
// RemoveAll removes all entities from one category
func (s *SystemSpatial) RemoveAll(category EntityCategory) {
switch category {
case CategoryEnemy:
for i := len(s.enemies) - 1; i >= 0; i-- {
s.enemies = append(s.enemies[:i], s.enemies[i+1:]...)
}
}
}
// MovePlayerBack moves the player back
func (s *SystemSpatial) MovePlayerBack() {
player := s.player
var v pixel.Vec
switch player.ComponentMovement.Direction {
case DirectionUp:
v = pixel.V(0, -48)
case DirectionRight:
v = pixel.V(-48, 0)
case DirectionDown:
v = pixel.V(0, 48)
case DirectionLeft:
v = pixel.V(48, 0)
}
player.ComponentSpatial.Rect = player.ComponentSpatial.PrevRect.Moved(v)
player.ComponentSpatial.PrevRect = player.ComponentSpatial.Rect
}
// MoveMoveableObstacle moves a moveable obstacle
func (s *SystemSpatial) MoveMoveableObstacle(obstacleID EntityID, dir Direction) bool {
entity, ok := s.moveableObstacle(obstacleID)
if ok && !entity.ComponentMovement.MovingFromHit {
entity.ComponentMovement.MovingFromHit = true
entity.ComponentMovement.RemainingMoves = entity.ComponentMovement.MaxMoves
entity.ComponentMovement.Direction = dir
return true
}
return false
}
// UndoEnemyRect resets current rect to previous rect
func (s *SystemSpatial) UndoEnemyRect(enemyID EntityID) {
enemy, ok := s.enemy(enemyID)
if ok {
enemy.ComponentSpatial.Rect = enemy.ComponentSpatial.PrevRect
}
}
// MoveEnemyBack moves the enemy back
func (s *SystemSpatial) MoveEnemyBack(enemyID EntityID, directionHit Direction) {
enemy, ok := s.enemy(enemyID)
if ok && !enemy.ComponentMovement.MovingFromHit {
enemy.ComponentMovement.MovingFromHit = true
enemy.ComponentMovement.RemainingMoves = enemy.ComponentMovement.HitBackMoves
enemy.ComponentMovement.Direction = directionHit
}
}
// GetEnemySpatial returns the spatial component
func (s *SystemSpatial) GetEnemySpatial(enemyID EntityID) (*ComponentSpatial, bool) {
for _, enemy := range s.enemies {
if enemy.ID == enemyID {
return enemy.ComponentSpatial, true
}
}
return &ComponentSpatial{}, false
}
// EnemyMovingFromHit indicates if the enemy is moving after being hit
func (s *SystemSpatial) EnemyMovingFromHit(enemyID EntityID) bool {
enemy, ok := s.enemy(enemyID)
if ok {
if enemy.ID == enemyID {
return enemy.ComponentMovement.MovingFromHit == true
}
}
return false
}
// Update changes spatial data based on movement data
func (s *SystemSpatial) Update() {
s.movePlayer()
s.moveSword()
s.moveArrow()
for i := 0; i < len(s.moveableObstacles); i++ {
entity := s.moveableObstacles[i]
s.moveMoveableObstacle(entity)
}
for i := 0; i < len(s.enemies); i++ {
enemy := s.enemies[i]
switch enemy.ComponentMovement.PatternName {
case "random":
s.moveEnemyRandom(enemy)
case "left-right":
s.moveEnemyLeftRight(enemy)
}
}
}
func (s *SystemSpatial) moveableObstacle(id EntityID) (spatialEntity, bool) {
for _, e := range s.moveableObstacles {
if e.ID == id {
return *e, true
}
}
return spatialEntity{}, false
}
func (s *SystemSpatial) enemy(id EntityID) (spatialEntity, bool) {
for _, e := range s.enemies {
if e.ID == id {
return *e, true
}
}
return spatialEntity{}, false
}
func delta(dir Direction, modX, modY float64) pixel.Vec {
switch dir {
case DirectionUp:
return pixel.V(0, modY)
case DirectionRight:
return pixel.V(modX, 0)
case DirectionDown:
return pixel.V(0, -modY)
case DirectionLeft:
return pixel.V(-modX, 0)
default:
return pixel.V(0, 0)
}
}
func (s *SystemSpatial) moveSword() {
sword := s.sword
speed := sword.ComponentMovement.Speed
w := sword.ComponentSpatial.Width
h := sword.ComponentSpatial.Height
if speed > 0 {
sword.ComponentSpatial.PrevRect = sword.ComponentSpatial.Rect
v := delta(sword.ComponentMovement.Direction, speed+w, speed+h)
sword.ComponentSpatial.Rect = s.player.ComponentSpatial.Rect.Moved(v)
} else {
sword.ComponentSpatial.Rect = s.player.ComponentSpatial.Rect
}
}
func (s *SystemSpatial) moveArrow() {
arrow := s.arrow
speed := arrow.ComponentMovement.Speed
if arrow.ComponentMovement.RemainingMoves > 0 {
arrow.ComponentSpatial.PrevRect = arrow.ComponentSpatial.Rect
v := delta(arrow.ComponentMovement.Direction, speed, speed)
arrow.ComponentSpatial.Rect = arrow.ComponentSpatial.Rect.Moved(v)
} else {
arrow.ComponentSpatial.Rect = s.player.ComponentSpatial.Rect
}
}
func (s *SystemSpatial) movePlayer() {
player := s.player
speed := player.ComponentMovement.Speed
if player.ComponentDash.Charge == player.ComponentDash.MaxCharge {
speed += player.ComponentDash.SpeedMod
}
if speed > 0 {
v := delta(player.ComponentMovement.Direction, speed, speed)
player.ComponentSpatial.PrevRect = player.ComponentSpatial.Rect
player.ComponentSpatial.Rect = player.ComponentSpatial.Rect.Moved(v)
}
}
func (s *SystemSpatial) moveMoveableObstacle(entity *spatialEntity) {
if entity.ComponentMovement.RemainingMoves > 0 {
speed := entity.ComponentMovement.MaxSpeed
entity.ComponentSpatial.PrevRect = entity.ComponentSpatial.Rect
moveVec := delta(entity.ComponentMovement.Direction, speed, speed)
entity.ComponentSpatial.Rect = entity.ComponentSpatial.Rect.Moved(moveVec)
entity.ComponentMovement.RemainingMoves--
} else {
entity.ComponentMovement.MovingFromHit = false
entity.ComponentMovement.RemainingMoves = 0
}
}
func (s *SystemSpatial) moveEnemyRandom(enemy *spatialEntity) {
if enemy.ComponentMovement.RemainingMoves == 0 {
enemy.ComponentMovement.MovingFromHit = false
enemy.ComponentMovement.RemainingMoves = s.Rand.Intn(enemy.ComponentMovement.MaxMoves)
enemy.ComponentMovement.Direction = RandomDirection(s.Rand)
} else if enemy.ComponentMovement.RemainingMoves > 0 {
var speed float64
if enemy.ComponentMovement.MovingFromHit {
speed = enemy.ComponentMovement.HitSpeed
} else {
speed = enemy.ComponentMovement.MaxSpeed
}
enemy.ComponentSpatial.PrevRect = enemy.ComponentSpatial.Rect
moveVec := delta(enemy.ComponentMovement.Direction, speed, speed)
enemy.ComponentSpatial.Rect = enemy.ComponentSpatial.Rect.Moved(moveVec)
enemy.ComponentMovement.RemainingMoves--
} else {
enemy.ComponentMovement.MovingFromHit = false
enemy.ComponentMovement.RemainingMoves = int(enemy.ComponentSpatial.Rect.W())
}
}
func (s *SystemSpatial) moveEnemyLeftRight(enemy *spatialEntity) {
if enemy.ComponentMovement.RemainingMoves == 0 {
enemy.ComponentMovement.MovingFromHit = false
enemy.ComponentMovement.RemainingMoves = enemy.ComponentMovement.MaxMoves
switch enemy.ComponentMovement.Direction {
case DirectionLeft:
enemy.ComponentMovement.Direction = DirectionRight
case DirectionRight:
enemy.ComponentMovement.Direction = DirectionLeft
}
} else if enemy.ComponentMovement.RemainingMoves > 0 {
var speed float64
if enemy.ComponentMovement.MovingFromHit {
speed = enemy.ComponentMovement.HitSpeed
} else {
speed = enemy.ComponentMovement.MaxSpeed
}
enemy.ComponentSpatial.PrevRect = enemy.ComponentSpatial.Rect
moveVec := delta(enemy.ComponentMovement.Direction, speed, speed)
enemy.ComponentSpatial.Rect = enemy.ComponentSpatial.Rect.Moved(moveVec)
enemy.ComponentMovement.RemainingMoves--
} else {
enemy.ComponentMovement.MovingFromHit = false
enemy.ComponentMovement.RemainingMoves = int(enemy.ComponentSpatial.Rect.W())
}
}