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
This commit is contained in:
17
src/main.js
17
src/main.js
@@ -3,24 +3,12 @@ import { getCamera } from './cameras/PerspectiveCamera.js';
|
|||||||
import {addAssetsOnScene} from './utils/addAssetsOnScene.js';
|
import {addAssetsOnScene} from './utils/addAssetsOnScene.js';
|
||||||
import * as PlayerMovement from './utils/playerMovement.js';
|
import * as PlayerMovement from './utils/playerMovement.js';
|
||||||
import * as Animations from './utils/animateMesh.js';
|
import * as Animations from './utils/animateMesh.js';
|
||||||
|
import * as PlayerCollisions from './utils/playerCollisions.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*/
|
|
||||||
|
|
||||||
let Clock = new THREE.Clock();
|
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
|
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 | */
|
/* | 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);
|
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
|
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
|
Animations.updateAnimations(Clock.getDelta());//Playing animations
|
||||||
|
|
||||||
renderer.render( scene, camera );//Calculating and redering new frame
|
renderer.render( scene, camera );//Calculating and redering new frame
|
||||||
|
|||||||
@@ -378,12 +378,17 @@ export async function addAssetsOnScene(scene){
|
|||||||
ADDING LINK PLATES
|
ADDING LINK PLATES
|
||||||
-------------------- */
|
-------------------- */
|
||||||
|
|
||||||
|
let linkPlates = [];
|
||||||
|
|
||||||
const linkPlate = await loadModel('src/objects/models/linkPlate.gltf');
|
const linkPlate = await loadModel('src/objects/models/linkPlate.gltf');
|
||||||
linkPlate.scene.scale.set(0.4,0.05,0.4);
|
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
|
linkPlate.scene.rotateOnWorldAxis(new THREE.Vector3(1,0,0), MathUtils.degToRad(90));//Must be standing up
|
||||||
alignGround(ground, linkPlate.scene);
|
alignGround(ground, linkPlate.scene);
|
||||||
linkPlate.scene.position.y = 2;
|
linkPlate.scene.position.y = 2;
|
||||||
|
linkPlate.scene.position.z = 1;
|
||||||
scene.add( linkPlate.scene );
|
scene.add( linkPlate.scene );
|
||||||
|
linkPlate.onEnterHitbox = ()=>{console.log('"Hello World !"')};
|
||||||
|
linkPlates.push(linkPlate);
|
||||||
|
|
||||||
/* --------------------
|
/* --------------------
|
||||||
ADDING GRASS
|
ADDING GRASS
|
||||||
@@ -463,6 +468,7 @@ export async function addAssetsOnScene(scene){
|
|||||||
pathTiles: pathTiles,
|
pathTiles: pathTiles,
|
||||||
nameplateModel: nameplateModel,
|
nameplateModel: nameplateModel,
|
||||||
worldStatue: worldStatue,
|
worldStatue: worldStatue,
|
||||||
printerStatue: printerStatue
|
printerStatue: printerStatue,
|
||||||
|
linkPlates:linkPlates
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
8
src/utils/notification.js
Normal file
8
src/utils/notification.js
Normal file
@@ -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);
|
||||||
|
}
|
||||||
42
src/utils/playerCollisions.js
Normal file
42
src/utils/playerCollisions.js
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user