commit 9c4c3f9b2f462adbd774d36ce9080c41174afa10
parent d97747aff8b014fc43501ad4923228c8848634fa
Author: Matsuda Kenji <info@mtkn.jp>
Date: Mon, 19 Dec 2022 09:12:51 +0900
recode collision
Diffstat:
M | main.c | | | 27 | ++++++++++++++------------- |
M | obj.h | | | 6 | ++---- |
2 files changed, 16 insertions(+), 17 deletions(-)
diff --git a/main.c b/main.c
@@ -55,6 +55,8 @@ void handle_inputs(void);
void end_menu(void);
void next_tick(float);
void cleanup(void);
+int check_collision(Obj *, Obj *);
+int handle_collision(Obj *, Obj *);
void
setup(void)
@@ -152,7 +154,6 @@ game_init(void)
fprintf(stderr, "couldn't create object: block");
exit(1);
}
- printf("square: %f %f\n", square->width, square->height);
square->px = 100;
square->py = 100;
@@ -197,7 +198,7 @@ game_play(void)
XClearArea(display, window, 0, 0, win_width, win_height, False);
draw_object(square, pgc);
draw_object(block, bgc);
- XDrawString(display, window, fgc, square->px, square->py, "player", 6);
+ //XDrawString(display, window, fgc, square->px, square->py, "player", 6);
}
}
@@ -310,17 +311,17 @@ handle_collision(void)
}
if (block->py < square->py + square->height &&
- square->py < block->py + block->height){
- if (block->px < square->px + square->width &&
- square->px < block->px + block->width){
- if (square->ppx + square->width <= block->ppx + block->width/2){
- obj_move(block, square->px + square->width, block->py);
- printf("hello\n");
- }
- else if (block->ppx <= square->ppx - block->width/2){
- obj_move(block, square->px - block->width, block->py);
- printf("world\n");
- }
+ square->py < block->py + block->height &&
+ block->px < square->px + square->width &&
+ square->px < block->px + block->width){
+ if (square->ppy + square->height <= block->ppy){
+ obj_move(block, block->px, square->py + square->height);
+ }else if (block->ppy + block->height <= square->ppy){
+ obj_move(block, block->px, square->py - block->height);
+ }else if (square->ppx + square->width <= block->ppx){
+ obj_move(block, square->px + square->width, block->py);
+ }else if (block->ppx + block->width <= square->ppx){
+ obj_move(block, square->px - block->width, block->py);
}
}
}
diff --git a/obj.h b/obj.h
@@ -37,10 +37,8 @@ Obj
void
obj_move(Obj *obj, float px, float py)
{
- obj->ppx = obj->px;
- obj->ppy = obj->py;
- obj->px = px;
- obj->py = py;
+ obj->ppx = obj->px = px;
+ obj->ppy = obj->py = py;
}
void