blobwarsAttrition/src/entities/traps/laserTrap.c

209 lines
3.7 KiB
C
Raw Normal View History

2018-01-27 12:55:15 +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 "laserTrap.h"
static void init(void);
static void tick(void);
static void action(void);
static void touch(Entity *other);
static void activate(int active);
static void load(cJSON *root);
static void save(cJSON *root);
2018-01-27 12:55:15 +01:00
Entity *initLaserTrap(void)
2018-01-27 12:55:15 +01:00
{
Trap *t;
t = malloc(sizeof(Trap));
memset(t, 0, sizeof(Trap));
2018-01-27 12:55:15 +01:00
initEntity((Entity*)t);
2018-01-28 10:34:14 +01:00
t->type = ET_TRAP;
t->flags |= EF_WEIGHTLESS | EF_IGNORE_BULLETS | EF_NO_ENVIRONMENT | EF_NO_CLIP | EF_ALWAYS_PROCESS;
2018-01-27 12:55:15 +01:00
t->onTime = FPS * 2;
t->offTime = FPS * 2;
2018-01-27 12:55:15 +01:00
2018-01-30 00:00:14 +01:00
t->sprite[0] = t->sprite[1] = t->sprite[2] = getSprite("LaserTrap");
2018-01-27 12:55:15 +01:00
t->active = 1;
2018-01-27 12:55:15 +01:00
t->init = init;
t->tick = tick;
t->action = action;
t->touch = touch;
t->activate = activate;
t->load = load;
t->save = save;
return (Entity*)t;
2018-01-27 12:55:15 +01:00
}
static void init(void)
{
if (!self->active)
{
self->flags |= EF_GONE;
}
}
static void tick(void)
{
Trap *t;
t = (Trap*)self;
if (!t->active && t->spriteTime == -1)
2018-01-27 12:55:15 +01:00
{
t->flags |= EF_GONE;
2018-01-27 12:55:15 +01:00
}
}
static void action(void)
{
Trap *t;
t = (Trap*)self;
if (t->offTime != 0)
2018-01-27 12:55:15 +01:00
{
if (!t->active)
2018-01-27 12:55:15 +01:00
{
t->thinkTime = t->onTime;
t->spriteFrame = 0;
t->spriteTime = 0;
t->flags &= ~EF_GONE;
2018-01-27 12:55:15 +01:00
}
else if (t->offTime > 0)
2018-01-27 12:55:15 +01:00
{
t->thinkTime = t->offTime;
t->spriteTime = 0;
2018-01-27 12:55:15 +01:00
}
t->active = !t->active;
2018-01-27 12:55:15 +01:00
}
else
{
t->spriteTime = -1;
t->spriteFrame = 4;
2018-01-27 12:55:15 +01:00
}
}
static void touch(Entity *other)
{
Trap *t;
t = (Trap*)self;
2018-01-27 12:55:15 +01:00
if (other != NULL && (other->type == ET_BOB || other->type == ET_ENEMY))
{
if (!(other->flags & EF_IMMUNE))
{
if (other->health > 0 || rand() % 100 < 5)
{
other->dx = rrnd(-12, 12);
other->dy = rrnd(-8, 0);
if (t->offTime != 0)
2018-01-27 12:55:15 +01:00
{
swapSelf(other);
2018-01-27 12:55:15 +01:00
other->applyDamage((int) (other->healthMax / 4));
swapSelf(other);
2018-01-27 12:55:15 +01:00
}
else
{
/* instant kill */
swapSelf(other);
2018-01-27 12:55:15 +01:00
other->applyDamage((int) other->health);
swapSelf(other);
2018-01-27 12:55:15 +01:00
}
playSound(SND_FLESH_HIT, CH_ANY);
2018-01-27 12:55:15 +01:00
}
if (other == (Entity*)world.bob && world.bob->stunTimer == 0)
2018-01-27 12:55:15 +01:00
{
2018-01-29 23:12:18 +01:00
stunBob();
2018-01-27 12:55:15 +01:00
}
}
if (other->flags & EF_EXPLODES)
{
addSparkParticles(t->x, t->y);
2018-01-27 12:55:15 +01:00
}
else
{
addSmallFleshChunk(t->x, t->y);
2018-01-27 12:55:15 +01:00
}
}
}
static void activate(int active)
{
self->active = !active;
if (!self->active)
{
self->flags |= EF_GONE;
}
else
{
self->flags &= ~EF_GONE;
}
if (!self->active)
{
observeActivation(self);
if (!isOnScreen(self))
{
2018-01-28 17:33:37 +01:00
setGameplayMessage(MSG_GAMEPLAY, _("Lasers disabled ..."));
2018-01-27 12:55:15 +01:00
}
}
}
static void load(cJSON *root)
{
Trap *t;
t = (Trap*)self;
if (cJSON_GetObjectItem(root, "active"))
{
t->active = cJSON_GetObjectItem(root, "active")->valueint;
}
t->onTime = cJSON_GetObjectItem(root, "onTime")->valueint;
t->offTime = cJSON_GetObjectItem(root, "offTime")->valueint;
}
static void save(cJSON *root)
{
Trap *t;
t = (Trap*)self;
cJSON_AddNumberToObject(root, "active", t->active);
cJSON_AddNumberToObject(root, "onTime", t->onTime);
cJSON_AddNumberToObject(root, "offTime", t->offTime);
}