blobwarsAttrition/src/entities/structures/powerPoint.c

160 lines
3.1 KiB
C
Raw Normal View History

2018-01-27 12:15:23 +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 "powerPoint.h"
2018-02-01 08:50:37 +01:00
static void init(void);
2018-01-27 12:15:23 +01:00
static void tick(void);
static void action(void);
static void touch(Entity *other);
2018-01-30 23:47:40 +01:00
static void load(cJSON *root);
static void save(cJSON *root);
2018-01-27 12:15:23 +01:00
2018-01-30 09:29:09 +01:00
Entity *initPowerPoint(void)
2018-01-27 12:15:23 +01:00
{
Structure *s;
2018-01-30 09:29:09 +01:00
s = createStructure();
2018-01-28 10:34:14 +01:00
s->type = ET_POWER_POINT;
2018-01-27 12:15:23 +01:00
2018-01-30 00:00:14 +01:00
s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite("PowerPoint");
2018-01-27 12:15:23 +01:00
s->flags |= EF_WEIGHTLESS | EF_NO_CLIP | EF_NO_ENVIRONMENT | EF_IGNORE_BULLETS;
2018-01-27 12:15:23 +01:00
s->requiredPower = 100;
2018-01-27 12:15:23 +01:00
s->isStatic = 1;
2018-01-27 12:15:23 +01:00
2018-02-01 08:50:37 +01:00
s->init = init;
s->tick = tick;
s->action = action;
s->touch = touch;
2018-01-30 23:47:40 +01:00
s->load = load;
s->save = save;
2018-01-30 09:29:09 +01:00
return (Entity*)s;
2018-01-27 12:15:23 +01:00
}
2018-02-01 08:50:37 +01:00
static void init(void)
{
Structure *s;
s = (Structure*)self;
if (s->requiredPower == 100 && game.cells != 0)
{
s->requiredPower = rrnd(game.cells * 0.7, game.cells * 0.9);
}
else if (s->requiredPower == 0)
{
s->spriteFrame = 3;
s->active = 1;
}
}
2018-01-27 12:15:23 +01:00
static void tick(void)
{
Structure *s;
s = (Structure*)self;
if (!s->active)
2018-01-27 12:15:23 +01:00
{
s->bobTouching = MAX(s->bobTouching - 1, 0);
2018-01-27 12:15:23 +01:00
if (s->bobTouching == 0)
2018-01-27 12:15:23 +01:00
{
s->spriteFrame = 0;
s->spriteTime = -1;
s->thinkTime = FPS / 2;
2018-01-27 12:15:23 +01:00
}
}
}
static void action(void)
{
Structure *s;
s = (Structure*)self;
s->spriteFrame = MIN(s->spriteFrame + 1, 3);
2018-01-27 12:15:23 +01:00
if (!s->active && s->spriteFrame == 3)
2018-01-27 12:15:23 +01:00
{
activateEntities(s->targetNames, 1);
2018-01-27 12:15:23 +01:00
setGameplayMessage(MSG_GAMEPLAY, s->message);
2018-01-27 12:15:23 +01:00
if (!dev.cheatPower)
{
world.bob->power -= s->requiredPower;
2018-01-27 12:15:23 +01:00
}
s->requiredPower = 0;
2018-01-27 12:15:23 +01:00
s->active = 1;
2018-01-27 12:15:23 +01:00
}
else
{
s->thinkTime = FPS / 2;
2018-01-27 12:15:23 +01:00
}
}
static void touch(Entity *other)
{
Structure *s;
s = (Structure*)self;
if (!s->active && other->type == ET_BOB && other->dx == 0)
2018-01-27 12:15:23 +01:00
{
if (world.bob->power < s->requiredPower && s->bobTouching == 0 && !dev.cheatPower)
2018-01-27 12:15:23 +01:00
{
2018-01-28 17:33:37 +01:00
setGameplayMessage(MSG_GAMEPLAY, _("Not enough power (%d units required)"), s->requiredPower);
2018-01-27 12:15:23 +01:00
}
else
{
s->bobTouching = 2;
2018-01-27 12:15:23 +01:00
}
}
}
2018-01-30 23:47:40 +01:00
static void load(cJSON *root)
{
Structure *s;
s = (Structure*)self;
s->requiredPower = cJSON_GetObjectItem(root, "requiredPower")->valueint;
STRNCPY(s->targetNames, cJSON_GetObjectItem(root, "targetNames")->valuestring, MAX_DESCRIPTION_LENGTH);
2018-01-30 23:47:40 +01:00
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "PowerPoint");
cJSON_AddNumberToObject(root, "requiredPower", s->requiredPower);
cJSON_AddStringToObject(root, "targetNames", s->targetNames);
}