commit 838d715a1bc51cffa8e428c6c1bc2529a6a69e34
parent c3e6f47599470632fc0e04587209f810d6c42475
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 13 Jan 2023 12:26:33 +0900
add enemy
Diffstat:
4 files changed, 69 insertions(+), 25 deletions(-)
diff --git a/ex9/main.c b/ex9/main.c
@@ -68,6 +68,7 @@ void handle_inputs(enum key_state[], Object *, int *);
/* collision functions */
void col_pf(Object *, Object *); // player-flag
void col_pb(Object *, Object *); // player-block
+void col_pe(Object *, Object *); // player-enemy
/* misc: these functions use interfaces from multiple header files */
/* TODO: I want to rewrite these more cleanly */
@@ -152,6 +153,7 @@ game_play(void)
col_func[i][j] = NULL;
col_func[TPLAYER][TFLAG] = col_func[TFLAG][TPLAYER] = &col_pf;
col_func[TPLAYER][TBLOCK] = col_func[TBLOCK][TPLAYER] = &col_pb;
+ col_func[TPLAYER][TENEMY] = col_func[TENEMY][TPLAYER] = &col_pe;
/* load world into ol */
for (int i = 0; i < WORLD_WIDTH * WORLD_HEIGHT; i++) {
@@ -161,7 +163,8 @@ game_play(void)
i / WORLD_WIDTH * BLOCK_SIZE,
0, 0, 0, 0,
BLOCK_SIZE, BLOCK_SIZE,
- BLOCK_SIZE * BLOCK_SIZE));
+ BLOCK_SIZE * BLOCK_SIZE,
+ NULL));
if (olc == NULL) {
fprintf(stderr, "error: adding item to list ol\n");
} else {
@@ -173,7 +176,8 @@ game_play(void)
i / WORLD_WIDTH * BLOCK_SIZE,
0, 0, 0, GRAVITY,
BLOCK_SIZE, BLOCK_SIZE,
- BLOCK_SIZE * BLOCK_SIZE);
+ BLOCK_SIZE * BLOCK_SIZE,
+ NULL);
olc = laddfront(ol, player);
if (player == NULL || olc == NULL) {
fprintf(stderr, "error: creating player\n");
@@ -181,15 +185,29 @@ game_play(void)
} else {
ol = olc;
}
+ } else if (world_map[i] == 'e') {
+ olc = laddfront(ol, create_object(TENEMY, 0xFF0000,
+ i % WORLD_WIDTH * BLOCK_SIZE,
+ i / WORLD_WIDTH * BLOCK_SIZE,
+ -5, 0, 0, 0,
+ BLOCK_SIZE, BLOCK_SIZE,
+ BLOCK_SIZE * BLOCK_SIZE,
+ NULL));
+ if (olc == NULL) {
+ fprintf(stderr, "error: adding item to list ol\n");
+ } else {
+ ol = olc;
+ }
} else if (world_map[i] == 'f') {
olc = laddfront(ol, create_object(TFLAG, 0xFFFF00,
i % WORLD_WIDTH * BLOCK_SIZE,
i / WORLD_WIDTH * BLOCK_SIZE,
0, 0, 0, 0,
BLOCK_SIZE, BLOCK_SIZE,
- BLOCK_SIZE * BLOCK_SIZE));
+ BLOCK_SIZE * BLOCK_SIZE,
+ NULL));
if (olc == NULL) {
- fprintf(stderr, "error: adding item to list fl\n");
+ fprintf(stderr, "error: adding item to list ol\n");
} else {
ol = olc;
}
@@ -209,7 +227,10 @@ game_play(void)
for (int k = 0; k < SUB_FRAME; k++) {
// TODO: should be done for ol
- next_tick(player, 1e9 / FPS / SUB_FRAME);
+ for (olc = ol; olc != NULL; olc = olc->next) {
+ ((Object *)olc->item)->next_tick((Object *)olc->item,
+ 1e9 / FPS / SUB_FRAME);
+ }
// game over when fall out of the world
if (player->p.y > WORLD_HEIGHT * BLOCK_SIZE) {
@@ -288,7 +309,6 @@ game_over(void)
{
char *menu_char = "GAME OVER";
- x_clear_area();
x_draw_string(0x00FFFF,
WIN_WIDTH / 2 - 10 * strlen(menu_char)/2,
WIN_HEIGHT / 2,
@@ -432,26 +452,41 @@ handle_inputs(enum key_state key_state[], Object *player, int *debug)
}
}
-void col_pf(Object *o0, Object *o1) {
+void
+col_pf(Object *o0, Object *o1)
+{
if (!test_collision(o0, o1))
return;
- handle_collision_mf(o0, o1);
+ if (o0->type == TFLAG && o1->type == TPLAYER)
+ handle_collision_mf(o1, o0);
+ else if (o0->type == TPLAYER && o1->type == TFLAG)
+ handle_collision_mf(o0, o1);
+ else
+ fprintf(stderr, "col_pf: Error. invalid object types\n");
next_menu = GAME_CLEAR;
}
-void col_pb(Object *op, Object *ob) {
+void
+col_pb(Object *op, Object *ob)
+{
if (op->type == TBLOCK && ob->type == TPLAYER) {
- col_pb(ob, op);
- return;
- }
- if (op->type != TPLAYER || ob->type != TBLOCK) {
+ handle_collision_mf(ob, op);
+ if(is_on_floor(ob, op))
+ op->on_floor = 1;
+ } else if (op->type == TPLAYER && ob->type == TBLOCK) {
+ handle_collision_mf(op, ob);
+ if(is_on_floor(op, ob))
+ op->on_floor = 1;
+ } else {
fprintf(stderr, "col_pb: invalid object type: [%d, %d]\n",
op->type, ob->type);
- return;
}
- handle_collision_mf(op, ob);
- if(is_on_floor(op, ob))
- op->on_floor = 1;
+}
+
+void
+col_pe(Object *op, Object *oe)
+{
+ next_menu = GAME_OVER;
}
diff --git a/ex9/object.c b/ex9/object.c
@@ -22,7 +22,8 @@ typedef struct LLD {
Object *
create_object(enum object_type t, uint32_t color,
float px, float py, float vx, float vy, float ax, float ay,
- int w, int h, int m)
+ int w, int h, int m,
+ void (*next_tick)(Object *, long))
{
Object *o;
o = (Object *)malloc(sizeof(Object));
@@ -39,6 +40,9 @@ create_object(enum object_type t, uint32_t color,
o->body.rectangle.h = h;
o->m = m;
o->on_floor = 0;
+ if (next_tick == NULL)
+ next_tick = *o_next_tick;
+ o->next_tick = next_tick;
return o;
}
@@ -76,7 +80,7 @@ test_collision_rr(Object *o1, Object *o2)
}
void
-next_tick(Object *o, long ndt)
+o_next_tick(Object *o, long ndt)
{
switch (o->shape) {
case SRECTANGLE:
@@ -279,8 +283,11 @@ lhandle_collision(List *p)
}
lqsort(lld, (int (*)(void *, void *))&lldcmpdst);
for (tmplld = lld; tmplld != NULL; tmplld = tmplld->next) {
- ohandle_collision(((LLD *)tmplld->item)->o0,
- ((LLD *)tmplld->item)->o1);
+ if (test_collision(((LLD *)tmplld->item)->o0,
+ ((LLD *)tmplld->item)->o1)) {
+ ohandle_collision(((LLD *)tmplld->item)->o0,
+ ((LLD *)tmplld->item)->o1);
+ }
}
lfree(lld);
lfree(stack);
diff --git a/ex9/object.h b/ex9/object.h
@@ -49,6 +49,7 @@ typedef struct Object {
union Body body;
int m;
int on_floor;
+ void (*next_tick)(struct Object *, long);
} Object;
extern void (* col_func[NUM_OBJ_TYPE][NUM_OBJ_TYPE])(Object *, Object *);
@@ -56,11 +57,12 @@ extern void (* col_func[NUM_OBJ_TYPE][NUM_OBJ_TYPE])(Object *, Object *);
int object_dist(Object *, Object *);
void ohandle_collision(Object *, Object *);
int test_collision(Object *, Object *); // 1 if collide, 0 if not
-void next_tick(Object *o, long);
+void o_next_tick(Object *o, long);
void handle_collision_mf(Object *, Object *);
Object *create_object(enum object_type, uint32_t,
float, float, float, float, float, float,
- int, int, int);
+ int, int, int,
+ void (*)(Object *, long));
void free_object(Object *);
int is_on_floor(Object *, Object *);
int is_on_floor_before(Object *, Object *);
diff --git a/ex9/world_map.h b/ex9/world_map.h
@@ -53,11 +53,11 @@ char world_map[WORLD_WIDTH * WORLD_HEIGHT + 1] =
".................................................................................................................................................................................................................................f.............."
".................................................................................................................................................................................................................................f.............."
".................................................................................................................................................................................................................................f.............."
-".................................................................................................................................................................................................................................f.............."
+"....................e.e..........................................................................................................................................................................................................f.............."
"................bbbbbbbbbb.......................................................................................................................................................................................................f.............."
"....................b............................................................................................................................................................................................................f.............."
"....................b............................................................................................................................................................................................................f.............."
-"...p................b............................................................................................................................................................................................................f.............."
+"...p........e.......b.............................e..............................................................................................................................................................................f.............."
"bbbbbbbbbbbbbbbbbbbbbbbbb.......bbbbbbbbbbbbbbbbbbbbbbbb...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"........................b.......b......................b...b...................................................................................................................................................................................."
"........................b.......b......................b...b...................................................................................................................................................................................."