-
Notifications
You must be signed in to change notification settings - Fork 1
/
actor.go
341 lines (305 loc) · 7.04 KB
/
actor.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import "math/rand"
//go:generate stringer -type=Action
// Kind is the type of actor (LAD/STONE)
type Kind int
// These consts are for the Kind type
const (
NONE Kind = iota
LAD
STONE
)
// The Actor struct holds info about an actor I.E the Lad or a Rock
type Actor struct {
Type Kind
Y int
X int
Ch byte
Dir Action
DirRequest Action
JumpStep int
}
// Action is what the actor is currently doing
type Action int
// The Action constans define what the Actor currently is, or is requested to be, doing
const (
STOPPED Action = iota
PENDING
UP
UPRIGHT
RIGHT
DOWNRIGHT
DOWN
DOWNLEFT
LEFT
UPLEFT
FALLING
JUMP // Generic jump set by keyhandler
JUMPRIGHT
JUMPUP
JUMPLEFT
)
var jumpLeft = []Action{UPLEFT, UPLEFT, LEFT, LEFT, DOWNLEFT, DOWNLEFT}
var jumpRight = []Action{UPRIGHT, UPRIGHT, RIGHT, RIGHT, DOWNRIGHT, DOWNRIGHT}
var jumpUp = []Action{UP, UP, STOPPED, DOWN, DOWN}
var jumpPaths = map[Action][]Action{
JUMPUP: jumpUp,
JUMPLEFT: jumpLeft,
JUMPRIGHT: jumpRight,
}
var dirs = map[Action]struct {
x, y int
}{
STOPPED: {0, 0},
UP: {0, -1},
UPRIGHT: {1, -1},
RIGHT: {1, 0},
DOWNRIGHT: {1, 1},
DOWN: {0, 1},
DOWNLEFT: {-1, 1},
LEFT: {-1, 0},
UPLEFT: {-1, -1},
FALLING: {0, 1},
JUMP: {0, 0},
JUMPRIGHT: {0, 0},
JUMPLEFT: {0, 0},
JUMPUP: {0, 0},
}
//
// A moving jummp is UR/UR/R/R/DR/DR
// or UL/UL/L/L/DL/DL
// A standing jump is U/U/-/D/D
//
// ====================
// ----234-----23------
// ---1---5----14------
// --0-----6---05------
// ====================
//
//
// MoveActor handles the movements of an Actor
//
func MoveActor(a *Actor, m MapData) {
loopAgain: // If just started falling we need to retest all conditions
// A STONE can only be LEFT/RIGHT/DOWN/FALLING or PENDING
if a.Type == STONE {
if a.DirRequest == PENDING {
return
}
// If stopped select a random direction
if a.Dir == STOPPED {
switch rand.Intn(2) {
case 0:
a.DirRequest = LEFT
case 1:
a.DirRequest = RIGHT
}
}
// Just reverse direction if at the playfield edge
if a.X == 0 || a.X == 78 {
ReverseDirection(a)
}
// Start to fall if not on solid ground
if a.Dir != FALLING && !OnSolid(*a, m) {
a.DirRequest = FALLING
}
// If we just got on a ladder then randomize direction
if OnLadder(*a, m) && (a.Dir == LEFT || a.Dir == RIGHT) {
switch rand.Intn(4) {
case 0:
a.DirRequest = LEFT
case 1:
a.DirRequest = RIGHT
case 2, 3:
a.DirRequest = DOWN
}
}
// If on an Eater kill the stone
if OnEater(*a, m) {
InitActor(a, STONE, dispensers[0])
}
}
// If stopped or going left or going right and requst to do something else, then try to do it
if (a.Dir == STOPPED && a.DirRequest == LEFT) ||
(a.Dir == STOPPED && a.DirRequest == RIGHT) ||
(a.Dir == STOPPED && a.DirRequest == FALLING) ||
(a.Dir == STOPPED && a.DirRequest == UP) ||
(a.Dir == STOPPED && a.DirRequest == DOWN) ||
(a.Dir == LEFT && a.DirRequest == RIGHT) ||
(a.Dir == RIGHT && a.DirRequest == LEFT) ||
(a.Dir == LEFT && a.DirRequest == LEFT) ||
(a.Dir == RIGHT && a.DirRequest == RIGHT) ||
(a.Dir == PENDING && a.DirRequest != PENDING) {
a.Dir = a.DirRequest
a.DirRequest = STOPPED
}
// Handle starting of jumps
if a.DirRequest == JUMP {
switch a.Dir {
case STOPPED:
a.DirRequest = a.Dir
a.Dir = JUMPUP
a.JumpStep = 0
case LEFT:
a.DirRequest = a.Dir
a.Dir = JUMPLEFT
a.JumpStep = 0
case RIGHT:
a.DirRequest = a.Dir
a.Dir = JUMPRIGHT
a.JumpStep = 0
}
}
// Do the jumping
if a.Dir == JUMPUP || a.Dir == JUMPLEFT || a.Dir == JUMPRIGHT {
jd := jumpPaths[a.Dir][a.JumpStep]
switch m.Field[a.Y+dirs[jd].y][a.X+dirs[jd].x] {
case ' ': // Just jumping in air
a.X += dirs[jd].x
a.Y += dirs[jd].y
a.JumpStep++
if a.JumpStep >= len(jumpPaths[a.Dir]) {
a.Dir = a.DirRequest
}
case 'H': // Jumping onto a ladder
a.Y += dirs[jd].y
a.X += dirs[jd].x
a.Dir = STOPPED
a.DirRequest = STOPPED
default: // If bumped into something try falling
a.Dir = FALLING
}
}
// Don't allow player to end up outside of the playfield
ClampToPlayfield(a)
// If at a ladder and want to go up
if (a.DirRequest == UP && m.Field[a.Y][a.X] == 'H') ||
(a.DirRequest == DOWN && m.Field[a.Y][a.X] == 'H') {
a.Dir = a.DirRequest
a.DirRequest = STOPPED
}
// If falling then continue doing that until not in free space anymore,
// then continue the previous direction (if any)
if a.Dir == FALLING {
if OnSolid(*a, m) {
a.Dir = a.DirRequest
} else {
a.Y++
}
return
}
// Climb up until ladder is no more
if a.Dir == UP {
if m.Field[a.Y-1][a.X] == 'H' {
a.Y--
} else {
a.Dir = STOPPED
}
}
// Climb down until ladder is no more
if a.Dir == DOWN {
if m.Field[a.Y+1][a.X] == 'H' {
a.Y++
} else {
a.Dir = STOPPED
}
}
if a.Dir == LEFT && (m.Field[a.Y][a.X] == ' ' || m.Field[a.Y][a.X] == 'H' || m.Field[a.Y][a.X] == '*') {
// Stepped out into the void?, the start falling, but remember the previous direction
if m.Field[a.Y+1][a.X] == ' ' {
a.DirRequest = a.Dir
a.Dir = FALLING
goto loopAgain
}
a.X--
}
if a.Dir == RIGHT && (m.Field[a.Y][a.X] == ' ' || m.Field[a.Y][a.X] == 'H' || m.Field[a.Y][a.X] == '*') {
// Stepped out into the void?, the start falling, but remember the previous direction
if m.Field[a.Y+1][a.X] == ' ' {
a.DirRequest = a.Dir
a.Dir = FALLING
goto loopAgain
}
a.X++
}
// Don't allow player to end up outside of the playfeild
ClampToPlayfield(a)
return
}
//
// ClampToPlayfield makes sure that if the actor tries to walk or jump of the playfield edges
// the actor stays inside the playfield and sart falling//
//
func ClampToPlayfield(a *Actor) {
if a.X <= 0 && (a.Dir == LEFT || a.Dir == JUMPLEFT) {
a.X = 0
a.Dir = FALLING
a.DirRequest = STOPPED
}
if a.X >= 78 && (a.Dir == RIGHT || a.Dir == JUMPRIGHT) {
a.X = 78
a.Dir = FALLING
a.DirRequest = STOPPED
}
}
//
// OnSolid returns true if standing on something solid I.E Floor, Disaperaring floor
// or a Ladder
//
func OnSolid(a Actor, m MapData) bool {
switch m.Field[a.Y+1][a.X] {
case '=', '-', 'H':
return true
}
return false
}
//
// OnLadder returns true when the actor is on a Ladder
//
func OnLadder(a Actor, m MapData) bool {
switch m.Field[a.Y+1][a.X] {
case 'H':
return true
}
return false
}
//
// OnEater returns true when the actor is standing on a Eater
//
func OnEater(a Actor, m MapData) bool {
switch m.Field[a.Y][a.X] {
case '*':
return true
}
return false
}
//
// InitActor set the fields of an Actor type to reasonable
// initial values
//
func InitActor(a *Actor, t Kind, xy XY) {
a.Type = t
a.X = xy.x
a.Y = xy.y
a.Ch = 'X'
switch t {
case LAD:
a.DirRequest = STOPPED
a.Dir = STOPPED
case STONE:
a.Dir = PENDING
a.DirRequest = PENDING
}
}
//
// ReverseDirection makes the actor to go in the opposite direction,
// it only works when the actor is moving left or right
//
func ReverseDirection(a *Actor) {
switch a.Dir {
case LEFT:
a.Dir = RIGHT
case RIGHT:
a.Dir = LEFT
}
}