This commit is contained in:
Frank Villaro-Dixon 2015-01-01 05:57:59 +01:00
parent 9fd08a1145
commit 7af5bc65a2
7 changed files with 80 additions and 8 deletions

27
col.c Normal file
View file

@ -0,0 +1,27 @@
/*
** col.c - Collisions
**
** 2014 - Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
*/
#include <stdio.h>
#include <stdlib.h>
#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;
}

14
col.h Normal file
View file

@ -0,0 +1,14 @@
/*
** col.h - <+DESC+>
**
** 2014 - Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
*/
#ifndef COL_H
#define COL_H
int
has_collision(struct s_game *g, struct s_obstacle o);
#endif /* ndef COL_H */

14
game.c
View file

@ -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) { //{{{

7
game.h
View file

@ -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);

9
main.c
View file

@ -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++;
}

15
obs.c
View file

@ -7,6 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
#include "col.h"
#include <math.h>
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;
}

2
obs.h
View file

@ -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 */