Skip to content

Commit fac9ab6

Browse files
committed
update some ai creations
1 parent 599d546 commit fac9ab6

File tree

9 files changed

+933
-64
lines changed

9 files changed

+933
-64
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/ai/mario-sokoban/mario.jpg

9.92 KB
Loading
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import { dnaManager } from '@lib/dna/dna_manager';
2+
import { gfx3DebugRenderer } from '@lib/gfx3/gfx3_debug_renderer';
3+
import { gfx3Manager } from '@lib/gfx3/gfx3_manager';
4+
import { Screen } from '@lib/screen/screen';
5+
import { Gfx3CameraOrbit } from '@lib/gfx3_camera/gfx3_camera_orbit';
6+
import { Gfx3Jolt } from '@lib/gfx3_physics/gfx3_physics_jolt';
7+
// ---------------------------------------------------------------------------------------
8+
import { PhysicsSystem, CanComponent, PlayerComponent, LAYER_MOVING, LAYER_NON_MOVING } from './physics';
9+
import { GraphicsSystem } from './graphics';
10+
import { EntityComponent } from './entity';
11+
import { InputSystem, InputComponent } from './input';
12+
// ---------------------------------------------------------------------------------------
13+
14+
class CanChaseScreen extends Screen {
15+
camera: Gfx3CameraOrbit;
16+
playerEid: number;
17+
canEid: number;
18+
gameOver: boolean;
19+
inputSystem: InputSystem;
20+
physicsSystem: PhysicsSystem;
21+
graphicsSystem: GraphicsSystem;
22+
23+
constructor() {
24+
super();
25+
this.camera = new Gfx3CameraOrbit(0);
26+
this.playerEid = 0;
27+
this.canEid = 0;
28+
this.gameOver = false;
29+
this.inputSystem = new InputSystem(this.camera);
30+
this.physicsSystem = new PhysicsSystem();
31+
this.graphicsSystem = new GraphicsSystem();
32+
}
33+
34+
async onEnter() {
35+
dnaManager.setup([this.inputSystem, this.physicsSystem, this.graphicsSystem]);
36+
37+
// Créer la pente
38+
this.#createSlope();
39+
40+
// Créer les murs latéraux
41+
this.#createWalls();
42+
43+
// Créer le joueur
44+
this.playerEid = await this.#createPlayer();
45+
46+
// Créer la canette
47+
this.canEid = await this.#createCan();
48+
49+
// Positionner la caméra pour voir la scène
50+
this.camera.setTarget([0, 15, -25]); // Centrer sur la zone de jeu
51+
this.camera.setDistance(25);
52+
this.camera.phi = Math.PI * 0.25; // Angle d'élévation plus modéré
53+
this.camera.theta = 0; // Angle horizontal
54+
}
55+
56+
update(ts: number) {
57+
if (this.gameOver) return;
58+
59+
this.camera.update(ts);
60+
dnaManager.update(ts);
61+
62+
const playerEntity = dnaManager.getComponent(this.playerEid, EntityComponent);
63+
const canEntity = dnaManager.getComponent(this.canEid, EntityComponent);
64+
65+
// Suivre le joueur avec la caméra (avec un décalage pour voir la scène)
66+
this.camera.setTarget([playerEntity.x, playerEntity.y + 3, playerEntity.z]);
67+
68+
// Vérifier collision joueur/canette
69+
const distance = Math.sqrt(
70+
Math.pow(playerEntity.x - canEntity.x, 2) +
71+
Math.pow(playerEntity.z - canEntity.z, 2)
72+
);
73+
74+
if (distance < 2) {
75+
this.gameOver = true;
76+
console.log("Game Over! La canette vous a rattrapé!");
77+
}
78+
}
79+
80+
draw() {
81+
gfx3Manager.beginDrawing();
82+
dnaManager.draw();
83+
gfx3Manager.endDrawing();
84+
}
85+
86+
render() {
87+
gfx3Manager.beginRender();
88+
gfx3Manager.beginPassRender(0);
89+
gfx3DebugRenderer.render();
90+
gfx3Manager.endPassRender();
91+
gfx3Manager.endRender();
92+
}
93+
94+
#createSlope() {
95+
// Pente principale (inclinée)
96+
const slopeLength = 100;
97+
const slopeWidth = 20;
98+
const slopeHeight = 20;
99+
const angle = Math.atan(slopeHeight / slopeLength);
100+
101+
// Position au centre de la pente
102+
const centerY = slopeHeight / 2;
103+
const centerZ = 0;
104+
105+
// Rotation autour de l'axe X pour incliner la pente
106+
const rotationX = angle;
107+
108+
this.physicsSystem.createBox(
109+
[0, centerY, centerZ],
110+
[Math.sin(rotationX/2), 0, 0, Math.cos(rotationX/2)],
111+
[slopeWidth/2, 1, slopeLength/2],
112+
Gfx3Jolt.EMotionType_Static,
113+
LAYER_NON_MOVING
114+
);
115+
}
116+
117+
#createWalls() {
118+
// Murs latéraux pour empêcher de sortir de la pente
119+
const wallHeight = 15;
120+
const wallLength = 60;
121+
122+
// Mur gauche
123+
this.physicsSystem.createBox(
124+
[-12, wallHeight/2, 0],
125+
[0, 0, 0, 1],
126+
[1, wallHeight/2, wallLength/2],
127+
Gfx3Jolt.EMotionType_Static,
128+
LAYER_NON_MOVING
129+
);
130+
131+
// Mur droit
132+
this.physicsSystem.createBox(
133+
[12, wallHeight/2, 0],
134+
[0, 0, 0, 1],
135+
[1, wallHeight/2, wallLength/2],
136+
Gfx3Jolt.EMotionType_Static,
137+
LAYER_NON_MOVING
138+
);
139+
}
140+
141+
async #createPlayer(): Promise<number> {
142+
const eid = dnaManager.createEntity();
143+
144+
const entity = new EntityComponent();
145+
entity.x = 0;
146+
entity.y = 22;
147+
entity.z = -20; // Positionner juste après la canette sur la pente
148+
dnaManager.addComponent(eid, entity);
149+
150+
const player = new PlayerComponent();
151+
dnaManager.addComponent(eid, player);
152+
153+
const input = new InputComponent();
154+
dnaManager.addComponent(eid, input);
155+
156+
// Créer le corps physique directement
157+
this.physicsSystem.createPlayerBody(eid, entity);
158+
159+
return eid;
160+
}
161+
162+
async #createCan(): Promise<number> {
163+
const eid = dnaManager.createEntity();
164+
console.log(`Creating can entity with ID: ${eid}`);
165+
166+
const entity = new EntityComponent();
167+
entity.x = 0;
168+
entity.y = 24; // Un peu plus haut que la pente
169+
entity.z = -35; // En haut de la pente, devant le joueur
170+
dnaManager.addComponent(eid, entity);
171+
console.log(`Added EntityComponent to can at: ${entity.x}, ${entity.y}, ${entity.z}`);
172+
173+
const can = new CanComponent();
174+
dnaManager.addComponent(eid, can);
175+
console.log(`Added CanComponent to entity ${eid}`);
176+
177+
// Créer le corps physique directement
178+
this.physicsSystem.createCanBody(eid, entity);
179+
console.log(`Created physics body for can`);
180+
181+
return eid;
182+
}
183+
}
184+
185+
export { CanChaseScreen };
186+
187+
188+
189+

src/ai/can-chase/entity.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { DNAComponent } from '@lib/dna/dna_component';
2+
// ---------------------------------------------------------------------------------------
3+
4+
export class EntityComponent extends DNAComponent {
5+
x: number;
6+
y: number;
7+
z: number;
8+
9+
constructor() {
10+
super('Entity');
11+
this.x = 0;
12+
this.y = 0;
13+
this.z = 0;
14+
}
15+
}

src/ai/can-chase/graphics.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { dnaManager } from '@lib/dna/dna_manager';
2+
import { DNASystem } from '@lib/dna/dna_system';
3+
// ---------------------------------------------------------------------------------------
4+
5+
class GraphicsSystem extends DNASystem {
6+
constructor() {
7+
super();
8+
super.addRequiredComponentTypename('Entity');
9+
}
10+
11+
onEntityUpdate(ts: number, eid: number): void {
12+
// Pour l'instant, on utilise juste le debug renderer
13+
// On pourrait ajouter des modèles 3D plus tard
14+
}
15+
16+
onEntityDraw(eid: number): void {
17+
// Le rendu est géré par le debug renderer de Jolt
18+
}
19+
}
20+
21+
export { GraphicsSystem };

src/ai/can-chase/input.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { inputManager } from '@lib/input/input_manager';
2+
import { DNAComponent } from '@lib/dna/dna_component';
3+
import { DNASystem } from '@lib/dna/dna_system';
4+
import { dnaManager } from '@lib/dna/dna_manager';
5+
// ---------------------------------------------------------------------------------------
6+
7+
export class InputComponent extends DNAComponent {
8+
moveForward: boolean;
9+
moveBackward: boolean;
10+
moveLeft: boolean;
11+
moveRight: boolean;
12+
13+
constructor() {
14+
super('Input');
15+
this.moveForward = false;
16+
this.moveBackward = false;
17+
this.moveLeft = false;
18+
this.moveRight = false;
19+
}
20+
}
21+
22+
export class InputSystem extends DNASystem {
23+
camera: any;
24+
25+
constructor(camera: any) {
26+
super();
27+
super.addRequiredComponentTypename('Input');
28+
this.camera = camera;
29+
}
30+
31+
onEntityUpdate(ts: number, eid: number): void {
32+
const input = dnaManager.getComponent(eid, InputComponent);
33+
34+
input.moveForward = !!(inputManager.isActiveAction('UP') || inputManager.isActiveAction('MOVE_FORWARD'));
35+
input.moveBackward = !!(inputManager.isActiveAction('DOWN') || inputManager.isActiveAction('MOVE_BACKWARD'));
36+
input.moveLeft = !!(inputManager.isActiveAction('LEFT') || inputManager.isActiveAction('MOVE_LEFT'));
37+
input.moveRight = !!(inputManager.isActiveAction('RIGHT') || inputManager.isActiveAction('MOVE_RIGHT'));
38+
39+
// Log pour debug
40+
if (input.moveForward || input.moveBackward || input.moveLeft || input.moveRight) {
41+
console.log(`Input detected for entity ${eid}: forward=${input.moveForward}, backward=${input.moveBackward}, left=${input.moveLeft}, right=${input.moveRight}`);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)