summaryrefslogtreecommitdiff
path: root/src/game.c
blob: 316a88f759cc827291f6ec36f517760e140b6a65 (plain)
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
/*
 *  Copyright (C) 2018  Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <SDL.h>
#include <SDL_mixer.h>

#include "aabb.h"
#include "whale.h"
#include "whale-svg.h"
#include "macros.h"
#include "stage.h"
#include "svg-node.h"

/* the game */

typedef enum whale_game_fsm_t {
	WHALE_GAME_SETTING_UP,
	WHALE_GAME_STARTING,
	WHALE_GAME_TARGETING,
	WHALE_GAME_LAUNCHING,
	WHALE_GAME_FLYING,
	WHALE_GAME_INSIDE,
	WHALE_GAME_OUTSIDE,
	WHALE_GAME_LEAVE
} whale_game_fsm_t;

static Mix_Chunk	*launch_sfx, *crash_sfx, *winner_sfx;
static stage_node_t	*bobby, *crater, *astro, *planets;
static unsigned		bobby_armed;
static whale_game_fsm_t	state;

void whale_game_event(whale_t *whale, SDL_Event *ev)
{
	stage_t	*stage = whale_get_stage(whale);

	switch (ev->type) {
	case SDL_KEYDOWN:
		switch (ev->key.keysym.sym) {
		case SDLK_ESCAPE:
			state = WHALE_GAME_LEAVE;
			break;

		case SDLK_SPACE:
			if (state == WHALE_GAME_INSIDE || state == WHALE_GAME_OUTSIDE) {
				/* resetup game */
				state = WHALE_GAME_SETTING_UP;
			}

		default:
			break;
		}

		break;

	case SDL_MOUSEBUTTONDOWN:
		if (state == WHALE_GAME_TARGETING) {
			if (!bobby_armed)
				bobby_armed = whale_ticks(whale, WHALE_TICKS_TIMER);
		} else if (state == WHALE_GAME_INSIDE || state == WHALE_GAME_OUTSIDE) {
			/* resetup game */
			state = WHALE_GAME_SETTING_UP;
		}
		break;

	case SDL_MOUSEBUTTONUP:
		if (state == WHALE_GAME_TARGETING) {
			if (bobby_armed) {
				/* launch bobby */
				Mix_PlayChannel(-1, launch_sfx, 0);
				state = WHALE_GAME_LAUNCHING;
			}
		}
		break;

	case SDL_MOUSEMOTION:
		if (state == WHALE_GAME_TARGETING) {
			/* convert the mouse X, Y coordinates into an angle, it's
			 * trivial since the whale is always in the bottom left
			 */
			int	width, height;
			double	angle;
			v2f_t	v;

			stage_get_output_size(stage, &width, &height);
			
			v.x = (float)ev->motion.x;
			v.y = (float)height - ev->motion.y;

			v = v2f_normalize(&v);
			angle = -atan2f(v.y, v.x) * 57.4712f /* rad2deg */;
			
			stage_node_set_angle(stage, bobby, angle);
		}
		break;

	default:
		break;
	}
}


void whale_game_update(whale_t *whale)
{
	static const aabb_t	bobby_end_aabb = {{-1.f, -1.f}, {-.8f, -.8f}};
	static aabb_t		crater_end_aabb = {{0.f, -1.f}, {0.f, -.7f}};
	static aabb_t		bobby_start_aabb, crater_start_aabb;
	static int		bobby_keyboard_armed, prev;
	static whale_svg_t	*bobby_svg, *dead_svg;
	static int		initialized;
	static v2f_t		bobby_vector;
	stage_t			*stage = whale_get_stage(whale);
	unsigned		now = whale_ticks(whale, WHALE_TICKS_TIMER);

	if (!initialized) {
		bobby = stage_node_lookup_name(stage, "bobby");
		fatal_if(!bobby, "Unable to lookup whale node");
		stage_node_get_object(stage, bobby, (void **)&bobby_svg); /* XXX FIXME: NOO */

		dead_svg = whale_svg_new_file("assets/dead.svg");
		fatal_if(!dead_svg, "Unable to load dead bobby svg");

		crater = stage_node_lookup_name(stage, "crater");
		fatal_if(!crater, "Unable to lookup crater node");

		astro = stage_node_lookup_name(stage, "astro");
		fatal_if(!astro, "Unable to lookup astro node");

		planets = svg_node_new_file(stage, "planets", "assets/planets.svg", 0, (aabb_t){{-1.f, -.7f}, {1.f, 1.f}});
		fatal_if(!planets, "Unable to load planets svg");
		stage_node_set_active(stage, planets);

		stage_node_set_alpha(stage, bobby, 1.0f);
		stage_node_set_alpha(stage, crater, 1.0f);

		winner_sfx = Mix_LoadWAV("assets/winner.wav");
		fatal_if(!winner_sfx, "Unable to load winner sfx");

		launch_sfx = Mix_LoadWAV("assets/launch.wav");
		fatal_if(!launch_sfx, "Unable to load launch sfx");

		crash_sfx = Mix_LoadWAV("assets/crash.wav");
		fatal_if(!crash_sfx, "Unable to load crash sfx");

		initialized = 1;
	}

	switch (state) {
	case WHALE_GAME_SETTING_UP: {
		float	crater_x;

		stage_node_set_angle(stage, bobby, 0);
		stage_node_get_aabb(stage, bobby, &bobby_start_aabb);
		stage_node_get_aabb(stage, crater, &crater_start_aabb);
		stage_node_set_object(stage, bobby, bobby_svg);

		/* place crater destination randomly along the bottom */
		crater_x = ((float)rand()) / RAND_MAX * .6f;
		crater_end_aabb.min.x = crater_x - .2f;
		crater_end_aabb.max.x = crater_x + .2f;

		bobby_keyboard_armed = bobby_armed = 0;
		whale_ticks_reset(whale, WHALE_TICKS_TIMER);
		prev = now = whale_ticks(whale, WHALE_TICKS_TIMER);
		state++;
		break;
	}

	case WHALE_GAME_STARTING:
		if (now < 500) {
			float	t = ((float)now) * (1.0f / 500.0f);
			aabb_t	aabb;

			t *= t;

			aabb = aabb_lerp(&bobby_start_aabb, &bobby_end_aabb, t);
			stage_node_set_aabb(stage, bobby, &aabb);

			aabb = aabb_lerp(&crater_start_aabb, &crater_end_aabb, t);
			stage_node_set_aabb(stage, crater, &aabb);

			if (astro) { /* XXX this is ugly; astro is serving as a "first start" flag */
				stage_node_set_alpha(stage, astro, 1.f - t);
				stage_node_set_alpha(stage, planets, t);
			}
		} else {
			stage_node_set_aabb(stage, bobby, &bobby_end_aabb);
			stage_node_set_aabb(stage, crater, &crater_end_aabb);

			if (astro) {
				astro = stage_node_free(stage, astro);
				stage_node_set_alpha(stage, planets, 1.f); /* XXX: see above comment about astro abuse */
			}

			state++;
		}
		break;

	case WHALE_GAME_TARGETING: {
		const Uint8	*key_state = SDL_GetKeyboardState(NULL);
		float		t = ((float)(now - prev)) * .05f;	/* scale simulation things according to time passed */

		if (key_state[SDL_SCANCODE_LEFT] || key_state[SDL_SCANCODE_A]) {
			double	angle;
			
			stage_node_get_angle(stage, bobby, &angle);
			angle += -3.f * t;
			stage_node_set_angle(stage, bobby, angle);
		}

		if (key_state[SDL_SCANCODE_RIGHT] || key_state[SDL_SCANCODE_D]) {
			double	angle;

			stage_node_get_angle(stage, bobby, &angle);
			angle += 3.f * t;
			stage_node_set_angle(stage, bobby, angle);
		}

		if (key_state[SDL_SCANCODE_SPACE]) {
			/* arm bobby */
			if (!bobby_keyboard_armed) {
				bobby_keyboard_armed = 1;
				bobby_armed = now;
			}
		} else if (bobby_keyboard_armed) {
			/* launch bobby */
			Mix_PlayChannel(-1, launch_sfx, 0);
			state = WHALE_GAME_LAUNCHING;
		}
		break;
	}

	case WHALE_GAME_LAUNCHING: {
		double	angle;
		double	power = (now - bobby_armed) * (1.0f / 5000);

		/* convert angle and power into a vector */
		stage_node_get_angle(stage, bobby, &angle);
		angle *= -(M_PI / 180.f); /* to radians */
		bobby_vector.x = cos(angle) * power;
		bobby_vector.y = sin(angle) * power;

		state++;
		break;
	}

	case WHALE_GAME_FLYING: {
		float	t = ((float)(now - prev)) * .05f;	/* scale simulation things according to time passed */
		double	angle;
		v2f_t	move;
		aabb_t	aabb;

		move = v2f_mult_scalar(&bobby_vector, t);

		/* spin the bobby while he's flying */
		stage_node_get_angle(stage, bobby, &angle);
		angle += 4.0f * t;
		stage_node_set_angle(stage, bobby, angle);

		/* move him on his trajectory */
		/* XXX: this is pretty silly, but it's the interfaces I have at the moment */
		stage_node_get_aabb(stage, bobby, &aabb);
		aabb.min = v2f_add(&aabb.min, &move);
		aabb.max = v2f_add(&aabb.max, &move);
		stage_node_set_aabb(stage, bobby, &aabb);

		/* check if we're inside the crater (crater aabb is .4f wide) */
		/* FIXME: I should really add some generic aabb overlap test type stuff to aabb.h */
		/* FIXME XXX: yes I know the collision detection is janky/ad-hoc, there was no time! */
		if (aabb.min.x > (crater_end_aabb.min.x + .08f) &&
		    aabb.max.x < (crater_end_aabb.max.x - .08f) &&
		    aabb.min.y < (crater_end_aabb.max.y - .06f)) {

			state = WHALE_GAME_INSIDE;
			Mix_PlayChannel(-1, winner_sfx, 0);

		} else if (aabb.min.y < -1.f) { /* or if we're in the ground */

			state = WHALE_GAME_OUTSIDE;
			Mix_PlayChannel(-1, crash_sfx, 0);
			stage_node_set_object(stage, bobby, dead_svg);
		}

		/* alter trajectory w/gravity */
		bobby_vector.y += -.01f * t;
		break;
	}

	case WHALE_GAME_INSIDE: {
		aabb_t	aabb;

		/* bobby simming inside crater lake */
		aabb.min.x = crater_end_aabb.min.x + .1f;
		aabb.max.x = crater_end_aabb.max.x - .1f;
		aabb.min.y = crater_end_aabb.max.y - .2f;
		aabb.max.y = crater_end_aabb.max.y;
		stage_node_set_aabb(stage, bobby, &aabb);

		stage_node_set_angle(stage, bobby, cos((double)now * .001) * 10.0);

		break;
	}

	case WHALE_GAME_OUTSIDE:
		break;

	case WHALE_GAME_LEAVE:
		whale_set_context(whale, WHALE_CONTEXT_CREDITS);
		break;
	}

	/* last 20 seconds of song are credits */
	if (whale_ticks(whale, WHALE_TICKS_MUSIC) > 206000)
		whale_set_context(whale, WHALE_CONTEXT_CREDITS);

	prev = now;
}
© All Rights Reserved