Generate random map

This commit is contained in:
Mylloon 2022-11-24 21:01:59 +01:00
parent d82d65677b
commit bd91b46e23
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 62 additions and 14 deletions

27
js/Cone.js Normal file
View file

@ -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;
}
}

View file

@ -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);
}
};
}

View file

@ -1,7 +0,0 @@
import { Triangle } from "./Triangle.js";
export class Pique extends Triangle {
constructor() {
super();
}
}

View file

@ -1,7 +0,0 @@
import { Element } from "./Element.js";
export class Triangle extends Element {
constructor() {
super();
}
}

View file

@ -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);