Skip to content
Snippets Groups Projects
Commit 1498a047 authored by Cedric.dosreis's avatar Cedric.dosreis
Browse files

moving enemies

parent 5830f3d3
Branches
No related tags found
No related merge requests found
Cedric Dos Reis
# lab5 - IHM
READ `./src/README.md` for installation guide
## Features
User move the caracter within a path using arrow key.
Enemies spawn.
Caracter eats food on collision.
Caracter dies if he colides with an enemy.
Enemies move randomly in available direction within a path.
Loading map structure from an image file. Read `./src/maps/README.md` for more informations
### Flask server with 2 routes
`/get/map/<id>` : get an 1D array of string defining the map strcuture. `<id>` : file name of the map (without extension).
`/get/maps/` : get a 2D array of string which contains the list of maps in `./src/maps/` (This route is not used).
## Features to ba added
Let the user choose which map he wants to play in.
......@@ -26,15 +26,29 @@ class Enemy {
//autoriser les movement uniquement lorsque la position x et y est très proche de nombre entier
if((Math.abs(posX) % 1 < 0.05 | Math.abs(posX) % 1 > 0.95)
& (Math.abs(posY) % 1 < 0.05 | Math.abs(posY) % 1 > 0.95)) {
& (Math.abs(posY) % 1 < 0.05 | Math.abs(posY) % 1 > 0.95)) {
// postion to coordinates
let x = Math.round(posX + SIZE/2 - 0.5);
let y = (Math.round(posY - SIZE/2 - 0.5) * -1) -1;
var directions = this.getPossibleDirections(x,y,path);
// 10% chance to change direction if 2 direction available
if (directions.length == 2 & Math.floor(Math.random()*10) == 0){
this.direction = directions[ Math.floor(Math.random()*directions.length) ];
}
// 25% chance to change direction if 3 directions available
if (directions.length == 3 & Math.floor(Math.random()*3) == 0){
this.direction = directions[ Math.floor(Math.random()*directions.length) ];
}
// 50% chance to change direction if 3 directions available
if (directions.length == 4 & Math.floor(Math.random()) == 0){
this.direction = directions[ Math.floor(Math.random()*directions.length) ];
}
// check if direction can be changed according to path
var nextBox;
// check collision with wall
......@@ -51,23 +65,46 @@ class Enemy {
case DIRECTION.RIGHT:
nextBox = path[y][(x+1)%SIZE]
break;
case DIRECTION.NONE:
nextBox = 0;
break;
}
if (nextBox == 0){
//change direction
this.direction = DIRECTION.NONE;
this.direction = directions[ Math.floor(Math.random()*directions.length) ];
}
}
}
setDirection(dir){
// postion to coordinates
this.newDirection = dir;
//this.direction = dir;
getPossibleDirections(x,y, path){
// look for possible directions from actual position
var possibleDirections = []
if (path[(y+1)%SIZE][x] == 1){
possibleDirections.push(DIRECTION.DOWN);
}
if (path[(y-1)%SIZE][x] == 1){
possibleDirections.push(DIRECTION.UP);
}
if (path[y][(x-1)%SIZE] == 1){
possibleDirections.push(DIRECTION.LEFT);
}
if (path[y][(x+1)% SIZE] == 1){
possibleDirections.push(DIRECTION.RIGHT);
}
// choose random direction
//this.direction = possibleDirection[ Math.floor(Math.random()*possibleDirection.length) ];
return possibleDirections;
}
getDirection(){
return this.direction;
}
getObject(){
return this.object;
}
}
\ No newline at end of file
......@@ -29,13 +29,13 @@ var camera;
var renderer;
var player;
var enemies = [];
var foods = []
var foods = [];
var walls = [];
var plane;
var mapGenerator
var score = 0;
var enemies_spawns =[]
const MAX_ENEMIES = 4;
var enemies_spawns = []
const MAX_ENEMIES = 5;
const SIZE = 21
......@@ -74,8 +74,9 @@ function main() {
//create objects
LoadMap('map1');
// laod map from image
[plane, walls, foods, playerObj, path, enemies_spawns] = mapGenerator.generateMap("map1", scene);
player = new Player(playerObj);
// spawn enemey
for (let i = 0; i < MAX_ENEMIES; i++) {
......@@ -92,6 +93,8 @@ function main() {
enemies.forEach(enemy => {
enemy.move(path);
});
//checkCollision();
requestAnimationFrame(render);
renderer.render(scene, camera);
......@@ -112,11 +115,6 @@ function spawnEnemy(){
}
function LoadMap(id){
[plane, walls, foods, playerObj, path, enemies_spawns] = mapGenerator.generateMap(id, scene);
player = new Player(playerObj);
}
// check player collision with food/specail food and enemies
function checkCollision() {
......@@ -139,7 +137,7 @@ function checkCollision() {
}*/
ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize());
collisionResults = ray.intersectObjects(enemies);
collisionResults = ray.intersectObjects(enemies.getObject());
if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()) {
alert('Game Over');
player.setDirection(DIRECTION.NONE);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment