projectile collisions

player can be hit by projectiles now.
This commit is contained in:
Lukas Weber 2010-10-17 09:27:44 +02:00
parent 38fff7fc29
commit 4cdadbc918
8 changed files with 38 additions and 6 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
build/
.ctagsdb

View file

@ -37,4 +37,10 @@ void init_global() {
global.fairies = NULL;
global.frames = 0;
global.game_over = 0;
}
void game_over() {
global.game_over = 1;
printf("Game Over!");
}

View file

@ -54,10 +54,13 @@ typedef struct {
Texture projwave;
Texture water;
} textures;
int game_over;
} Global;
extern Global global;
void init_global();
void game_over();
#endif

View file

@ -55,7 +55,7 @@ void player_draw(Player* plr) {
void player_logic(Player* plr) {
if(plr->fire && !(global.frames % 4)) {
create_projectile(plr->x-10, plr->y, 20, 0, simple, &_projs.youmu, ((Color){1,1,1}));
create_projectile(plr->x+10, plr->y, 20, 0, simple, &_projs.youmu, ((Color){1,1,1}));
create_projectile(plr->x-10, plr->y-20, 20, 0, simple, &_projs.youmu, ((Color){1,1,1}));
create_projectile(plr->x+10, plr->y-20, 20, 0, simple, &_projs.youmu, ((Color){1,1,1}));
}
}

View file

@ -118,13 +118,28 @@ void free_projectiles() {
global.projs = NULL;
}
int test_collision(Projectile *p) {
float angle = atan((float)(global.plr.y - p->y)/(global.plr.x - p->x));
int projr = sqrt(pow(p->tex->w/4*cos(angle),2)*8/10 + pow(p->tex->h/2*sin(angle)*8/10,2));
if(sqrt(pow(p->x-global.plr.x,2) + pow(p->y-global.plr.y,2)) < projr+5)
return 1;
return 0;
}
void process_projectiles() {
Projectile *proj = global.projs, *del = NULL;
while(proj != NULL) {
proj->rule(proj);
if(proj->x < 0 || proj->x > VIEWPORT_W || proj->y < 0 || proj->y > VIEWPORT_H)
if(proj->x + proj->tex->w/2 < 0 || proj->x - proj->tex->w/2 > VIEWPORT_W
|| proj->y + proj->tex->h/2 < 0 || proj->y - proj->tex->h/2 > VIEWPORT_H)
del = proj;
if(test_collision(proj))
game_over();
proj = proj->next;
}

View file

@ -70,6 +70,7 @@ void delete_projectile(Projectile *proj);
void draw_projectile(Projectile *proj);
void free_projectiles();
int test_collision(Projectile *p);
void process_projectiles();
void simple(Projectile *p);

View file

@ -41,6 +41,9 @@ void stage_input() {
break;
case SDLK_ESCAPE:
exit(1);
break;
default:
break;
}
} else if(event.type == SDL_KEYUP) {
switch(event.key.keysym.sym) {
@ -50,6 +53,8 @@ void stage_input() {
case SDLK_y:
global.plr.fire = False;
break;
default:
break;
}
} else if(event.type == SDL_QUIT) {
exit(1);

View file

@ -33,7 +33,8 @@ void simpleFairy(Fairy *f) {
for(a = 120; a <= 220; a += 10)
create_projectile(f->x, f->y, 2, a, simple, &_projs.rice, ((Color){0,0,1}));
}
f->moving = 1;
f->dir = 1;
f->x += 1;
}
@ -88,7 +89,7 @@ void stage0_events() {
case 200:
break;
case 250:
create_projectile(500, 0, 1, 220, simple, &_projs.bigball, ((Color){1,0,0}));
create_projectile(200, 200, 0, 220, simple, &_projs.bigball, ((Color){1,0,0}));
}
}
@ -100,7 +101,7 @@ void stage0_loop() {
glFogf(GL_FOG_MODE, GL_EXP);
glFogf(GL_FOG_DENSITY, 0.005);
while(1) {
while(!global.game_over) {
stage_logic();
stage_input();
stage0_events();