mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-05 03:00:46 +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;
|
defaultCancel = dC;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var resp;
|
||||||
|
|
||||||
await obj.emitThen(eventName + '_cancel', data, cancel).catch((err)=> setTimeout(() => {throw err;},0));
|
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));
|
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));
|
if (!hiddenCancelled && !cancelled) {
|
||||||
else if (cancelFunc && defaultCancel) await cancelFunc(data).catch((err)=> setTimeout(() => {throw err;},0));
|
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));
|
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
|
else return sample.slice(count); // Negative, returns from end
|
||||||
}
|
}
|
||||||
|
|
||||||
serv.selectorString = (str, pos, world) => {
|
serv.selectorString = (str, pos, world, allowUser=true) => {
|
||||||
pos = pos.clone();
|
pos = pos.clone();
|
||||||
var player = serv.getPlayer(str);
|
var player = serv.getPlayer(str);
|
||||||
if (!player && str[0] != '@') return null;
|
if (!player && str[0] != '@') return null;
|
||||||
|
else if (player) return allowUser ? [player] : null;
|
||||||
var match = str.match(/^@([a,r,p,e])(?:\[([^\]]+)\])?$/);
|
var match = str.match(/^@([a,r,p,e])(?:\[([^\]]+)\])?$/);
|
||||||
if (match == null) throw new UserError('Invalid selector format');
|
if (match == null) throw new UserError('Invalid selector format');
|
||||||
var typeConversion = {
|
var typeConversion = {
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,8 @@ module.exports.server=function(serv,options) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!entity.velocity || !entity.size) return;
|
if (!entity.velocity || !entity.size) return;
|
||||||
var oldPosAndOnGround = await entity.calculatePhysics(delta);
|
var posAndOnGround = await entity.calculatePhysics(delta);
|
||||||
if (!oldPosAndOnGround.oldPos.equals(new Vec3(0,0,0)))
|
if (entity.type == 'mob') entity.sendPosition(posAndOnGround);
|
||||||
if (entity.type == 'mob') entity.sendPosition(oldPosAndOnGround);
|
|
||||||
})
|
})
|
||||||
).catch((err)=> setTimeout(() => {throw err;},0));
|
).catch((err)=> setTimeout(() => {throw err;},0));
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -158,7 +158,7 @@ module.exports.player=function(player,serv)
|
||||||
sendLogin();
|
sendLogin();
|
||||||
await player.sendMap();
|
await player.sendMap();
|
||||||
player.sendSpawnPosition();
|
player.sendSpawnPosition();
|
||||||
player.sendPosition();
|
player.sendSelfPosition();
|
||||||
player.updateHealth(player.health);
|
player.updateHealth(player.health);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,14 +27,14 @@ module.exports.entity=function(entity){
|
||||||
entity.velocity.z = getFriction(entity.velocity.z, entity.friction.z, delta);
|
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);
|
newPos.x += getMoveAmount('x', xBlock, entity, delta, sizeSigned.x);
|
||||||
entity.position.y += getMoveAmount('y', yBlock, entity, delta, sizeSigned.y);
|
newPos.y += getMoveAmount('y', yBlock, entity, delta, sizeSigned.y);
|
||||||
entity.position.z += getMoveAmount('z', zBlock, entity, delta, sizeSigned.z);
|
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) });
|
//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,
|
gamemode:player.gameMode,
|
||||||
levelType:'default'
|
levelType:'default'
|
||||||
});
|
});
|
||||||
player.sendPosition();
|
player.sendSelfPosition();
|
||||||
player.updateHealth(20);
|
player.updateHealth(20);
|
||||||
player.nearbyEntities=[];
|
player.nearbyEntities=[];
|
||||||
player.updateAndSpawn();
|
player.updateAndSpawn();
|
||||||
|
|
|
||||||
|
|
@ -9,17 +9,14 @@ module.exports.player = (player, serv) => {
|
||||||
usage: '/teleport [target player] <destination player or x> [y] [z]',
|
usage: '/teleport [target player] <destination player or x> [y] [z]',
|
||||||
op: true,
|
op: true,
|
||||||
parse(str) {
|
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) {
|
action(args) {
|
||||||
if(args.length === 2 && args[0] !== args[1]) {
|
if(args.length === 2) {
|
||||||
let player_from;
|
let entities_from = serv.selectorString(args[0]);
|
||||||
let player_to;
|
let entity_to = serv.selectorString(args[1])[0];
|
||||||
|
|
||||||
if(!(player_from = serv.getPlayer(args[0])) || !(player_to = serv.getPlayer(args[1])))
|
entities_from.forEach(e => e.teleport(entity_to.position.scaled(1/32)));
|
||||||
return false;
|
|
||||||
|
|
||||||
player_from.teleport(player_to.position.clone());
|
|
||||||
} else if(args.length === 3) {
|
} else if(args.length === 3) {
|
||||||
let x = serv.posFromString(args[0], player.position.x / 32);
|
let x = serv.posFromString(args[0], player.position.x / 32);
|
||||||
let y = serv.posFromString(args[1], player.position.y / 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));
|
player.teleport(new Vec3(x, y, z));
|
||||||
} else if(args.length === 4) {
|
} else if(args.length === 4) {
|
||||||
let player_from;
|
let entities_from = serv.selectorString(args[0]);
|
||||||
|
|
||||||
if(!(player_from = serv.getPlayer(args[0])))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
let x = serv.posFromString(args[1], player_from.x / 32);
|
let x = serv.posFromString(args[1], player_from.x / 32);
|
||||||
let y = serv.posFromString(args[2], player_from.y / 32);
|
let y = serv.posFromString(args[2], player_from.y / 32);
|
||||||
let z = serv.posFromString(args[3], player_from.z / 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
|
headYaw: convYaw
|
||||||
});
|
});
|
||||||
}, () => {
|
}, () => {
|
||||||
player.sendPosition();
|
player.sendSelfPosition();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
player._client.on('position', ({x,y,z,onGround} = {}) =>
|
player._client.on('position', ({x,y,z,onGround} = {}) => {
|
||||||
player.sendRelativePositionChange((new Vec3(x, y, z)).toFixedPosition(), 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._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);
|
sendLook(yaw,pitch,onGround);
|
||||||
});
|
});
|
||||||
|
|
||||||
player.sendRelativePositionChange = (newPosition, onGround) => {
|
player.sendSelfPosition = () => {
|
||||||
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._client.write('position', {
|
player._client.write('position', {
|
||||||
x: player.position.x/32,
|
x: player.position.x/32,
|
||||||
y: player.position.y/32,
|
y: player.position.y/32,
|
||||||
|
|
@ -99,25 +65,25 @@ module.exports.player=function(player)
|
||||||
};
|
};
|
||||||
|
|
||||||
player.teleport = async (position) => {
|
player.teleport = async (position) => {
|
||||||
await player.sendRelativePositionChange(position.scaled(32).floored(), false);
|
var notCancelled = await player.sendSelfPosition(position.scaled(32).floored(), false, true);
|
||||||
player.sendPosition();
|
if (notCancelled) player.sendSelfPosition();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.entity=function(entity,serv){
|
module.exports.entity=function(entity,serv){
|
||||||
entity.sendPosition = ({oldPos,onGround}) => {
|
entity.sendPosition = (position, onGround, teleport=false) => {
|
||||||
entity.behavior('move', {
|
if (entity.position.equals(position) && entity.onGround == onGround) return Promise.resolve();
|
||||||
old: oldPos,
|
return entity.behavior(teleport ? 'teleport' : 'move', {
|
||||||
|
position: position,
|
||||||
onGround: onGround
|
onGround: onGround
|
||||||
}, ({old,onGround}) => {
|
}, ({position,onGround}) => {
|
||||||
var diff = entity.position.minus(old);
|
var diff = position.minus(entity.position);
|
||||||
|
|
||||||
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
||||||
entity._writeOthersNearby('entity_teleport', {
|
entity._writeOthersNearby('entity_teleport', {
|
||||||
entityId: entity.id,
|
entityId: entity.id,
|
||||||
x: entity.position.x,
|
x: position.x,
|
||||||
y: entity.position.y,
|
y: position.y,
|
||||||
z: entity.position.z,
|
z: position.z,
|
||||||
yaw: entity.yaw,
|
yaw: entity.yaw,
|
||||||
pitch: entity.pitch,
|
pitch: entity.pitch,
|
||||||
onGround: onGround
|
onGround: onGround
|
||||||
|
|
@ -129,13 +95,14 @@ module.exports.entity=function(entity,serv){
|
||||||
dZ: diff.z,
|
dZ: diff.z,
|
||||||
onGround: onGround
|
onGround: onGround
|
||||||
}, entity);
|
}, entity);
|
||||||
|
|
||||||
|
entity.position = position;
|
||||||
|
entity.onGround = onGround;
|
||||||
}, () => {
|
}, () => {
|
||||||
entity.position = oldPos;
|
if (entity.type == 'player') player.sendSelfPosition();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
entity.sendVelocity = (vel, maxVel) => {
|
entity.sendVelocity = (vel, maxVel) => {
|
||||||
var velocity = vel.scaled(32).floored(); // Make fixed point
|
var velocity = vel.scaled(32).floored(); // Make fixed point
|
||||||
var maxVelocity = maxVel.scaled(32).floored();
|
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) {
|
function addVelocityWithMax(current, newVel, max) {
|
||||||
var x, y, z;
|
var x, y, z;
|
||||||
if (current.x > max.x || current.x < -max.x) x = current.x;
|
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();
|
await player.sendMap();
|
||||||
|
|
||||||
player.sendPosition();
|
player.sendSelfPosition();
|
||||||
player.emit('change_world');
|
player.emit('change_world');
|
||||||
|
|
||||||
await player.waitPlayerLogin();
|
await player.waitPlayerLogin();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue