mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-06 11:40:46 +00:00
Adding behaviors
This commit is contained in:
parent
00244db976
commit
3ba46f0e97
12 changed files with 159 additions and 89 deletions
|
|
@ -1,20 +1,23 @@
|
||||||
module.exports = (obj) => {
|
module.exports = (obj) => {
|
||||||
return async (eventName, data, func, opt) => {
|
return async (eventName, data, func, cancelFunc) => {
|
||||||
var hiddenCancelled = false;
|
var hiddenCancelled = false;
|
||||||
var cancelled = false;
|
var cancelled = false;
|
||||||
var cancelCount = 0;
|
var cancelCount = 0;
|
||||||
var cancel = (hidden) => { // Hidden shouldn't be used often but it's not hard to implement so meh
|
var defaultCancel = true;
|
||||||
|
var cancel = (dC=true, hidden=false) => { // Hidden shouldn't be used often but it's not hard to implement so meh
|
||||||
if (hidden) hiddenCancelled = true;
|
if (hidden) hiddenCancelled = true;
|
||||||
else {
|
else {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
cancelCount++;
|
cancelCount++;
|
||||||
}
|
}
|
||||||
|
defaultCancel = dC;
|
||||||
}
|
}
|
||||||
|
|
||||||
await obj.emit(eventName + '_cancel', data, cancel);
|
await obj.emit(eventName + '_cancel', data, cancel);
|
||||||
await obj.emit(eventName, data, cancelled, cancelCount);
|
await obj.emit(eventName, data, cancelled, cancelCount);
|
||||||
|
|
||||||
if (!hiddenCancelled && !cancelled) await func(data);
|
if (!hiddenCancelled && !cancelled) await func(data);
|
||||||
|
else if (cancelFunc && defaultCancel) cancelFunc(data);
|
||||||
|
|
||||||
await obj.emit(eventName + '_done', data, cancelled);
|
await obj.emit(eventName + '_done', data, cancelled);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,14 @@
|
||||||
module.exports.player=function(player)
|
module.exports.player=function(player)
|
||||||
{
|
{
|
||||||
player._client.on("arm_animation", () =>
|
player._client.on("arm_animation", () =>
|
||||||
player._writeOthersNearby("animation", {
|
player.behavior('punch', {}, () => {
|
||||||
entityId: player.id,
|
player._writeOthersNearby("animation", {
|
||||||
animation: 0
|
entityId: player.id,
|
||||||
}));
|
animation: 0
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
player._client.on("entity_action", ({actionId} = {}) => {
|
player._client.on("entity_action", ({actionId} = {}) => {
|
||||||
if(actionId == 3) {
|
if(actionId == 3) {
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,17 @@ module.exports.player=function(player,serv)
|
||||||
};
|
};
|
||||||
|
|
||||||
player.sendBlock = (position, blockType, blockData) => // Call from player.setBlock unless you want "local" fake blocks
|
player.sendBlock = (position, blockType, blockData) => // Call from player.setBlock unless you want "local" fake blocks
|
||||||
player._client.write("block_change",{
|
player.behavior('sendBlock', {
|
||||||
|
position: position,
|
||||||
|
id: blockType,
|
||||||
|
damage: blockData
|
||||||
|
}, ({position, id, damage}) => {
|
||||||
|
player._client.write("block_change",{
|
||||||
location:position,
|
location:position,
|
||||||
type:blockType<<4 | blockData
|
type:blockType<<4 | blockData
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
player.setBlock = (position,blockType,blockData) => serv.setBlock(player.world,position,blockType,blockData);
|
player.setBlock = (position,blockType,blockData) => serv.setBlock(player.world,position,blockType,blockData);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,21 @@ module.exports.player=function(player,serv)
|
||||||
{
|
{
|
||||||
player._client.on('chat', ({message} = {}) => {
|
player._client.on('chat', ({message} = {}) => {
|
||||||
if(message[0]=="/") {
|
if(message[0]=="/") {
|
||||||
var command = message.slice(1);
|
player.behavior('command', {
|
||||||
player.handleCommand(command);
|
message: message
|
||||||
|
}, ({message}) => {
|
||||||
|
var command = message.slice(1);
|
||||||
|
player.handleCommand(command);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
serv.broadcast('<' + player.username + '>' + ' ' + message);
|
player.behavior('chat', {
|
||||||
player.emit("chat",message);
|
message: message,
|
||||||
|
broadcastMessage: '<' + player.username + '>' + ' ' + message,
|
||||||
|
broadcast: true
|
||||||
|
}, ({message, broadcast, broadcastMessage}) => {
|
||||||
|
if (broadcast) serv.broadcast(broadcastMessage);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,12 @@ module.exports.server=function(serv) {
|
||||||
serv.on('tick', (delta,count) => {
|
serv.on('tick', (delta,count) => {
|
||||||
if (!serv.doDaylightCycle) return;
|
if (!serv.doDaylightCycle) return;
|
||||||
if (count % 20 == 0) {
|
if (count % 20 == 0) {
|
||||||
serv.setTime((serv.time + 20) % 24000); // Vanilla only does it every second
|
serv.behavior('changeTime', {
|
||||||
|
old: serv.time,
|
||||||
|
newTime: serv.time + 20
|
||||||
|
}, ({newTime}) => {
|
||||||
|
serv.setTime((serv.time + 20) % 24000); // Vanilla only does it every second
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,11 @@ var Vec3 = require("vec3").Vec3;
|
||||||
|
|
||||||
module.exports.player=function(player,serv)
|
module.exports.player=function(player,serv)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
function cancelDig({position, block}) {
|
||||||
|
player.sendBlock(position, block.id, block.data);
|
||||||
|
}
|
||||||
|
|
||||||
player._client.on("block_dig",({location,status} = {}) => {
|
player._client.on("block_dig",({location,status} = {}) => {
|
||||||
var pos=new Vec3(location.x,location.y,location.z);
|
var pos=new Vec3(location.x,location.y,location.z);
|
||||||
player.world.getBlock(pos)
|
player.world.getBlock(pos)
|
||||||
|
|
@ -19,31 +24,30 @@ module.exports.player=function(player,serv)
|
||||||
block: block
|
block: block
|
||||||
}, ({position}) => {
|
}, ({position}) => {
|
||||||
return startDigging(position);
|
return startDigging(position);
|
||||||
});
|
}, cancelDig);
|
||||||
else if(status==2)
|
else if(status==2)
|
||||||
player.behavior('finishDig', { // Finish dig survival
|
player.behavior('finishDig', { // Finish dig survival
|
||||||
position: position,
|
position: position,
|
||||||
block: block
|
block: block
|
||||||
}, ({position}) => {
|
}, ({position}) => {
|
||||||
return completeDigging(position);
|
return completeDigging(position);
|
||||||
});
|
}, cancelDig);
|
||||||
else if(status==1)
|
else if(status==1)
|
||||||
player.behavior('cancelDig', { // Cancel dig survival
|
player.behavior('cancelDig', { // Cancel dig survival
|
||||||
position: position,
|
position: position,
|
||||||
block: block
|
block: block
|
||||||
}, ({position}) => {
|
}, ({position}) => {
|
||||||
return cancelDigging(position);
|
return cancelDigging(position);
|
||||||
});
|
}, cancelDig);
|
||||||
else if(status==0 && player.gameMode==1)
|
else if(status==0 && player.gameMode==1)
|
||||||
player.behavior('dig', { // Start/finish dig creative
|
player.behavior('dig', { // Start/finish dig creative
|
||||||
position: position,
|
position: position,
|
||||||
block: block
|
block: block
|
||||||
}, ({position}) => {
|
}, ({position}) => {
|
||||||
return creativeDigging(position);
|
return creativeDigging(position);
|
||||||
});
|
}, cancelDig);
|
||||||
}
|
}, cancelDig)
|
||||||
)}
|
})
|
||||||
)
|
|
||||||
.catch((err)=> setTimeout(() => {throw err;},0))
|
.catch((err)=> setTimeout(() => {throw err;},0))
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,8 @@ module.exports.player=function(player,serv)
|
||||||
position: placedPosition,
|
position: placedPosition,
|
||||||
reference: referencePosition,
|
reference: referencePosition,
|
||||||
playSound: true,
|
playSound: true,
|
||||||
sound: 'dig.' + (materialToSound[blocks[heldItem.blockId].material] || 'stone')
|
sound: 'dig.' + (materialToSound[blocks[heldItem.blockId].material] || 'stone'),
|
||||||
|
world: player.world
|
||||||
}, ({direction, heldItem, position, reference, playSound, sound}) => {
|
}, ({direction, heldItem, position, reference, playSound, sound}) => {
|
||||||
if (playSound) {
|
if (playSound) {
|
||||||
serv.playSound(sound, player.world, placedPosition.clone().add(new Vec3(0.5, 0.5, 0.5)), {
|
serv.playSound(sound, player.world, placedPosition.clone().add(new Vec3(0.5, 0.5, 0.5)), {
|
||||||
|
|
@ -46,6 +47,10 @@ module.exports.player=function(player,serv)
|
||||||
location:position
|
location:position
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}, async () => {
|
||||||
|
var id = await player.world.getBlockType(placedPosition);
|
||||||
|
var damage = await player.world.getBlockData(placedPosition);
|
||||||
|
player.sendBlock(placedPosition, id, damage);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -17,22 +17,31 @@ module.exports.player=function(player,serv)
|
||||||
var attackedEntity = serv.entities[entityId];
|
var attackedEntity = serv.entities[entityId];
|
||||||
if(!attackedEntity || (attackedEntity.gameMode != 0 && attackedEntity.type == 'player')) return;
|
if(!attackedEntity || (attackedEntity.gameMode != 0 && attackedEntity.type == 'player')) return;
|
||||||
|
|
||||||
attackedEntity.updateHealth(attackedEntity.health - 1);
|
player.behavior('attack', {
|
||||||
serv.playSound('game.player.hurt', player.world, attackedEntity.position.scaled(1/32));
|
attackedEntity: attackedEntity,
|
||||||
|
sound: 'game.player.hurt',
|
||||||
|
playSound: true,
|
||||||
|
damage: 1,
|
||||||
|
velocity: attackedEntity.position.minus(player.position).plus(new Vec3(0, 0.5, 0)).scaled(5),
|
||||||
|
maxVelocity: new Vec3(4, 4, 4),
|
||||||
|
animation: true
|
||||||
|
}, ({entity, sound, playSound, damage, velocity, maxVelocity, animation}) => {
|
||||||
|
attackedEntity.updateHealth(attackedEntity.health - dealDamage);
|
||||||
|
serv.playSound(sound, player.world, attackedEntity.position.scaled(1/32));
|
||||||
|
|
||||||
var attackVelocity = attackedEntity.position.minus(player.position).plus(new Vec3(0, 0.5, 0)).scaled(5/32);
|
attackedEntity.sendVelocity(velocity.scaled(1/32), maxVelocity);
|
||||||
attackedEntity.sendVelocity(attackVelocity, new Vec3(4, 4, 4));
|
|
||||||
|
|
||||||
if(attackedEntity.health<=0)
|
if(attackedEntity.health<=0 && animation)
|
||||||
attackedEntity._writeOthers('entity_status',{
|
attackedEntity._writeOthers('entity_status',{
|
||||||
|
entityId:attackedEntity.id,
|
||||||
|
entityStatus:3
|
||||||
|
});
|
||||||
|
else if (animation)
|
||||||
|
attackedEntity._writeOthers('animation',{
|
||||||
entityId:attackedEntity.id,
|
entityId:attackedEntity.id,
|
||||||
entityStatus:3
|
animation:1
|
||||||
});
|
});
|
||||||
else
|
});
|
||||||
attackedEntity._writeOthers('animation',{
|
|
||||||
entityId:attackedEntity.id,
|
|
||||||
animation:1
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
player._client.on("use_entity", ({mouse,target} = {}) => {
|
player._client.on("use_entity", ({mouse,target} = {}) => {
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,18 @@ module.exports.player=function(player)
|
||||||
{
|
{
|
||||||
player._client.on("client_command", ({payload}) => {
|
player._client.on("client_command", ({payload}) => {
|
||||||
if(payload == 0) {
|
if(payload == 0) {
|
||||||
player._client.write("respawn",{
|
player.behavior('requestRespawn', {}, () => {
|
||||||
dimension:0,
|
player._client.write("respawn",{
|
||||||
difficulty:0,
|
dimension:0,
|
||||||
gamemode:player.gameMode,
|
difficulty:0,
|
||||||
levelType:'default'
|
gamemode:player.gameMode,
|
||||||
|
levelType:'default'
|
||||||
|
});
|
||||||
|
player.sendPosition();
|
||||||
|
player.updateHealth(20);
|
||||||
|
player.nearbyEntities=[];
|
||||||
|
player.updateAndSpawn();
|
||||||
});
|
});
|
||||||
player.sendPosition();
|
|
||||||
player.updateHealth(20);
|
|
||||||
player.nearbyEntities=[];
|
|
||||||
player.updateAndSpawn();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
@ -40,11 +40,12 @@ module.exports.player=function(player,serv) {
|
||||||
serv.playSound(sound, player.world, null, opt);
|
serv.playSound(sound, player.world, null, opt);
|
||||||
};
|
};
|
||||||
|
|
||||||
player._client.on('block_place', ({location}={}) => {
|
player._client.on('placeBlock_cancel', ({location}={}, cancel) => {
|
||||||
if (player.crouching) return;
|
if (player.crouching) return;
|
||||||
var pos=new Vec3(location.x,location.y,location.z);
|
var pos=new Vec3(location.x,location.y,location.z);
|
||||||
player.world.getBlockType(pos).then((id) => {
|
player.world.getBlockType(pos).then((id) => {
|
||||||
if (id != 25) return;
|
if (id != 25) return;
|
||||||
|
cancel();
|
||||||
if (!player.world.blockEntityData[pos.toString()]) player.world.blockEntityData[pos.toString()] = {};
|
if (!player.world.blockEntityData[pos.toString()]) player.world.blockEntityData[pos.toString()] = {};
|
||||||
var data = player.world.blockEntityData[pos.toString()];
|
var data = player.world.blockEntityData[pos.toString()];
|
||||||
if (typeof data.note == 'undefined') data.note = -1;
|
if (typeof data.note == 'undefined') data.note = -1;
|
||||||
|
|
@ -54,11 +55,12 @@ module.exports.player=function(player,serv) {
|
||||||
}).catch((err)=> setTimeout(() => {throw err;},0));
|
}).catch((err)=> setTimeout(() => {throw err;},0));
|
||||||
});
|
});
|
||||||
|
|
||||||
player._client.on('block_dig', ({location,status} = {}) => {
|
player._client.on('dig_cancel', ({location,status} = {}, cancel) => {
|
||||||
if (status != 0 || player.gameMode == 1) return;
|
if (status != 0 || player.gameMode == 1) return;
|
||||||
var pos=new Vec3(location.x,location.y,location.z);
|
var pos=new Vec3(location.x,location.y,location.z);
|
||||||
player.world.getBlockType(pos).then((id) => {
|
player.world.getBlockType(pos).then((id) => {
|
||||||
if (id != 25) return;
|
if (id != 25) return;
|
||||||
|
cancel();
|
||||||
if (!player.world.blockEntityData[pos.toString()]) player.world.blockEntityData[pos.toString()] = {};
|
if (!player.world.blockEntityData[pos.toString()]) player.world.blockEntityData[pos.toString()] = {};
|
||||||
var data = player.world.blockEntityData[pos.toString()];
|
var data = player.world.blockEntityData[pos.toString()];
|
||||||
if (typeof data.note == 'undefined') data.note = 0;
|
if (typeof data.note == 'undefined') data.note = 0;
|
||||||
|
|
|
||||||
|
|
@ -17,21 +17,29 @@ module.exports.player=function(player)
|
||||||
}
|
}
|
||||||
function sendLook(yaw,pitch,onGround)
|
function sendLook(yaw,pitch,onGround)
|
||||||
{
|
{
|
||||||
var convYaw=conv(yaw);
|
player.behavior('look', {
|
||||||
var convPitch=conv(pitch);
|
yaw: yaw,
|
||||||
if (convYaw == player.yaw && convPitch == player.pitch) return;
|
pitch: pitch,
|
||||||
player._writeOthersNearby("entity_look", {
|
|
||||||
entityId: player.id,
|
|
||||||
yaw: convYaw,
|
|
||||||
pitch: convPitch,
|
|
||||||
onGround: onGround
|
onGround: onGround
|
||||||
});
|
}, ({yaw, pitch, onGround}) => {
|
||||||
player.yaw = convYaw;
|
var convYaw=conv(yaw);
|
||||||
player.pitch = convPitch;
|
var convPitch=conv(pitch);
|
||||||
player.onGround = onGround;
|
if (convYaw == player.yaw && convPitch == player.pitch) return;
|
||||||
player._writeOthersNearby("entity_head_rotation", {
|
player._writeOthersNearby("entity_look", {
|
||||||
entityId: player.id,
|
entityId: player.id,
|
||||||
headYaw: convYaw
|
yaw: convYaw,
|
||||||
|
pitch: convPitch,
|
||||||
|
onGround: onGround
|
||||||
|
});
|
||||||
|
player.yaw = convYaw;
|
||||||
|
player.pitch = convPitch;
|
||||||
|
player.onGround = onGround;
|
||||||
|
player._writeOthersNearby("entity_head_rotation", {
|
||||||
|
entityId: player.id,
|
||||||
|
headYaw: convYaw
|
||||||
|
});
|
||||||
|
}, () => {
|
||||||
|
player.sendPosition();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,33 +52,39 @@ module.exports.player=function(player)
|
||||||
});
|
});
|
||||||
|
|
||||||
function sendRelativePositionChange(newPosition, onGround) {
|
function sendRelativePositionChange(newPosition, onGround) {
|
||||||
if (player.position.distanceTo(new Vec3(0, 0, 0)) != 0) {
|
player.behavior('move', {
|
||||||
var diff = newPosition.minus(player.position);
|
onGround: onGround,
|
||||||
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
position: newPosition
|
||||||
{
|
}, ({onGround, position}) => {
|
||||||
player._writeOthersNearby('entity_teleport', {
|
if (player.position.distanceTo(new Vec3(0, 0, 0)) != 0) {
|
||||||
entityId:player.id,
|
var diff = newPosition.minus(player.position);
|
||||||
x: newPosition.x,
|
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
||||||
y: newPosition.y,
|
{
|
||||||
z: newPosition.z,
|
player._writeOthersNearby('entity_teleport', {
|
||||||
yaw: player.yaw,
|
entityId:player.id,
|
||||||
pitch: player.pitch,
|
x: newPosition.x,
|
||||||
onGround: onGround
|
y: newPosition.y,
|
||||||
});
|
z: newPosition.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
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (diff.distanceTo(new Vec3(0, 0, 0)) != 0) {
|
player.position = newPosition;
|
||||||
player._writeOthersNearby('rel_entity_move', {
|
player.onGround = onGround;
|
||||||
entityId: player.id,
|
}, () => {
|
||||||
dX: diff.x,
|
player.sendPosition();
|
||||||
dY: diff.y,
|
});
|
||||||
dZ: diff.z,
|
|
||||||
onGround: onGround
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
player.position = newPosition;
|
|
||||||
player.onGround = onGround;
|
|
||||||
player.emit("positionChanged");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
player.sendPosition = () => {
|
player.sendPosition = () => {
|
||||||
|
|
|
||||||
|
|
@ -70,14 +70,20 @@ module.exports.player=function(player,serv) {
|
||||||
|
|
||||||
player.sendChunk = (chunkX,chunkZ,column) =>
|
player.sendChunk = (chunkX,chunkZ,column) =>
|
||||||
{
|
{
|
||||||
player._client.write('map_chunk', {
|
return player.behavior('sendChunk', {
|
||||||
x: chunkX,
|
x: chunkX,
|
||||||
z: chunkZ,
|
z: chunkZ,
|
||||||
groundUp: true,
|
chunk: column
|
||||||
bitMap: 0xffff,
|
}, ({x, z, chunk}) => {
|
||||||
chunkData: column.dump()
|
player._client.write('map_chunk', {
|
||||||
});
|
x: x,
|
||||||
return Promise.resolve();
|
z: z,
|
||||||
|
groundUp: true,
|
||||||
|
bitMap: 0xffff,
|
||||||
|
chunkData: chunk.dump()
|
||||||
|
});
|
||||||
|
return Promise.resolve();
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
function spiral(arr)
|
function spiral(arr)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue