lingo3d
Version:
Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor
226 lines • 8.04 kB
JavaScript
import { rad2Deg } from "@lincode/math";
import store, { Reactive } from "@lincode/reactivity";
import { interpret } from "xstate";
import { dummyDefaults, dummySchema } from "../../interface/IDummy";
import Model from "../Model";
import poseMachine from "./poseMachine";
import dirPath from "../../api/path/dirPath";
import { indexChildrenNames } from "../../memo/indexChildrenNames";
import { dummyUrlPtr, ybotUrlPtr } from "../../pointers/assetsPathPointers";
import { dummyGroundedSystem } from "../../systems/dummyGroundedSystem";
import { dummySystem } from "../../systems/dummySystem";
class Dummy extends Model {
static componentName = "dummy";
static defaults = dummyDefaults;
static schema = dummySchema;
static includeKeys = ["strideRight", "strideForward", "strideMove"];
poseService = interpret(poseMachine);
constructor() {
super();
this.width = 20;
this.depth = 20;
this.scale = 1.7;
this.src = ybotUrlPtr[0];
this.runtimeDefaults = { src: ybotUrlPtr[0] };
const [setType, getType] = store(undefined);
const [setSpine, getSpine] = store(undefined);
this.createEffect(() => {
const spineName = this.spineNameState.get();
setSpine(undefined);
setType(undefined);
const handle = this.$events.on("loaded", (loaded) => {
this.runtimeDefaults = { src: ybotUrlPtr[0] };
setType("other");
if (spineName) {
setSpine(this.find(spineName));
if (spineName === "mixamorigSpine")
setType("mixamo");
else if (spineName === "Spine" &&
(indexChildrenNames(loaded).get("Wolf3D_Body") ||
indexChildrenNames(loaded).get("Wolf3D_Avatar")))
setType("readyplayerme");
return;
}
if (indexChildrenNames(loaded).get("Wolf3D_Body") ||
indexChildrenNames(loaded).get("Wolf3D_Avatar")) {
setSpine(this.find("Spine"));
setType("readyplayerme");
return;
}
const spine = this.find("mixamorigSpine");
setSpine(spine);
spine && setType("mixamo");
});
return () => {
handle.cancel();
};
}, [this.srcState.get, this.spineNameState.get]);
const [setPose, getPose] = store("idle");
this.createEffect(() => {
const type = getType();
if (!type)
return;
const preset = this.presetState.get();
const prefix = preset === "rifle" ? "rifle-" : "";
const { src } = this;
let url = dirPath(src) + "/";
if (type === "readyplayerme")
url = dummyUrlPtr[0] + "readyplayerme/";
else if (src !== ybotUrlPtr[0]) {
super.animations = this.animationsState.get();
this.animation = getPose();
return () => {
super.animations = {};
};
}
super.animations = {
idle: url + prefix + "idle.fbx",
running: url + prefix + "running.fbx",
runningBackwards: url + prefix + "running-backwards.fbx",
jumping: url + prefix + "falling.fbx",
death: url + "death.fbx",
...this.animationsState.get()
};
this.animation = getPose();
return () => {
super.animations = {};
};
}, [
this.presetState.get,
this.srcState.get,
getType,
this.animationsState.get
]);
const { poseService } = this;
this.createEffect(() => {
const pose = getPose();
this.animation = pose;
if (pose !== "jumping")
return;
this.velocityY = this.jumpHeight;
dummyGroundedSystem.add(this, { poseService });
return () => {
dummyGroundedSystem.delete(this);
};
}, [getPose]);
poseService
.onTransition((state) => state.changed && setPose(state.value))
.start();
this.then(() => poseService.stop());
this.createEffect(() => {
const { $loadedObject3d: loadedObject3d } = this;
if (!loadedObject3d)
return;
const { strideForward, strideRight, strideMove } = this;
if (!strideForward && !strideRight) {
poseService.send("RUN_STOP");
return;
}
let strideMode = this.strideModeState.get();
if (strideMode === "aim" &&
!("runningBackwards" in this.animations))
strideMode = "free";
const backwards = strideMode === "aim" ? strideForward > 0 : false;
const sf = backwards ? -strideForward : strideForward;
const sr = backwards ? strideRight : -strideRight;
const angle = 90 - Math.atan2(-sf, -sr) * rad2Deg;
const spine = getSpine();
const spineQuaternion = spine?.quaternion.clone();
const loadedItemQuaternion = loadedObject3d.quaternion.clone();
dummySystem.add(this, {
poseService,
backwards,
strideMode,
spine,
spineQuaternion,
loadedItemQuaternion,
strideMove,
angle,
strideForward,
strideRight
});
return () => {
if (strideMode === "aim" &&
!this.strideForward &&
!this.strideRight)
loadedObject3d.quaternion.set(0, 0, 0, 0);
dummySystem.delete(this);
};
}, [
this.animationsState.get,
this.strideModeState.get,
this.strideMoveState.get,
this.strideForwardState.get,
this.strideRightState.get,
getSpine
]);
}
spineNameState = new Reactive(undefined);
get spineName() {
return this.spineNameState.get();
}
set spineName(val) {
this.spineNameState.set(val);
}
get resize() {
return super.resize;
}
set resize(val) { }
srcState = new Reactive(undefined);
get src() {
return super.src;
}
set src(val) {
super.src = val;
this.srcState.set(val);
}
animationsState = new Reactive({});
get animations() {
return super.animations;
}
set animations(val) {
this.animationsState.set(val);
}
presetState = new Reactive("default");
get preset() {
return this.presetState.get();
}
set preset(val) {
this.presetState.set(val);
}
strideForwardState = new Reactive(0);
get strideForward() {
return this.strideForwardState.get();
}
set strideForward(val) {
this.strideForwardState.set(val);
}
strideRightState = new Reactive(0);
get strideRight() {
return this.strideRightState.get();
}
set strideRight(val) {
this.strideRightState.set(val);
}
strideMoveState = new Reactive(false);
get strideMove() {
return this.strideMoveState.get();
}
set strideMove(val) {
this.strideMoveState.set(val);
}
strideModeState = new Reactive("aim");
get strideMode() {
return this.strideModeState.get();
}
set strideMode(val) {
this.strideModeState.set(val);
}
jumpHeight = 10;
jump(height = 10) {
this.jumpHeight = height;
this.poseService.send("JUMP_START");
}
}
export default Dummy;
//# sourceMappingURL=index.js.map