tbftss/src/battle/jumpgate.c

160 lines
3.3 KiB
C
Raw Normal View History

2015-11-09 23:46:57 +01:00
/*
2016-02-21 16:50:27 +01:00
Copyright (C) 2015-2016 Parallel Realities
2015-11-09 23:46:57 +01:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "jumpgate.h"
2015-11-09 23:46:57 +01:00
static void think(void);
static void draw(void);
2015-11-09 23:46:57 +01:00
static void handleFleeingEntities(void);
static void addEscapeEffect(Entity *ent);
2015-11-09 23:46:57 +01:00
static SDL_Texture *portal;
static float portalAngle;
Entity *spawnJumpgate(void)
2015-11-09 23:46:57 +01:00
{
Entity *jumpgate = spawnEntity();
jumpgate->type = ET_JUMPGATE;
jumpgate->health = jumpgate->maxHealth = FPS;
jumpgate->texture = getTexture("gfx/entities/jumpgate.png");
jumpgate->action = think;
jumpgate->draw = draw;
jumpgate->flags |= EF_NO_MT_BOX;
portal = getTexture("gfx/entities/portal.png");
portalAngle = 0;
SDL_QueryTexture(jumpgate->texture, NULL, NULL, &jumpgate->w, &jumpgate->h);
return jumpgate;
2015-11-09 23:46:57 +01:00
}
int jumpgateEnabled(void)
{
return (battle.jumpgate && (!(battle.jumpgate->flags & EF_DISABLED)));
}
void activateJumpgate(int activate)
{
if (battle.jumpgate)
{
if (activate)
{
battle.jumpgate->flags &= ~EF_DISABLED;
}
else
{
battle.jumpgate->flags |= EF_DISABLED;
}
}
}
2015-11-09 23:46:57 +01:00
static void think(void)
{
self->thinkTime = 4;
self->angle += 0.1;
2015-11-09 23:46:57 +01:00
if (self->angle >= 360)
{
self->angle -= 360;
}
if (jumpgateEnabled())
{
handleFleeingEntities();
}
if (!battle.jumpgate)
{
battle.jumpgate = self;
}
if (battle.jumpgate == self)
{
2016-03-10 00:21:51 +01:00
portalAngle += 2;
if (portalAngle >= 360)
{
portalAngle -= 360;
}
}
2015-11-09 23:46:57 +01:00
}
static void handleFleeingEntities(void)
{
Entity *e, **candidates;
int i;
candidates = getAllEntsWithin(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, self);
2015-11-14 00:35:51 +01:00
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
2015-11-09 23:46:57 +01:00
{
if (e->health > 0 && e->flags & EF_RETREATING && getDistance(self->x, self->y, e->x, e->y) <= 255)
2015-11-09 23:46:57 +01:00
{
e->alive = ALIVE_ESCAPED;
addEscapeEffect(e);
playBattleSound(SND_JUMP, e->x, e->y);
2015-11-09 23:46:57 +01:00
}
}
}
static void addEscapeEffect(Entity *ent)
{
Effect *e;
int i, n, speed;
n = ent->w * ent->h;
for (i = 0 ; i < n ; i++)
{
e = malloc(sizeof(Effect));
memset(e, 0, sizeof(Effect));
battle.effectTail->next = e;
battle.effectTail = e;
speed = 50 + rand() % 50;
e->type = EFFECT_POINT;
e->x = ent->x + (rand() % ent->w) - (rand() % ent->w);
e->y = ent->y + (rand() % ent->h) - (rand() % ent->w);
e->dx = self->x - e->x;
e->dx /= speed;
e->dy = self->y - e->y;
e->dy /= speed;
e->r = e->g = e->b = e->a = (rand() % 255);
e->health = speed;
}
}
static void draw(void)
{
if (jumpgateEnabled())
{
blitRotated(portal, self->x - battle.camera.x, self->y - battle.camera.y, portalAngle);
}
blitRotated(self->texture, self->x - battle.camera.x, self->y - battle.camera.y, self->angle);
}