refactor: centralize system names, component types, entity types, and animation states into a new Constants module.

This commit is contained in:
Juan Sebastián Montoya 2026-01-06 18:58:12 -05:00
parent e9db84abd1
commit 3db2bb9160
20 changed files with 385 additions and 313 deletions

62
src/core/Constants.js Normal file
View file

@ -0,0 +1,62 @@
/**
* Centralized Constants and Enums
*/
export const GameState = {
START: 'start',
PLAYING: 'playing',
PAUSED: 'paused',
GAME_OVER: 'gameOver'
};
export const ComponentType = {
POSITION: 'Position',
VELOCITY: 'Velocity',
SPRITE: 'Sprite',
HEALTH: 'Health',
COMBAT: 'Combat',
AI: 'AI',
EVOLUTION: 'Evolution',
STATS: 'Stats',
SKILLS: 'Skills',
SKILL_PROGRESS: 'SkillProgress',
ABSORBABLE: 'Absorbable',
STEALTH: 'Stealth'
};
export const EntityType = {
SLIME: 'slime',
HUMANOID: 'humanoid',
BEAST: 'beast',
ELEMENTAL: 'elemental',
PROJECTILE: 'projectile'
};
export const AnimationState = {
IDLE: 'idle',
WALK: 'walk'
};
export const VFXType = {
IMPACT: 'impact',
ABSORPTION: 'absorption'
};
export const SystemName = {
MENU: 'MenuSystem',
UI: 'UISystem',
PLAYER_CONTROLLER: 'PlayerControllerSystem',
ABSORPTION: 'AbsorptionSystem',
COMBAT: 'CombatSystem',
PROJECTILE: 'ProjectileSystem',
VFX: 'VFXSystem',
MOVEMENT: 'MovementSystem',
AI: 'AISystem',
DEATH: 'DeathSystem',
RENDER: 'RenderSystem',
INPUT: 'InputSystem',
SKILL_EFFECT: 'SkillEffectSystem',
SKILL: 'SkillSystem',
STEALTH: 'StealthSystem',
HEALTH_REGEN: 'HealthRegenerationSystem'
};

View file

@ -2,6 +2,7 @@ import { System } from './System.js';
import { Entity } from './Entity.js';
import { EventBus } from './EventBus.js';
import { LevelLoader } from './LevelLoader.js';
import { GameState, SystemName } from './Constants.js';
/**
* Main game engine - manages ECS, game loop, and systems
@ -48,8 +49,8 @@ export class Engine {
}
/**
* Emit an event locally
*/
* Emit an event locally
*/
emit(event, data) {
this.events.emit(event, data);
}
@ -118,20 +119,21 @@ export class Engine {
this.deltaTime = Math.min(this.deltaTime, 0.1);
// Update all systems
const menuSystem = this.systems.find(s => s.name === 'MenuSystem');
const gameState = menuSystem ? menuSystem.getGameState() : 'playing';
const isPaused = gameState === 'paused' || gameState === 'start' || gameState === 'gameOver';
const menuSystem = this.systems.find(s => s.name === SystemName.MENU);
const gameState = menuSystem ? menuSystem.getGameState() : GameState.PLAYING;
const isPaused = [GameState.PAUSED, GameState.START, GameState.GAME_OVER].includes(gameState);
const unskippedSystems = [SystemName.MENU, SystemName.UI, SystemName.RENDER];
this.systems.forEach(system => {
// Skip game systems if paused/start menu (but allow MenuSystem, UISystem, and RenderSystem)
if (isPaused && system.name !== 'MenuSystem' && system.name !== 'UISystem' && system.name !== 'RenderSystem') {
if (isPaused && !unskippedSystems.includes(system.name)) {
return;
}
system.update(this.deltaTime, this.entities);
});
// Update input system's previous states at end of frame
const inputSystem = this.systems.find(s => s.name === 'InputSystem');
const inputSystem = this.systems.find(s => s.name === SystemName.INPUT);
if (inputSystem && inputSystem.updatePreviousStates) {
inputSystem.updatePreviousStates();
}

View file

@ -1,3 +1,5 @@
import { EntityType, AnimationState } from './Constants.js';
/**
* Sprite Library defining pixel art grids as 2D arrays.
* 0: Transparent
@ -7,8 +9,8 @@
*/
export const SpriteLibrary = {
// 8x8 Slime - Bottom-heavy blob
slime: {
idle: [
[EntityType.SLIME]: {
[AnimationState.IDLE]: [
[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 0, 0], // Top
@ -30,7 +32,7 @@ export const SpriteLibrary = {
[1, 1, 1, 1, 1, 1, 1, 1] // Squashed base
]
],
walk: [
[AnimationState.WALK]: [
[
[0, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 0],
@ -55,8 +57,8 @@ export const SpriteLibrary = {
},
// 8x8 Humanoid - Simple Walk Cycle
humanoid: {
idle: [
[EntityType.HUMANOID]: {
[AnimationState.IDLE]: [
[
[0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 2, 1, 1, 2, 0, 0],
@ -68,7 +70,7 @@ export const SpriteLibrary = {
[0, 0, 1, 0, 0, 1, 0, 0]
]
],
walk: [
[AnimationState.WALK]: [
[
[0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 2, 1, 1, 2, 0, 0],
@ -93,8 +95,8 @@ export const SpriteLibrary = {
},
// 8x8 Beast - Bounding Cycle
beast: {
idle: [
[EntityType.BEAST]: {
[AnimationState.IDLE]: [
[
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 1],
@ -106,7 +108,7 @@ export const SpriteLibrary = {
[0, 1, 0, 0, 0, 0, 1, 0]
]
],
walk: [
[AnimationState.WALK]: [
[
[1, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 1, 1, 1, 1, 0],
@ -131,8 +133,8 @@ export const SpriteLibrary = {
},
// 8x8 Elemental - Floating Pulse
elemental: {
idle: [
[EntityType.ELEMENTAL]: {
[AnimationState.IDLE]: [
[
[0, 0, 2, 1, 1, 2, 0, 0],
[0, 1, 1, 2, 2, 1, 1, 0],
@ -156,8 +158,8 @@ export const SpriteLibrary = {
]
},
projectile: {
idle: [
[EntityType.PROJECTILE]: {
[AnimationState.IDLE]: [
[
[1, 1],
[1, 1]