2018-01-27 09:03:26 +01:00
|
|
|
/*
|
|
|
|
Copyright (C) 2018 Parallel Realities
|
|
|
|
|
|
|
|
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 "cannon.h"
|
|
|
|
|
|
|
|
static void applyDamage(int damage);
|
|
|
|
static void walk(void);
|
|
|
|
static void die(void);
|
|
|
|
static void animate(void);
|
2018-01-28 09:30:53 +01:00
|
|
|
static SDL_Rect *getBounds(void);
|
2018-01-27 09:03:26 +01:00
|
|
|
static int canFire(Entity *target);
|
|
|
|
static void preFire(void);
|
|
|
|
|
|
|
|
void initCannon(Entity *e)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Unit *u;
|
|
|
|
|
2018-01-27 09:03:26 +01:00
|
|
|
initUnit(e);
|
2018-01-28 09:30:53 +01:00
|
|
|
|
|
|
|
u = (Unit*)self;
|
2018-01-28 10:34:14 +01:00
|
|
|
|
|
|
|
u->type = ET_ENEMY;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->sprite[FACING_LEFT] = getSpriteIndex("CannonLeft");
|
|
|
|
u->sprite[FACING_RIGHT] = getSpriteIndex("CannonRight");
|
|
|
|
u->sprite[FACING_DIE] = getSpriteIndex("CannonLeft");
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->weaponType = WPN_MISSILE;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->maxShotsToFire = 4;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->reload = 0;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->canCarryItem = 1;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->health = u->healthMax = 50;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->flags |= EF_EXPLODES;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->animate = animate;
|
|
|
|
u->applyDamage = applyDamage;
|
|
|
|
u->walk = walk;
|
|
|
|
u->die = die;
|
|
|
|
u->getBounds= getBounds;
|
|
|
|
u->canFire = canFire;
|
2018-01-27 09:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void applyDamage(int damage)
|
|
|
|
{
|
|
|
|
if (self->health > 0)
|
|
|
|
{
|
|
|
|
self->health -= damage;
|
|
|
|
|
|
|
|
self->facing = self->x < world.bob->x ? FACING_RIGHT : FACING_LEFT;
|
|
|
|
|
|
|
|
self->thinkTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void die(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Unit *u;
|
2018-01-27 09:03:26 +01:00
|
|
|
int mx, my;
|
2018-01-28 09:30:53 +01:00
|
|
|
|
|
|
|
u = (Unit*)self;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->health--;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (u->health % 3 == 0)
|
2018-01-27 09:03:26 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
mx = (int) ((u->x + (u->w / 2)) / MAP_TILE_SIZE);
|
|
|
|
my = (int) ((u->y + u->h) / MAP_TILE_SIZE);
|
2018-01-27 09:03:26 +01:00
|
|
|
addScorchDecal(mx, my);
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
addExplosion(u->x, u->y, 50, self);
|
2018-01-27 09:03:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (u->health <= -50)
|
2018-01-27 09:03:26 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
updateObjective(u->name);
|
2018-01-27 09:03:26 +01:00
|
|
|
updateObjective("ENEMY");
|
2018-01-28 09:30:53 +01:00
|
|
|
fireTriggers(u->name);
|
2018-01-27 09:03:26 +01:00
|
|
|
|
|
|
|
dropCarriedItem();
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->alive = ALIVE_DEAD;
|
2018-01-27 09:03:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void patrol(void)
|
|
|
|
{
|
|
|
|
self->facing = rand() % 2 ? FACING_LEFT : FACING_RIGHT;
|
|
|
|
|
|
|
|
self->thinkTime = rrnd(FPS / 2, FPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lookForPlayer(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Unit *u;
|
2018-01-27 09:03:26 +01:00
|
|
|
int r;
|
2018-01-28 09:30:53 +01:00
|
|
|
|
|
|
|
u = (Unit*)self;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->thinkTime = rrnd(FPS / 2, FPS);
|
2018-01-27 09:03:26 +01:00
|
|
|
|
|
|
|
if (world.state != WS_IN_PROGRESS || dev.cheatBlind)
|
|
|
|
{
|
|
|
|
patrol();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if ((u->facing == FACING_LEFT && world.bob->x > u->x) || (u->facing == FACING_RIGHT && world.bob->x < u->x))
|
2018-01-27 09:03:26 +01:00
|
|
|
{
|
|
|
|
patrol();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (getDistance(world.bob->x, world.bob->y, u->x, u->y) > 650)
|
2018-01-27 09:03:26 +01:00
|
|
|
{
|
|
|
|
patrol();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enemyCanSeePlayer(self))
|
|
|
|
{
|
|
|
|
patrol();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = rand() % 100;
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (u->isMissionTarget)
|
2018-01-27 09:03:26 +01:00
|
|
|
{
|
|
|
|
r = rand() % 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 15)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->shotsToFire = rrnd(1, u->maxShotsToFire);
|
|
|
|
u->action = preFire;
|
2018-01-27 09:03:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void preFire(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Unit *u;
|
|
|
|
|
|
|
|
u = (Unit*)self;
|
|
|
|
|
|
|
|
u->facing = (world.bob->x < u->x) ? FACING_LEFT : FACING_RIGHT;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (u->reload > 0)
|
2018-01-27 09:03:26 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->attack();
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
u->shotsToFire--;
|
2018-01-27 09:03:26 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
if (u->shotsToFire == 0)
|
2018-01-27 09:03:26 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->walk();
|
2018-01-27 09:03:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void walk(void)
|
|
|
|
{
|
|
|
|
self->action = lookForPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void animate(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
static SDL_Rect *getBounds(void)
|
2018-01-27 09:03:26 +01:00
|
|
|
{
|
|
|
|
self->bounds.x = self->x + 36;
|
|
|
|
self->bounds.y = self->y;
|
|
|
|
self->bounds.w = 36;
|
|
|
|
self->bounds.h = self->h;
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
return &self->bounds;
|
2018-01-27 09:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int canFire(Entity *target)
|
|
|
|
{
|
|
|
|
return abs(target->y - self->y) <= MAP_TILE_SIZE;
|
|
|
|
}
|