Skip to content

044 | State - Part 2: Create State System#44

Open
Pedrohamoura-Git wants to merge 5 commits intomainfrom
feat/066
Open

044 | State - Part 2: Create State System#44
Pedrohamoura-Git wants to merge 5 commits intomainfrom
feat/066

Conversation

@Pedrohamoura-Git
Copy link
Copy Markdown
Contributor

@Pedrohamoura-Git Pedrohamoura-Git commented Mar 9, 2026

Information

Describe What Was Done

  • Create State System responsible for processing and defining the character's state every frame.
  • Create a Dead Handler responsible for defining the correct dead state.

Detailed Summary (AI Generated)

🤖| Summary

This PR introduces a new StateSystem in the game's ECS architecture to manage player state transitions and integrates it into the MatchScene. It also includes supporting handlers and types for managing state logic.

New System: StateSystem

  • Implemented a new StateSystem in the file StateSystem.ts. Key functionalities include:
    • Validating and processing entity states (e.g., handling ALIVE_STATE).
    • Delegating dead-state handling to a dedicated DeadHandler.
    • Supporting checks for whether a player entity is alive via the isPlayerAlive method.

New Dead Handler

  • Added a DeadHandler in handlers/dead/DeadHandler.ts to manage actions when the player's state is dead:
    • Resolves player state based on ALIVE_STATE values (e.g., DEAD, DEAD_NO_HEAD).
    • Updates player state attributes like inputAction and characterState.

Integration with MatchScene

  • Updated MatchScene.ts to integrate the new StateSystem:
    • Instantiated StateSystem in initializeSystems.
    • Added stateSystem.update to the update lifecycle, enabling player state updates.

New Supporting Files

  • Created new helper modules and type definitions:
    • state/types.p.ts: Defines types for StateSystem and its properties.
    • handlers/dead/types.p.ts: Defines type for DeadHandler properties.
    • handlers/dead/index.ts: Exports DeadHandler.
    • state/index.ts: Exports StateSystem.

Index Export Updates

  • Updated index.ts within the ecs/systems directory to export the new StateSystem.

Evidence

⏪️ | Before - No death animation to show
⏩ | After
Gravacao.de.Tela.2026-03-10.212655.mp4

Rollback Plan

Revert from the branch.


↗️ | Click to See The Preview

Copilot AI review requested due to automatic review settings March 9, 2026 09:14
@Pedrohamoura-Git Pedrohamoura-Git added feature Create new feature client Changes in the client package labels Mar 9, 2026
@Pedrohamoura-Git Pedrohamoura-Git changed the base branch from main to feat/037 March 9, 2026 09:15

This comment was marked as outdated.

@Pedrohamoura-Git Pedrohamoura-Git marked this pull request as ready for review March 28, 2026 12:14

constructor({ scene }: StateSystemProp) {
if (!scene) throw new Error('scene parameter is missing or invalid');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
constructor() {

Since the scene is not used within this System, I believe it's best to remove it from both the parameters and the validation. This keeps the code clean and avoids unnecessary dependencies.

Comment on lines +15 to +18
if (!(entities instanceof Map)) {
throw new Error(`Entities is not a Map: ${typeof entities}`);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!(entities instanceof Map)) {
throw new Error(`Entities is not a Map: ${typeof entities}`);
}

I don't believe adding this validation is necessary, as the system's typing ensure the entities is always valid

}

entities.forEach(({ state, input, animation, sprite }) => {
if (!state || !input || !animation || !sprite?.body) return;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!state || !input || !animation || !sprite?.body) return;
if (!state) return;

As Copilot pointed out, the system only depends on the state. Requiring additional fields would unnecessarily exclude simpler entities that might not need those extra components.

this.keymapSystem = new KeymapSystem({ scene: this });
this.inputSystem = new InputSystem();
this.stateSystem = new StateSystem({ scene: this });
this.movementSystem = new MovementSystem();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.movementSystem = new MovementSystem();
this.stateSystem = new StateSystem({ scene: this });


update() {
this.inputSystem.update({ entities: this.entities });
this.stateSystem.update({ entities: this.entities });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to call the StateSystem after applying velocity? This ensures the entity's state (like “walking”) is immediately consistent with its movement intent. Running it in this order avoids a one-frame lag between the actual movement and the visual state update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

client Changes in the client package feature Create new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants