diff --git a/lab5/src/maps/map1.png b/lab5/src/maps/map1.png index 4de1dacccaa2756703ec60c0a1a6a280a75a8f50..20c2eca08c7b6fa358439ab9ad9d12bf877cbd7e 100644 Binary files a/lab5/src/maps/map1.png and b/lab5/src/maps/map1.png differ diff --git a/lab5/src/static/js/GameManager.js b/lab5/src/static/js/GameManager.js index 3397bb799be5974c8ac5f433b2e1c46e79688587..cf3f1a47a1bbecc174c32d5ee40154c2c6c441b8 100644 --- a/lab5/src/static/js/GameManager.js +++ b/lab5/src/static/js/GameManager.js @@ -52,7 +52,7 @@ function main() { //create objects var plane; scene.add(camera); - [plane, walls, foods, playerObj, path] = mapGenerator.generateMap(2, scene); + [plane, walls, foods, playerObj, path] = mapGenerator.generateMap(1, scene); // get all wall object from scene @@ -64,7 +64,7 @@ function main() { // player - player = new Player(playerObj, ); + player = new Player(playerObj); function render(){ diff --git a/lab5/src/static/js/Player.js b/lab5/src/static/js/Player.js index 37d8180c9a28c056675e1f6e0c1dec6ee77fa76c..0cfb2c2cb04f7d26d4021c90fd78a72e31bb2672 100644 --- a/lab5/src/static/js/Player.js +++ b/lab5/src/static/js/Player.js @@ -1,5 +1,5 @@ class Player { - constructor(obj,) { + constructor(obj) { this.object = obj; this.direction = DIRECTION.NONE; this.newDirection = DIRECTION.NONE; @@ -12,38 +12,80 @@ class Player { this.object.position.y += y; + var posX = this.object.position.x; + var posY = this.object.position.y; + + // check for edge -> loop - if(this.object.position.x > SIZE /2 | this.object.position.x < SIZE /2 * (-1)){ + if(posX > SIZE /2 | posX < SIZE /2 * (-1)){ this.object.position.x *= -1; } - if(this.object.position.y > SIZE /2 | this.object.position.y < SIZE /2 * (-1)){ + if(posY > SIZE /2 | posY < 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 + //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)) { - 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; + let x = Math.round(posX + SIZE/2 - 0.5); + let y = (Math.round(posY - SIZE/2 - 0.5) * -1) -1; + + // check if direction can be changed according to path + if (this.newDirection != this.direction){ + var nextBox; + // check if a path is available (not a wall) in the new direction + switch(this.newDirection){ + case DIRECTION.DOWN: + nextBox = path[(y+1)%SIZE][x]; + break; + case DIRECTION.UP: + nextBox = path[(y-1)%SIZE][x]; + break; + case DIRECTION.LEFT: + nextBox = path[y][(x-1)%SIZE]; + break; + case DIRECTION.RIGHT: + nextBox = path[y][(x+1)%SIZE] + break; + } - this.direction = this.newDirection; - /*switch(this.newDirection){ + if (nextBox == 1){ + //change direction + this.direction = this.newDirection; + } + } + + var nextBox; + // check collision with wall + switch(this.direction){ case DIRECTION.DOWN: - + nextBox = path[(y+1)%SIZE][x]; break; case DIRECTION.UP: - break; + nextBox = path[(y-1)%SIZE][x]; + break; case DIRECTION.LEFT: + nextBox = path[y][(x-1)%SIZE]; break; case DIRECTION.RIGHT: + nextBox = path[y][(x+1)%SIZE] break; - }*/ + } + if (nextBox == 0){ + //change direction + this.direction = DIRECTION.NONE; + } + + } + //console.log (this.object.position.x + " " + this.object.position.y); @@ -60,8 +102,4 @@ class Player { return this.direction; } - getObject(){ - return this.object; - } - } \ No newline at end of file