blobwarsAttrition/src/entities/entity.c

193 lines
3.9 KiB
C
Raw Normal View History

2018-01-25 08:41:10 +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.
*/
2018-01-31 22:50:49 +01:00
#include "entity.h"
2018-01-25 08:41:10 +01:00
2018-02-01 08:50:37 +01:00
static void init(void);
static void reset(void);
static void action(void);
static void applyDamage(int damage);
static float bounce(float x);
2018-01-30 23:47:40 +01:00
static void tick(void);
2018-02-04 12:00:32 +01:00
static void die(void);
2018-02-03 16:53:21 +01:00
static void activate(int active);
2018-01-30 23:47:40 +01:00
static void touch(Entity *other);
2018-01-31 23:54:14 +01:00
static void animate(void);
static void changeEnvironment(void);
2018-01-30 23:47:40 +01:00
static void load(cJSON *root);
static void save(cJSON *root);
static SDL_Rect *getCurrentSprite(void);
2018-01-29 23:12:18 +01:00
void initEntity(Entity *e)
2018-01-25 08:41:10 +01:00
{
2018-01-29 09:34:39 +01:00
e->uniqueId = world.entityCounter++;
2018-01-25 08:41:10 +01:00
e->environment = ENV_AIR;
e->alive = ALIVE_ALIVE;
e->isOnGround = 0;
e->isSolid = 0;
e->isStatic = 0;
e->isMissionTarget = 0;
e->health = 1;
2018-01-25 08:41:10 +01:00
e->facing = FACING_LEFT;
e->flags = 0;
e->thinkTime = 0;
2018-02-04 10:56:53 +01:00
e->spriteFrame = -1;
2018-02-02 09:00:27 +01:00
e->spriteTime = 0;
2018-02-01 08:50:37 +01:00
e->init = init;
e->reset = reset;
e->action = action;
2018-01-31 23:54:14 +01:00
e->animate = animate;
2018-01-30 23:47:40 +01:00
e->tick = tick;
e->touch = touch;
2018-02-03 16:53:21 +01:00
e->activate = activate;
e->applyDamage = applyDamage;
e->bounce = bounce;
e->getCurrentSprite = getCurrentSprite;
2018-02-04 12:00:32 +01:00
e->die = die;
e->changeEnvironment = changeEnvironment;
2018-01-29 23:12:18 +01:00
2018-01-30 23:47:40 +01:00
e->load = load;
e->save = save;
2018-01-29 23:12:18 +01:00
world.entityTail->next = e;
world.entityTail = e;
2018-01-25 08:41:10 +01:00
}
2018-02-01 08:50:37 +01:00
static void init(void)
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Cannot init() entity [name=%s, type=%d, x=%d, y=%d]", self->name, self->type, (int)self->x, (int)self->y);
}
static void reset(void)
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Cannot reset() entity [name=%s, type=%d, x=%d, y=%d]", self->name, self->type, (int)self->x, (int)self->y);
}
static void action(void)
{
}
2018-01-31 23:54:14 +01:00
static void animate(void)
2018-01-25 08:41:10 +01:00
{
2018-01-31 23:54:14 +01:00
Sprite *s;
2018-02-03 12:32:03 +01:00
s = self->sprite[self->facing];
2018-01-31 23:54:14 +01:00
if (self->spriteTime != -1)
{
2018-02-03 12:32:03 +01:00
if (--self->spriteTime <= 0)
2018-01-31 23:54:14 +01:00
{
2018-02-03 12:32:03 +01:00
if (++self->spriteFrame >= s->numFrames)
2018-01-31 23:54:14 +01:00
{
self->spriteFrame = 0;
}
self->spriteTime = self->sprite[self->facing]->times[self->spriteFrame];
}
}
2018-01-25 08:41:10 +01:00
}
static float bounce(float x)
2018-01-25 08:41:10 +01:00
{
if (!(self->flags & EF_BOUNCES))
2018-01-25 08:41:10 +01:00
{
return 0;
}
else if (self->flags & EF_FRICTIONLESS)
2018-01-25 08:41:10 +01:00
{
return -x;
}
x = -x * FRICTION;
if (x > -1 && x < 1)
{
self->flags &= ~EF_BOUNCES;
2018-01-25 08:41:10 +01:00
x = 0;
}
return x;
}
static void applyDamage(int damage)
2018-01-25 08:41:10 +01:00
{
}
2018-01-30 23:47:40 +01:00
static void tick(void)
{
}
static void touch(Entity *other)
{
}
2018-02-03 16:53:21 +01:00
static void activate(int active)
{
}
2018-02-04 12:00:32 +01:00
static void die(void)
{
2018-02-09 23:34:01 +01:00
self->spriteFrame = 0;
2018-02-04 12:00:32 +01:00
self->alive = ALIVE_DEAD;
}
static void changeEnvironment(void)
{
}
void entityIdle(void)
{
}
static SDL_Rect *getCurrentSprite(void)
{
Sprite *s;
s = self->sprite[self->facing];
if (self->spriteFrame >= s->numFrames)
{
2018-02-25 13:36:34 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "WARNING: %s (%d) bad sprite frames - %d > %d\n", self->name, self->type, self->spriteFrame, s->numFrames);
self->spriteFrame = 0;
}
return &s->frames[self->spriteFrame]->rect;
}
2018-01-30 23:47:40 +01:00
static void load(cJSON *root)
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Entity [name=%s, type=%d, x=%d, y=%d] cannot be loaded", self->name, self->type, (int)self->x, (int)self->y);
exit(1);
}
static void save(cJSON *root)
{
2018-02-01 08:50:37 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Entity [name=%s, type=%d, x=%d, y=%d] cannot be saved", self->name, self->type, (int)self->x, (int)self->y);
2018-01-30 23:47:40 +01:00
exit(1);
}