mirror of
https://github.com/danbulant/flying-squid
synced 2026-06-14 20:11:03 +00:00
Big changes to player/entity.sendPosition and entity/player.teleport. Not fully working yet.
This commit is contained in:
parent
67af4a8144
commit
fe2f220f10
9 changed files with 55 additions and 83 deletions
|
|
@ -13,13 +13,20 @@ module.exports = (obj) => {
|
|||
defaultCancel = dC;
|
||||
};
|
||||
|
||||
var resp;
|
||||
|
||||
await obj.emitThen(eventName + '_cancel', data, cancel).catch((err)=> setTimeout(() => {throw err;},0));
|
||||
await obj.emitThen(eventName, data, cancelled, cancelCount).catch((err)=> setTimeout(() => {throw err;},0));
|
||||
|
||||
if (!hiddenCancelled && !cancelled) await func(data).catch((err)=> setTimeout(() => {throw err;},0));
|
||||
else if (cancelFunc && defaultCancel) await cancelFunc(data).catch((err)=> setTimeout(() => {throw err;},0));
|
||||
if (!hiddenCancelled && !cancelled) {
|
||||
resp = await func(data).catch((err)=> setTimeout(() => {throw err;},0));
|
||||
if (typeof resp == 'undefined') resp = true;
|
||||
} else if (cancelFunc && defaultCancel) {
|
||||
resp = await cancelFunc(data).catch((err)=> setTimeout(() => {throw err;},0));
|
||||
if (typeof resp == 'undefined') resp = false;
|
||||
}
|
||||
|
||||
await obj.emitThen(eventName + '_done', data, cancelled).catch((err)=> setTimeout(() => {throw err;},0));
|
||||
return data;
|
||||
return resp;
|
||||
}
|
||||
};
|
||||
|
|
@ -212,10 +212,11 @@ module.exports.server = function(serv) {
|
|||
else return sample.slice(count); // Negative, returns from end
|
||||
}
|
||||
|
||||
serv.selectorString = (str, pos, world) => {
|
||||
serv.selectorString = (str, pos, world, allowUser=true) => {
|
||||
pos = pos.clone();
|
||||
var player = serv.getPlayer(str);
|
||||
if (!player && str[0] != '@') return null;
|
||||
else if (player) return allowUser ? [player] : null;
|
||||
var match = str.match(/^@([a,r,p,e])(?:\[([^\]]+)\])?$/);
|
||||
if (match == null) throw new UserError('Invalid selector format');
|
||||
var typeConversion = {
|
||||
|
|
|
|||
|
|
@ -19,9 +19,8 @@ module.exports.server=function(serv,options) {
|
|||
}
|
||||
}
|
||||
if (!entity.velocity || !entity.size) return;
|
||||
var oldPosAndOnGround = await entity.calculatePhysics(delta);
|
||||
if (!oldPosAndOnGround.oldPos.equals(new Vec3(0,0,0)))
|
||||
if (entity.type == 'mob') entity.sendPosition(oldPosAndOnGround);
|
||||
var posAndOnGround = await entity.calculatePhysics(delta);
|
||||
if (entity.type == 'mob') entity.sendPosition(posAndOnGround);
|
||||
})
|
||||
).catch((err)=> setTimeout(() => {throw err;},0));
|
||||
});
|
||||
|
|
|
|||
|
|
@ -158,7 +158,7 @@ module.exports.player=function(player,serv)
|
|||
sendLogin();
|
||||
await player.sendMap();
|
||||
player.sendSpawnPosition();
|
||||
player.sendPosition();
|
||||
player.sendSelfPosition();
|
||||
player.updateHealth(player.health);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -27,14 +27,14 @@ module.exports.entity=function(entity){
|
|||
entity.velocity.z = getFriction(entity.velocity.z, entity.friction.z, delta);
|
||||
}
|
||||
|
||||
var oldPos = entity.position.clone();
|
||||
var newPos = entity.position.clone();
|
||||
|
||||
entity.position.x += getMoveAmount('x', xBlock, entity, delta, sizeSigned.x);
|
||||
entity.position.y += getMoveAmount('y', yBlock, entity, delta, sizeSigned.y);
|
||||
entity.position.z += getMoveAmount('z', zBlock, entity, delta, sizeSigned.z);
|
||||
newPos.x += getMoveAmount('x', xBlock, entity, delta, sizeSigned.x);
|
||||
newPos.y += getMoveAmount('y', yBlock, entity, delta, sizeSigned.y);
|
||||
newPos.z += getMoveAmount('z', zBlock, entity, delta, sizeSigned.z);
|
||||
|
||||
//serv.emitParticle(30, serv.overworld, entity.position.scaled(1/32), { size: new Vec3(0, 0, 0) });
|
||||
return { oldPos: oldPos, onGround: yBlock}
|
||||
return { position: newPos, onGround: yBlock}
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ module.exports.player=function(player)
|
|||
gamemode:player.gameMode,
|
||||
levelType:'default'
|
||||
});
|
||||
player.sendPosition();
|
||||
player.sendSelfPosition();
|
||||
player.updateHealth(20);
|
||||
player.nearbyEntities=[];
|
||||
player.updateAndSpawn();
|
||||
|
|
|
|||
|
|
@ -9,17 +9,14 @@ module.exports.player = (player, serv) => {
|
|||
usage: '/teleport [target player] <destination player or x> [y] [z]',
|
||||
op: true,
|
||||
parse(str) {
|
||||
return str.match(/^(((\w* )?~?-?\d* ~?-?\d* ~?-?\d*)|(\w* \w*))$/) ? str.split(' ') : false;
|
||||
return str.match(/^(((.* )?~?-?\d* ~?-?\d* ~?-?\d*)|(.+ .+))$/) ? str.split(' ') : false;
|
||||
},
|
||||
action(args) {
|
||||
if(args.length === 2 && args[0] !== args[1]) {
|
||||
let player_from;
|
||||
let player_to;
|
||||
if(args.length === 2) {
|
||||
let entities_from = serv.selectorString(args[0]);
|
||||
let entity_to = serv.selectorString(args[1])[0];
|
||||
|
||||
if(!(player_from = serv.getPlayer(args[0])) || !(player_to = serv.getPlayer(args[1])))
|
||||
return false;
|
||||
|
||||
player_from.teleport(player_to.position.clone());
|
||||
entities_from.forEach(e => e.teleport(entity_to.position.scaled(1/32)));
|
||||
} else if(args.length === 3) {
|
||||
let x = serv.posFromString(args[0], player.position.x / 32);
|
||||
let y = serv.posFromString(args[1], player.position.y / 32);
|
||||
|
|
@ -27,16 +24,13 @@ module.exports.player = (player, serv) => {
|
|||
|
||||
player.teleport(new Vec3(x, y, z));
|
||||
} else if(args.length === 4) {
|
||||
let player_from;
|
||||
|
||||
if(!(player_from = serv.getPlayer(args[0])))
|
||||
return false;
|
||||
let entities_from = serv.selectorString(args[0]);
|
||||
|
||||
let x = serv.posFromString(args[1], player_from.x / 32);
|
||||
let y = serv.posFromString(args[2], player_from.y / 32);
|
||||
let z = serv.posFromString(args[3], player_from.z / 32);
|
||||
|
||||
player_from.teleport(new Vec3(x, y, z));
|
||||
entities_from.forEach(e => e.teleport(new Vec3(x, y, z)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -39,55 +39,21 @@ module.exports.player=function(player)
|
|||
headYaw: convYaw
|
||||
});
|
||||
}, () => {
|
||||
player.sendPosition();
|
||||
player.sendSelfPosition();
|
||||
});
|
||||
}
|
||||
|
||||
player._client.on('position', ({x,y,z,onGround} = {}) =>
|
||||
player.sendRelativePositionChange((new Vec3(x, y, z)).toFixedPosition(), onGround));
|
||||
player._client.on('position', ({x,y,z,onGround} = {}) => {
|
||||
console.log(x,y,z);
|
||||
player.sendPosition((new Vec3(x, y, z)).toFixedPosition(), onGround);
|
||||
});
|
||||
|
||||
player._client.on('position_look', ({x,y,z,onGround,yaw,pitch} = {}) => {
|
||||
player.sendRelativePositionChange((new Vec3(x, y, z)).toFixedPosition(), onGround);
|
||||
player.sendPosition((new Vec3(x, y, z)).toFixedPosition(), onGround);
|
||||
sendLook(yaw,pitch,onGround);
|
||||
});
|
||||
|
||||
player.sendRelativePositionChange = (newPosition, onGround) => {
|
||||
return player.behavior('move', {
|
||||
onGround: onGround,
|
||||
position: newPosition
|
||||
}, async ({onGround, position}) => {
|
||||
if (player.position.distanceTo(new Vec3(0, 0, 0)) != 0) {
|
||||
var diff = position.minus(player.position);
|
||||
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
||||
{
|
||||
player._writeOthersNearby('entity_teleport', {
|
||||
entityId:player.id,
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
z: position.z,
|
||||
yaw: player.yaw,
|
||||
pitch: player.pitch,
|
||||
onGround: onGround
|
||||
});
|
||||
}
|
||||
else if (diff.distanceTo(new Vec3(0, 0, 0)) != 0) {
|
||||
player._writeOthersNearby('rel_entity_move', {
|
||||
entityId: player.id,
|
||||
dX: diff.x,
|
||||
dY: diff.y,
|
||||
dZ: diff.z,
|
||||
onGround: onGround
|
||||
});
|
||||
}
|
||||
}
|
||||
player.position = position;
|
||||
player.onGround = onGround;
|
||||
}, () => {
|
||||
player.sendPosition();
|
||||
});
|
||||
};
|
||||
|
||||
player.sendPosition = () => {
|
||||
player.sendSelfPosition = () => {
|
||||
player._client.write('position', {
|
||||
x: player.position.x/32,
|
||||
y: player.position.y/32,
|
||||
|
|
@ -99,25 +65,25 @@ module.exports.player=function(player)
|
|||
};
|
||||
|
||||
player.teleport = async (position) => {
|
||||
await player.sendRelativePositionChange(position.scaled(32).floored(), false);
|
||||
player.sendPosition();
|
||||
var notCancelled = await player.sendSelfPosition(position.scaled(32).floored(), false, true);
|
||||
if (notCancelled) player.sendSelfPosition();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.entity=function(entity,serv){
|
||||
entity.sendPosition = ({oldPos,onGround}) => {
|
||||
entity.behavior('move', {
|
||||
old: oldPos,
|
||||
entity.sendPosition = (position, onGround, teleport=false) => {
|
||||
if (entity.position.equals(position) && entity.onGround == onGround) return Promise.resolve();
|
||||
return entity.behavior(teleport ? 'teleport' : 'move', {
|
||||
position: position,
|
||||
onGround: onGround
|
||||
}, ({old,onGround}) => {
|
||||
var diff = entity.position.minus(old);
|
||||
|
||||
}, ({position,onGround}) => {
|
||||
var diff = position.minus(entity.position);
|
||||
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
||||
entity._writeOthersNearby('entity_teleport', {
|
||||
entityId: entity.id,
|
||||
x: entity.position.x,
|
||||
y: entity.position.y,
|
||||
z: entity.position.z,
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
z: position.z,
|
||||
yaw: entity.yaw,
|
||||
pitch: entity.pitch,
|
||||
onGround: onGround
|
||||
|
|
@ -129,13 +95,14 @@ module.exports.entity=function(entity,serv){
|
|||
dZ: diff.z,
|
||||
onGround: onGround
|
||||
}, entity);
|
||||
|
||||
entity.position = position;
|
||||
entity.onGround = onGround;
|
||||
}, () => {
|
||||
entity.position = oldPos;
|
||||
if (entity.type == 'player') player.sendSelfPosition();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
entity.sendVelocity = (vel, maxVel) => {
|
||||
var velocity = vel.scaled(32).floored(); // Make fixed point
|
||||
var maxVelocity = maxVel.scaled(32).floored();
|
||||
|
|
@ -152,6 +119,10 @@ module.exports.entity=function(entity,serv){
|
|||
}
|
||||
};
|
||||
|
||||
entity.teleport = (pos) => { // Overwritten in players inject above
|
||||
entity.sendPosition(entity.position, false, true);
|
||||
}
|
||||
|
||||
function addVelocityWithMax(current, newVel, max) {
|
||||
var x, y, z;
|
||||
if (current.x > max.x || current.x < -max.x) x = current.x;
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ module.exports.player=function(player,serv,settings) {
|
|||
|
||||
await player.sendMap();
|
||||
|
||||
player.sendPosition();
|
||||
player.sendSelfPosition();
|
||||
player.emit('change_world');
|
||||
|
||||
await player.waitPlayerLogin();
|
||||
|
|
|
|||
Loading…
Reference in a new issue