forked from tas/major_tom
Raise hard map size cap from 4096 to 8192 via MAX_MAP_SIZE constant. Replace fixed 16 KB fgets buffer with dynamic line reader that handles arbitrarily long tile rows. Check calloc returns for tile layer allocation. Add entity distance culling: skip updates for entities beyond 2x screen width from camera, skip rendering beyond 64 px margin. Dead entities bypass culling so cleanup callbacks always run. Player, spacecraft, drone, and moving platforms set ENTITY_ALWAYS_UPDATE to opt out. Replace naive 4-neighbor flood fill with scanline algorithm (O(height) stack instead of O(area)) for safe use on large maps. Raise MAX_ENTITY_SPAWNS from 128 to 512 and MAX_EXIT_ZONES from 8 to 16 to support populated large levels.
81 lines
2.3 KiB
C
81 lines
2.3 KiB
C
#ifndef JNR_ENTITY_H
|
|
#define JNR_ENTITY_H
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include "config.h"
|
|
#include "engine/physics.h"
|
|
#include "engine/animation.h"
|
|
#include "util/vec2.h"
|
|
|
|
/* Forward declarations */
|
|
typedef struct Tilemap Tilemap;
|
|
typedef struct Camera Camera;
|
|
|
|
typedef enum EntityType {
|
|
ENT_NONE = 0,
|
|
ENT_PLAYER,
|
|
ENT_ENEMY_GRUNT,
|
|
ENT_ENEMY_FLYER,
|
|
ENT_PROJECTILE,
|
|
ENT_PICKUP,
|
|
ENT_PARTICLE,
|
|
ENT_TURRET,
|
|
ENT_MOVING_PLATFORM,
|
|
ENT_FLAME_VENT,
|
|
ENT_FORCE_FIELD,
|
|
ENT_POWERUP,
|
|
ENT_DRONE,
|
|
ENT_ASTEROID,
|
|
ENT_SPACECRAFT,
|
|
ENT_TYPE_COUNT
|
|
} EntityType;
|
|
|
|
/* Entity flags */
|
|
#define ENTITY_INVINCIBLE (1 << 0)
|
|
#define ENTITY_FACING_LEFT (1 << 1)
|
|
#define ENTITY_DEAD (1 << 2)
|
|
#define ENTITY_ALWAYS_UPDATE (1 << 3) /* never culled by distance */
|
|
|
|
typedef struct Entity {
|
|
EntityType type;
|
|
bool active;
|
|
Body body;
|
|
Animation anim;
|
|
int health;
|
|
int max_health;
|
|
int damage;
|
|
uint32_t flags;
|
|
float timer; /* general-purpose timer */
|
|
void *data; /* type-specific data pointer */
|
|
} Entity;
|
|
|
|
/* Function pointer types for entity behaviors */
|
|
typedef void (*EntityUpdateFn)(Entity *self, float dt, const Tilemap *map);
|
|
typedef void (*EntityRenderFn)(Entity *self, const Camera *cam);
|
|
typedef void (*EntityDestroyFn)(Entity *self);
|
|
|
|
typedef struct EntityManager {
|
|
Entity entities[MAX_ENTITIES];
|
|
int count;
|
|
/* dispatch tables */
|
|
EntityUpdateFn update_fn[ENT_TYPE_COUNT];
|
|
EntityRenderFn render_fn[ENT_TYPE_COUNT];
|
|
EntityDestroyFn destroy_fn[ENT_TYPE_COUNT];
|
|
} EntityManager;
|
|
|
|
void entity_manager_init(EntityManager *em);
|
|
Entity *entity_spawn(EntityManager *em, EntityType type, Vec2 pos);
|
|
void entity_destroy(EntityManager *em, Entity *e);
|
|
void entity_update_all(EntityManager *em, float dt, const Tilemap *map,
|
|
const Camera *cam);
|
|
void entity_render_all(EntityManager *em, const Camera *cam);
|
|
void entity_manager_clear(EntityManager *em);
|
|
|
|
/* Register behavior functions for an entity type */
|
|
void entity_register(EntityManager *em, EntityType type,
|
|
EntityUpdateFn update, EntityRenderFn render,
|
|
EntityDestroyFn destroy);
|
|
|
|
#endif /* JNR_ENTITY_H */
|