blobwarsAttrition/src/world/entities/items/item.c

211 lines
3.8 KiB
C
Raw Normal View History

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"
static void reset(void);
static void tick(void);
static void touch(Entity *other);
static void changeEnvironment(void);
static void die(void);
static void destructablePickupItem(Structure *s);
static void enemyPickupItem(Unit *u);
2018-01-26 20:14:18 +01:00
static void bobPickupItem(void);
void initItem(Entity *e)
{
Item *i;
2018-01-26 20:14:18 +01:00
initEntity(e);
i = (Item*)e;
2018-01-26 20:14:18 +01:00
STRNCPY(i->spriteName, "Weapon", MAX_NAME_LENGTH);
2018-01-26 20:14:18 +01:00
i->flags |= EF_IGNORE_BULLETS;
2018-01-26 20:14:18 +01:00
i->isMissionTarget = 1;
i->canBePickedUp = 1;
i->canBeCarried = 0;
i->collected = 0;
2018-01-26 20:14:18 +01:00
i->sprite[FACING_LEFT] = i->sprite[FACING_RIGHT] = i->sprite[FACING_DIE] = getSpriteIndex(i->spriteName);
2018-01-26 20:14:18 +01:00
i->tick = tick;
i->touch = touch;
i->changeEnvironment = changeEnvironment;
i->reset = reset;
i->die = die;
2018-01-26 20:14:18 +01:00
}
static void reset(void)
{
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)
{
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)
{
enemyPickupItem((Unit*)other);
2018-01-26 20:14:18 +01:00
}
else if (other->type == ET_DESTRUCTABLE)
{
destructablePickupItem((Structure*)other);
2018-01-26 20:14:18 +01:00
}
}
}
static void bobPickupItem(void)
{
Item *i;
i = (Item*)self;
if (!i->isMissionTarget)
2018-01-26 20:14:18 +01:00
{
if (i->thinkTime == 0)
2018-01-26 20:14:18 +01:00
{
i->alive = ALIVE_DEAD;
addKey(i->name);
2018-01-26 20:14:18 +01:00
game.keysFound++;
updateObjective("KEY");
setGameplayMessage(MSG_STANDARD, "Picked up a %s", i->name);
2018-01-26 20:14:18 +01:00
playSound(SND_KEY, CH_ITEM);
}
else
{
setGameplayMessage(MSG_GAMEPLAY, "Can't carry any more keys");
}
}
else if (i->canBeCarried)
2018-01-26 20:14:18 +01:00
{
if (numCarriedItems() < MAX_ITEMS)
{
i->flags |= EF_GONE;
2018-01-26 20:14:18 +01:00
if (!i->collected)
2018-01-26 20:14:18 +01:00
{
updateObjective(i->name);
i->collected = 1;
2018-01-26 20:14:18 +01:00
}
addBobItem(i);
2018-01-26 20:14:18 +01:00
setGameplayMessage(MSG_STANDARD, "Picked up a %s", i->name);
2018-01-26 20:14:18 +01:00
playSound(SND_ITEM, CH_ITEM);
}
else
{
setGameplayMessage(MSG_GAMEPLAY, "Can't carry any more items");
}
}
else
{
i->alive = ALIVE_DEAD;
updateObjective(i->name);
2018-01-26 20:14:18 +01:00
if (strcmp(world.id, "teeka") != 0)
{
setGameplayMessage(MSG_STANDARD, "Picked up a %s", i->name);
2018-01-26 20:14:18 +01:00
}
playSound(SND_ITEM, CH_ITEM);
}
}
static void enemyPickupItem(Unit *u)
2018-01-26 20:14:18 +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
{
u->carriedItem = i;
2018-01-26 20:14:18 +01:00
i->flags |= EF_GONE;
2018-01-26 20:14:18 +01:00
}
}
static void destructablePickupItem(Structure *s)
2018-01-26 20:14:18 +01:00
{
Item *i;
i = (Item*)self;
if (s->carriedItem == NULL && s->alive == ALIVE_ALIVE)
2018-01-26 20:14:18 +01:00
{
s->carriedItem = i;
2018-01-26 20:14:18 +01:00
i->flags |= EF_GONE;
2018-01-26 20:14:18 +01:00
}
}
static void changeEnvironment(void)
{
Item *i;
i = (Item*)self;
if (i->environment == ENV_SLIME || i->environment == ENV_LAVA)
2018-01-26 20:14:18 +01:00
{
addTeleportStars(self);
i->x = i->startX;
i->y = i->startY;
2018-01-26 20:14:18 +01:00
addTeleportStars(self);
playSound(SND_APPEAR, CH_ANY);
}
}
static void die(void)
{
/* we will handle this ourselves! */
}