From aee1f94ea36a5a222a6618f919aa47d6fd2aeb0c Mon Sep 17 00:00:00 2001 From: Kum1ta Date: Mon, 19 Aug 2024 00:42:53 +0200 Subject: [PATCH] Game - Update player class --- site/real_game/class/Player.js | 66 +++++++++------ site/real_game/index.html | 2 + site/real_game/main.js | 23 ++++-- site/real_game/package-lock.json | 137 ++++++++++++++++--------------- site/real_game/package.json | 1 + 5 files changed, 131 insertions(+), 98 deletions(-) diff --git a/site/real_game/class/Player.js b/site/real_game/class/Player.js index c6e12b4..6b27134 100644 --- a/site/real_game/class/Player.js +++ b/site/real_game/class/Player.js @@ -6,7 +6,7 @@ /* By: edbernar pressedButton = ['w', 'a'] - Les lignes avec cleanup sont l'êquivalent d'un destructeur en CPP - Pour appliquer des actions sur les touches, il suffit de faire ça dans la fonction - update en regardant si la touche voulue est pressée dans la variable "pressedButton" + update en regardant si la touche voulue est dans la variable "pressedButton" - Par défaut, la caméra est accroché, si on veut qu'elle ne bouge plus, il faut - modifier "cameraFixed" à true + modifier "cameraFixed" à true (se fait avec la touche 'm' en jeu) - Si on utilise une touche qui ne sera utilisée que pour un appui simple, il faudra la - mettre dans 'addEventListerner('keypress') et pas dans update() + mettre dans 'addEventListerner('keypress') et pas dans update() pour eviter les + problèmes de touche non détecté + - La variable "limits" sert à délimiter les mouvements de la barre */ /* @@ -32,18 +34,27 @@ import * as THREE from 'three'; - Ajouter une camera sur l'object (OK) - Faire une fonction pour changer le mode de la camera (fix ou accrochée) (OK) - Ajouter une rotation quand la caméra est fixe (OK) + - Corriger bug quand changement de caméra (camera qui se remet au dessus + quand on repasse au dessus alors qu'elle devrait être en dessous vu que la + barre est en haut). Mais peut etre faire ça quand la barre aura des mouvements + définis sur la hauteur. */ let playerExist = false; +const limits = { + up : 2, + down: 0.2, + left: -4, + right: 4, +} class Player { pressedButton = []; object = null; camera = null; - speed = 0.05; + speed = 0.1; cameraFixed = false; - orbital = null; constructor (object, renderer) { @@ -51,10 +62,7 @@ class Player throw Error("Player is already init."); playerExist = true; this.object = object; - this.camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 10000); - this.orbital = new OrbitControls(this.camera, renderer.domElement); - - this.orbital.enableZoom = false; + this.camera = new THREE.PerspectiveCamera(80, window.innerWidth / window.innerHeight, 0.1, 10000); this.cleanup = new FinalizationRegistry((heldValue) => { playerExist = false; @@ -86,13 +94,16 @@ class Player { this.cameraFixed = !this.cameraFixed; if (!this.cameraFixed) + { this.setCameraPosition( this.object.position.x, this.object.position.y + 0.5, this.object.position.z + 1 ); + this.camera.rotation.set(0, 0, 0); + } else - this.setCameraPosition(0, 1, 1) + this.setCameraPosition(0, 1, 0.7) } }); } @@ -103,27 +114,36 @@ class Player i = 0; if (this.cameraFixed) - { - this.orbital.target = new THREE.Vector3(this.object.position.x, this.object.position.y, this.object.position.z - 5); - this.orbital.update(); - } - else - this.camera.rotation.set(0, 0, 0); + this.camera.lookAt(new THREE.Vector3(this.object.position.x / 4, (this.object.position.y + 1) / 1.75, this.object.position.z - 1.5)); while (i < this.pressedButton.length) { - if (this.pressedButton[i] == 'w' || this.pressedButton[i] == 's') + if (this.pressedButton[i] == 'w' && this.object.position.y < limits.up) { - this.object.position.y += (this.pressedButton[i] == 'w' ? this.speed : -this.speed); + this.object.position.y += this.speed; if (!this.cameraFixed) - this.camera.position.y += (this.pressedButton[i] == 'w' ? this.speed : -this.speed); + this.camera.position.y += (this.speed / 2); } - if (this.pressedButton[i] == 'a' || this.pressedButton[i] == 'd') + if (this.pressedButton[i] == 's' && this.object.position.y > limits.down) { - this.object.position.x += (this.pressedButton[i] == 'a' ? -this.speed : this.speed); + this.object.position.y -= this.speed; if (!this.cameraFixed) - this.camera.position.x += (this.pressedButton[i] == 'a' ? -this.speed : this.speed); + this.camera.position.y -= (this.speed / 2); + } + if (this.pressedButton[i] == 'd' && this.object.position.x < limits.right) + { + this.object.position.x += this.speed; + if (!this.cameraFixed) + this.camera.position.x += this.speed; + } + if (this.pressedButton[i] == 'a' && this.object.position.x > limits.left) + { + this.object.position.x -= this.speed; + if (!this.cameraFixed) + this.camera.position.x -= this.speed; } i++; + + } } diff --git a/site/real_game/index.html b/site/real_game/index.html index 6aaa555..88a7967 100644 --- a/site/real_game/index.html +++ b/site/real_game/index.html @@ -6,6 +6,8 @@ body { margin: 0; padding: 0; + width: 100%; + height: 100%; } diff --git a/site/real_game/main.js b/site/real_game/main.js index 449e862..eee5917 100644 --- a/site/real_game/main.js +++ b/site/real_game/main.js @@ -6,7 +6,7 @@ /* By: edbernar =8.0.0" }, "optionalDependencies": { - "@rollup/rollup-android-arm-eabi": "4.20.0", - "@rollup/rollup-android-arm64": "4.20.0", - "@rollup/rollup-darwin-arm64": "4.20.0", - "@rollup/rollup-darwin-x64": "4.20.0", - "@rollup/rollup-linux-arm-gnueabihf": "4.20.0", - "@rollup/rollup-linux-arm-musleabihf": "4.20.0", - "@rollup/rollup-linux-arm64-gnu": "4.20.0", - "@rollup/rollup-linux-arm64-musl": "4.20.0", - "@rollup/rollup-linux-powerpc64le-gnu": "4.20.0", - "@rollup/rollup-linux-riscv64-gnu": "4.20.0", - "@rollup/rollup-linux-s390x-gnu": "4.20.0", - "@rollup/rollup-linux-x64-gnu": "4.20.0", - "@rollup/rollup-linux-x64-musl": "4.20.0", - "@rollup/rollup-win32-arm64-msvc": "4.20.0", - "@rollup/rollup-win32-ia32-msvc": "4.20.0", - "@rollup/rollup-win32-x64-msvc": "4.20.0", + "@rollup/rollup-android-arm-eabi": "4.21.0", + "@rollup/rollup-android-arm64": "4.21.0", + "@rollup/rollup-darwin-arm64": "4.21.0", + "@rollup/rollup-darwin-x64": "4.21.0", + "@rollup/rollup-linux-arm-gnueabihf": "4.21.0", + "@rollup/rollup-linux-arm-musleabihf": "4.21.0", + "@rollup/rollup-linux-arm64-gnu": "4.21.0", + "@rollup/rollup-linux-arm64-musl": "4.21.0", + "@rollup/rollup-linux-powerpc64le-gnu": "4.21.0", + "@rollup/rollup-linux-riscv64-gnu": "4.21.0", + "@rollup/rollup-linux-s390x-gnu": "4.21.0", + "@rollup/rollup-linux-x64-gnu": "4.21.0", + "@rollup/rollup-linux-x64-musl": "4.21.0", + "@rollup/rollup-win32-arm64-msvc": "4.21.0", + "@rollup/rollup-win32-ia32-msvc": "4.21.0", + "@rollup/rollup-win32-x64-msvc": "4.21.0", "fsevents": "~2.3.2" } }, diff --git a/site/real_game/package.json b/site/real_game/package.json index 4443593..98df8eb 100644 --- a/site/real_game/package.json +++ b/site/real_game/package.json @@ -3,6 +3,7 @@ "dev": "vite" }, "dependencies": { + "rollup": "^4.21.0", "three": "^0.167.1", "vite": "^5.4.1" }