day 4
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import { Delegate, ResourcesC, UpdateController } from "@24tools/playable_template";
|
||||
import { Character } from "./Character/Character";
|
||||
import { ResourcesType } from "./Enums/ResourcesType";
|
||||
import { MeshType } from "./Enums/MeshType";
|
||||
import { GLTF } from "three/examples/jsm/loaders/GLTFLoader";
|
||||
import { BaseAnimation } from "./Enums/BaseAnimation";
|
||||
import { PhysicsBody, PhysicsLayer } from "../PhysicsC";
|
||||
import { Object3D } from "three";
|
||||
import { ThreeC } from "../ThreeC";
|
||||
import { PlayerInput } from "./Input/PlayerInput";
|
||||
import { MoveC } from "./Movment/MoveC";
|
||||
import { Vec3 } from "cannon-es";
|
||||
import { contain } from "three/src/extras/TextureUtils";
|
||||
import { Vector3CToT, Vector3TToC } from "./Helper";
|
||||
import { RotationC } from "./Movment/RotationC";
|
||||
import { FollowCameraC } from "./Movment/CameraMovment/FollowCamera";
|
||||
import { Vector3 } from "three";
|
||||
|
||||
export class Player {
|
||||
private static inited: boolean = false;
|
||||
private static isRunning: boolean = false;
|
||||
|
||||
private static updateDelegate: Delegate<number>;
|
||||
|
||||
private static container: Object3D = new Object3D;
|
||||
private static input: PlayerInput;
|
||||
private static movement: MoveC;
|
||||
private static rotation: RotationC;
|
||||
private static spawnPosition: Vector3 = new Vector3(0, 0, 0);
|
||||
|
||||
static character: Character;
|
||||
static physics: PhysicsBody;
|
||||
|
||||
static SetSpawnPosition(position: Vector3) {
|
||||
this.spawnPosition.copy(position);
|
||||
}
|
||||
|
||||
static Init() {
|
||||
if (this.inited) return;
|
||||
this.inited = true
|
||||
const asset = ResourcesC.getResource<GLTF>(ResourcesType.Mesh, MeshType.Character)
|
||||
this.character = new Character(asset);
|
||||
|
||||
this.container.position.copy(this.spawnPosition);
|
||||
this.character.playAnimation(BaseAnimation.Idle)
|
||||
this.container.add(this.character.tObj);
|
||||
ThreeC.addToScene(this.container);
|
||||
|
||||
this.InitPhisic();
|
||||
const input = new PlayerInput();
|
||||
const moveSpeed = 3;
|
||||
const rotationSpeed = 8;
|
||||
this.movement = new MoveC(input, moveSpeed);
|
||||
this.rotation = new RotationC(this.container, input, rotationSpeed);
|
||||
|
||||
this.updateDelegate = new Delegate<number>((delta) => this.Update(delta));
|
||||
UpdateController.Instance.onUpdate.addListener(this.updateDelegate);
|
||||
|
||||
FollowCameraC.Init(this.container);
|
||||
}
|
||||
|
||||
private static InitPhisic() {
|
||||
this.physics = new PhysicsBody(
|
||||
this.container,
|
||||
false,
|
||||
1,
|
||||
PhysicsLayer.Player,
|
||||
PhysicsLayer.Wall,
|
||||
);
|
||||
}
|
||||
|
||||
private static StartRunning() {
|
||||
if (this.isRunning) return;
|
||||
this.character.playAnimation(BaseAnimation.Run);
|
||||
this.isRunning = true;
|
||||
}
|
||||
private static StopRunning() {
|
||||
if (!this.isRunning) return;
|
||||
this.character.playAnimation(BaseAnimation.Idle);
|
||||
this.AnimationValue = 1;
|
||||
this.isRunning = false;
|
||||
}
|
||||
|
||||
private static set AnimationValue(value: number) {
|
||||
this.character.AnimationSpeed = value;
|
||||
this.character.AnimationWeight = value * 12.5 + 87.5;
|
||||
}
|
||||
|
||||
private static Update(delta: number) {
|
||||
const diraction = this.movement.Diraction;
|
||||
const weight = this.movement.Weight;
|
||||
|
||||
if (diraction.length() > 0) {
|
||||
this.StartRunning();
|
||||
this.AnimationValue = weight;
|
||||
FollowCameraC.inputDirection.copy(diraction).normalize();
|
||||
} else {
|
||||
this.StopRunning();
|
||||
}
|
||||
|
||||
|
||||
|
||||
const cPos = Vector3TToC(diraction);
|
||||
|
||||
this.physics.getPhysicsBody().velocity.copy(cPos);
|
||||
this.MoveVisual(delta);
|
||||
}
|
||||
|
||||
private static MoveVisual(delta: number) {
|
||||
const lerpSpeed = 10;
|
||||
const targetPos = (Vector3CToT(this.physics.getPhysicsBody().position));
|
||||
|
||||
this.container.position.lerp(targetPos, delta * lerpSpeed)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user