blobwarsAttrition/src/entities/structures/teleporter.c

161 lines
3.3 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 "teleporter.h"
2018-02-01 08:50:37 +01:00
static void init(void);
2018-01-27 12:55:15 +01:00
static void action(void);
static void touch(Entity *other);
static void activate(int active);
static void getCollisionBounds(SDL_Rect *r);
2018-01-30 23:47:40 +01:00
static void load(cJSON *root);
static void save(cJSON *root);
2018-01-27 12:55:15 +01:00
2018-01-30 09:29:09 +01:00
Entity *initTeleporter(void)
2018-01-27 12:55:15 +01:00
{
2018-01-30 09:29:09 +01:00
Structure *s;
2018-01-27 12:55:15 +01:00
2018-01-30 09:29:09 +01:00
s = createStructure();
2018-01-28 10:34:14 +01:00
2018-01-30 09:29:09 +01:00
s->type = ET_TELEPORTER;
2018-01-31 22:50:49 +01:00
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("TeleporterActive");
2018-01-30 09:29:09 +01:00
s->flags |= EF_WEIGHTLESS | EF_NO_CLIP | EF_IGNORE_BULLETS | EF_NO_TELEPORT;
2018-01-27 12:55:15 +01:00
2018-01-30 09:29:09 +01:00
s->plane = PLANE_FOREGROUND;
2018-01-27 12:55:15 +01:00
2018-01-30 09:29:09 +01:00
s->isStatic = 1;
2018-01-27 12:55:15 +01:00
2018-01-30 09:29:09 +01:00
s->active = 1;
2018-02-01 08:50:37 +01:00
s->init = init;
2018-01-30 09:29:09 +01:00
s->action = action;
s->touch = touch;
s->activate = activate;
s->getCollisionBounds = getCollisionBounds;
2018-01-30 23:47:40 +01:00
s->load = load;
s->save = save;
2018-01-27 12:55:15 +01:00
2018-01-30 09:29:09 +01:00
return (Entity*)s;
2018-01-27 12:55:15 +01:00
}
2018-02-01 08:50:37 +01:00
static void init(void)
{
Structure *s;
s = (Structure*)self;
if (s->active)
{
s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite("TeleporterActive");
}
else
{
s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite("TeleporterInactive");
}
s->spriteTime = 0;
s->spriteFrame = 0;
2018-02-01 08:50:37 +01:00
}
2018-01-27 12:55:15 +01:00
static void action(void)
{
if (self->active)
{
addTeleporterEffect(self->x + rand() % 85, self->y);
}
self->thinkTime = 1;
}
static void touch(Entity *other)
{
float tx, ty;
if (self->active && other != self && (other->flags & (EF_TELEPORTING | EF_NO_TELEPORT)) == 0)
2018-01-27 12:55:15 +01:00
{
tx = self->tx;
other->tx += self->w / 2;
other->tx -= other->w / 2;
ty = self->ty;
other->ty += self->h / 2;
other->ty -= other->h / 2;
teleportEntity(other, tx, ty);
2018-02-26 19:56:13 +01:00
playBattleSound(SND_TELEPORT, self->uniqueId % MAX_SND_CHANNELS, self->x, self->y);
2018-01-27 12:55:15 +01:00
}
}
static void activate(int active)
{
self->active = active;
self->init();
if (self->active)
{
observeActivation(self);
if (!isOnScreen(self))
{
2018-04-01 18:41:45 +02:00
setGameplayMessage(MSG_GAMEPLAY, app.strings[ST_TELEPORTER]);
2018-01-27 12:55:15 +01:00
}
}
}
2018-01-30 23:47:40 +01:00
static void getCollisionBounds(SDL_Rect *r)
{
r->x = self->x + 16;
r->y = self->y - 4;
r->w = 64;
r->h = self->h;
}
2018-01-30 23:47:40 +01:00
static void load(cJSON *root)
{
2018-02-03 17:13:37 +01:00
Structure *s;
s = (Structure*)self;
s->active = cJSON_GetObjectItem(root, "active")->valueint;
s->tx = cJSON_GetObjectItem(root, "tx")->valueint;
s->ty = cJSON_GetObjectItem(root, "ty")->valueint;
if (game.plus & PLUS_MIRROR)
{
s->tx = MAP_PIXEL_WIDTH - s->tx;
}
2018-01-30 23:47:40 +01:00
}
static void save(cJSON *root)
{
2018-02-03 17:13:37 +01:00
Structure *s;
s = (Structure*)self;
2018-01-30 23:47:40 +01:00
cJSON_AddStringToObject(root, "type", "Teleporter");
2018-02-03 17:13:37 +01:00
cJSON_AddNumberToObject(root, "active", s->active);
cJSON_AddNumberToObject(root, "tx", s->tx);
cJSON_AddNumberToObject(root, "ty", s->ty);
2018-01-30 23:47:40 +01:00
}