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
|
#include <stdint.h>
#include <stdlib.h>
#include "vga.h"
#include "sound.h"
#include "entities.h"
#include "game.h"
#include "player.h"
#include "effect.h"
#include "pickup.h"
#define MAX_FRAME 3
#define MAX_BONUS_FRAME 4
#define MAX_TTL ((uint16_t)800)
typedef enum
{
PICKUP_TIME = 0,
PICKUP_BONUS,
PICKUP_PICKAXE,
PICKUP_GOLD_KEY,
PICKUP_SILVER_KEY,
} PickupType;
static const Rect frames_in[2 * 4] =
{
{ 96, 16, 144, 144 },
{ 128, 16, 144, 144 },
{ 112, 16, 144, 144 },
/* not used */
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 }
};
static const Rect frames[2 * 4] =
{
/* time */
{ 80, 16, 144, 144 },
/* pickaxe */
{ 64, 16, 144, 144 },
/* gold key */
{ 0, 0, 144, 144 },
/* siver key */
{ 0, 0, 144, 144 },
/* not used */
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 }
};
static const Rect frames_bonus0[2 * 4] =
{
{ 0, 48, 144, 144 },
{ 16, 48, 144, 144 },
{ 32, 48, 144, 144 },
{ 48, 48, 144, 144 },
/* not used */
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 }
};
static const Rect frames_bonus1[2 * 4] =
{
{ 0, 64, 144, 144 },
{ 16, 64, 144, 144 },
{ 32, 64, 144, 144 },
{ 48, 64, 144, 144 },
/* not used */
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 }
};
static const Rect frames_bonus2[2 * 4] =
{
{ 64, 64, 144, 144 },
{ 80, 64, 144, 144 },
{ 96, 64, 144, 144 },
{ 112, 64, 144, 144 },
/* not used */
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 },
{ 0, 0, 144, 144 }
};
static const Rect *frames_bonuses[] =
{
frames_bonus0, frames_bonus1, frames_bonus2
};
void pickup_time_init(Entity *e)
{
e->frames = (const Rect *)frames_in;
e->flags = PICKUP_TIME;
e->counter = MAX_TTL * 2 + (rand() % MAX_TTL);
e->update = pickup_wait_update;
}
void pickup_bonus_init(Entity *e)
{
e->frames = (const Rect *)frames_in;
e->flags = PICKUP_BONUS;
e->counter = MAX_TTL + (rand() % MAX_TTL);
e->update = pickup_wait_update;
}
void pickup_pickaxe_init(Entity *e)
{
e->frames = (const Rect *)frames_in;
e->flags = PICKUP_PICKAXE;
e->counter = MAX_TTL / 2 + (rand() % MAX_TTL);
e->update = pickup_wait_update;
}
void pickup_wait_update(Entity *e)
{
if (e->counter-- == 0)
{
e->update = pickup_in_update;
sound_play_efx(EFX_WARP);
}
}
void pickup_in_update(Entity *e)
{
if (e->delay++ == WALK_DELAY - 2)
{
e->delay = 0;
e->frame++;
if (e->frame == MAX_FRAME)
{
switch (e->flags)
{
case PICKUP_TIME:
e->frames = (const Rect *)frames;
e->frame = 0;
break;
case PICKUP_BONUS:
e->frames = (const Rect *)frames_bonuses[rand() % 3];
e->frame = 0;
break;
case PICKUP_PICKAXE:
e->frames = (const Rect *)frames;
e->frame = 1;
break;
}
e->counter = 0;
e->update = pickup_update;
}
}
}
void pickup_update(Entity *e)
{
if (e->counter++ == MAX_TTL)
{
effect_out_init(e);
sound_play_efx(EFX_WARP);
return;
}
/* the bonuses have an animation */
if (e->flags == PICKUP_BONUS)
{
e->delay++;
/* first frame lasts longer */
if ((e->frame == 0 && e->delay == WALK_DELAY * 4)
|| (e->frame != 0 && e->delay == WALK_DELAY - 2))
{
e->delay = 0;
e->frame++;
if (e->frame == MAX_BONUS_FRAME)
e->frame = 0;
}
}
if (player_collision_pickup(e))
{
switch (e->flags)
{
case PICKUP_TIME:
reset_time();
break;
case PICKUP_BONUS:
add_score(250);
break;
case PICKUP_PICKAXE:
add_pickaxe();
break;
}
e->used = 0;
sound_play_efx(EFX_PICKUP);
}
}
|