2010-10-12 10:55:23 +02:00
|
|
|
/*
|
2011-03-05 19:03:32 +01:00
|
|
|
* This software is licensed under the terms of the MIT-License
|
|
|
|
* See COPYING for further information.
|
|
|
|
* ---
|
|
|
|
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
|
2010-10-12 10:55:23 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "projectile.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "global.h"
|
2010-10-27 19:51:49 +02:00
|
|
|
#include "list.h"
|
2010-10-12 10:55:23 +02:00
|
|
|
|
2011-04-26 16:55:18 +02:00
|
|
|
Color *rgba(float r, float g, float b, float a) {
|
|
|
|
Color *clr = malloc(sizeof(Color));
|
|
|
|
clr->r = r;
|
|
|
|
clr->g = g;
|
|
|
|
clr->b = b;
|
|
|
|
clr->a = a;
|
|
|
|
|
|
|
|
return clr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Color *rgb(float r, float g, float b) {
|
|
|
|
return rgba(r, g, b, 1.0);
|
|
|
|
}
|
|
|
|
|
2011-06-26 13:45:27 +02:00
|
|
|
Projectile *create_particle4c(char *name, complex pos, Color *clr, ProjDRule draw, ProjRule rule, complex a1, complex a2, complex a3, complex a4) {
|
|
|
|
Projectile *p = create_projectile_p(&global.particles, prefix_get_tex(name, "part/"), pos, clr, draw, rule, a1, a2, a3, a4);
|
2011-05-13 19:03:02 +02:00
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
p->type = Particle;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2011-06-26 13:45:27 +02:00
|
|
|
Projectile *create_projectile4c(char *name, complex pos, Color *clr, ProjRule rule, complex a1, complex a2, complex a3, complex a4) {
|
|
|
|
return create_projectile_p(&global.projs, prefix_get_tex(name, "proj/"), pos, clr, ProjDraw, rule, a1, a2, a3, a4);
|
2011-05-13 19:03:02 +02:00
|
|
|
}
|
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
Projectile *create_projectile_p(Projectile **dest, Texture *tex, complex pos, Color *clr,
|
2011-06-26 13:45:27 +02:00
|
|
|
ProjDRule draw, ProjRule rule, complex a1, complex a2, complex a3, complex a4) {
|
2011-05-06 17:09:43 +02:00
|
|
|
Projectile *p = create_element((void **)dest, sizeof(Projectile));
|
2010-10-12 10:55:23 +02:00
|
|
|
|
2011-03-07 07:26:43 +01:00
|
|
|
p->birthtime = global.frames;
|
2011-03-18 19:03:06 +01:00
|
|
|
p->pos = pos;
|
|
|
|
p->pos0 = pos;
|
|
|
|
p->angle = 0;
|
2010-10-27 19:51:49 +02:00
|
|
|
p->rule = rule;
|
2011-05-13 19:03:02 +02:00
|
|
|
p->draw = draw;
|
2011-05-21 15:02:19 +02:00
|
|
|
p->tex = tex;
|
2010-10-27 19:51:49 +02:00
|
|
|
p->type = FairyProj;
|
|
|
|
p->clr = clr;
|
2010-10-24 14:44:48 +02:00
|
|
|
|
2011-06-26 13:45:27 +02:00
|
|
|
p->args[0] = a1;
|
|
|
|
p->args[1] = a2;
|
|
|
|
p->args[2] = a3;
|
|
|
|
p->args[3] = a4;
|
2010-10-24 14:44:48 +02:00
|
|
|
|
2011-05-13 19:03:02 +02:00
|
|
|
if(dest != &global.particles && clr != NULL) {
|
|
|
|
Color *color = rgba(clr->r, clr->g, clr->b, clr->a);
|
|
|
|
|
2011-06-26 13:45:27 +02:00
|
|
|
create_projectile_p(&global.particles, tex, pos, color, Shrink, bullet_flare_move, 16, add_ref(p), 0, 0);
|
2011-03-18 19:03:06 +01:00
|
|
|
}
|
2010-10-17 10:39:07 +02:00
|
|
|
return p;
|
2010-10-12 10:55:23 +02:00
|
|
|
}
|
|
|
|
|
2011-04-26 22:39:50 +02:00
|
|
|
void _delete_projectile(void **projs, void *proj) {
|
2011-05-21 15:02:19 +02:00
|
|
|
Projectile *p = proj;
|
|
|
|
p->rule(p, EVENT_DEATH);
|
|
|
|
|
|
|
|
if(p->clr)
|
|
|
|
free(p->clr);
|
2011-05-13 19:03:02 +02:00
|
|
|
del_ref(proj);
|
2011-04-26 22:39:50 +02:00
|
|
|
delete_element(projs, proj);
|
|
|
|
}
|
|
|
|
|
2011-05-06 17:09:43 +02:00
|
|
|
void delete_projectile(Projectile **projs, Projectile *proj) {
|
|
|
|
_delete_projectile((void **)projs, proj);
|
2010-10-12 10:55:23 +02:00
|
|
|
}
|
|
|
|
|
2011-05-06 17:09:43 +02:00
|
|
|
void delete_projectiles(Projectile **projs) {
|
|
|
|
delete_all_elements((void **)projs, _delete_projectile);
|
2010-10-12 10:55:23 +02:00
|
|
|
}
|
|
|
|
|
2011-04-26 16:55:18 +02:00
|
|
|
int collision_projectile(Projectile *p) {
|
2010-10-17 10:39:07 +02:00
|
|
|
if(p->type == FairyProj) {
|
2011-06-28 19:23:02 +02:00
|
|
|
float angle = carg(global.plr.pos - p->pos) + p->angle;
|
2011-03-18 19:03:06 +01:00
|
|
|
int projr = sqrt(pow(p->tex->w/4*cos(angle),2)*8/10 + pow(p->tex->h/2*sin(angle)*8/10,2));
|
2010-10-17 22:25:10 +02:00
|
|
|
|
2011-03-18 19:03:06 +01:00
|
|
|
if(cabs(global.plr.pos - p->pos) < projr + 1)
|
2010-10-17 10:39:07 +02:00
|
|
|
return 1;
|
|
|
|
} else {
|
2011-04-26 12:04:45 +02:00
|
|
|
Enemy *e = global.enemies;
|
|
|
|
while(e != NULL) {
|
|
|
|
if(e->hp != ENEMY_IMMUNE && cabs(e->pos - p->pos) < 15) {
|
2011-04-10 10:23:24 +02:00
|
|
|
global.points += 100;
|
2011-04-26 12:04:45 +02:00
|
|
|
e->hp--;
|
2010-10-17 10:39:07 +02:00
|
|
|
return 2;
|
|
|
|
}
|
2011-04-26 12:04:45 +02:00
|
|
|
e = e->next;
|
2010-10-17 10:39:07 +02:00
|
|
|
}
|
2011-04-08 18:59:03 +02:00
|
|
|
|
|
|
|
if(global.boss != NULL && cabs(global.boss->pos - p->pos) < 15) {
|
|
|
|
global.boss->dmg++;
|
|
|
|
return 2;
|
|
|
|
}
|
2010-10-17 10:39:07 +02:00
|
|
|
}
|
2010-10-17 09:27:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-06 17:09:43 +02:00
|
|
|
void draw_projectiles(Projectile *projs) {
|
2011-03-07 13:41:10 +01:00
|
|
|
Projectile *proj;
|
2010-10-24 14:44:48 +02:00
|
|
|
|
2011-05-13 19:03:02 +02:00
|
|
|
for(proj = projs; proj; proj = proj->next)
|
|
|
|
proj->draw(proj, global.frames - proj->birthtime);
|
2010-10-18 14:40:15 +02:00
|
|
|
}
|
2010-10-17 09:27:44 +02:00
|
|
|
|
2011-05-13 19:03:02 +02:00
|
|
|
void process_projectiles(Projectile **projs, char collision) {
|
2011-05-06 17:09:43 +02:00
|
|
|
Projectile *proj = *projs, *del = NULL;
|
2011-05-13 19:03:02 +02:00
|
|
|
|
|
|
|
char killed = 0;
|
|
|
|
char col = 0;
|
|
|
|
int action;
|
2010-10-12 10:55:23 +02:00
|
|
|
while(proj != NULL) {
|
2011-05-13 19:03:02 +02:00
|
|
|
action = proj->rule(proj, global.frames - proj->birthtime);
|
2011-05-06 17:09:43 +02:00
|
|
|
|
2011-05-13 19:03:02 +02:00
|
|
|
if(proj->type == DeadProj && killed < 2) {
|
|
|
|
killed++;
|
|
|
|
action = ACTION_DESTROY;
|
2011-06-26 13:45:27 +02:00
|
|
|
create_particle1c("flare", proj->pos, NULL, Fade, timeout, 30);
|
2011-05-13 19:03:02 +02:00
|
|
|
create_item(proj->pos, 0, BPoint)->auto_collect = 10;
|
|
|
|
}
|
|
|
|
|
2011-05-06 17:09:43 +02:00
|
|
|
if(collision)
|
2011-05-13 19:03:02 +02:00
|
|
|
col = collision_projectile(proj);
|
2010-10-12 10:55:23 +02:00
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
if(col && proj->type != Particle) {
|
|
|
|
Color *clr = NULL;
|
|
|
|
if(proj->clr) {
|
|
|
|
clr = malloc(sizeof(Color));
|
|
|
|
memcpy(clr, proj->clr, sizeof(Color));
|
|
|
|
}
|
2011-06-26 13:45:27 +02:00
|
|
|
create_projectile_p(&global.particles, proj->tex, proj->pos, clr, DeathShrink, timeout_linear, 10, 5*cexp(proj->angle*I), 0, 0);
|
2011-05-21 15:02:19 +02:00
|
|
|
}
|
2011-06-26 13:45:27 +02:00
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
if(col == 1 && global.frames - abs(global.plr.recovery) >= 0)
|
|
|
|
plr_death(&global.plr);
|
2010-10-17 09:27:44 +02:00
|
|
|
|
2011-05-13 19:03:02 +02:00
|
|
|
if(action == ACTION_DESTROY || col
|
|
|
|
|| creal(proj->pos) + proj->tex->w/2 < 0 || creal(proj->pos) - proj->tex->w/2 > VIEWPORT_W
|
|
|
|
|| cimag(proj->pos) + proj->tex->h/2 < 0 || cimag(proj->pos) - proj->tex->h/2 > VIEWPORT_H) {
|
2010-10-18 14:40:15 +02:00
|
|
|
del = proj;
|
|
|
|
proj = proj->next;
|
2011-05-06 17:09:43 +02:00
|
|
|
delete_projectile(projs, del);
|
2010-10-18 14:40:15 +02:00
|
|
|
if(proj == NULL) break;
|
|
|
|
} else {
|
|
|
|
proj = proj->next;
|
2010-10-17 10:39:07 +02:00
|
|
|
}
|
2010-10-27 19:51:49 +02:00
|
|
|
}
|
2010-10-12 10:55:23 +02:00
|
|
|
}
|
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
|
2011-05-13 19:03:02 +02:00
|
|
|
int linear(Projectile *p, int t) { // sure is physics in here; a[0]: velocity
|
2011-05-06 17:09:43 +02:00
|
|
|
p->angle = carg(p->args[0]);
|
|
|
|
p->pos = p->pos0 + p->args[0]*t;
|
2011-05-13 19:03:02 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-06-27 13:36:35 +02:00
|
|
|
int accelerated(Projectile *p, int t) {
|
|
|
|
p->angle = carg(p->args[0]);
|
|
|
|
p->pos = p->pos0 + p->args[0]*t + p->args[1]*t*t;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-06-28 19:23:02 +02:00
|
|
|
int asymptotic(Projectile *p, int t) { // v = a[0]*(a[1] + 1); a[1] -> 0
|
|
|
|
p->angle = carg(p->args[0] + p->args[1]);
|
|
|
|
|
|
|
|
p->args[1] *= 0.8;
|
|
|
|
p->pos += p->args[0]*(p->args[1] + 1);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
void _ProjDraw(Projectile *proj, int t) {
|
2011-06-27 13:36:35 +02:00
|
|
|
if(proj->clr != NULL && !tconfig.intval[NO_SHADER]) {
|
2011-05-13 19:03:02 +02:00
|
|
|
GLuint shader = get_shader("bullet_color");
|
|
|
|
glUseProgramObjectARB(shader);
|
|
|
|
|
|
|
|
glUniform4fv(glGetUniformLocation(shader, "color"), 1, (GLfloat *)proj->clr);
|
|
|
|
}
|
2011-06-27 13:36:35 +02:00
|
|
|
if(proj->clr != NULL && tconfig.intval[NO_SHADER])
|
|
|
|
glColor3f(0,0,0);
|
2011-05-13 19:03:02 +02:00
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
draw_texture_p(0,0, proj->tex);
|
|
|
|
|
2011-06-27 13:36:35 +02:00
|
|
|
glColor3f(1,1,1);
|
|
|
|
|
|
|
|
if(!tconfig.intval[NO_SHADER])
|
|
|
|
glUseProgramObjectARB(0);
|
2011-05-21 15:02:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProjDraw(Projectile *proj, int t) {
|
2011-05-13 19:03:02 +02:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(creal(proj->pos), cimag(proj->pos), 0);
|
|
|
|
glRotatef(proj->angle*180/M_PI+90, 0, 0, 1);
|
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
_ProjDraw(proj, t);
|
2011-05-13 19:03:02 +02:00
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
glPopMatrix();
|
2010-10-12 10:55:23 +02:00
|
|
|
}
|
2011-05-13 19:03:02 +02:00
|
|
|
|
2011-06-28 19:23:02 +02:00
|
|
|
void Blast(Projectile *p, int t) {
|
|
|
|
if(t == 1) {
|
|
|
|
p->args[1] = frand()*360 + frand()*I;
|
|
|
|
p->args[2] = frand() + frand()*I;
|
|
|
|
}
|
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(creal(p->pos), cimag(p->pos), 0);
|
|
|
|
glRotatef(creal(p->args[1]), cimag(p->args[1]), creal(p->args[2]), cimag(p->args[2]));
|
|
|
|
glScalef(t/p->args[0], t/p->args[0], 1);
|
|
|
|
glColor4f(0.3,0.6,1,1 - t/p->args[0]);
|
|
|
|
draw_texture_p(0,0,p->tex);
|
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
glColor4f(1,1,1,1);
|
|
|
|
}
|
|
|
|
|
2011-05-13 19:03:02 +02:00
|
|
|
void Shrink(Projectile *p, int t) {
|
|
|
|
glPushMatrix();
|
2011-05-14 17:26:43 +02:00
|
|
|
float s = 2.0-t/p->args[0]*2;
|
2011-05-13 19:03:02 +02:00
|
|
|
glTranslatef(creal(p->pos), cimag(p->pos), 0);
|
2011-06-24 19:16:05 +02:00
|
|
|
glRotatef(p->angle*180/M_PI+90, 0, 0, 1);
|
2011-05-13 19:03:02 +02:00
|
|
|
glScalef(s, s, 1);
|
2011-05-21 15:02:19 +02:00
|
|
|
|
|
|
|
_ProjDraw(p, t);
|
2011-05-13 19:03:02 +02:00
|
|
|
glPopMatrix();
|
2011-05-21 15:02:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeathShrink(Projectile *p, int t) {
|
|
|
|
glPushMatrix();
|
|
|
|
float s = 2.0-t/p->args[0]*2;
|
|
|
|
glTranslatef(creal(p->pos), cimag(p->pos), 0);
|
2011-06-24 19:16:05 +02:00
|
|
|
glRotatef(p->angle*180/M_PI+90, 0, 0, 1);
|
2011-05-21 15:02:19 +02:00
|
|
|
glScalef(s, 1, 1);
|
2011-06-24 19:16:05 +02:00
|
|
|
|
2011-05-21 15:02:19 +02:00
|
|
|
_ProjDraw(p, t);
|
|
|
|
glPopMatrix();
|
2011-05-13 19:03:02 +02:00
|
|
|
}
|
|
|
|
|
2011-06-28 19:23:02 +02:00
|
|
|
void GrowFade(Projectile *p, int t) {
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(creal(p->pos), cimag(p->pos), 0);
|
|
|
|
glRotatef(p->angle*180/M_PI+90, 0, 0, 1);
|
|
|
|
glScalef(t/p->args[0]*(1+p->args[1]), t/p->args[0]*(1+p->args[1]), 1);
|
|
|
|
|
|
|
|
if(p->clr)
|
|
|
|
glColor4f(p->clr->r,p->clr->g,p->clr->b,1-t/p->args[0]);
|
|
|
|
else
|
|
|
|
glColor4f(1,1,1,1-t/p->args[0]);
|
|
|
|
|
|
|
|
draw_texture_p(0,0,p->tex);
|
|
|
|
|
|
|
|
glColor4f(1,1,1,1);
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
2011-05-13 19:03:02 +02:00
|
|
|
int bullet_flare_move(Projectile *p, int t) {
|
2011-05-14 17:26:43 +02:00
|
|
|
if(t > 16 || REF(p->args[1]) == NULL) {
|
|
|
|
free_ref(p->args[1]);
|
2011-05-13 19:03:02 +02:00
|
|
|
return ACTION_DESTROY;
|
2011-06-28 19:23:02 +02:00
|
|
|
}
|
2011-05-13 19:03:02 +02:00
|
|
|
|
2011-05-14 17:26:43 +02:00
|
|
|
p->pos = ((Projectile *) REF(p->args[1]))->pos;
|
|
|
|
p->angle = ((Projectile *) REF(p->args[1]))->angle;
|
2011-05-13 19:03:02 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fade(Projectile *p, int t) {
|
2011-05-14 17:26:43 +02:00
|
|
|
glColor4f(1,1,1, 1.0 - (float)t/p->args[0]);
|
2011-05-21 15:02:19 +02:00
|
|
|
ProjDraw(p, t);
|
2011-05-13 19:03:02 +02:00
|
|
|
glColor4f(1,1,1,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int timeout(Projectile *p, int t) {
|
|
|
|
if(t >= creal(p->args[0]))
|
|
|
|
return ACTION_DESTROY;
|
|
|
|
|
|
|
|
return 1;
|
2011-05-21 15:02:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int timeout_linear(Projectile *p, int t) {
|
|
|
|
if(t >= creal(p->args[0]))
|
|
|
|
return ACTION_DESTROY;
|
|
|
|
|
|
|
|
p->angle = carg(p->args[1]);
|
|
|
|
p->pos = p->pos0 + p->args[1]*t;
|
2011-06-26 16:10:13 +02:00
|
|
|
|
|
|
|
return 1;
|
2011-05-13 19:03:02 +02:00
|
|
|
}
|