commit 5259a36aa180ec2c14c6fe2baebbce3602c0ae84
parent 838d715a1bc51cffa8e428c6c1bc2529a6a69e34
Author: Matsuda Kenji <info@mtkn.jp>
Date: Fri, 13 Jan 2023 14:51:44 +0900
Add enemy
Diffstat:
2 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/ex9/main.c b/ex9/main.c
@@ -48,6 +48,7 @@ enum debug_msg {
DFPS,
DONFLOOR,
DPOS,
+ DVEL,
NDMSG,
DMAXLEN = 128,
};
@@ -69,6 +70,8 @@ void handle_inputs(enum key_state[], Object *, int *);
void col_pf(Object *, Object *); // player-flag
void col_pb(Object *, Object *); // player-block
void col_pe(Object *, Object *); // player-enemy
+void col_eb(Object *, Object *); // enemy-block
+void col_ee(Object *, Object *); // enemy-enemy
/* misc: these functions use interfaces from multiple header files */
/* TODO: I want to rewrite these more cleanly */
@@ -154,6 +157,8 @@ game_play(void)
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;
+ col_func[TENEMY][TBLOCK] = col_func[TBLOCK][TENEMY] = &col_eb;
+ col_func[TENEMY][TENEMY] = col_func[TENEMY][TENEMY] = &col_ee;
/* load world into ol */
for (int i = 0; i < WORLD_WIDTH * WORLD_HEIGHT; i++) {
@@ -189,7 +194,7 @@ game_play(void)
olc = laddfront(ol, create_object(TENEMY, 0xFF0000,
i % WORLD_WIDTH * BLOCK_SIZE,
i / WORLD_WIDTH * BLOCK_SIZE,
- -5, 0, 0, 0,
+ -10, 0, 0, GRAVITY,
BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE * BLOCK_SIZE,
NULL));
@@ -240,11 +245,15 @@ game_play(void)
// bind within the world
if (player->p.x < 0) {
player->p.x = 0;
+ if (player->v.x < 0)
+ player->v.x = 0;
}
if (WORLD_WIDTH * BLOCK_SIZE < player->p.x +
player->body.rectangle.w) {
player->p.x = WORLD_WIDTH * BLOCK_SIZE -
player->body.rectangle.w;
+ if (player->v.x > 0)
+ player->v.x = 0;
}
// scrolling
@@ -260,6 +269,10 @@ game_play(void)
}
player->on_floor = 0;
+ if (debug) {
+ snprintf(debug_msg[DVEL], DMAXLEN,
+ "vel: (%3.0f, %3.0f)", player->v.x, player->v.y);
+ }
lhandle_collision(ol);
}
@@ -484,6 +497,33 @@ col_pb(Object *op, Object *ob)
}
void
+col_eb(Object *oe, Object *ob)
+{
+ float pvx;
+ if (oe->type == TBLOCK && ob->type == TENEMY) {
+ pvx = ob->v.x;
+ handle_collision_mf(ob, oe);
+ if (ob->v.x == 0)
+ ob->v.x = -pvx;
+ } else if (oe->type == TENEMY && ob->type == TBLOCK) {
+ pvx = oe->v.x;
+ handle_collision_mf(oe, ob);
+ if (oe->v.x == 0)
+ oe->v.x = -pvx;
+ } else {
+ fprintf(stderr, "col_pb: invalid object type: [%d, %d]\n",
+ oe->type, ob->type);
+ }
+}
+
+void
+col_ee(Object *o0, Object *o1)
+{
+ o0->v.x *= -1;
+ o1->v.x *= -1;
+}
+
+void
col_pe(Object *op, Object *oe)
{
next_menu = GAME_OVER;
diff --git a/ex9/world_map.h b/ex9/world_map.h
@@ -54,10 +54,10 @@ char world_map[WORLD_WIDTH * WORLD_HEIGHT + 1] =
".................................................................................................................................................................................................................................f.............."
".................................................................................................................................................................................................................................f.............."
"....................e.e..........................................................................................................................................................................................................f.............."
-"................bbbbbbbbbb.......................................................................................................................................................................................................f.............."
+"................bbbbbbbbb........................................................................................................................................................................................................f.............."
"....................b............................................................................................................................................................................................................f.............."
"....................b............................................................................................................................................................................................................f.............."
-"...p........e.......b.............................e..............................................................................................................................................................................f.............."
+"b..p........e.......b...........b..............e..e....b.........................................................................................................................................................................f.............."
"bbbbbbbbbbbbbbbbbbbbbbbbb.......bbbbbbbbbbbbbbbbbbbbbbbb...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"........................b.......b......................b...b...................................................................................................................................................................................."
"........................b.......b......................b...b...................................................................................................................................................................................."