aboutsummaryrefslogtreecommitdiff
path: root/game.c
blob: 73d062b6daacb7014e8c31ced308610eeb1bfb1c (plain) (blame)
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
#include "asset.h"
#include "enemy.h"
#include "game.h"
#include "platform.h"
#include "sprite.h"
#include "systems.h"

static void menu_new(Menu* menu) {
	Game* game;

	game = menu->ptr;
	game_change_state(game, game_state_game);
}

static void menu_restart(Menu* menu) {
	Game* game;

	game = menu->ptr;
	game_change_state(game, game_state_game);
}

static void menu_main_menu(Menu* menu) {
	Game* game;

	game = menu->ptr;
	game_change_state(game, game_state_menu);
}

static void menu_quit(Menu* menu) {
	(void)menu;
	platform_quit();
}

static void menu_credits(Menu* menu) {
	Game* game;

	game = menu->ptr;
	game_change_state(game, game_state_credits);
}

static void menu_init(Game* game) {
	const BM_Font* font;

	font = get_default_font();

	init_menu(&game->menu, game);
	menu_add(&game->menu, "New", menu_new, font);
	menu_add(&game->menu, "Continue", 0, font);
	menu_add(&game->menu, "Credits", menu_credits, font);
	menu_add(&game->menu, "Quit", menu_quit, font);
}

static void menu_update(Game* game) {
	const Sprite* msg;
	const Bitmap* bmp;
	int h, y;

	update_menu(&game->menu);

	h = game->menu.h;
	y = game->menu.y;

	msg = get_sprite(sprite_author);
	bmp = get_bitmap(msg->bitmap);
	render_bitmap(
		bmp,
		renderer_w / 2 - msg->rect.w / 2,
		renderer_h - 26,
		&msg->rect
	);

	msg = get_sprite(sprite_free);
	bmp = get_bitmap(msg->bitmap);
	render_bitmap(
		bmp,
		renderer_w / 2 - msg->rect.w / 2,
		renderer_h - 15,
		&msg->rect
	);

	render_menu(&game->menu, get_default_font());
}

static void menu_deinit(Game* game) {

}

static void gameplay_init(Game* game) {
	init_world(&game->world);

	init_player(&game->world.player, &game->world);

	new_skull(&game->world, 0, 0);
}

static void gameplay_update(Game* game) {
	update_player(&game->world.player, &game->world);
	enemy_system(&game->world);
	bullet_system(&game->world);
	collision_system(&game->world);
	animation_system(&game->world);
	sprite_system(&game->world);
}

static void gameplay_deinit(Game* game) {

}

static void credits_init(Game* game) {

}

static void credits_update(Game* game) {

}

static void credits_deinit(Game* game) {

}

static void dead_init(Game* game) {
	const BM_Font* font = get_default_font();
	init_menu(&game->menu, game);
	menu_add(&game->menu, "Restart", menu_restart, font);
	menu_add(&game->menu, "Main Menu", menu_main_menu, font);
}

static void dead_update(Game* game) {
	const BM_Font* font;

	font = get_default_font();

	update_menu(&game->menu);

	sprite_system(&game->world);

	rfont_text(
		font,
		renderer_w / 2 - 40,
		game->menu.y - 15,
		"You died"
	);
	render_menu(&game->menu, get_default_font());
}

static void dead_deinit(Game* game) {

}

static const Game_State_Fns game_state_fns[] = {
	/* game_state_menu */
	{
		menu_init,
		menu_update,
		menu_deinit
	},
	/* game_state_game */
	{
		gameplay_init,
		gameplay_update,
		gameplay_deinit
	},
	/* game_state_credits */
	{
		credits_init,
		credits_update,
		credits_deinit
	},
	/* game_state_dead */
	{
		dead_init,
		dead_update,
		dead_deinit
	}
};

static const Game_State_Fns* get_state_fns(Game_State state) {
	return &game_state_fns[state];
}

void game_init(Game* game, Game_State state) {
	game->state = state;
	game->fns = get_state_fns(state);
	game->fns->state_init(game);
}

void game_update(Game* game) {
	game->fns->state_update(game);
}

void game_deinit(Game* game) {
	game->fns->state_deinit(game);
}

void game_change_state(Game* game, Game_State state) {
	game->fns->state_deinit(game);
	game->state = state;
	game->fns = get_state_fns(state);
	game->fns->state_init(game);
}