Factory Actors
Source files: FactoryActors.hpp, FactoryActors.cpp, FactoryActorsInit.cpp, CreateMermaidActor.cpp, CreatePlayerActor.cpp
Purpose: Centralized builder that instantiates actors (player and enemies) from JSON-driven definitions. It attaches the shared ECS components (transform, health, visuals, collision), then layers specialized logic per actor type (animations, AI patterns, shooting, VFX).
Data flow
- InitializeEnemyInfoMap(json_folder): scans a folder of JSON files and fills the
enemy_info_map_cache withEnnemyInfodescriptors. Each file is named after the enemy tag (stem of filename). - CreateActor(entity, reg, tag): looks up the
EnnemyInfobytag, applies shared components viaCreateBasicActor, then dispatches toCreatePlayerActorfortag == "player"or toCreateBasicEnnemyplus any specialized constructors (e.g., mermaid). - Runtime spawning: systems or scene scripts spawn an entity, then call
CreateActorwith the desired tag; all components are attached in one place.
EnnemyInfo schema (JSON)
Keys read from each JSON file in the provided folder (defaults are applied if missing):
| Field | Type | Purpose |
|---|---|---|
tag | string | Logical identifier ("player", "mermaid", ...). |
health | int | Starting HP. |
speed | float | Baseline speed used by movement/shoot systems. |
spritePath | string | Sprite sheet used by the base Drawable. |
hitbox | object {x, y} | Hitbox dimensions. |
offset_healthbar | object {x, y} | Offset for positioning the health bar. |
transform.scale | object {x, y} | Scale applied to the base Transform. |
Shared setup (CreateBasicActor)
Attaches common components for any actor:
Transform: positioned at origin with provided scale, origin set toCENTER.HealthandHealthBar: HP pool and on-screen bar offset.HitBox: AABB sized from config.Velocity: zeroed, ready for movement systems.Drawable: sprite path + z-layer (actors). Note: currently added twice; if unintended, consider removing the duplicate.
Player path (CreatePlayerActor)
Adds player-specific components:
AnimatedSprite: idle sheet plus dedicatedHitandDeathanimations (custom sizes/durations).Inputs: mutable input state populated byInputSystem.PlayerTag: speed, shoot cooldown, charge time, and charge state from config constants.AnimationEnterPlayer: flag enabling the entry animation effect.ParticleEmitter: rear thruster VFX (yellow-to-red gradient, continuous emission, actor-layered).
Enemy path (CreateBasicEnnemy)
Adds enemy-specific components:
EnemyTag: speed baseline for AI systems.AnimatedSprite: default sheet withHit,Death, andAttackanimations; defaults tocurrentAnimation = "Default".
Mermaid specialization (CreateMermaidActor)
PatternMovement: sine horizontal pattern with configured offsets andBASIC_SPEED.EnemyShootTag: projectile speed/damage and spawn offset.FrameEvents: triggers on frame 5 of theAttackanimation to spawn a projectile viaCreateEnemyProjectile(safe-guarded bytry/catch).TimedEvents: periodic callback (everyBASIC_SHOOT_COOLDOWN) that switches the animation toAttackif the enemy is still alive.- Adds
EnemyShootTagto the entity after configuring callbacks.
Projectile creation helper (CreateEnemyProjectile)
Builds enemy projectiles with consistent components:
Transform: positioned using the shoot offset scaled by the shooter’s transform; centered origin.Drawable: projectile sprite on projectile layer.Projectile: damage, direction, speed, owner ID, and an enemy flag.HitBox: small 8x8 AABB.Velocity: initialized from direction.ParticleEmitter: short red trail for visual feedback.
JSON loading behavior
Helper getters enforce types and apply defaults. Missing or malformed keys fall back to sane defaults; transform.scale defaults to (1,1) when absent. Files that cannot be opened are logged to std::cerr and skipped.
Usage notes
- Call
InitializeEnemyInfoMaponce at startup with the folder containing enemy JSON definitions, then useCreateActorwhenever spawning an actor. - Extend the factory by adding new specialized creators and branching on
info.taginsideCreateActor. - Keep
EnnemyInfovalues in sync with art assets (sprite sizes, hitboxes) to avoid visual/collision mismatch.