Animate entities.

This commit is contained in:
Steve 2018-01-31 22:54:14 +00:00
parent d11443158d
commit 3fa7c7a525
5 changed files with 44 additions and 7 deletions

View File

@ -21,6 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "bob.h"
static SDL_Rect *getCurrentSprite(void);
static void (*superAnimate)(void);
static void animate(void);
static void load(cJSON *root);
static void save(cJSON *root);
@ -36,7 +38,10 @@ void initBob(void)
u->sprite[FACING_RIGHT] = getSprite("BobRight");
u->sprite[FACING_DIE] = getSprite("BobSpin");
superAnimate = u->animate;
u->getCurrentSprite = getCurrentSprite;
u->animate = animate;
u->load = load;
u->save = save;
}
@ -61,6 +66,14 @@ static SDL_Rect *getCurrentSprite(void)
return &world.bob->sprite[FACING_DIE]->frames[0]->rect;
}
static void animate(void)
{
if (world.bob->dx != 0 || world.bob->stunTimer > 0 || world.bob->flags & EF_WEIGHTLESS || world.bob->health <= 0)
{
superAnimate();
}
}
static void load(cJSON *root)
{
world.bob->x = cJSON_GetObjectItem(root, "x")->valueint;

View File

@ -33,6 +33,7 @@ static void teleport(void);
static void attack(void);
static void die1(void);
static void die2(void);
static void (*superAnimate)(void);
static Sprite *aimedSprite;
@ -50,6 +51,8 @@ void initBlobBoss(Entity *e)
b->teleportTimer = FPS * rrnd(4, 6);
superAnimate = b->animate;
b->activate = activate;
b->walk = walk;
b->tick = tick;
@ -193,7 +196,7 @@ static void animate(void)
if (b->dx != 0 || b->health <= 0 || b->stunTimer > 0)
{
animateEntity(self);
superAnimate();
}
}

View File

@ -28,7 +28,6 @@ extern int isPlayingMusic(void);
extern float limit(float i, float a, float b);
extern double randF(void);
extern void playSound(int snd, int ch);
extern void animateEntity(Entity *e);
extern Bullet *createBaseBullet(Unit *owner);
extern Sprite *getSprite(char *name);
extern int getDistance(int x1, int y1, int x2, int y2);

View File

@ -20,12 +20,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "entity.h"
void animateEntity(void);
static void applyDamage(int damage);
static float bounce(float x);
static SDL_Rect *getBounds(void);
static void tick(void);
static void touch(Entity *other);
static void animate(void);
static void load(cJSON *root);
static void save(cJSON *root);
static SDL_Rect *getCurrentSprite(void);
@ -50,7 +50,7 @@ void initEntity(Entity *e)
e->thinkTime = 0;
e->animate = animateEntity;
e->animate = animate;
e->tick = tick;
e->touch = touch;
e->applyDamage = applyDamage;
@ -75,8 +75,30 @@ static SDL_Rect *getBounds(void)
return &self->bounds;
}
void animateEntity(void)
static void animate(void)
{
Sprite *s;
if (self->spriteTime != -1)
{
self->spriteTime--;
if (self->spriteTime <= 0)
{
s = self->sprite[self->facing];
self->spriteFrame++;
if (self->spriteFrame > s->numFrames)
{
self->spriteFrame = 0;
}
self->spriteTime = self->sprite[self->facing]->times[self->spriteFrame];
self->w = s->w;
self->h = s->h;
}
}
}
void setEntitySize(Entity *e)

View File

@ -123,6 +123,8 @@ struct Entity {
int thinkTime;
int facing;
Sprite *sprite[3];
int spriteTime;
int spriteFrame;
int plane;
int isSolid;
int isStatic;
@ -154,8 +156,6 @@ struct Entity {
struct EntityExt {
struct Entity;
char spriteName[MAX_NAME_LENGTH];
int spriteTime;
int spriteFrame;
Item *carriedItem;
};