-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
388 lines (346 loc) · 14.3 KB
/
game.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "game.h"
#include "../RIT/RIT.h"
#include "../engine/ecs/world.h"
#include "../engine/manager/time.h"
#include "../engine/renderer.h"
#include "../timer/timer.h"
#include "LPC17xx.h"
#include "entities/age_textui.h"
#include "entities/creature.h"
#include "entities/cuddle_sfx.h"
#include "entities/happiness_imageui.h"
#include "entities/hungriness_imageui.h"
#include "entities/meal.h"
#include "entities/menu_bar.h"
#include "entities/menu_item_textui.h"
#include "entities/reset_textui.h"
#include "entities/snack.h"
#include "entities/volume_ui.h"
#include "game_assets.h"
void (*on_reset)(void);
void create_meal_entity() {
SpriteSheet meal_sprite_sheet = {.width = 19,
.height = 19,
.frames = 1,
.scale = 1,
&meal_sprite_pixel_index_data[0][0]};
AnimationController anim_controller = {.current_animation = 0,
.current_frame = 0,
.animations = meal_sprite_sheet,
.on_playback = _dummy_playback};
AnimatedSprite sprite = {.offset = {0, 0},
.anim_controller = anim_controller,
.is_currently_playing = 1,
.is_flip_x = 0,
.blend = DEFAULT_BLEND};
Entity meal = {.id = 0,
.tag = ImageUI,
.position = {215, 0},
.sprite = sprite,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = DEFAULT_SCRIPT_COMPONENT,
.is_visible = 0};
meal.script.on_update = meal_on_update;
meal.position.y =
SCREEN_CENTER_Y + 11 * 3 -
meal_sprite_sheet.height; // Used to align with creature height, to
// look like it is on the ground
add_entity(&meal);
}
void create_snack_entity() {
SpriteSheet snack_sprite_sheet = {.width = 17,
.height = 20,
.frames = 1,
.scale = 1,
&snack_sprite_pixel_index_data[0][0]};
AnimationController anim_controller = {.current_animation = 0,
.current_frame = 0,
.animations = snack_sprite_sheet,
.on_playback = _dummy_playback};
AnimatedSprite sprite = {.offset = {0, 0},
.anim_controller = anim_controller,
.is_currently_playing = 1,
.is_flip_x = 0,
.blend = DEFAULT_BLEND};
Entity snack = {.id = 1,
.tag = ImageUI,
.position = {215, 0},
.sprite = sprite,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = DEFAULT_SCRIPT_COMPONENT,
.is_visible = 0};
snack.script.on_update = snack_on_update;
snack.position.y =
SCREEN_CENTER_Y + 11 * 3 -
snack_sprite_sheet.height; // Used to align with creature height, to
// look like it is on the ground
add_entity(&snack);
}
void create_creature_entity() {
SpriteSheet sprite_sheet_idle = {24, 24, 4, 3,
&creature_idle_sheet_pixel_index_data[0][0]};
SpriteSheet sprite_sheet_runaway = {
24, 24, 3, 3, &creature_runaway_sheet_pixel_index_data[0][0]};
SpriteSheet sprite_sheet_run = {24, 24, 7, 3,
&creature_run_sheet_pixel_index_data[0][0]};
// SpriteSheet sprite_sheet_walk = {24, 24, 6, 3,
// &creature_walk_sheet_pixel_index_data[0][0]};
SpriteSheet sprite_sheet_walk = {24, 24, 7, 3,
&creature_run_sheet_pixel_index_data[0][0]};
AnimationController anim_controller = {
.current_animation = 0,
.current_frame = 0,
.animations = {sprite_sheet_idle, sprite_sheet_runaway, sprite_sheet_run,
sprite_sheet_walk},
.on_playback = _dummy_playback};
AnimatedSprite sprite = {
{0, 0}, anim_controller, 1, 0, .blend = DEFAULT_BLEND};
Entity creature = {.id = 2,
.tag = Character,
.position = {0, 0},
.sprite = sprite,
.text = {.offset = {20 * 3 * 0.5, -8},
.content = "",
.color = WHITE_COLOR,
.highlight_color = BACKGROUND_COLOR},
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = DEFAULT_SCRIPT_COMPONENT,
.is_visible = 1};
creature.script.on_load = creature_on_load;
creature.script.on_update = creature_on_update;
creature.script.before_new_frame = creature_before_new_frame;
creature.script.on_pressed = creature_on_pressed;
move_entity_to_screen_center(&creature);
add_entity(&creature);
}
void create_age_textui_entity() {
Entity age = NEW_TEXT_UI_ENTITY(3, 0, 0, "00:00:00", WHITE_COLOR);
age.script.on_update = age_ui_on_update;
move_entity_to_screen_center_x(&age);
add_entity(&age);
}
void create_happiness_imageui_entities() {
SpriteSheet sprite_sheet_happiness = {.width = 13,
.height = 12,
.frames = 1,
.scale = 1,
&happiness_icon_pixel_index_data[0][0]};
AnimationController anim_controller_happiness = {
.current_animation = 0,
.current_frame = 0,
.animations = sprite_sheet_happiness,
.on_playback = _dummy_playback};
AnimatedSprite sprite_happiness = {
.offset = {0, 0},
.anim_controller = anim_controller_happiness,
.is_currently_playing = 1,
.is_flip_x = 0,
.blend = DEFAULT_BLEND};
Script happiness_ui_script = DEFAULT_SCRIPT_COMPONENT;
happiness_ui_script.on_update = happiness_ui_on_update;
Entity happiness_1 = {.id = 4,
.tag = ImageUI,
.position = {20, 40},
.sprite = sprite_happiness,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = happiness_ui_script,
.is_visible = 1};
happiness_1.script.metadata = 1;
add_entity(&happiness_1);
Entity happiness_2 = {.id = 5,
.tag = ImageUI,
.position = {40, 40},
.sprite = sprite_happiness,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = happiness_ui_script,
.is_visible = 1};
happiness_2.script.metadata = 2;
add_entity(&happiness_2);
Entity happiness_3 = {.id = 6,
.tag = ImageUI,
.position = {60, 40},
.sprite = sprite_happiness,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = happiness_ui_script,
.is_visible = 1};
happiness_3.script.metadata = 3;
add_entity(&happiness_3);
}
void create_hungriness_imageui_entities() {
SpriteSheet sprite_sheet_hungriness = {
.width = 16,
.height = 12,
.frames = 1,
.scale = 1,
&hungriness_icon_pixel_index_data[0][0]};
AnimationController anim_controller_hungriness = {
.current_animation = 0,
.current_frame = 0,
.animations = sprite_sheet_hungriness,
.on_playback = _dummy_playback};
AnimatedSprite sprite_hungriness = {
.offset = {0, 0},
.anim_controller = anim_controller_hungriness,
.is_currently_playing = 1,
.is_flip_x = 0,
.blend = DEFAULT_BLEND};
Script hungriness_ui_script = DEFAULT_SCRIPT_COMPONENT;
hungriness_ui_script.on_update = hungriness_ui_on_update;
Entity hungriness_1 = {.id = 7,
.tag = ImageUI,
.position = {164, 40},
.sprite = sprite_hungriness,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = hungriness_ui_script,
.is_visible = 1};
hungriness_1.script.metadata = 1;
add_entity(&hungriness_1);
Entity hungriness_2 = {
.id = 8,
.tag = ImageUI,
.position = {184, 40},
.sprite = sprite_hungriness,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = hungriness_ui_script,
.is_visible = 1,
};
hungriness_2.script.metadata = 2;
add_entity(&hungriness_2);
Entity hungriness_3 = {.id = 9,
.tag = ImageUI,
.position = {204, 40},
.sprite = sprite_hungriness,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = hungriness_ui_script,
.is_visible = 1};
hungriness_3.script.metadata = 3;
add_entity(&hungriness_3);
}
void create_meal_textui_entity() {
Entity meal_textui =
NEW_TEXT_UI_ENTITY(10, 0, MAX_SCREEN_Y - 30, "Meal", WHITE_COLOR);
meal_textui.script.metadata = 1;
meal_textui.script.on_update = menu_item_ui_on_update;
Position center = screen_centered_entity_position(&meal_textui);
meal_textui.position.x = center.x - MAX_SCREEN_X / 4;
add_entity(&meal_textui);
}
void create_snack_textui_entity() {
Entity snack_textui =
NEW_TEXT_UI_ENTITY(11, 0, MAX_SCREEN_Y - 30, "Snack", WHITE_COLOR);
snack_textui.script.metadata = 2;
snack_textui.script.on_update = menu_item_ui_on_update;
Position center = screen_centered_entity_position(&snack_textui);
snack_textui.position.x = center.x + MAX_SCREEN_X / 4;
add_entity(&snack_textui);
}
void create_menu_bar_logic_entity() {
Entity menu_bar = NEW_TEXT_UI_ENTITY(12, 0, 0, "", WHITE_COLOR);
menu_bar.script.on_load = menu_bar_on_load;
menu_bar.script.on_input = menu_bar_on_input;
add_entity(&menu_bar);
}
void create_reset_textui_entity() {
Entity reset_textui =
NEW_TEXT_UI_ENTITY(13, 0, MAX_SCREEN_Y - 90, "RESET", BACKGROUND_COLOR);
reset_textui.script.on_load = reset_ui_on_load;
reset_textui.script.on_update = reset_ui_on_update;
reset_textui.script.on_input = reset_ui_on_input;
Position center = screen_centered_entity_position(&reset_textui);
reset_textui.position.x = center.x;
add_entity(&reset_textui);
}
void create_cuddle_sfx_entity() {
SpriteSheet sprite_sheet_happiness = {.width = 13,
.height = 12,
.frames = 1,
.scale = 1,
&happiness_icon_pixel_index_data[0][0]};
AnimationController anim_controller = {.current_animation = 0,
.current_frame = 0,
.animations = {sprite_sheet_happiness},
.on_playback = _dummy_playback};
AnimatedSprite sprite = {
{0, 0}, anim_controller, 1, 0, .blend = DEFAULT_BLEND};
Entity cuddle_sfx = {.id = 14,
.tag = Character,
.position = {0, 0},
.sprite = sprite,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = DEFAULT_SCRIPT_COMPONENT,
.is_visible = 1};
cuddle_sfx.script.on_load = cuddle_sfx_on_load;
cuddle_sfx.script.before_new_frame = cuddle_sfx_before_new_frame;
add_entity(&cuddle_sfx);
}
void create_volume_ui_entity() {
SpriteSheet sprite_sheet_volume_on = {.width = 13,
.height = 10,
.frames = 1,
.scale = 1,
&volume_on_icon_pixel_index_data[0][0]};
SpriteSheet sprite_sheet_volume_off = {
.width = 13,
.height = 10,
.frames = 1,
.scale = 1,
&volume_off_icon_pixel_index_data[0][0]};
AnimationController anim_controller = {
.current_animation = 0,
.current_frame = 0,
.animations = {sprite_sheet_volume_on, sprite_sheet_volume_off},
.on_playback = _dummy_playback};
AnimatedSprite sprite = {
{0, 0}, anim_controller, 1, 0, .blend = DEFAULT_BLEND};
Entity volume_icon = {.id = 15,
.tag = ImageUI,
.position = {MAX_SCREEN_X - 35, 2},
.sprite = sprite,
.text = DEFAULT_TEXT_COMPONENT,
.kinematic = DEFAULT_KINEMATIC_COMPONENT,
.script = DEFAULT_SCRIPT_COMPONENT,
.is_visible = 1};
volume_icon.script.on_update = volume_ui_on_update;
add_entity(&volume_icon);
}
void game_world_setup() {
renderer_setup();
world_reset();
create_meal_entity();
create_snack_entity();
create_creature_entity();
create_age_textui_entity();
create_happiness_imageui_entities();
create_hungriness_imageui_entities();
create_meal_textui_entity();
create_snack_textui_entity();
create_menu_bar_logic_entity();
create_reset_textui_entity();
create_cuddle_sfx_entity();
create_volume_ui_entity();
// disable_timer(0);
// disable_RIT();
//
// reset_RIT();
// reset_timer(0);
reset_clock();
init_RIT(RIT_INTERVAL_IN_CLOCK_COUNT);
// init_timer(0, 0, 0, 3, ONE_SECOND_FOR_TIMER_IN_CLOCK_COUNT);
// /*init timer 0, use MatchReg 0, in interrupt only mode (1), with value
// 25,000,000 (clock count to reach 1 second with 25MHz frequency)*/
// enable_timer(0);
enable_RIT();
NVIC_SetPriority(RIT_IRQn, 3);
// NVIC_SetPriority(TIMER0_IRQn, 2);
NVIC_SetPriority(TIMER1_IRQn, 1);
on_reset = game_world_setup;
}