blobwarsAttrition/src/entities/unit.c

222 lines
3.5 KiB
C
Raw Normal View History

2018-01-24 23:15:24 +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 "unit.h"
2018-01-25 09:11:45 +01:00
void unitTick(void);
2018-01-24 23:15:24 +01:00
static void attack(void);
static int canFire(Entity *target);
2018-01-29 23:12:18 +01:00
static void preFire(void);
2018-01-24 23:15:24 +01:00
2018-01-29 23:12:18 +01:00
Unit *createUnit(void)
2018-01-24 23:15:24 +01:00
{
Unit *u;
2018-01-29 23:12:18 +01:00
u = malloc(sizeof(Unit));
memset(u, 0, sizeof(Unit));
world.entityTail->next = (Entity*)u;
world.entityTail = (Entity*)u;
2018-01-24 23:15:24 +01:00
2018-01-29 23:12:18 +01:00
initEntity((Entity*)u);
u->oxygen = MAX_OXYGEN;
2018-01-24 23:15:24 +01:00
u->canCarryItem = rand() % 100 < 85;
2018-01-24 23:15:24 +01:00
if (world.isOutpostMission)
{
u->canCarryItem = 1;
u->health = u->healthMax = rrnd(1, 4);
2018-01-24 23:15:24 +01:00
}
u->spriteFrame = 0;
2018-01-24 23:15:24 +01:00
u->startX = u->startY = -1;
2018-01-24 23:15:24 +01:00
u->tick = unitTick;
u->action = lookForPlayer;
2018-01-29 23:12:18 +01:00
u->preFire = preFire;
u->attack = attack;
u->canFire = canFire;
2018-01-29 23:12:18 +01:00
return u;
2018-01-24 23:15:24 +01:00
}
void reInitUnit(Entity *e)
{
Unit *u;
u = (Unit*)self;
if (u->startX == -1 && u->startY == -1)
2018-01-24 23:15:24 +01:00
{
u->startX = (int) u->x;
u->startY = (int) u->y;
2018-01-24 23:15:24 +01:00
}
if (u->isMissionTarget)
2018-01-24 23:15:24 +01:00
{
u->flags |= EF_BOMB_SHIELD;
2018-01-24 23:15:24 +01:00
}
}
2018-01-25 09:11:45 +01:00
void unitTick(void)
2018-01-24 23:15:24 +01:00
{
Unit *u;
u = (Unit*)self;
if (u->alive == ALIVE_ALIVE)
2018-01-24 23:15:24 +01:00
{
u->reload = limit(u->reload - 1, 0, FPS);
2018-01-24 23:15:24 +01:00
}
switch (u->environment)
2018-01-24 23:15:24 +01:00
{
case ENV_AIR:
u->oxygen = limit(u->oxygen + 4, 0, MAX_OXYGEN);
2018-01-24 23:15:24 +01:00
break;
case ENV_WATER:
u->oxygen = limit(u->oxygen - 1, 0, MAX_OXYGEN);
if (u->oxygen == 0 && world.frameCounter % 30 == 0)
2018-01-24 23:15:24 +01:00
{
u->health--;
2018-01-24 23:15:24 +01:00
}
break;
case ENV_SLIME:
case ENV_LAVA:
if (u->alive == ALIVE_ALIVE)
2018-01-24 23:15:24 +01:00
{
u->health = 0;
2018-01-24 23:15:24 +01:00
}
break;
}
if (u->flags & EF_WATER_BREATHING)
2018-01-24 23:15:24 +01:00
{
u->oxygen = MAX_OXYGEN;
2018-01-24 23:15:24 +01:00
}
if (u->spawnedIn)
2018-01-24 23:15:24 +01:00
{
if (getDistance(u->x, u->y, world.bob->x, world.bob->y) < 1000)
2018-01-24 23:15:24 +01:00
{
u->spawnedInTimer = FPS * 5;
2018-01-24 23:15:24 +01:00
}
u->spawnedInTimer--;
if (u->spawnedInTimer <= 0)
2018-01-24 23:15:24 +01:00
{
u->alive = ALIVE_DEAD;
2018-01-24 23:15:24 +01:00
}
}
}
void unitReappear(void)
{
}
static void attack(void)
{
Unit *u;
u = (Unit*)self;
if (u->canFire((Entity*)world.bob))
2018-01-24 23:15:24 +01:00
{
switch (u->weaponType)
2018-01-24 23:15:24 +01:00
{
case WPN_AIMED_PISTOL:
fireAimedShot(self);
break;
case WPN_MACHINE_GUN:
fireMachineGun(self);
break;
case WPN_GRENADES:
fireGrenade(self);
break;
case WPN_PLASMA:
firePlasma(self);
break;
case WPN_SPREAD:
fireSpread(self, 3);
break;
case WPN_LASER:
fireLaser(self);
break;
case WPN_SHOTGUN:
fireShotgun(self);
break;
case WPN_MISSILE:
fireMissile(self);
break;
default:
printf("Can't fire weapon: %d\n", u->weaponType);
2018-01-24 23:15:24 +01:00
break;
}
}
}
2018-01-29 23:12:18 +01:00
static void preFire(void)
{
Unit *u;
u = (Unit*)self;
if (!(u->flags & EF_WEIGHTLESS))
{
if (world.bob->y < u->y && u->isOnGround && rand() % 4 == 0)
{
u->dy = JUMP_POWER;
}
}
u->facing = (world.bob->x < u->x) ? FACING_LEFT : FACING_RIGHT;
if (u->reload > 0)
{
return;
}
u->attack();
u->shotsToFire--;
if (u->shotsToFire == 0)
{
u->walk();
}
}
static int canFire(Entity *target)
{
return 0;
}