Skip to content

gwen-kit-platformer — Usage Avancé (Niveau 3)

Le kit expose ses composants ECS et systèmes internes pour les cas où les factories ne suffisent pas.

Assembler sa propre scène

ts
import {
  PlatformerInputSystem,
  PlatformerMovementSystem,
  PlatformerController,
  PlatformerIntent,
  PLATFORMER_CONTROLLER_DEFAULTS,
} from '@djodjonx/gwen-kit-platformer';
import { defineScene } from '@djodjonx/gwen-engine-core';

export const CustomScene = defineScene({
  name: 'CustomGame',
  systems: [
    GroundDetectionSystem, // votre système de détection sol
    PlatformerInputSystem,
    PlatformerMovementSystem,
    EnemyAISystem,         // IA qui écrit dans PlatformerIntent
    AnimationSystem,
  ],
  onEnter(api) { /* ... */ },
  onExit(api)  { /* ... */ },
});

Brancher une IA sur PlatformerIntent

PlatformerMovementSystem lit PlatformerIntent — il ne sait pas si c'est un joueur ou une IA qui l'a écrit. Une IA écrit directement dans PlatformerIntent sans toucher au système de mouvement :

ts
import { defineSystem } from '@djodjonx/gwen-engine-core';
import { PlatformerController, PlatformerIntent } from '@djodjonx/gwen-kit-platformer';

export const EnemyAISystem = defineSystem('EnemyAISystem', () => ({
  onUpdate(api, dt) {
    for (const eid of api.query([PlatformerController, PlatformerIntent])) {
      // L'IA décide du mouvement
      const targetX = /* calcul pathfinding */ 0;
      api.addComponent(eid, PlatformerIntent, {
        moveX:           targetX,
        jumpJustPressed: shouldJump(eid),
        jumpPressed:     false,
      });
    }
  },
}));

Composant PlatformerController — référence

ChampTypeDéfautDescription
speedf32300Vitesse horizontale max (px/s)
jumpForcef32500Impulsion verticale de saut (px/s)
coyoteMsf32110Fenêtre coyote time (ms)
jumpBufferMsf32110Fenêtre jump buffer (ms)
maxFallSpeedf32600Plafond de vitesse de chute (px/s)

Composant PlatformerIntent — référence

ChampTypeDescription
moveXf32Direction horizontale : -1 (gauche), 0 (immobile), 1 (droite)
jumpJustPressedbooltrue sur la première frame d'appui saut
jumpPressedbooltrue tant que le bouton saut est maintenu

Contrat Physics2DAPI requis

PlatformerMovementSystem utilise ces méthodes de Physics2DAPI :

MéthodeSignatureDescription
getLinearVelocity(eid) => {x,y} | nullVélocité courante
setLinearVelocity(eid, vx, vy) => voidApplique une vélocité
isGrounded(eid) => booleanDétection sol (foot sensor)

Si isGrounded n'est pas disponible sur votre implémentation physics, créez un composant Grounded { value: bool } mis à jour par un système de collision dans systemsBefore.

Released under the MPL-2.0 License.