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

player movement on a defined path array (no more collision with three.js for walls)

parent 8c75bf46
Branches
No related tags found
No related merge requests found
lab5/src/maps/map1.png

285 B | W: | H:

lab5/src/maps/map1.png

360 B | W: | H:

lab5/src/maps/map1.png
lab5/src/maps/map1.png
lab5/src/maps/map1.png
lab5/src/maps/map1.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -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(){
......
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment