commit fcd90e72f24211f1accd6c1413711e1169591e54
parent 989b1f7f1620efd7e949daa9941c1fd08ca73c1b
Author: Matsuda Kenji <info@mtkn.jp>
Date: Wed, 4 Jan 2023 11:08:10 +0900
add goal flag
Diffstat:
M | ex9/main.c | | | 60 | +++++++++++++++++++++++++++++++++++++++++++++++++----------- |
M | ex9/world_map.h | | | 50 | +++++++++++++++++++++++++------------------------- |
2 files changed, 74 insertions(+), 36 deletions(-)
diff --git a/ex9/main.c b/ex9/main.c
@@ -120,26 +120,36 @@ game_play(void)
struct timespec ts;
struct OH *blh[WORLD_WIDTH]; // block list header
struct OL *blc;
- int scroll_dst = 35;
+ struct OH *flh; // flag
+ struct OL *flc;
+
+ int scroll_dst = 0;
for (int xi = 0; xi < WORLD_WIDTH; xi++)
blh[xi] = create_ol();
+ flh = create_ol();
for (int i = 0; i < WORLD_WIDTH * WORLD_HEIGHT; i++) {
if (world_map[i] == 'b') {
int xi = i % WORLD_WIDTH;
// TODO: don't forget to free these things.
append_ol(blh[xi], create_object(i % WORLD_WIDTH * BLOCK_SIZE,
- i / WORLD_WIDTH * BLOCK_SIZE,
- 0, 0, 0, 0,
- BLOCK_SIZE, BLOCK_SIZE,
- BLOCK_SIZE * BLOCK_SIZE));
+ i / WORLD_WIDTH * BLOCK_SIZE,
+ 0, 0, 0, 0,
+ BLOCK_SIZE, BLOCK_SIZE,
+ BLOCK_SIZE * BLOCK_SIZE));
} else if (world_map[i] == 'p') {
player = create_object(i % WORLD_WIDTH * BLOCK_SIZE,
- i / WORLD_WIDTH * BLOCK_SIZE,
- 0, 0, 0, GRAVITY,
- BLOCK_SIZE, BLOCK_SIZE,
- BLOCK_SIZE * BLOCK_SIZE);
+ i / WORLD_WIDTH * BLOCK_SIZE,
+ 0, 0, 0, GRAVITY,
+ BLOCK_SIZE, BLOCK_SIZE,
+ BLOCK_SIZE * BLOCK_SIZE);
+ } else if (world_map[i] == 'f') {
+ append_ol(flh, create_object(i % WORLD_WIDTH * BLOCK_SIZE,
+ i / WORLD_WIDTH * BLOCK_SIZE,
+ 0, 0, 0, 0,
+ BLOCK_SIZE, BLOCK_SIZE,
+ BLOCK_SIZE * BLOCK_SIZE));
}
}
@@ -169,13 +179,25 @@ game_play(void)
}
// scrolling
+ int scroll_margin = 200;
+ if (player->p.x - scroll_dst > WIN_WIDTH - scroll_margin) {
+ scroll_dst = player->p.x - WIN_WIDTH + scroll_margin;
+ if (scroll_dst > WORLD_WIDTH * BLOCK_SIZE - WIN_WIDTH)
+ scroll_dst = WORLD_WIDTH * BLOCK_SIZE - WIN_WIDTH;
+ }
+ if (player->p.x - scroll_dst < scroll_margin) {
+ scroll_dst = player->p.x - scroll_margin;
+ if (scroll_dst < 0)
+ scroll_dst = 0;
+ }
struct OH *collidings;
collidings = create_ol();
player_is_falling = 1;
for (int xi = (int) player->p.x / BLOCK_SIZE;
- xi <= (int) player->p.x / BLOCK_SIZE + 1 && xi < WORLD_WIDTH;
+ xi <= (int) player->p.x / BLOCK_SIZE + 1 &&
+ xi < WORLD_WIDTH;
xi++) {
blc = blh[xi]->first;
while (blc != NULL) {
@@ -207,6 +229,7 @@ game_play(void)
t1 = ts.tv_nsec;
dt = t1 > t0 ? t1 - t0 : t1 - t0 + 1e9;
}
+
#ifdef COUNT_FPS
// count fps.
fps_count++;
@@ -218,7 +241,8 @@ game_play(void)
x_clear_area();
for (int xi = scroll_dst / BLOCK_SIZE;
- xi < (WIN_WIDTH + scroll_dst) / BLOCK_SIZE; xi++) {
+ xi < (WIN_WIDTH + scroll_dst) / BLOCK_SIZE + 1 &&
+ xi < WORLD_WIDTH; xi++) {
blc = blh[xi]->first;
while (blc != NULL) {
x_draw_rectangle(0x00FF00,
@@ -231,6 +255,19 @@ game_play(void)
x_draw_rectangle(0x009FFF,
player->p.x - scroll_dst, player->p.y, // position
player->body.rectangle.w, player->body.rectangle.h);
+
+ flc = flh->first;
+ if (0 <= flc->o->p.x - scroll_dst &&
+ flc->o->p.x - scroll_dst < WIN_WIDTH) {
+ while (flc != NULL) {
+ x_draw_rectangle(0xFFFF00,
+ flc->o->p.x - scroll_dst, flc->o->p.y, // position
+ flc->o->body.rectangle.w,
+ flc->o->body.rectangle.h);
+ flc = flc->next;
+ }
+ }
+
char status_string[128];
snprintf(status_string, 128, "falling: %d", player_is_falling);
x_draw_string(0x00FFFF,
@@ -240,6 +277,7 @@ game_play(void)
}
for (int xi = 0; xi < WORLD_WIDTH; xi++)
free_obj_and_ol(blh[xi]);
+ free_obj_and_ol(flh);
}
void
diff --git a/ex9/world_map.h b/ex9/world_map.h
@@ -33,31 +33,31 @@ char world_map[WORLD_WIDTH * WORLD_HEIGHT + 1] =
"................................................................................................................................................................................................................................................"
"................................................................................................................................................................................................................................................"
"....................................bbbbbbbbbb.................................................................................................................................................................................................."
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"..............................................bbbbbbbbbb........................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"....................................bbbbbbbbbb.................................................................................................................................................................................................."
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"..........................bbbbbbbbbb............................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................bbbbbbbbbb......................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"................................................................................................................................................................................................................................................"
-"...p............................................................................................................................................................................................................................................"
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+"..............................................bbbbbbbbbb.........................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+"....................................bbbbbbbbbb...................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+"..........................bbbbbbbbbb.............................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+"................bbbbbbbbbb.......................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+".................................................................................................................................................................................................................................f.............."
+"...p.............................................................................................................................................................................................................................f.............."
"bbbbbbbbbbbbbbbbbbbbbbbbb.......bbbbbbbbbbbbbbbbbbbbbbbb...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"........................b.......b......................b...b...................................................................................................................................................................................."
"........................b.......b......................b...b...................................................................................................................................................................................."