blobwarsAttrition/src/entities/structures/lift.c

165 lines
3.6 KiB
C
Raw Normal View History

2018-01-27 11:58:47 +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 "lift.h"
2018-02-01 08:50:37 +01:00
static void init(void);
2018-01-27 11:58:47 +01:00
static void action(void);
static void activate(int active);
2018-01-30 23:47:40 +01:00
static void load(cJSON *root);
static void save(cJSON *root);
2018-01-27 11:58:47 +01:00
2018-01-30 09:29:09 +01:00
Entity *initLift(Entity *e)
2018-01-27 11:58:47 +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_LIFT;
s->state = LIFT_GOTO_FINISH;
2018-01-27 11:58:47 +01:00
s->flags |= EF_WEIGHTLESS | EF_NO_ENVIRONMENT | EF_EXPLODES | EF_NO_CLIP | EF_ALWAYS_PROCESS | EF_NO_TELEPORT;
2018-01-27 11:58:47 +01:00
s->speed = 2;
2018-01-27 11:58:47 +01:00
s->waitTime = 2;
2018-01-27 11:58:47 +01:00
s->startX = s->startY = -1;
2018-01-27 11:58:47 +01:00
2018-01-30 00:00:14 +01:00
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("Lift");
2018-01-27 11:58:47 +01:00
s->active = 1;
2018-01-27 11:58:47 +01:00
2018-02-01 08:50:37 +01:00
s->init = init;
s->action = action;
s->activate = activate;
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 11:58:47 +01:00
}
2018-02-01 08:50:37 +01:00
static void init(void)
{
Structure *s;
s = (Structure*)self;
if (s->startX == -1 && s->startY == -1)
{
s->startX = s->x;
s->startY = s->y;
}
}
2018-01-27 11:58:47 +01:00
static void action(void)
{
Structure *s;
s = (Structure*)self;
s->dx = s->dy = 0;
2018-01-27 11:58:47 +01:00
if (s->active)
2018-01-27 11:58:47 +01:00
{
if (s->state == LIFT_GOTO_START)
2018-01-27 11:58:47 +01:00
{
getSlope(s->startX, s->startY, s->x, s->y, &s->dx, &s->dy);
2018-01-27 11:58:47 +01:00
s->dx *= s->speed;
s->dy *= s->speed;
2018-01-27 11:58:47 +01:00
2018-03-29 09:21:36 +02:00
if (fabs(s->x - s->startX) < s->speed && fabs(s->y - s->startY) < s->speed)
2018-01-27 11:58:47 +01:00
{
s->x = s->startX;
s->y = s->startY;
s->state = LIFT_GOTO_FINISH;
s->thinkTime = s->waitTime * FPS;
s->dx = s->dy = 0;
2018-01-27 11:58:47 +01:00
}
}
else if (s->state == LIFT_GOTO_FINISH)
2018-01-27 11:58:47 +01:00
{
getSlope(s->tx, s->ty, s->x, s->y, &s->dx, &s->dy);
2018-01-27 11:58:47 +01:00
s->dx *= s->speed;
s->dy *= s->speed;
2018-01-27 11:58:47 +01:00
2018-03-29 09:21:36 +02:00
if (fabs(s->x - s->tx) < s->speed && fabs(s->y - s->ty) < s->speed)
2018-01-27 11:58:47 +01:00
{
s->x = s->tx;
s->y = s->ty;
s->state = LIFT_GOTO_START;
s->thinkTime = s->waitTime * FPS;
s->dx = s->dy = 0;
2018-01-27 11:58:47 +01:00
}
}
}
}
static void activate(int active)
{
self->active = active;
if (self->active)
{
observeActivation(self);
if (!isOnScreen(self))
{
setGameplayMessage(MSG_GAMEPLAY, _("Platform activated ..."));
2018-01-27 11:58:47 +01:00
}
}
}
2018-01-30 23:47:40 +01:00
static void load(cJSON *root)
{
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;
s->speed = cJSON_GetObjectItem(root, "speed")->valueint;
2018-02-03 12:32:03 +01:00
s->state = lookup(cJSON_GetObjectItem(root, "state")->valuestring);
2018-01-30 23:47:40 +01:00
s->startX = cJSON_GetObjectItem(root, "startX")->valueint;
s->startY = cJSON_GetObjectItem(root, "startY")->valueint;
s->waitTime = cJSON_GetObjectItem(root, "waitTime")->valueint;
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "Lift");
cJSON_AddNumberToObject(root, "active", s->active);
cJSON_AddNumberToObject(root, "tx", s->tx);
cJSON_AddNumberToObject(root, "ty", s->ty);
cJSON_AddNumberToObject(root, "speed", s->speed);
2018-02-03 12:32:03 +01:00
cJSON_AddStringToObject(root, "state", getLookupName("LIFT_", s->state));
2018-01-30 23:47:40 +01:00
cJSON_AddNumberToObject(root, "startX", s->startX);
cJSON_AddNumberToObject(root, "startY", s->startY);
cJSON_AddNumberToObject(root, "waitTime", s->waitTime);
}