From 2c9630ced28d4ec44d9ab8a6689b3e5c9345c161 Mon Sep 17 00:00:00 2001 From: "Cedric.dosreis" <cedric.dosreis@gmail.com> Date: Tue, 17 Dec 2019 10:39:40 +0100 Subject: [PATCH] player movement on a defined path array (no more collision with three.js for walls) --- lab5/src/maps/map1.png | Bin 285 -> 360 bytes lab5/src/static/js/GameManager.js | 4 +- lab5/src/static/js/Player.js | 72 +++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/lab5/src/maps/map1.png b/lab5/src/maps/map1.png index 4de1dacccaa2756703ec60c0a1a6a280a75a8f50..20c2eca08c7b6fa358439ab9ad9d12bf877cbd7e 100644 GIT binary patch delta 333 zcmbQs^nz)EN<Cw7kh>GZx^prw85kH?(j9#r85lP9bN@+XWnf_7EbxddW?<ku48n}Z z^H-!WFfjb}ba4!^@LwC!DAuCDaryWE`gamczDqvsz4cW1cuQ*O-mPUXzDn-;v*$(Q z2e#T^W!3uR)=f?gS6U7v$6FiaZ+vI|#`<ofnbPvEHwT{A+syfXGx>0gNZ#h;XY)R> ztyyyRj;CvI`Q)QH5hV_H?2gz;ocUO@RC<qytGb-z8T-s*4lhGl)y_Of?QC_KGN(kn z#j0zO)9c8@<Foko*<3CX@1D5t(AINuB0_dX7TXtdF5mGkhVwy&yRz#0{TgxBM)4Vk zH07rM%XAE|IJE83@|SL3Wdj*4Wmyz0Uz+*bG=87I{r$U5-fJwwpH_+~Oq#Ys#pf>b pyxSLhoR=)h-zf5zXYXIe9O>EY>#F1$85kHCJYD@<);T3K0RX)dlc4|r delta 257 zcmaFCG?!_DO8sL`7sn6_|E(d6LWdPNn*ZC+lGTiG*|@9p&bzR^0?I-&I#~8!Z|am@ z`Qo`!#QA#%0u)3Qlg?jEQT_O#bnE(4QU5wG&Hd!yp;lTtb<@;OW!+OR#T;I~Rn9`A z_xkCrpTFn)XOk;PIHz%UOZc|n*Fo1T_TAHUU2*>I;cK-GRqD(1stjj!@aU#g-VhX; z5OdI=Ywh9mDWwZcmX@Zz%C`#q<>jk>Z~nEC@Ryy7_kFZ$b1l2l`2GDKn}9E&+^$ER z&26e!d<EC{zvEouC$~Iw>7L?i_xIX=E&TL@FHNG>+PJti(r@Mb6Ep1|9a{U}X;Q*c R1_lNOPgg&ebxsLQ3;;1^ey#uj diff --git a/lab5/src/static/js/GameManager.js b/lab5/src/static/js/GameManager.js index 3397bb7..cf3f1a4 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 37d8180..0cfb2c2 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 -- GitLab