123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
- import {FBXLoader} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/FBXLoader.js';
- export const player = (() => {
- class Player {
- constructor(params) {
- this.position_ = new THREE.Vector3(0, 0, 0);
- this.velocity_ = 0.0;
- // this.mesh_ = new THREE.Mesh(
- // new THREE.BoxBufferGeometry(1, 1, 1),
- // new THREE.MeshStandardMaterial({
- // color: 0x80FF80,
- // }),
- // );
- // this.mesh_.castShadow = true;
- // this.mesh_.receiveShadow = true;
- // params.scene.add(this.mesh_);
- this.playerBox_ = new THREE.Box3();
- this.params_ = params;
- this.LoadModel_();
- this.InitInput_();
- }
- LoadModel_() {
- const loader = new FBXLoader();
- loader.setPath('./resources/Dinosaurs/FBX/');
- loader.load('Velociraptor.fbx', (fbx) => {
- fbx.scale.setScalar(0.0025);
- fbx.quaternion.setFromAxisAngle(
- new THREE.Vector3(0, 1, 0), Math.PI / 2);
- this.mesh_ = fbx;
- this.params_.scene.add(this.mesh_);
- fbx.traverse(c => {
- let materials = c.material;
- if (!(c.material instanceof Array)) {
- materials = [c.material];
- }
-
- for (let m of materials) {
- if (m) {
- m.specular = new THREE.Color(0x000000);
- m.color.offsetHSL(0, 0, 0.25);
- }
- }
- c.castShadow = true;
- c.receiveShadow = true;
- });
- const m = new THREE.AnimationMixer(fbx);
- this.mixer_ = m;
- for (let i = 0; i < fbx.animations.length; ++i) {
- if (fbx.animations[i].name.includes('Run')) {
- const clip = fbx.animations[i];
- const action = this.mixer_.clipAction(clip);
- action.play();
- }
- }
- });
- }
- InitInput_() {
- this.keys_ = {
- spacebar: false,
- };
- this.oldKeys = {...this.keys_};
- document.addEventListener('keydown', (e) => this.OnKeyDown_(e), false);
- document.addEventListener('keyup', (e) => this.OnKeyUp_(e), false);
- }
- OnKeyDown_(event) {
- switch(event.keyCode) {
- case 32:
- this.keys_.space = true;
- break;
- }
- }
- OnKeyUp_(event) {
- switch(event.keyCode) {
- case 32:
- this.keys_.space = false;
- break;
- }
- }
- CheckCollisions_() {
- const colliders = this.params_.world.GetColliders();
- this.playerBox_.setFromObject(this.mesh_);
- for (let c of colliders) {
- const cur = c.collider;
- if (cur.intersectsBox(this.playerBox_)) {
- this.gameOver = true;
- }
- }
- }
- Update(timeElapsed) {
- if (this.keys_.space && this.position_.y == 0.0) {
- this.velocity_ = 30;
- }
- const acceleration = -75 * timeElapsed;
- this.position_.y += timeElapsed * (
- this.velocity_ + acceleration * 0.5);
- this.position_.y = Math.max(this.position_.y, 0.0);
- this.velocity_ += acceleration;
- this.velocity_ = Math.max(this.velocity_, -100);
- if (this.mesh_) {
- this.mixer_.update(timeElapsed);
- this.mesh_.position.copy(this.position_);
- this.CheckCollisions_();
- }
- }
- };
- return {
- Player: Player,
- };
- })();
|