commit 9af836feebbf47829f7defca013e3991a55d7406
parent 56a5cf1a9090b294e7418597de51a0da505df8ce
Author: Matsuda Kenji <info@mtkn.jp>
Date: Thu, 12 Jan 2023 09:38:40 +0900
move falling status inside Object
Diffstat:
3 files changed, 23 insertions(+), 19 deletions(-)
diff --git a/ex9/main.c b/ex9/main.c
@@ -45,8 +45,6 @@ enum next_menu {
};
/* variables */
-struct Object *player;
-int player_is_falling;
enum next_menu next_menu = START_MENU;
void (* col_func[NUM_OBJ_TYPE][NUM_OBJ_TYPE])(struct Object *,
struct Object *);
@@ -59,7 +57,7 @@ void game_over(void);
void game_clear(void);
/* events */
void receive_events(enum key_state[]);
-void handle_inputs(enum key_state[]);
+void handle_inputs(enum key_state[], struct Object *);
/* collision functions */
void col_pf(struct Object *, struct Object *); // player-flag
void col_pb(struct Object *, struct Object *); // player-block
@@ -132,6 +130,7 @@ game_play(void)
#endif
struct timespec ts;
struct List *ol; // block list
+ struct Object *player;
struct List *olc; // cursor used to scan ol
int scroll_dst = 0;
@@ -189,7 +188,7 @@ game_play(void)
while (next_menu == GAME_PLAY){
receive_events(key_state);
- handle_inputs(key_state);
+ handle_inputs(key_state, player);
clock_gettime(CLOCK_MONOTONIC, &ts);
t0 = ts.tv_nsec;
@@ -226,16 +225,7 @@ game_play(void)
scroll_dst = 0;
}
- // TODO: state of object should be contained in object itself.
- player_is_falling = 1;
- for (olc = ol; olc != NULL; olc = olc->next) {
- if (olc->item == player)
- continue;
- if (is_on_floor_before(player, (struct Object *)olc->item)) {
- player_is_falling = 0;
- }
- }
-
+ player->on_floor = 0;
lhandle_collision(ol);
}
@@ -368,7 +358,7 @@ receive_events(enum key_state key_state[])
}
void
-handle_inputs(enum key_state key_state[])
+handle_inputs(enum key_state key_state[], struct Object *player)
{
if (key_state[KEY_Q] == KEY_DOWN){
next_menu = GAME_OVER;
@@ -387,15 +377,15 @@ handle_inputs(enum key_state key_state[])
player->a.x = -500;
}
} else {
- if (player_is_falling)
- player->a.x = -player->v.x;
- else
+ if (player->on_floor)
player->a.x = -3 * player->v.x;
+ else
+ player->a.x = -player->v.x;
}
if (player->v.x < -200) player->v.x = -200;
if (player->v.x > 200) player->v.x = 200;
- if (!player_is_falling && key_state[KEY_SPACE] == KEY_DOWN) {
+ if (player->on_floor && key_state[KEY_SPACE] == KEY_DOWN) {
player->v.y = -450;
}
}
@@ -418,6 +408,8 @@ void col_pb(struct Object *op, struct Object *ob) {
return;
}
handle_collision_mf(op, ob);
+ if(is_on_floor(op, ob))
+ op->on_floor = 1;
}
diff --git a/ex9/object.c b/ex9/object.c
@@ -38,6 +38,7 @@ create_object(enum object_type t, uint32_t color,
o->body.rectangle.w = w;
o->body.rectangle.h = h;
o->m = m;
+ o->on_floor = 0;
return o;
}
@@ -161,6 +162,15 @@ object_dist(struct Object *o1, struct Object *o2)
}
int
+is_on_floor(struct Object *p, struct Object *f)
+{
+ if (p->p.x + p->body.rectangle.w < f->p.x ||
+ f->p.x + f->body.rectangle.w < p->p.x)
+ return 0;
+ return p->p.y + p->body.rectangle.h == f->p.y;
+}
+
+int
is_on_floor_before(struct Object *p, struct Object *f)
{
if (p->pp.x + p->body.rectangle.w < f->pp.x ||
diff --git a/ex9/object.h b/ex9/object.h
@@ -48,6 +48,7 @@ struct Object {
struct Point a;
union Body body;
int m;
+ int on_floor;
};
extern void (* col_func[NUM_OBJ_TYPE][NUM_OBJ_TYPE])(struct Object *,
@@ -62,6 +63,7 @@ struct Object *create_object(enum object_type, uint32_t,
float, float, float, float, float, float,
int, int, int);
void free_object(struct Object *);
+int is_on_floor(struct Object *, struct Object *);
int is_on_floor_before(struct Object *, struct Object *);
void oprint(struct Object *);