aboutsummaryrefslogtreecommitdiff
path: root/world.c
blob: ae64049b07d6673223b42ff41d85e1c94c64f010 (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
#include "plat.h"
#include "world.h"

void init_world(World* w) {
	w->particle_count = 0;
	w->enemy_count = 0;
	w->deathzone_count = 0;
	w->effect_count = 0;
	w->projectile_count = 0;
	w->frame = 0;
	w->freeze = 0;
	w->kills = 0;
	init_player(&w->player);
	init_laser(&w->laser);
}

Particle* inst_particle(
	World* w,
	int x,
	int y,
	int anim,
	int bmp
) {
	Particle* p;
	assert(w->particle_count < max_particles);
	p = &w->particles[w->particle_count++];
	init_particle(p, x, y, anim, bmp);
	return p;
}

Enemy* inst_enemy(
	World* w,
	Enemy_Type t,
	int x,
	int y
) {
	Enemy* e;
	assert(w->enemy_count < max_enemies);
	e = &w->enemies[w->enemy_count++];
	init_enemy(e, t, x, y);
	return e;
}

Deathzone* inst_deathzone(
	World* w,
	const Rect* r,
	int vx,
	int vy,
	int hp,
	int life,
	int friendly
) {
	Deathzone* d;
	assert(w->deathzone_count < max_deathzones);
	d = &w->deathzones[w->deathzone_count++];
	init_deathzone(d, r, vx, vy, hp, life, friendly);
	return d;
}

Effect* inst_effect(
	World* w,
	int x,
	int y,
	int vx,
	int vy,
	int c
) {
	Effect* e;
	assert(w->effect_count < max_effects);
	e = &w->effects[w->effect_count++];
	init_effect(e, x, y, vx, vy, c);
	return e;
}

Projectile* inst_projectile(
	World* w,
	int x,
	int y,
	const Rect* r,
	int anim
) {
	Projectile* p;
	assert(w->projectile_count < max_projectiles);
	p = &w->projectiles[w->projectile_count++];
	init_projectile(p, x, y, r, anim);
	return p;
}

void update_world(World* w, const App* a) {
	int i;
	if (w->laser.life)
		update_laser(&w->laser);
	for (i = w->particle_count - 1; i >= 0; i--)
		if (update_particle(&w->particles[i]))
			w->particles[i] = w->particles[--w->particle_count];
	if (w->freeze) {
		w->freeze--;
		return;
	}
	for (i = w->deathzone_count - 1; i >= 0; i--)
		if (update_deathzone(&w->deathzones[i]))
			w->deathzones[i] = w->deathzones[--w->deathzone_count];
	update_player(&w->player, w, a, &w->map);
	for (i = w->effect_count - 1; i >= 0; i--)
		if (update_effect(&w->effects[i]))
			w->effects[i] = w->effects[--w->effect_count];
	for (i = w->enemy_count - 1; i >= 0; i--)
		if (update_enemy(&w->enemies[i], w)) {
			w->enemies[i] = w->enemies[--w->enemy_count];
			w->kills++;
		}
	for (i = w->projectile_count - 1; i >= 0; i--)
		if (update_projectile(&w->projectiles[i], w))
			w->projectiles[i] = w->projectiles[--w->projectile_count];
	w->frame++;
}

void ren_world(const World* w, struct Renderer* r) {
	int i;
	ren_player(&w->player, r);
	render_map(&w->map, r);
	for (i = 0; i < w->particle_count; i++)
		ren_particle(&w->particles[i], r);
	for (i = 0; i < w->effect_count; i++)
		ren_effect(&w->effects[i], r);
	for (i = 0; i < w->enemy_count; i++)
		ren_enemy(&w->enemies[i], r);
	for (i = 0; i < w->projectile_count; i++)
		ren_projectile(&w->projectiles[i], r);
	if (w->laser.life)
		ren_laser(&w->laser, r);
	ren_hud(w, r);
}