Skip to content

Commit

Permalink
Merge pull request #46 from TheNexusCity/fix-pet-lookup-metabot
Browse files Browse the repository at this point in the history
Fix pet look up owner #35
  • Loading branch information
lalalune authored Apr 30, 2022
2 parents f38d0b6 + 8bab3a3 commit 4d913cf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 39 deletions.
76 changes: 40 additions & 36 deletions character-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1457,36 +1457,48 @@ class RemotePlayer extends InterpolatedPlayer {
// TODO: Handle attaching the remote
}

if(e.changes.keys.get('transform')){
const transform = this.playerMap.get('transform');
if (transform) {
const remoteTimeDiff = transform[10];
lastPosition.copy(this.position);
this.position.fromArray(transform, 0);

if(this.avatar)
this.characterPhysics.setPosition(this.position);

this.quaternion.fromArray(transform, 3);

this.positionInterpolant?.snapshot(remoteTimeDiff);
this.quaternionInterpolant?.snapshot(remoteTimeDiff);

for (const actionBinaryInterpolant of this
.actionBinaryInterpolantsArray) {
actionBinaryInterpolant.snapshot(remoteTimeDiff);
}
if(e.changes.keys.get('transform')) {
const transform = this.playerMap.get('transform');
if (transform) {
const remoteTimeDiff = transform[10];
lastPosition.copy(this.position);
this.position.fromArray(transform, 0);
if(this.avatar)
this.characterPhysics.setPosition(this.position);
this.quaternion.fromArray(transform, 3);

this.positionInterpolant?.snapshot(remoteTimeDiff);
this.quaternionInterpolant?.snapshot(remoteTimeDiff);

for (const actionBinaryInterpolant of this
.actionBinaryInterpolantsArray) {
actionBinaryInterpolant.snapshot(remoteTimeDiff);
}

if (this.avatar) {
this.avatar.setVelocity(
remoteTimeDiff / 1000,
lastPosition,
this.position,
this.quaternion
);
if (this.avatar) {
this.avatar.setVelocity(
remoteTimeDiff / 1000,
lastPosition,
this.position,
this.quaternion
);
}
}

//TODO: need to update wearupdate when transform
this.appManager.apps.forEach((app) => {
if(app.getComponent('wear') || app.getComponent('pet')) {
app.dispatchEvent({
type: 'wearupdate',
player: this,
app,
wear: true,
})
}
});
}
}
};

this.playerMap.observe(observePlayerFn);
Expand All @@ -1498,15 +1510,7 @@ class RemotePlayer extends InterpolatedPlayer {
this.appManager.loadApps();
this.appManager.callBackFn = (app, event, flag) => {
if (event == 'wear') {
if (flag == 'add') {
console.log("Calling wear on app")
app.dispatchEvent({
type: 'wearupdate',
player: this,
app,
wear: true,
})
} else if (flag == 'remove') {
if (flag == 'remove') {
console.log("Calling unwear on app")
this.unwear(app)
}
Expand Down
25 changes: 22 additions & 3 deletions metaverse_components/pet.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const localEuler = new THREE.Euler();
const localMatrix = new THREE.Matrix4();

export default (app, component) => {
const {useFrame, useCleanup, useLocalPlayer, useActivate} = metaversefile;
const {useFrame, useCleanup, useLocalPlayer, useRemotePlayers, useActivate} = metaversefile;

let petSpec = null;
let petMixer = null;
Expand Down Expand Up @@ -86,11 +86,30 @@ export default (app, component) => {
_unwear();
}
});



const _getCurrentPlayer = () => {
const localPlayer = useLocalPlayer();
let currentPlayer = localPlayer
const remotePlayers = useRemotePlayers();
const players = [localPlayer]
.concat(remotePlayers)
for (const player of players) {
const wearActionIndex = player.findActionIndex(action => {
return action.type === 'wear' && action.instanceId === app.instanceId;
});
if (wearActionIndex !== -1) {
currentPlayer = player
}
}
return currentPlayer
};

const smoothVelocity = new THREE.Vector3();
const lastLookQuaternion = new THREE.Quaternion();
const _getAppDistance = () => {
const localPlayer = useLocalPlayer();
const localPlayer = _getCurrentPlayer();
const position = localVector.copy(localPlayer.position);
position.y = 0;
const distance = app.position.distanceTo(position);
Expand Down Expand Up @@ -190,7 +209,7 @@ export default (app, component) => {
}

if (!bone.quaternion.equals(lastLookQuaternion)) {
const localPlayer = useLocalPlayer();
const localPlayer = _getCurrentPlayer();
const {position, quaternion} = localPlayer;
localQuaternion2.setFromRotationMatrix(
localMatrix.lookAt(
Expand Down

0 comments on commit 4d913cf

Please sign in to comment.