2018-01-26 20:14:18 +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 "item.h"
|
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
static void init(void);
|
2018-01-26 20:14:18 +01:00
|
|
|
static void reset(void);
|
|
|
|
static void tick(void);
|
|
|
|
static void touch(Entity *other);
|
|
|
|
static void changeEnvironment(void);
|
|
|
|
static void die(void);
|
2018-01-28 09:30:53 +01:00
|
|
|
static void destructablePickupItem(Structure *s);
|
|
|
|
static void enemyPickupItem(Unit *u);
|
2018-01-26 20:14:18 +01:00
|
|
|
static void bobPickupItem(void);
|
2018-01-30 23:47:40 +01:00
|
|
|
static void load(cJSON *root);
|
|
|
|
static void save(cJSON *root);
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
Entity *createItem(void)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Item *i;
|
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
i = malloc(sizeof(Item));
|
|
|
|
memset(i, 0, sizeof(Item));
|
2018-01-28 09:30:53 +01:00
|
|
|
|
2018-01-30 09:29:09 +01:00
|
|
|
initEntity((Entity*)i);
|
2018-01-28 10:34:14 +01:00
|
|
|
|
|
|
|
i->type = ET_ITEM;
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
STRNCPY(i->spriteName, "Weapon", MAX_NAME_LENGTH);
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
i->flags |= EF_IGNORE_BULLETS;
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
i->isMissionTarget = 1;
|
|
|
|
i->canBePickedUp = 1;
|
|
|
|
i->canBeCarried = 0;
|
|
|
|
i->collected = 0;
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-01-30 00:00:14 +01:00
|
|
|
i->sprite[FACING_LEFT] = i->sprite[FACING_RIGHT] = i->sprite[FACING_DIE] = getSprite(i->spriteName);
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
i->init = init;
|
2018-01-28 09:30:53 +01:00
|
|
|
i->tick = tick;
|
|
|
|
i->touch = touch;
|
|
|
|
i->changeEnvironment = changeEnvironment;
|
|
|
|
i->reset = reset;
|
|
|
|
i->die = die;
|
2018-01-30 23:47:40 +01:00
|
|
|
i->load = load;
|
|
|
|
i->save = save;
|
2018-01-30 09:29:09 +01:00
|
|
|
|
|
|
|
return (Entity*)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entity *initItem(void)
|
|
|
|
{
|
|
|
|
return createItem();
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 08:50:37 +01:00
|
|
|
static void init(void)
|
|
|
|
{
|
|
|
|
Item *i;
|
|
|
|
|
|
|
|
i = (Item*)self;
|
|
|
|
|
|
|
|
i->sprite[FACING_LEFT] = i->sprite[FACING_RIGHT] = i->sprite[FACING_DIE] = getSprite(i->spriteName);
|
|
|
|
}
|
|
|
|
|
2018-01-26 20:14:18 +01:00
|
|
|
static void reset(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Item *i;
|
|
|
|
|
|
|
|
i = (Item*)self;
|
|
|
|
|
|
|
|
i->startX = (int) self->x;
|
|
|
|
i->startY = (int) self->y;
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tick(void)
|
|
|
|
{
|
|
|
|
if (self->isOnGround)
|
|
|
|
{
|
|
|
|
self->dx *= 0.95;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hack, to ensure the size doesn't exceed the maxmium of something carrying it */
|
|
|
|
self->w = self->w > 32 ? 32 : self->w;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void touch(Entity *other)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Item *i;
|
|
|
|
|
|
|
|
i = (Item*)self;
|
|
|
|
|
|
|
|
if (i->alive == ALIVE_ALIVE && other != NULL && i->canBePickedUp)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
|
|
|
if (other->type == ET_BOB && !world.bob->stunTimer)
|
|
|
|
{
|
|
|
|
bobPickupItem();
|
|
|
|
}
|
|
|
|
else if (other->type == ET_ENEMY)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
enemyPickupItem((Unit*)other);
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
else if (other->type == ET_DESTRUCTABLE)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
destructablePickupItem((Structure*)other);
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bobPickupItem(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Item *i;
|
|
|
|
|
|
|
|
i = (Item*)self;
|
|
|
|
|
|
|
|
if (!i->isMissionTarget)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
if (i->thinkTime == 0)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-02-18 10:28:25 +01:00
|
|
|
if (addItem(i, 1))
|
2018-02-14 08:12:47 +01:00
|
|
|
{
|
|
|
|
game.stats[STAT_KEYS_FOUND]++;
|
|
|
|
updateObjective("KEY");
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-04-01 18:41:45 +02:00
|
|
|
setGameplayMessage(MSG_STANDARD, app.strings[ST_PICKED_UP], i->name);
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-02-25 18:29:44 +01:00
|
|
|
playSound(SND_KEY, i->uniqueId % MAX_SND_CHANNELS);
|
2018-02-14 08:12:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-12 17:05:41 +02:00
|
|
|
setGameplayMessage(MSG_GAMEPLAY, app.strings[ST_CANNOT_CARRY_KEYS]);
|
2018-02-14 08:12:47 +01:00
|
|
|
}
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-28 09:30:53 +01:00
|
|
|
else if (i->canBeCarried)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-02-18 10:28:25 +01:00
|
|
|
if (addItem(i, 1))
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
if (!i->collected)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
updateObjective(i->name);
|
|
|
|
i->collected = 1;
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
|
2018-04-01 18:41:45 +02:00
|
|
|
setGameplayMessage(MSG_STANDARD, app.strings[ST_PICKED_UP], i->name);
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-02-25 18:29:44 +01:00
|
|
|
playSound(SND_ITEM, i->uniqueId % MAX_SND_CHANNELS);
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-12 17:05:41 +02:00
|
|
|
setGameplayMessage(MSG_GAMEPLAY, app.strings[ST_CANNOT_CARRY_ITEMS]);
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
i->alive = ALIVE_DEAD;
|
|
|
|
updateObjective(i->name);
|
2018-01-26 20:14:18 +01:00
|
|
|
|
|
|
|
if (strcmp(world.id, "teeka") != 0)
|
|
|
|
{
|
2018-04-01 18:41:45 +02:00
|
|
|
setGameplayMessage(MSG_STANDARD, app.strings[ST_PICKED_UP], i->name);
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
|
2018-02-25 18:29:44 +01:00
|
|
|
playSound(SND_ITEM, i->uniqueId % MAX_SND_CHANNELS);
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
static void enemyPickupItem(Unit *u)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Item *i;
|
|
|
|
|
|
|
|
i = (Item*)self;
|
|
|
|
|
|
|
|
if (u->canCarryItem && u->carriedItem == NULL && u->alive == ALIVE_ALIVE)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
u->carriedItem = i;
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
i->flags |= EF_GONE;
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
static void destructablePickupItem(Structure *s)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Item *i;
|
|
|
|
|
|
|
|
i = (Item*)self;
|
|
|
|
|
|
|
|
if (s->carriedItem == NULL && s->alive == ALIVE_ALIVE)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
s->carriedItem = i;
|
2018-01-26 20:14:18 +01:00
|
|
|
|
2018-01-28 09:30:53 +01:00
|
|
|
i->flags |= EF_GONE;
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void changeEnvironment(void)
|
|
|
|
{
|
2018-01-28 09:30:53 +01:00
|
|
|
Item *i;
|
|
|
|
|
|
|
|
i = (Item*)self;
|
|
|
|
|
|
|
|
if (i->environment == ENV_SLIME || i->environment == ENV_LAVA)
|
2018-01-26 20:14:18 +01:00
|
|
|
{
|
|
|
|
addTeleportStars(self);
|
2018-01-28 09:30:53 +01:00
|
|
|
i->x = i->startX;
|
|
|
|
i->y = i->startY;
|
2018-01-26 20:14:18 +01:00
|
|
|
addTeleportStars(self);
|
2018-02-26 19:56:13 +01:00
|
|
|
playBattleSound(SND_APPEAR, -1, i->x, i->y);
|
2018-01-26 20:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void die(void)
|
|
|
|
{
|
|
|
|
/* we will handle this ourselves! */
|
|
|
|
}
|
2018-01-30 23:47:40 +01:00
|
|
|
|
|
|
|
static void load(cJSON *root)
|
|
|
|
{
|
|
|
|
Item *i;
|
|
|
|
|
|
|
|
i = (Item*)self;
|
|
|
|
|
|
|
|
i->canBeCarried = cJSON_GetObjectItem(root, "canBeCarried")->valueint;
|
|
|
|
i->canBePickedUp = cJSON_GetObjectItem(root, "canBePickedUp")->valueint;
|
|
|
|
i->isMissionTarget = cJSON_GetObjectItem(root, "isMissionTarget")->valueint;
|
|
|
|
STRNCPY(i->spriteName, cJSON_GetObjectItem(root, "spriteName")->valuestring, MAX_NAME_LENGTH);
|
|
|
|
i->startX = cJSON_GetObjectItem(root, "startX")->valueint;
|
|
|
|
i->startY = cJSON_GetObjectItem(root, "startY")->valueint;
|
|
|
|
if (cJSON_GetObjectItem(root, "collected"))
|
|
|
|
{
|
|
|
|
i->collected = cJSON_GetObjectItem(root, "collected")->valueint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void save(cJSON *root)
|
|
|
|
{
|
|
|
|
Item *i;
|
|
|
|
|
|
|
|
i = (Item*)self;
|
|
|
|
|
2018-03-01 23:30:33 +01:00
|
|
|
switch (i->type)
|
2018-02-10 20:03:55 +01:00
|
|
|
{
|
2018-03-01 23:30:33 +01:00
|
|
|
case ET_KEY:
|
|
|
|
cJSON_AddStringToObject(root, "type", i->sprite[0]->name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_HEART:
|
|
|
|
cJSON_AddStringToObject(root, "type", "Heart");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_CELL:
|
|
|
|
cJSON_AddStringToObject(root, "type", "Cell");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
cJSON_AddStringToObject(root, "type", "Item");
|
|
|
|
break;
|
2018-02-10 20:03:55 +01:00
|
|
|
}
|
2018-03-01 23:30:33 +01:00
|
|
|
|
2018-01-30 23:47:40 +01:00
|
|
|
cJSON_AddNumberToObject(root, "canBeCarried", i->canBeCarried);
|
|
|
|
cJSON_AddNumberToObject(root, "canBePickedUp", i->canBePickedUp);
|
|
|
|
cJSON_AddNumberToObject(root, "isMissionTarget", i->isMissionTarget);
|
|
|
|
cJSON_AddStringToObject(root, "spriteName", i->spriteName);
|
|
|
|
cJSON_AddNumberToObject(root, "startX", i->startX);
|
|
|
|
cJSON_AddNumberToObject(root, "startY", i->startY);
|
|
|
|
cJSON_AddNumberToObject(root, "collected", i->collected);
|
|
|
|
}
|