blobwarsAttrition/src/entities/cannons/cannon.c

235 lines
3.9 KiB
C
Raw Normal View History

2018-01-27 09:03:26 +01:00
/*
2019-06-02 17:13:34 +02:00
Copyright (C) 2018-2019 Parallel Realities
2018-01-27 09:03:26 +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 "cannon.h"
static void applyDamage(int damage);
static void walk(void);
static void die(void);
2018-02-15 08:49:28 +01:00
static void die2(void);
2018-01-27 09:03:26 +01:00
static void animate(void);
static int canFire(Entity *target);
static void preFire(void);
static void getCollisionBounds(SDL_Rect *r);
2018-01-27 09:03:26 +01:00
2018-02-14 23:31:21 +01:00
Entity *initCannon(void)
2018-01-27 09:03:26 +01:00
{
Unit *u;
2019-06-02 17:13:34 +02:00
2018-01-29 23:12:18 +01:00
u = createUnit();
2019-06-02 17:13:34 +02:00
2018-02-15 08:49:28 +01:00
u->unitType = "Cannon";
2019-06-02 17:13:34 +02:00
2018-01-28 10:34:14 +01:00
u->type = ET_ENEMY;
2018-01-27 09:03:26 +01:00
2018-01-30 00:00:14 +01:00
u->sprite[FACING_LEFT] = getSprite("CannonLeft");
u->sprite[FACING_RIGHT] = getSprite("CannonRight");
u->sprite[FACING_DIE] = getSprite("CannonLeft");
2018-01-27 09:03:26 +01:00
u->weaponType = WPN_MISSILE;
2018-01-27 09:03:26 +01:00
u->maxShotsToFire = 4;
2018-01-27 09:03:26 +01:00
u->reload = 0;
2018-01-27 09:03:26 +01:00
u->canCarryItem = 1;
2018-01-27 09:03:26 +01:00
u->health = u->healthMax = 75;
2018-01-27 09:03:26 +01:00
u->flags |= EF_EXPLODES | EF_BOMB_SHIELD;
2018-01-27 09:03:26 +01:00
u->action = walk;
u->animate = animate;
u->applyDamage = applyDamage;
u->walk = walk;
u->die = die;
u->canFire = canFire;
u->getCollisionBounds = getCollisionBounds;
2019-06-02 17:13:34 +02:00
2018-02-14 23:31:21 +01:00
return (Entity*)u;
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-02-15 08:49:28 +01:00
{
Unit *u;
u = (Unit*)self;
u->flags |= EF_BOUNCES | EF_ALWAYS_PROCESS;
u->action = die2;
2019-06-02 17:13:34 +02:00
2018-02-15 08:49:28 +01:00
u->facing = FACING_DIE;
u->thinkTime = 0;
u->spriteTime = 0;
u->spriteFrame = 0;
if (u->environment == ENV_AIR)
{
u->dy = -9;
}
u->dx = (randF() - randF()) * 5;
u->flags &= ~EF_HALT_AT_EDGE;
}
static void die2(void)
2018-01-27 09:03:26 +01:00
{
Unit *u;
2018-01-27 09:03:26 +01:00
int mx, my;
2019-06-02 17:13:34 +02:00
u = (Unit*)self;
2018-01-27 09:03:26 +01:00
if (--u->health % 3 == 0)
2018-01-27 09:03:26 +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);
addExplosion(u->x, u->y, 50, self);
2019-06-02 17:13:34 +02:00
throwDebris(u->x + u->w / 2, u->y + u->h / 2, 1);
2018-01-27 09:03:26 +01:00
}
if (u->alive == ALIVE_DYING && u->health <= -50)
2018-01-27 09:03:26 +01:00
{
updateObjective(u->name);
2018-01-27 09:03:26 +01:00
updateObjective("ENEMY");
fireTriggers(u->name);
2018-01-27 09:03:26 +01:00
dropCarriedItem();
u->alive = ALIVE_DEAD;
2019-06-02 17:13:34 +02:00
addRandomWeapon(u->x, u->y);
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)
{
Unit *u;
2018-01-27 09:03:26 +01:00
int r;
2019-06-02 17:13:34 +02:00
u = (Unit*)self;
2018-01-27 09:03:26 +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;
}
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;
}
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;
if (u->isMissionTarget)
2018-01-27 09:03:26 +01:00
{
r = rand() % 35;
2018-01-27 09:03:26 +01:00
}
if (r < 25)
2018-01-27 09:03:26 +01:00
{
u->shotsToFire = rrnd(1, u->maxShotsToFire);
u->action = preFire;
2018-01-27 09:03:26 +01:00
}
}
static void preFire(void)
{
Unit *u;
2019-06-02 17:13:34 +02:00
u = (Unit*)self;
2019-06-02 17:13:34 +02:00
u->facing = (world.bob->x < u->x) ? FACING_LEFT : FACING_RIGHT;
2018-01-27 09:03:26 +01:00
if (u->reload > 0)
2018-01-27 09:03:26 +01:00
{
return;
}
u->attack();
2018-01-27 09:03:26 +01:00
if (--u->shotsToFire == 0)
2018-01-27 09:03:26 +01:00
{
u->walk();
2018-01-27 09:03:26 +01:00
}
}
static void walk(void)
{
self->action = lookForPlayer;
}
static void animate(void)
{
}
static void getCollisionBounds(SDL_Rect *r)
{
r->x = self->x + 36;
r->y = self->y;
r->w = 36;
r->h = self->h;
}
2018-01-27 09:03:26 +01:00
static int canFire(Entity *target)
{
2018-03-29 09:21:36 +02:00
return fabs(target->y - self->y) <= MAP_TILE_SIZE;
2018-01-27 09:03:26 +01:00
}