Generate random map
This commit is contained in:
parent
d82d65677b
commit
bd91b46e23
5 changed files with 62 additions and 14 deletions
27
js/Cone.js
Normal file
27
js/Cone.js
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
32
js/Env.js
32
js/Env.js
|
@ -1,5 +1,7 @@
|
||||||
import { Element } from "./Element.js";
|
import { Element } from "./Element.js";
|
||||||
import { Quality } from "./utils.js";
|
import { Quality } from "./utils.js";
|
||||||
|
import { Size } from "./Cone.js";
|
||||||
|
import { Spade } from "./Spade.js";
|
||||||
|
|
||||||
export class TypeEntity {
|
export class TypeEntity {
|
||||||
static other = 0;
|
static other = 0;
|
||||||
|
@ -100,6 +102,7 @@ export class Env {
|
||||||
* Animate all the players in the environnement
|
* Animate all the players in the environnement
|
||||||
*/
|
*/
|
||||||
animate = () => {
|
animate = () => {
|
||||||
|
// Player animation
|
||||||
this.elements
|
this.elements
|
||||||
.filter((entityData) => entityData[1] == TypeEntity.player)
|
.filter((entityData) => entityData[1] == TypeEntity.player)
|
||||||
.map((playerData) => playerData[0])
|
.map((playerData) => playerData[0])
|
||||||
|
@ -108,6 +111,14 @@ export class Env {
|
||||||
player.animation();
|
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.animate();
|
||||||
this.render();
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
import { Triangle } from "./Triangle.js";
|
|
||||||
|
|
||||||
export class Pique extends Triangle {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
import { Element } from "./Element.js";
|
|
||||||
|
|
||||||
export class Triangle extends Element {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -24,6 +24,9 @@ const main = () => {
|
||||||
env.addToScene(player, TypeEntity.player);
|
env.addToScene(player, TypeEntity.player);
|
||||||
addEventListener("keypress", player.controlUser);
|
addEventListener("keypress", player.controlUser);
|
||||||
|
|
||||||
|
// Generate random map
|
||||||
|
env.generateRandomMap(20);
|
||||||
|
|
||||||
// GUI
|
// GUI
|
||||||
const gui = new dat.gui.GUI({ closeOnTop: true });
|
const gui = new dat.gui.GUI({ closeOnTop: true });
|
||||||
const menu = Quality.buildGUI(env.quality);
|
const menu = Quality.buildGUI(env.quality);
|
||||||
|
|
Reference in a new issue