Files
portfolio/src/main.js
LJ5O 256654502e Added collisions with objects on scene
Player will now be stopped by fences
Also added the possibility to define the size of ground
Also edited speed of animations
Also optimized the loading of fences
2023-11-17 10:42:14 +01:00

48 lines
1.7 KiB
JavaScript

import * as THREE from 'three';
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';
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
/* --------------------- */
/* | SCENE CREATION | */
/* --------------------- */
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0EB1D2);
const camera = getCamera();
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//Here, we create/load every asset used, and add it in the scene
let assets = await addAssetsOnScene(scene);
/* --------------------- */
/* | MOVEMENT | */
/* --------------------- */
// Events to listen keyboard inputs
document.addEventListener('keydown', PlayerMovement.handleKeyDown);
document.addEventListener('keyup', PlayerMovement.handleKeyUp);
/* --------------------- */
/* | ANIMATIONS | */
/* --------------------- */
//We can add an animationMixer on objects, and define ready to play actions on objects
//https://threejs.org/docs/#api/en/animation/AnimationAction
Animations.addAnimationMixerOnMesh(assets.player, ["Walk"]);
function animate() {
requestAnimationFrame( animate );
PlayerMovement.updatePlayerPositionAnimation(assets.player, camera, scene);//Updating player position and rotation
Animations.updateAnimations(Clock.getDelta());//Playing animations
renderer.render( scene, camera );//Calculating and redering new frame
}
animate();