diff --git a/col.c b/col.c new file mode 100644 index 0000000..c6bda2b --- /dev/null +++ b/col.c @@ -0,0 +1,27 @@ +/* +** col.c - Collisions +** +** 2014 - Frank Villaro-Dixon +*/ + +#include +#include +#include "game.h" +#include "obs.h" + + +int +has_collision(struct s_game *g, struct s_obstacle o) { + int face = get_face_from_cursor(g); + + if(face != o.face) + return 0; + + if(o.distance <= CURSOR_DIST && + o.distance + o.thick >= CURSOR_DIST) { + return 1; + } + + return 0; + +} diff --git a/col.h b/col.h new file mode 100644 index 0000000..dd52151 --- /dev/null +++ b/col.h @@ -0,0 +1,14 @@ +/* +** col.h - <+DESC+> +** +** 2014 - Frank Villaro-Dixon +*/ + +#ifndef COL_H +#define COL_H + +int +has_collision(struct s_game *g, struct s_obstacle o); + +#endif /* ndef COL_H */ + diff --git a/game.c b/game.c index db5ab7c..39cca32 100644 --- a/game.c +++ b/game.c @@ -41,10 +41,14 @@ draw_hexagon(struct s_game *g) { //{{{ #define DFT_COLOR 128 for(faces = 0; faces < SHAPE_HEXAGON; faces++) { - draw_hexagon_side(g, faces, POLYGON_SIZE, SHAPE_THICK, g->color); + draw_hexagon_side(g, faces, POLYGON_SIZE, SHAPE_THICK-5, g->color); } } //}}} +int +get_face_from_cursor(struct s_game *g) { + return g->cursor_angle * g->polygon_type / ANGLE_MAX; +} SDL_Point get_circle_vertice(int angle, int dist) { //{{{ @@ -61,9 +65,9 @@ void draw_cursor(struct s_game *g) { //{{{ SDL_Point p1, p2, p3; - p1 = get_circle_vertice(g->cursor_angle, CURSOR_DIST+10); - p2 = get_circle_vertice(g->cursor_angle-TO_DEG(8), CURSOR_DIST); - p3 = get_circle_vertice(g->cursor_angle+TO_DEG(8), CURSOR_DIST); + p1 = get_circle_vertice(g->cursor_angle, CURSOR_DIST); + p2 = get_circle_vertice(g->cursor_angle-TO_DEG(8), CURSOR_DIST-10); + p3 = get_circle_vertice(g->cursor_angle+TO_DEG(8), CURSOR_DIST-10); SDL_Point pts[3] = {p1, p2, p3}; @@ -99,6 +103,8 @@ get_keys(struct s_game *g) { //{{{ g->cursor_angle += TO_DEG(3); g->cursor_angle %= ANGLE_MAX; + if(g->cursor_angle < 0) + g->cursor_angle += ANGLE_MAX; } //}}} void init_game(struct s_game *g) { //{{{ diff --git a/game.h b/game.h index c542ccd..4cad165 100644 --- a/game.h +++ b/game.h @@ -19,8 +19,8 @@ #define SHAPE_PENTAGON 5 #define SHAPE_HEXAGON 6 -#define SHAPE_THICK 5 -#define CURSOR_DIST 40 +#define SHAPE_THICK 15 +#define CURSOR_DIST (POLYGON_SIZE + SHAPE_THICK + 10) #define ANGLE_MAX 3600 #define OFFSET_POINT(p) {p.x += SCREEN_WIDTH/2; p.y += SCREEN_HEIGHT/2;} @@ -56,6 +56,9 @@ struct s_game { void init_game(struct s_game *g); +int +get_face_from_cursor(struct s_game *g); + void draw_game(struct s_game *g); diff --git a/main.c b/main.c index 8d4c1d5..67086e7 100644 --- a/main.c +++ b/main.c @@ -34,8 +34,15 @@ start: draw_game(&ga); SDL_Flip(ga.screen); + if(check_collisions(&ga)) { + puts("PERDU, connard !"); + SDL_Delay(2000); + goto start; + } + get_keys(&ga); - SDL_Delay(10); + + SDL_Delay(5); ga.counter++; } diff --git a/obs.c b/obs.c index 69d7c88..212b2aa 100644 --- a/obs.c +++ b/obs.c @@ -7,6 +7,7 @@ #include #include #include "game.h" +#include "col.h" #include void @@ -81,6 +82,18 @@ add_random_obstacle(struct s_game *g) { struct s_obstacle o; o = create_obstacle(SHAPE_THICK, rand()%g->polygon_type, 1); - add_obstacle(g, o); } + +int +check_collisions(struct s_game *g) { + int i; + for(i = 0; i < g->num_obs; i++) { + if(g->obs[i].used) { + if(has_collision(g, g->obs[i])) + return 1; + } + } + return 0; +} + diff --git a/obs.h b/obs.h index 985ffa9..6214cdd 100644 --- a/obs.h +++ b/obs.h @@ -29,6 +29,8 @@ update_obstacles(struct s_game *g); void add_random_obstacle(struct s_game *g); +int +check_collisions(struct s_game *g); #endif /* ndef OBS_H */