From ad02c2e06531d48e0553da1cbee817599c81326d Mon Sep 17 00:00:00 2001 From: LJ5O <75009579+LJ5O@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:44:42 +0100 Subject: [PATCH] Added checkPlayerCollisions This function can be used to check for a list of objects that the player may enter, it will execute onEnterHitbox and onLeaveHitbox methods when the player collides with a given object --- src/main.js | 17 +++----------- src/utils/addAssetsOnScene.js | 8 ++++++- src/utils/notification.js | 8 +++++++ src/utils/playerCollisions.js | 42 +++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 src/utils/notification.js create mode 100644 src/utils/playerCollisions.js diff --git a/src/main.js b/src/main.js index 469b540..3f9d95a 100644 --- a/src/main.js +++ b/src/main.js @@ -3,24 +3,12 @@ import { getCamera } from './cameras/PerspectiveCamera.js'; import {addAssetsOnScene} from './utils/addAssetsOnScene.js'; import * as PlayerMovement from './utils/playerMovement.js'; import * as Animations from './utils/animateMesh.js'; - -/*TODO*/ -import $ from 'jquery' - -function showNotification(HTMLText, color){ - $("#notification_bar p").html(HTMLText); - $("#notification_bar").css( 'opacity', 0.9 ).animate({ - width:320 - }, 500); -} - -showNotification("oui") -/*TODO*/ +import * as PlayerCollisions from './utils/playerCollisions.js'; let Clock = new THREE.Clock(); THREE.Cache.enabled = true;//So we can request several time the same file without worrying : https://threejs.org/docs/#api/en/loaders/FileLoader -const APP_DEBUG = true;//Turn true to show player position in browser console +const APP_DEBUG = false;//Turn true to show player position in browser console /* --------------------- */ /* | SCENE CREATION | */ @@ -60,6 +48,7 @@ function animate() { if(APP_DEBUG)console.log("Player position X : "+assets.player.scene.position.x+ " Y : "+assets.player.scene.position.y); PlayerMovement.updatePlayerPositionAnimation(assets.player, camera, scene);//Updating player position and rotation + PlayerCollisions.checkPlayerCollisions(assets.player, assets.linkPlates);//Checking collisions with player Animations.updateAnimations(Clock.getDelta());//Playing animations renderer.render( scene, camera );//Calculating and redering new frame diff --git a/src/utils/addAssetsOnScene.js b/src/utils/addAssetsOnScene.js index c480a68..fae31af 100644 --- a/src/utils/addAssetsOnScene.js +++ b/src/utils/addAssetsOnScene.js @@ -378,12 +378,17 @@ export async function addAssetsOnScene(scene){ ADDING LINK PLATES -------------------- */ + let linkPlates = []; + const linkPlate = await loadModel('src/objects/models/linkPlate.gltf'); linkPlate.scene.scale.set(0.4,0.05,0.4); linkPlate.scene.rotateOnWorldAxis(new THREE.Vector3(1,0,0), MathUtils.degToRad(90));//Must be standing up alignGround(ground, linkPlate.scene); linkPlate.scene.position.y = 2; + linkPlate.scene.position.z = 1; scene.add( linkPlate.scene ); + linkPlate.onEnterHitbox = ()=>{console.log('"Hello World !"')}; + linkPlates.push(linkPlate); /* -------------------- ADDING GRASS @@ -463,6 +468,7 @@ export async function addAssetsOnScene(scene){ pathTiles: pathTiles, nameplateModel: nameplateModel, worldStatue: worldStatue, - printerStatue: printerStatue + printerStatue: printerStatue, + linkPlates:linkPlates }; } \ No newline at end of file diff --git a/src/utils/notification.js b/src/utils/notification.js new file mode 100644 index 0000000..ab568ac --- /dev/null +++ b/src/utils/notification.js @@ -0,0 +1,8 @@ +import $ from 'jquery'; + +export function showNotification(HTMLText){ + $("#notification_bar p").html(HTMLText); + $("#notification_bar").css( 'opacity', 0.9 ).animate({ + width:320 + }, 500); +} \ No newline at end of file diff --git a/src/utils/playerCollisions.js b/src/utils/playerCollisions.js new file mode 100644 index 0000000..20ed559 --- /dev/null +++ b/src/utils/playerCollisions.js @@ -0,0 +1,42 @@ +import * as THREE from 'three'; + +export function checkPlayerCollisions(player, objects) { + if (!player || !player.scene) { + throw new Error('The player must have a "scene" property.'); + } + + const playerBoundingBox = new THREE.Box3().setFromObject(player.scene); + + for (const object of objects) { + if (!object || !object.scene) { + console.warn('An object in the list does not have a "scene" property. It will be ignored.'); + continue; + } + + const objectBoundingBox = new THREE.Box3().setFromObject(object.scene); + + //Check if the object is currently inside the hitbox + const isInsideHitbox = playerBoundingBox.intersectsBox(objectBoundingBox); + + //If the object was outside but is now inside, call onEnterHitbox + if (isInsideHitbox && !object.isInsideHitbox) { + if (object.onEnterHitbox && typeof object.onEnterHitbox === 'function') { + object.onEnterHitbox(); + } else { + console.warn('The onEnterHitbox method is not defined on an object checked for entering the player\'s hitbox.'); + } + } + + //If the object was inside but is now outside, call onLeaveHitbox + else if (!isInsideHitbox && object.isInsideHitbox) { + if (object.onLeaveHitbox && typeof object.onLeaveHitbox === 'function') { + object.onLeaveHitbox(); + } else { + console.warn('The onLeaveHitbox method is not defined on an object checked for leaving the player\'s hitbox.'); + } + } + + //Update the state for the next check + object.isInsideHitbox = isInsideHitbox; + } +} \ No newline at end of file