-
Notifications
You must be signed in to change notification settings - Fork 28
/
example-platformer-tiled.c
217 lines (178 loc) · 4.81 KB
/
example-platformer-tiled.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
// Build with: build example-platformer
// TODO: Player sprite and animation (walk, jump)
// TODO: Death condition.
// TODO: Camera.
// TODO: Parallax.
#define PUNITY_IMPLEMENTATION
#include "punity.h"
#define TILED_IMPLEMENTATION
#include "punity-tiled.h"
typedef struct
{
SceneEntity *E;
int jump;
int fall;
int grounded;
}
Player;
typedef struct Game_
{
Bitmap font;
Scene scene;
TiledScene scene1;
Bitmap tileset;
Sound sound_jump;
Sound sound_land;
Player player1;
Player player2;
}
Game;
static Game *GAME = 0;
enum {
SceneLayer_Ground = 1 << 0,
SceneLayer_Player = 1 << 1,
};
//
// Player
//
void
player_init(Player *P, TiledItem *item)
{
P->E = scene_entity_add(&GAME->scene,
item->rect,
SceneLayer_Player,
SceneLayer_Player | SceneLayer_Ground);
P->jump = 0;
P->fall = 1;
}
void
player_step(Player *P, int key_left, int key_right, int key_jump)
{
Collision C = {0};
// Left & right.
int dx = 0;
if (key_right) { dx = +1; }
if (key_left) { dx = -1; }
scene_entity_move_x(&GAME->scene, P->E, dx * 2, &C);
// Jump & fall.
static int PLAYER_JUMP[] = { 5, 4, 3, 2, 2, 1, 1, 1 };
if (key_jump && P->grounded) {
P->jump = 1;
sound_play(&GAME->sound_jump);
}
if (!P->jump) {
P->fall = minimum(P->fall + 1, 16);
if (!scene_entity_move_y(&GAME->scene, P->E, P->fall, &C)) {
if (!P->grounded) {
sound_play(&GAME->sound_land);
P->grounded = 1;
}
P->fall = 2;
} else {
P->grounded = 0;
}
} else {
P->grounded = 0;
P->jump++;
if (P->jump >= array_count(PLAYER_JUMP) ||
scene_entity_move_y(&GAME->scene, P->E, -(PLAYER_JUMP[P->jump - 1] * 2), &C) == false)
{
P->jump = 0;
}
}
// Draw the player.
rect_draw_push(P->E->box, 2, 10);
}
//
// Scene
//
int
scene_setup(TiledScene *tiled_scene)
{
// EXAMPLE FOR QUICK SETUP:
// Set this tilemap as base for collision detection.
// GAME->scene.tilemap = &tiledscene_find(tiled_scene, "base", 0)->tilemap;
// Set player from `player` rectangle object.
// player_init(&GAME->player1, tiledscene_find(tiled_scene, "player", 0)->item);
// MORE ADVANCED SETUP:
// Initialize entities.
for (TiledItem *item = tiled_scene->items;
item != tiled_scene->items_end;
item = tileditem_next(item))
{
switch (item->type)
{
case TiledType_TileMap:
if (strcmp(item->name, "base")) {
GAME->scene.tilemap = &item->tilemap;
}
break;
case TiledType_Rectangle:
if (strcmp(item->name, "player") == 0) {
player_init(&GAME->player1, item);
}
break;
case TiledType_Image:
break;
}
}
return 1;
}
//
// Game
//
int
init()
{
CORE->window.width = 16 * 8;
CORE->window.height = 16 * 8;
CORE->window.scale = 5;
GAME = bank_push_t(CORE->storage, Game, 1);
sound_load_resource(&GAME->sound_jump, "jump.ogg");
sound_load_resource(&GAME->sound_land, "land.ogg");
font_load_resource(&GAME->font, "font.png", 4, 7);
CORE->canvas.font = &GAME->font;
bitmap_load_resource(&GAME->tileset, "tileset.png", 0);
GAME->tileset.tile_width = 8;
GAME->tileset.tile_height = 8;
// Load scenes.
Tiled tiled;
tiled_init(&tiled);
tiledscene_load_resource(&tiled, &GAME->scene1, "map1.json", 0, 0);
tiled_free(&tiled);
// Initialize game scene.
scene_init(&GAME->scene, 16);
// Start at first scene.
scene_setup(&GAME->scene1);
return 1;
}
void
step()
{
canvas_clear(1);
if (key_down(KEY_ESCAPE)) {
CORE->running = 0;
}
if (key_pressed(KEY_RETURN)) {
window_fullscreen_toggle();
}
player_step(&GAME->player1,
key_down(KEY_LEFT), key_down(KEY_RIGHT), key_pressed(KEY_UP));
player_step(&GAME->player2,
key_down(KEY_A), key_down(KEY_D), key_pressed(KEY_W));
tilemap_draw(GAME->scene.tilemap);
// // Draw tilemap.
// int *t = tiles;
// for (int y = 0; y != 16; ++y) {
// for (int x = 0; x != 16; ++x, ++t) {
// switch (*t) {
// case 0: case 88: case 89: break;
// default:
// tile_draw_push(&GAME->tileset, x * 8, y * 8, *t, 4);
// }
// }
// }
char buf[256];
sprintf(buf, "%.3fms %.3f", CORE->perf_step * 1e3, CORE->time_delta);
text_draw(buf, 0, 0, 2);
}