diff --git a/lab5/README.md b/lab5/README.md index be1d356be311c69a8434259cbe7f7639670a8e1f..0d4e85804faf9b6228d13235131881d6a0318126 100644 --- a/lab5/README.md +++ b/lab5/README.md @@ -1,2 +1,26 @@ +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. diff --git a/lab5/src/static/js/Enemy.js b/lab5/src/static/js/Enemy.js index 19b87b231e1c77549eec158ba3f0be0a4f540894..d843a0feca5a22dd489d166860bb08b662e55362 100644 --- a/lab5/src/static/js/Enemy.js +++ b/lab5/src/static/js/Enemy.js @@ -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 diff --git a/lab5/src/static/js/GameManager.js b/lab5/src/static/js/GameManager.js index ae8b79350e0cf39389c37f4206aa41f63670d95e..9c4161cc23b2e41698744e54cadafab3db988545 100644 --- a/lab5/src/static/js/GameManager.js +++ b/lab5/src/static/js/GameManager.js @@ -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);