|
| 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 | + |
0 commit comments