From bd91b46e23122f896f5bf93a41f8c26796f7b06c Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 24 Nov 2022 21:01:59 +0100 Subject: [PATCH] Generate random map --- js/Cone.js | 27 +++++++++++++++++++++++++++ js/Env.js | 32 ++++++++++++++++++++++++++++++++ js/Pique.js | 7 ------- js/Triangle.js | 7 ------- js/main.js | 3 +++ 5 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 js/Cone.js delete mode 100644 js/Pique.js delete mode 100644 js/Triangle.js diff --git a/js/Cone.js b/js/Cone.js new file mode 100644 index 0000000..ca3a2c5 --- /dev/null +++ b/js/Cone.js @@ -0,0 +1,27 @@ +import { Element } from "./Element.js"; + +export class Size { + static little = { radius: 0.2, height: 0.4, radialSegments: 5 }; + static big = { radius: 0.5, height: 0.7, radialSegments: 12 }; +} + +export class Cone extends Element { + constructor(color, level) { + super(); + + this.data = new THREE.Mesh( + new THREE.ConeGeometry( + level.radius, + level.height, + level.radialSegments + ), + new THREE.MeshPhongMaterial({ color: color }) + ); + + // Create shadows + this.data.castShadow = true; + + // Move up + this.data.position.y = 0.2; + } +} diff --git a/js/Env.js b/js/Env.js index d75c4a7..5c86124 100644 --- a/js/Env.js +++ b/js/Env.js @@ -1,5 +1,7 @@ import { Element } from "./Element.js"; import { Quality } from "./utils.js"; +import { Size } from "./Cone.js"; +import { Spade } from "./Spade.js"; export class TypeEntity { static other = 0; @@ -100,6 +102,7 @@ export class Env { * Animate all the players in the environnement */ animate = () => { + // Player animation this.elements .filter((entityData) => entityData[1] == TypeEntity.player) .map((playerData) => playerData[0]) @@ -108,6 +111,14 @@ export class Env { player.animation(); } }); + + // Enemy animation + this.elements + .filter((entityData) => entityData[1] == TypeEntity.ennemy) + .map((ennemyData) => ennemyData[0]) + .forEach((ennemy) => { + ennemy.data.position.x -= 0.02; + }); }; /** @@ -123,4 +134,25 @@ export class Env { this.animate(); this.render(); }; + + /** + * Generate a random map of ennemies + * @param {number} numberOfEnnemies + */ + generateRandomMap = (numberOfEnnemies) => { + // Distance before the first ennemy hit the player + const startDelta = 5; + // Simple Spade + for (let index = 1; index < numberOfEnnemies + 1; index++) { + const spade = new Spade( + Math.random() * 0xffffff, + Math.round(Math.random()) ? Size.little : Size.big + ); + + // Space ennemy randomly + spade.data.position.x += startDelta + index + Math.random() * 20; + + this.addToScene(spade, TypeEntity.ennemy); + } + }; } diff --git a/js/Pique.js b/js/Pique.js deleted file mode 100644 index 9641f17..0000000 --- a/js/Pique.js +++ /dev/null @@ -1,7 +0,0 @@ -import { Triangle } from "./Triangle.js"; - -export class Pique extends Triangle { - constructor() { - super(); - } -} diff --git a/js/Triangle.js b/js/Triangle.js deleted file mode 100644 index 4283b80..0000000 --- a/js/Triangle.js +++ /dev/null @@ -1,7 +0,0 @@ -import { Element } from "./Element.js"; - -export class Triangle extends Element { - constructor() { - super(); - } -} diff --git a/js/main.js b/js/main.js index 3b92b08..45ed9d1 100644 --- a/js/main.js +++ b/js/main.js @@ -24,6 +24,9 @@ const main = () => { env.addToScene(player, TypeEntity.player); addEventListener("keypress", player.controlUser); + // Generate random map + env.generateRandomMap(20); + // GUI const gui = new dat.gui.GUI({ closeOnTop: true }); const menu = Quality.buildGUI(env.quality);