diff --git a/lab5/src/static/js/GameManager.js b/lab5/src/static/js/GameManager.js index 058441c6ee90a90b5963670e71960b38fc7d5dae..3397bb799be5974c8ac5f433b2e1c46e79688587 100644 --- a/lab5/src/static/js/GameManager.js +++ b/lab5/src/static/js/GameManager.js @@ -7,11 +7,12 @@ const DIRECTION = { NONE: 'NONE' }; +const SPEED = 0.05; const MOVEMENT = { - LEFT: { x: -0.08, y: 0 }, - RIGHT: { x: 0.08, y: 0 }, - UP: { x: 0, y : 0.08 }, - DOWN: { x: 0, y: -0.08 }, + LEFT: { x: -SPEED, y: 0 }, + RIGHT: { x: SPEED, y: 0 }, + UP: { x: 0, y : SPEED }, + DOWN: { x: 0, y: -SPEED }, NONE: { x: 0, y: 0 } }; @@ -19,7 +20,8 @@ const KEYS = { ArrowLeft: DIRECTION.LEFT, ArrowRight: DIRECTION.RIGHT, ArrowUp: DIRECTION.UP, - ArrowDown: DIRECTION.DOWN + ArrowDown: DIRECTION.DOWN, + Space : DIRECTION.NONE }; var scene; @@ -32,6 +34,8 @@ var walls = []; var mapGenerator var score = 0; +const SIZE = 21 + function main() { mapGenerator = new MapGenerator(); @@ -48,7 +52,7 @@ function main() { //create objects var plane; scene.add(camera); - [plane, walls, foods, playerObj] = mapGenerator.generateMap(1, scene); + [plane, walls, foods, playerObj, path] = mapGenerator.generateMap(2, scene); // get all wall object from scene @@ -60,13 +64,13 @@ function main() { // player - player = new Player(playerObj); + player = new Player(playerObj, ); function render(){ - checkCollision(); - player.move(); + //checkCollision(); + player.move(path); requestAnimationFrame(render); renderer.render(scene, camera); @@ -74,6 +78,9 @@ function main() { } render(); + const times = []; + let fps; + } function checkCollision() { @@ -109,11 +116,10 @@ function checkCollision() { collisionResults[0].object.material.opacity = 0.4;*/ } - collisionResults = ray.intersectObjects(foods); + /*collisionResults = ray.intersectObjects(foods); if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()) { - console.log('Miam'); score++; - } + }*/ } } diff --git a/lab5/src/static/js/MapGenerator.js b/lab5/src/static/js/MapGenerator.js index 0823aea9ffd1390420040b0202877ff05d21fed9..2e1a991328bea744e5c1469eb0de9297a15dead5 100644 --- a/lab5/src/static/js/MapGenerator.js +++ b/lab5/src/static/js/MapGenerator.js @@ -1,12 +1,11 @@ -function convert2Dto1D(x, y, size){ - return (y * size) + x; +function convert2Dto1D(x, y, sz){ + return (y * sz) + x; } class MapGenerator { - constructor() { - this.size = 21; + //SIZE = 21; } //// 2 dimension coordinates to 1 dimension @@ -17,12 +16,13 @@ class MapGenerator { var walls = []; var foods = []; + var path = []; var player; var map_content = []; // create ground - var geometry = new THREE.PlaneGeometry( this.size, this.size ); + var geometry = new THREE.PlaneGeometry( SIZE, SIZE ); var material = new THREE.MeshBasicMaterial( {color: 0x000000, side: THREE.DoubleSide} ); var plane = new THREE.Mesh( geometry, material ); scene.add(plane); @@ -44,9 +44,10 @@ class MapGenerator { // loop through map content in 2 dimension to prepare to position walls in ground let i = 0; let offsetX, offsetY, wallWidth, wallHeight, wallPosX, wallPosY; - for (let y = 0; y < this.size; y++) { - for (let x = 0; x < this.size; x++) { - i = convert2Dto1D(x,y,this.size); + for (let y = 0; y < SIZE; y++) { + path.push([]); + for (let x = 0; x < SIZE; x++) { + i = convert2Dto1D(x,y,SIZE); switch (map_content[i]){ // case "WALL": @@ -56,19 +57,21 @@ class MapGenerator { wallHeight = 1; wallPosX = x; wallPosY = y; - - map_content[i] = '' ; //empty the box to prevent from creating 2 walls at the same spot + + path[y].push(0); + + map_content[i] = 'WALL_DONE' ; //prevent from creating 2 walls at the same spot // check for a horizontal line of wall - while (map_content[convert2Dto1D(x + offsetX, y,this.size)] == 'WALL' & x + offsetX < this.size){ - map_content[convert2Dto1D(x + offsetX, y,this.size)] = ''; + while (map_content[convert2Dto1D(x + offsetX, y,SIZE)] == 'WALL' & x + offsetX < SIZE){ + map_content[convert2Dto1D(x + offsetX, y,SIZE)] = 'WALL_DONE'; wallWidth++; offsetX++; } //check for a vertical line of wall only if it was not a horizonatl wall - while (wallWidth <= 1 & map_content[convert2Dto1D(x, y + offsetY,this.size)] == 'WALL' & y + offsetY < this.size){ - map_content[convert2Dto1D(x, y + offsetY,this.size)] = '' + while (wallWidth <= 1 & map_content[convert2Dto1D(x, y + offsetY,SIZE)] == 'WALL' & y + offsetY < SIZE){ + map_content[convert2Dto1D(x, y + offsetY,SIZE)] = 'WALL_DONE'; wallHeight++; offsetY++; } @@ -78,8 +81,8 @@ class MapGenerator { var material = new THREE.MeshBasicMaterial( {color: 0x0000FF} ); var wall = new THREE.Mesh( geometry, material ); // re-position coreectly - wall.position.x = wallPosX - (this.size)/2 + wallWidth/2; - wall.position.y = wallPosY - (this.size)/2 + wallHeight /2; + wall.position.x = -wallPosX - wallWidth/2 + (SIZE)/2 ; + wall.position.y = -wallPosY - wallHeight/2 + (SIZE)/2 ; // add to scene scene.add(wall); walls.push(wall); @@ -89,42 +92,56 @@ class MapGenerator { var geometry = new THREE.SphereGeometry(0.1); var material = new THREE.MeshBasicMaterial( {color: 0xffffff} ); var food = new THREE.Mesh( geometry, material ); - food.position.x = x - (this.size - 1)/2; - food.position.y = y - (this.size - 1)/2; + food.position.x = -x + (SIZE - 1)/2; + food.position.y = -y + (SIZE - 1)/2; scene.add(food); foods.push(food); + + path[y].push(1); break; case "S_FOOD": var geometry = new THREE.SphereGeometry(0.2); var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} ); var food = new THREE.Mesh( geometry, material ); - food.position.x = x - (this.size - 1)/2; - food.position.y = y - (this.size - 1)/2; + food.position.x = -x + (SIZE - 1)/2; + food.position.y = -y + (SIZE - 1)/2; scene.add(food); foods.push(food); + path[y].push(1); break; case "": + path[y].push(1); + break; + + case "SPAWN_E": + path[y].push(1); break; + case "SPAWN_P": var geometry = new THREE.SphereGeometry(0.4); var material = new THREE.MeshBasicMaterial( {color: 0xffff00} ); player = new THREE.Mesh( geometry, material ); - player.position.x = x - (this.size - 1)/2; - player.position.y = y - (this.size - 1)/2; + player.position.x = -x + (SIZE - 1)/2; + player.position.y = -y + (SIZE - 1)/2; player.name = "Player"; scene.add(player); console.log(x, y); + path[y].push(1); + break; + + case "WALL_DONE": + path[y].push(0); break; } } } - + console.log(path); - return [plane, walls, foods, player]; + return [plane, walls, foods, player, path]; } } \ No newline at end of file diff --git a/lab5/src/static/js/Player.js b/lab5/src/static/js/Player.js index 218376fe9a3911013a72c454b9f20b88b07c9893..37d8180c9a28c056675e1f6e0c1dec6ee77fa76c 100644 --- a/lab5/src/static/js/Player.js +++ b/lab5/src/static/js/Player.js @@ -1,21 +1,59 @@ class Player { - constructor(obj) { + constructor(obj,) { this.object = obj; this.direction = DIRECTION.NONE; + this.newDirection = DIRECTION.NONE; + } - move(){ + move(path){ let { x, y } = MOVEMENT[this.direction]; this.object.position.x += x; this.object.position.y += y; + + // check for edge -> loop + if(this.object.position.x > SIZE /2 | this.object.position.x < SIZE /2 * (-1)){ + this.object.position.x *= -1; + } + if(this.object.position.y > SIZE /2 | this.object.position.y < SIZE /2 * (-1)){ + this.object.position.y *= -1; + } + + // TODO //autoriser les movement uniquement lorsque la position x ou y est sur .5 // x % 1 == 0.5 -> change direction + + if(this.newDirection != this.direction & x % 1 < 0.2 & y % 1 < 0.2){ + // postion to coordinates + x = Math.round(this.object.position.x + SIZE/2 - 0.5); + y = (Math.round(this.object.position.y - SIZE/2 - 0.5) * -1) -1; + + this.direction = this.newDirection; + /*switch(this.newDirection){ + case DIRECTION.DOWN: + + break; + case DIRECTION.UP: + break; + case DIRECTION.LEFT: + break; + case DIRECTION.RIGHT: + break; + }*/ + + } + + //console.log (this.object.position.x + " " + this.object.position.y); + + } setDirection(dir){ - this.direction = dir; + // postion to coordinates + this.newDirection = dir; + //this.direction = dir; } getDirection(){