Render entities.

This commit is contained in:
Steve 2018-01-31 21:50:49 +00:00
parent 59430875f0
commit cab6d3b205
21 changed files with 150 additions and 45 deletions

View File

@ -33,7 +33,7 @@ OBJS += atlas.o atlasTest.o aquaBlob.o
OBJS += battery.o blaze.o bob.o boss.o blobBoss.o
OBJS += camera.o cannon.o cardReader.o cell.o cherry.o combat.o consumable.o
OBJS += debris.o destructable.o door.o draw.o
OBJS += effects.o entities.o entityFactory.o exit.o explosions.o eyeDroid.o eyeDroidCommander.o evilBlob.o
OBJS += effects.o entities.o entity.o entityFactory.o exit.o explosions.o eyeDroid.o eyeDroidCommander.o evilBlob.o
OBJS += fleshChunk.o frost.o
OBJS += game.o grenade.o
OBJS += heart.o horizontalDoor.o horizontalLaserTrap.o hub.o hud.o

View File

@ -20,6 +20,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "bob.h"
static void load(cJSON *root);
static void save(cJSON *root);
void initBob(void)
{
Unit *u;
@ -27,6 +30,13 @@ void initBob(void)
u = createUnit();
u->type = ET_BOB;
u->sprite[FACING_LEFT] = getSprite("BobLeft");
u->sprite[FACING_RIGHT] = getSprite("BobRight");
u->sprite[FACING_DIE] = getSprite("BobSpin");
u->load = load;
u->save = save;
}
void addBobItem(Item *i)
@ -38,3 +48,18 @@ int numCarriedItems(void)
{
return 0;
}
static void load(cJSON *root)
{
world.bob->x = cJSON_GetObjectItem(root, "x")->valueint;
world.bob->y = cJSON_GetObjectItem(root, "y")->valueint;
world.bob->facing = lookup(cJSON_GetObjectItem(root, "facing")->valuestring);
}
static void save(cJSON *root)
{
cJSON_AddStringToObject(root, "type", "Bob");
cJSON_AddNumberToObject(root, "x", world.bob->x);
cJSON_AddNumberToObject(root, "y", world.bob->y);
cJSON_AddStringToObject(root, "facing", getLookupName("FACING_", world.bob->facing));
}

View File

@ -19,5 +19,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern Unit *createUnit(void);
extern Sprite *getSprite(char *name);
extern char *getLookupName(const char *prefix, long num);
extern long lookup(const char *name);
extern World world;

View File

@ -18,7 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "entities.h"
#include "entity.h"
void animateEntity(void);
static void applyDamage(int damage);

View File

@ -37,8 +37,6 @@ Entity *createItem(void)
i = malloc(sizeof(Item));
memset(i, 0, sizeof(Item));
world.entityTail->next = (Entity*)i;
world.entityTail = (Entity*)i;
initEntity((Entity*)i);

View File

@ -36,6 +36,8 @@ Entity *initPushBlock(void)
s->startX = s->startY = -1;
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("Crate1");
s->flags |= EF_EXPLODES | EF_ALWAYS_PROCESS;
s->activate = activate;

View File

@ -24,5 +24,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern void playSound(int snd, int ch);
extern void addTeleportStars(Entity *e);
extern Structure *createStructure(void);
extern Sprite *getSprite(char *name);
extern Entity *self;

View File

@ -26,8 +26,6 @@ Structure *createStructure(void)
s = malloc(sizeof(Structure));
memset(s, 0, sizeof(Structure));
world.entityTail->next = (Entity*)s;
world.entityTail = (Entity*)s;
initEntity((Entity*)s);

View File

@ -34,6 +34,8 @@ Entity *initTeleporter(void)
s->type = ET_TELEPORTER;
s->sprite[0] = s->sprite[1] = s->sprite[2] = getSprite("TeleporterActive");
s->flags |= EF_WEIGHTLESS | EF_NO_CLIP | EF_IGNORE_BULLETS | EF_NO_TELEPORT;
s->plane = PLANE_FOREGROUND;

View File

@ -28,5 +28,6 @@ extern void observeActivation(Entity *e);
extern int isOnScreen(Entity *e);
extern void setGameplayMessage(int type, char *format, ...);
extern Structure *createStructure(void);
extern Sprite *getSprite(char *name);
extern Entity *self;

View File

@ -33,11 +33,11 @@ Unit *createUnit(void)
u = malloc(sizeof(Unit));
memset(u, 0, sizeof(Unit));
world.entityTail->next = (Entity*)u;
world.entityTail = (Entity*)u;
initEntity((Entity*)u);
u->type = ET_ENEMY;
u->oxygen = MAX_OXYGEN;
u->canCarryItem = rand() % 100 < 85;

View File

@ -382,7 +382,7 @@ struct Sprite {
char name[MAX_NAME_LENGTH];
int *times;
char **filenames;
SDL_Rect *frames;
Atlas **frames;
int currentFrame;
float currentTime;
int w;

View File

@ -80,7 +80,7 @@ static void animateSprite(Sprite *s)
SDL_Rect getCurrentFrame(Sprite *s)
{
return s->frames[s->currentFrame];
return s->frames[s->currentFrame]->rect;
}
static void loadGameSprites(void)
@ -144,6 +144,7 @@ void loadSprite(cJSON *root)
s->times = malloc(sizeof(int) * s->numFrames);
s->filenames = malloc(sizeof(char*) * s->numFrames);
s->frames = malloc(sizeof(Atlas*) * s->numFrames);
i = 0;
@ -156,6 +157,8 @@ void loadSprite(cJSON *root)
filename = cJSON_GetObjectItem(frame, "content")->valuestring;
s->filenames[i] = malloc(strlen(filename) + 1);
STRNCPY(s->filenames[i], filename, strlen(filename));
s->frames[i] = getImageFromAtlas(filename);
}
i++;

View File

@ -24,3 +24,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern float wrap(float value, float low, float high);
extern char *readFile(const char *filename);
char **getFileList(const char *dir, int *count);
extern Atlas *getImageFromAtlas(char *filename);

View File

@ -37,6 +37,8 @@ void initAtlasTest(void)
initMap();
initEntities();
timeout = FPS * 2;
cameraTrack((Entity*)world.bob);
@ -53,6 +55,8 @@ static void logic(void)
static void draw(void)
{
drawMap();
drawEntities();
}
static void trackRandomEntity(void)

View File

@ -27,8 +27,10 @@ extern void loadMapData(char *filename);
extern void initMap(void);
extern void initHub(void);
extern void initGame(void);
extern void initEntities(void);
extern void loadWorld(char *filename);
extern void drawMap(void);
extern void drawEntities(void);
extern void cameraTrack(Entity *e);
extern App app;

42
src/world/entities.c Normal file
View File

@ -0,0 +1,42 @@
/*
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 "entities.h"
static Texture *atlasTexture;
void initEntities(void)
{
atlasTexture = getTexture("gfx/atlas/atlas.png");
}
void drawEntities(void)
{
Entity *e;
int x, y;
for (e = world.entityHead.next ; e != NULL ; e = e->next)
{
x = (-camera.x + e->x);
y = (-camera.y + e->y);
blitRect(atlasTexture->texture, x, y, &e->sprite[0]->frames[0]->rect, 0);
}
}

27
src/world/entities.h Normal file
View File

@ -0,0 +1,27 @@
/*
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 "../common.h"
extern Texture *getTexture(const char *filename);
extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center);
extern Camera camera;
extern World world;

View File

@ -126,16 +126,9 @@ static void loadSprites(cJSON *root)
static void loadBob(cJSON *root)
{
Bob *b;
world.bob = (Bob*)createEntity("Bob");
b = (Bob*)createEntity("Bob");
b->x = cJSON_GetObjectItem(root, "x")->valueint;
b->y = cJSON_GetObjectItem(root, "y")->valueint;
b->facing = lookup(cJSON_GetObjectItem(root, "facing")->valuestring);
initBob(b);
world.bob = b;
world.bob->load(root);
}
static void loadEntities(cJSON *root)