blobwarsAttrition/src/entities/structures/cardReader.c

152 lines
3.4 KiB
C
Raw Normal View History

2018-01-27 09:53:08 +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 "cardReader.h"
static void init(void);
2018-01-27 09:53:08 +01:00
static void tick(void);
2018-03-29 09:21:36 +02:00
static void activate(int active);
2018-01-27 09:53:08 +01:00
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 09:53:08 +01:00
Entity *initCardReader(void)
2018-01-27 09:53:08 +01:00
{
Structure *s;
s = createStructure();
2018-01-28 10:34:14 +01:00
s->type = ET_CARD_READER;
s->flags |= EF_WEIGHTLESS | EF_NO_CLIP | EF_NO_ENVIRONMENT | EF_IGNORE_BULLETS | EF_NO_TELEPORT;
2018-01-27 09:53:08 +01:00
STRNCPY(s->requiredItem, "Black Keycard", MAX_NAME_LENGTH);
2018-01-27 09:53:08 +01:00
s->isStatic = 1;
2018-01-27 09:53:08 +01:00
s->tick = tick;
s->init = init;
2018-03-29 09:21:36 +02:00
s->activate = activate;
s->touch = touch;
s->load = load;
s->save = save;
return (Entity*)s;
}
static void init(void)
{
Structure *s;
s = (Structure*)self;
if (!s->active)
2018-01-27 09:53:08 +01:00
{
2018-01-30 00:00:14 +01:00
s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite("CardReaderIdle");
2018-01-27 09:53:08 +01:00
}
else
{
2018-01-30 00:00:14 +01:00
s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite("CardReader");
2018-01-27 09:53:08 +01:00
}
}
static void tick(void)
{
Structure *s;
s = (Structure*)self;
if (!s->active)
2018-01-27 09:53:08 +01:00
{
s->bobTouching = MAX(s->bobTouching - 1, 0);
2018-01-27 09:53:08 +01:00
}
}
static void touch(Entity *other)
{
Structure *s;
s = (Structure*)self;
if (!s->active && other->type == ET_BOB)
2018-01-27 09:53:08 +01:00
{
if (hasItem(s->requiredItem) || dev.cheatKeys)
2018-01-27 09:53:08 +01:00
{
activateEntities(s->targetNames, 1);
2018-01-27 09:53:08 +01:00
2018-01-28 17:33:37 +01:00
setGameplayMessage(MSG_GAMEPLAY, _("%s removed"), s->requiredItem);
2018-01-27 09:53:08 +01:00
removeItem(s->requiredItem);
2018-01-27 09:53:08 +01:00
s->active = 1;
2018-01-30 00:00:14 +01:00
s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite("CardReader");
s->spriteTime = 0;
s->spriteFrame = 0;
2018-01-27 09:53:08 +01:00
2018-02-25 18:29:44 +01:00
playSound(SND_CONFIRMED, s->uniqueId % MAX_SND_CHANNELS);
2018-01-27 09:53:08 +01:00
}
else if (s->bobTouching == 0)
2018-01-27 09:53:08 +01:00
{
2018-01-28 17:33:37 +01:00
setGameplayMessage(MSG_GAMEPLAY, _("%s required"), s->requiredItem);
2018-02-25 18:29:44 +01:00
playSound(SND_DENIED, s->uniqueId % MAX_SND_CHANNELS);
2018-01-27 09:53:08 +01:00
}
s->bobTouching = 2;
2018-01-27 09:53:08 +01:00
}
}
2018-01-30 23:47:40 +01:00
2018-03-29 09:21:36 +02:00
static void activate(int active)
{
Structure *s;
s = (Structure*)self;
s->active = 1;
s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite("CardReader");
s->spriteTime = 0;
s->spriteFrame = 0;
}
2018-01-30 23:47:40 +01:00
static void load(cJSON *root)
{
Structure *s;
s = (Structure*)self;
if (cJSON_GetObjectItem(root, "active"))
{
s->active = cJSON_GetObjectItem(root, "active")->valueint;
}
2018-01-30 23:47:40 +01:00
STRNCPY(s->requiredItem, cJSON_GetObjectItem(root, "requiredCard")->valuestring, MAX_NAME_LENGTH);
STRNCPY(s->targetNames, cJSON_GetObjectItem(root, "targetNames")->valuestring, MAX_DESCRIPTION_LENGTH);
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "CardReader");
cJSON_AddNumberToObject(root, "active", s->active);
cJSON_AddStringToObject(root, "requiredCard", s->requiredItem);
cJSON_AddStringToObject(root, "targetNames", s->targetNames);
}