mirror of
https://github.com/danbulant/flying-squid
synced 2026-06-11 18:41:31 +00:00
Merge pull request #108 from demipixel/behaviors
Just like how caterpillars will turn into butterflies...
This commit is contained in:
commit
4d267adb48
20 changed files with 249 additions and 186 deletions
16
src/lib/behavior.js
Normal file
16
src/lib/behavior.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
module.exports = (obj) => {
|
||||
return (eventName, data, func, opt) => {
|
||||
var hiddenCancelled = false;
|
||||
var cancelled = false;
|
||||
var cancel = (hidden) => { // Hidden shouldn't be used often but it's not hard to implement so meh
|
||||
if (hidden) hiddenCancelled = true;
|
||||
else cancelled = true;
|
||||
}
|
||||
obj.emit(eventName + '_cancel', data, cancel);
|
||||
obj.emit(eventName, data, cancelled);
|
||||
|
||||
if (!hiddenCancelled && !cancelled) func(data);
|
||||
|
||||
obj.emit(eventName + '_done', data, cancelled);
|
||||
}
|
||||
}
|
||||
14
src/lib/entity.js
Normal file
14
src/lib/entity.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
var EventEmitter = require('events').EventEmitter;
|
||||
var PrismarineEntity = require("prismarine-entity");
|
||||
var util = require('util');
|
||||
util.inherits(PrismarineEntity, EventEmitter);
|
||||
|
||||
class Entity extends PrismarineEntity
|
||||
{
|
||||
constructor(id) {
|
||||
super(id);
|
||||
EventEmitter.call(this);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Entity;
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
|
||||
class Player extends EventEmitter
|
||||
{
|
||||
constructor() {
|
||||
super();
|
||||
this._client=null;
|
||||
this.entity=null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Player;
|
||||
|
|
@ -2,30 +2,21 @@ module.exports.player=function(player)
|
|||
{
|
||||
player._client.on("arm_animation", () =>
|
||||
player._writeOthersNearby("animation", {
|
||||
entityId: player.entity.id,
|
||||
entityId: player.id,
|
||||
animation: 0
|
||||
}));
|
||||
|
||||
function setMetadata(metadata)
|
||||
{
|
||||
player.entity.metadata = metadata;
|
||||
player._writeOthersNearby("entity_metadata", {
|
||||
entityId: player.entity.id,
|
||||
metadata: player.entity.metadata
|
||||
});
|
||||
}
|
||||
|
||||
player._client.on("entity_action", ({actionId} = {}) => {
|
||||
if(actionId == 3) {
|
||||
setMetadata([{"key":0,"type":0,"value": 0x08}]);
|
||||
player.setAndUpdateMetadata([{"key":0,"type":0,"value": 0x08}]);
|
||||
} else if(actionId == 4) {
|
||||
setMetadata([{"key":0,"type":0,"value": 0x00}]);
|
||||
player.setAndUpdateMetadata([{"key":0,"type":0,"value": 0x00}]);
|
||||
} else if(actionId == 0) {
|
||||
setMetadata([{"key":0,"type":0,"value": 0x02}]);
|
||||
player.entity.crouching = true;
|
||||
player.setAndUpdateMetadata([{"key":0,"type":0,"value": 0x02}]);
|
||||
player.crouching = true;
|
||||
} else if(actionId == 1) {
|
||||
setMetadata([{"key":0,"type":0,"value": 0x00}]);
|
||||
player.entity.crouching = false;
|
||||
player.setAndUpdateMetadata([{"key":0,"type":0,"value": 0x00}]);
|
||||
player.crouching = false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
@ -33,7 +33,7 @@ module.exports.player=function(player,serv)
|
|||
action(params) {
|
||||
var res = params.map((num, i) => { // parseInt paramaters
|
||||
if (num.indexOf('~') == 0) {
|
||||
return (player.entity.position[['', 'x', 'y', 'z'][i]] >> 5) + parseInt(num.slice(1) || 0);
|
||||
return (player.position[['', 'x', 'y', 'z'][i]] >> 5) + parseInt(num.slice(1) || 0);
|
||||
} else {
|
||||
return parseInt(num); // return parseInt>>5 if position, not id
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ module.exports.player=function(player)
|
|||
{
|
||||
player._client.on('block_place', async ({location} = {}) => {
|
||||
var referencePosition=new Vec3(location.x,location.y,location.z);
|
||||
if (player.entity.crouching) return;
|
||||
if (player.crouching) return;
|
||||
try {
|
||||
var id = await player.world.getBlockType(referencePosition);
|
||||
var blockAbove = await player.world.getBlockType(referencePosition.clone().add(new Vec3(0, 1, 0)));
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ module.exports.server=function(serv)
|
|||
|
||||
serv.getNearby= ({world,position,radius=8*16*32}) => serv.players.filter( player =>
|
||||
player.world == world &&
|
||||
player.entity.position.distanceTo(position) <= radius
|
||||
player.position.distanceTo(position) <= radius
|
||||
);
|
||||
|
||||
serv.getNearbyEntities= ({world,position,radius=8*16*32}) => Object.keys(serv.entities)
|
||||
|
|
@ -22,25 +22,30 @@ module.exports.server=function(serv)
|
|||
);
|
||||
};
|
||||
|
||||
module.exports.player=function(player,serv)
|
||||
module.exports.entity=function(entity,serv)
|
||||
{
|
||||
player._writeOthers= (packetName, packetFields) =>
|
||||
player
|
||||
.getOthers()
|
||||
.forEach((otherPlayer) => otherPlayer._client.write(packetName, packetFields));
|
||||
|
||||
entity.getNearby = () => serv
|
||||
.getNearbyEntities({
|
||||
world: entity.world,
|
||||
position: entity.position,
|
||||
radius: entity.viewDistance*32
|
||||
})
|
||||
.filter((e) => e != entity);
|
||||
|
||||
player._writeOthersNearby = (packetName, packetFields) =>
|
||||
serv._writeArray(packetName, packetFields, player.nearbyPlayers());
|
||||
entity.getOtherPlayers = () => serv.players.filter((p) => p != entity);
|
||||
|
||||
player.getOthers = () => serv.players.filter((otherPlayer) => otherPlayer != player);
|
||||
entity.getOthers = () => serv.entities.filter((e) => e != entity);
|
||||
|
||||
player.getNearbyPlayers = (radius=player.entity.viewDistance*32) => serv.getNearby({
|
||||
world: player.world,
|
||||
position: player.position,
|
||||
radius: radius
|
||||
});
|
||||
entity.getNearbyPlayers = (radius=entity.viewDistance*32) => entity.getNearby()
|
||||
.filter((e) => e.type == 'player');
|
||||
|
||||
player.nearbyPlayers = (radius=player.entity.viewDistance*32) => player.entity.nearbyEntities
|
||||
entity.nearbyPlayers = (radius=entity.viewDistance*32) => entity.nearbyEntities
|
||||
.filter(e => e.type == 'player')
|
||||
.map(e => e.player);
|
||||
};
|
||||
|
||||
entity._writeOthers = (packetName, packetFields) =>
|
||||
serv._writeArray(packetName, packetFields, entity.getOtherPlayers());
|
||||
|
||||
entity._writeOthersNearby = (packetName, packetFields) =>
|
||||
serv._writeArray(packetName, packetFields, entity.getNearbyPlayers());
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
var Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
|
||||
module.exports.player=function(player,serv)
|
||||
{
|
||||
|
|
@ -76,7 +76,7 @@ module.exports.player=function(player,serv)
|
|||
player.changeBlock(location,0,0);
|
||||
// Drop block
|
||||
serv.spawnObject(2, player.world, location.offset(0.5, 0.5, 0.5), {
|
||||
velocity: Vec3(Math.random()*4 - 2, Math.random()*2 + 2, Math.random()*4 - 2),
|
||||
velocity: new Vec3(Math.random()*4 - 2, Math.random()*2 + 2, Math.random()*4 - 2),
|
||||
itemId: currentlyDugBlock.type,
|
||||
itemDamage: currentlyDugBlock.metadata
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
var Entity=require("prismarine-entity");
|
||||
var util = require('util');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
util.inherits(Entity, EventEmitter);
|
||||
var Entity=require("../entity");
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
var entitiesByName=require("minecraft-data")(require("../version")).entitiesByName;
|
||||
|
||||
|
|
@ -14,7 +11,6 @@ module.exports.server=function(serv,options) {
|
|||
serv.initEntity = (type, entityType, world, position) => {
|
||||
serv.entityMaxId++;
|
||||
var entity = new Entity(serv.entityMaxId);
|
||||
EventEmitter.call(entity);
|
||||
|
||||
Object.keys(plugins)
|
||||
.filter(pluginName => plugins[pluginName].entity!=undefined)
|
||||
|
|
@ -35,9 +31,10 @@ module.exports.server=function(serv,options) {
|
|||
object.yaw = yaw;
|
||||
object.gravity = new Vec3(0, -20*32, 0);
|
||||
object.terminalvelocity = new Vec3(27*32, 27*32, 27*32);
|
||||
object.friction = (new Vec3(10*32, 0, 10*32)).floored();
|
||||
object.friction = new Vec3(15*32, 0, 15*32);
|
||||
object.size = new Vec3(0.25*32, 0.25*32, 0.25*32); // Hardcoded, will be dependent on type!
|
||||
object.deathTime = 60*1000; // 60 seconds
|
||||
object.pickupTime = 200;
|
||||
object.itemId = itemId;
|
||||
object.itemDamage = itemDamage;
|
||||
|
||||
|
|
@ -52,19 +49,17 @@ module.exports.server=function(serv,options) {
|
|||
mob.yaw = yaw;
|
||||
mob.gravity = new Vec3(0, -20*32, 0);
|
||||
mob.terminalvelocity = new Vec3(27*32, 27*32, 27*32);
|
||||
mob.friction = new Vec3(10*32, 0, 10*32);
|
||||
mob.friction = new Vec3(15*32, 0, 15*32);
|
||||
mob.size = new Vec3(0.75, 1.75, 0.75);
|
||||
mob.health = 20;
|
||||
mob.metadata = metadata;
|
||||
|
||||
mob.updateAndSpawn();
|
||||
};
|
||||
|
||||
serv.destroyEntity = entity => {
|
||||
serv._writeNearby('entity_destroy', {
|
||||
entity._writeOthersNearby('entity_destroy', {
|
||||
entityIds: [entity.id]
|
||||
}, {
|
||||
position: entity.position,
|
||||
world: entity.world
|
||||
});
|
||||
delete serv.entities[entity.id];
|
||||
};
|
||||
|
|
@ -76,6 +71,16 @@ module.exports.server=function(serv,options) {
|
|||
if (entity.deathTime && Date.now() - entity.bornTime >= entity.deathTime) {
|
||||
entity.destroy();
|
||||
return;
|
||||
} else if (entity.pickupTime && Date.now() - entity.bornTime >= entity.pickupTime) {
|
||||
var players = serv.getNearby({
|
||||
world: entity.world,
|
||||
position: entity.position,
|
||||
radius: 1.5*32 // Seems good for now
|
||||
});
|
||||
if (players.length) {
|
||||
players[0].collect(entity);
|
||||
entity.destroy();
|
||||
}
|
||||
}
|
||||
if (!entity.velocity || !entity.size) return;
|
||||
var oldPosAndOnGround = await entity.calculatePhysics(delta);
|
||||
|
|
@ -99,7 +104,7 @@ module.exports.player=function(player,serv){
|
|||
}
|
||||
},
|
||||
action({id}) {
|
||||
serv.spawnMob(id, player.world, player.entity.position.scaled(1/32), {
|
||||
serv.spawnMob(id, player.world, player.position.scaled(1/32), {
|
||||
velocity: Vec3((Math.random() - 0.5) * 10, Math.random()*10 + 10, (Math.random() - 0.5) * 10)
|
||||
});
|
||||
}
|
||||
|
|
@ -117,7 +122,7 @@ module.exports.player=function(player,serv){
|
|||
}
|
||||
},
|
||||
action({id}) {
|
||||
serv.spawnObject(id, player.world, player.entity.position.scaled(1/32), {
|
||||
serv.spawnObject(id, player.world, player.position.scaled(1/32), {
|
||||
velocity: Vec3((Math.random() - 0.5) * 10, Math.random()*10 + 10, (Math.random() - 0.5) * 10)
|
||||
});
|
||||
}
|
||||
|
|
@ -133,7 +138,7 @@ module.exports.player=function(player,serv){
|
|||
player.chat("No entity named "+name);
|
||||
return;
|
||||
}
|
||||
serv.spawnMob(entity.id, player.world, player.entity.position.scaled(1/32), {
|
||||
serv.spawnMob(entity.id, player.world, player.position.scaled(1/32), {
|
||||
velocity: Vec3((Math.random() - 0.5) * 10, Math.random()*10 + 10, (Math.random() - 0.5) * 10)
|
||||
});
|
||||
}
|
||||
|
|
@ -166,13 +171,18 @@ module.exports.entity=function(entity,serv){
|
|||
entity.updateAndSpawn();
|
||||
});
|
||||
|
||||
entity.setMetadata = (data) => {
|
||||
serv._writeNearby('entity_metadata', {
|
||||
entity.sendMetadata = (data) => {
|
||||
entity._writeOthersNearby('entity_metadata', {
|
||||
entityId: entity.id,
|
||||
metadata: data
|
||||
}, entity);
|
||||
});
|
||||
};
|
||||
|
||||
entity.setAndUpdateMetadata = (data) => {
|
||||
entity.metadata = data;
|
||||
entity.sendMetadata(data);
|
||||
}
|
||||
|
||||
entity.destroy = () => {
|
||||
serv.destroyEntity(entity);
|
||||
};
|
||||
|
|
@ -182,7 +192,7 @@ module.exports.entity=function(entity,serv){
|
|||
if (entity.type == 'player') {
|
||||
return {
|
||||
entityId: entity.id,
|
||||
playerUUID: entity.player._client.uuid,
|
||||
playerUUID: entity._client.uuid,
|
||||
x: entity.position.x,
|
||||
y: entity.position.y,
|
||||
z: entity.position.z,
|
||||
|
|
@ -225,34 +235,61 @@ module.exports.entity=function(entity,serv){
|
|||
}
|
||||
};
|
||||
|
||||
entity.getNearby = () => serv
|
||||
.getNearbyEntities({
|
||||
world: entity.world,
|
||||
position: entity.position,
|
||||
radius: entity.viewDistance*32
|
||||
})
|
||||
.filter((e) => e != entity);
|
||||
|
||||
entity.updateAndSpawn = () => {
|
||||
var updatedEntities=entity.getNearby();
|
||||
var entitiesToAdd=updatedEntities.filter(e => entity.nearbyEntities.indexOf(e)==-1);
|
||||
var entitiesToRemove=entity.nearbyEntities.filter(e => updatedEntities.indexOf(e)==-1);
|
||||
if (entity.type == 'player') {
|
||||
entity.player.despawnEntities(entitiesToRemove);
|
||||
entitiesToAdd.forEach(entity.player.spawnEntity);
|
||||
entity.player.lastPositionPlayersUpdated=entity.position.clone();
|
||||
} else {
|
||||
entity.lastPositionPlayersUpdated=entity.position.clone();
|
||||
entity.despawnEntities(entitiesToRemove);
|
||||
entitiesToAdd.forEach(entity.spawnEntity);
|
||||
}
|
||||
entity.lastPositionPlayersUpdated=entity.position.clone();
|
||||
|
||||
var playersToAdd = entitiesToAdd.filter(e => e.type == 'player').map(e => e.player);
|
||||
var playersToRemove = entitiesToRemove.filter(e => e.type == 'player').map(e => e.player);
|
||||
var playersToAdd = entitiesToAdd.filter(e => e.type == 'player');
|
||||
var playersToRemove = entitiesToRemove.filter(e => e.type == 'player');
|
||||
|
||||
playersToRemove.forEach(p => p.despawnEntities([entity]));
|
||||
playersToRemove.forEach(p => p.entity.nearbyEntities=p.entity.getNearby());
|
||||
playersToRemove.forEach(p => p.nearbyEntities=p.getNearby());
|
||||
playersToAdd.forEach(p => p.spawnEntity(entity));
|
||||
playersToAdd.forEach(p => p.entity.nearbyEntities=p.entity.getNearby());
|
||||
playersToAdd.forEach(p => p.nearbyEntities=p.getNearby());
|
||||
|
||||
entity.nearbyEntities=updatedEntities;
|
||||
};
|
||||
|
||||
entity.collect = (collectEntity) => {
|
||||
if (entity.type != 'player') serv.emit('error', 'Non-player entity (ttype ' + entity.type + ') cannot collect another entity');
|
||||
else {
|
||||
collectEntity._writeOthersNearby('collect', {
|
||||
collectedEntityId: collectEntity.id,
|
||||
collectorEntityId: entity.id
|
||||
});
|
||||
entity.playSoundAtSelf('random.pop');
|
||||
}
|
||||
}
|
||||
|
||||
entity.sendVelocity = (vel, maxVel) => {
|
||||
var velocity = vel.scaled(32).floored(); // Make fixed point
|
||||
var maxVelocity = maxVel.scaled(32).floored();
|
||||
var scaledVelocity = velocity.scaled(8000/32/20).floored(); // from fixed-position/second to unit => 1/8000 blocks per tick
|
||||
entity._writeOthersNearby('entity_velocity', {
|
||||
entityId: entity.id,
|
||||
velocityX: scaledVelocity.x,
|
||||
velocityY: scaledVelocity.y,
|
||||
velocityZ: scaledVelocity.z
|
||||
});
|
||||
if (entity.type != 'player') {
|
||||
if (maxVelocity) entity.velocity = addVelocityWithMax(entity.velocity, velocity, maxVelocity);
|
||||
else entity.velocity.add(velocity);
|
||||
}
|
||||
}
|
||||
function addVelocityWithMax(current, newVel, max) {
|
||||
var x, y, z;
|
||||
if (current.x > max.x || current.x < -max.x) x = current.x;
|
||||
else x = Math.max(-max.x, Math.min(max.x, current.x + newVel.x));
|
||||
if (current.y > max.y || current.y < -max.y) y = current.y;
|
||||
else y = Math.max(-max.y, Math.min(max.y, current.y + newVel.y));
|
||||
if (current.z > max.z || current.z < -max.z) z = current.z;
|
||||
else z = Math.max(-max.z, Math.min(max.z, current.z + newVel.z));
|
||||
return new Vec3(x, y, z);
|
||||
}
|
||||
};
|
||||
|
|
@ -13,7 +13,7 @@ module.exports.player=function(player)
|
|||
}
|
||||
player.heldItem = player.inventory[36+player.heldItemSlot];
|
||||
player._writeOthersNearby("entity_equipment",{
|
||||
entityId:player.entity.id,
|
||||
entityId:player.id,
|
||||
slot:0,
|
||||
item:player.heldItem
|
||||
});
|
||||
|
|
@ -23,31 +23,31 @@ module.exports.player=function(player)
|
|||
player.inventory[slot]=item;
|
||||
if (slot==36)
|
||||
player._writeOthersNearby("entity_equipment",{
|
||||
entityId:player.entity.id,
|
||||
entityId:player.id,
|
||||
slot:0,
|
||||
item:item
|
||||
});
|
||||
if (slot==5)
|
||||
player._writeOthersNearby("entity_equipment",{
|
||||
entityId:player.entity.id,
|
||||
entityId:player.id,
|
||||
slot:4,
|
||||
item:item
|
||||
});
|
||||
if (slot==6)
|
||||
player._writeOthersNearby("entity_equipment",{
|
||||
entityId:player.entity.id,
|
||||
entityId:player.id,
|
||||
slot:3,
|
||||
item:item
|
||||
});
|
||||
if (slot==7)
|
||||
player._writeOthersNearby("entity_equipment",{
|
||||
entityId:player.entity.id,
|
||||
entityId:player.id,
|
||||
slot:2,
|
||||
item:item
|
||||
});
|
||||
if (slot==8)
|
||||
player._writeOthersNearby("entity_equipment",{
|
||||
entityId:player.entity.id,
|
||||
entityId:player.id,
|
||||
slot:1,
|
||||
item:item
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
var Entity=require("prismarine-entity");
|
||||
var Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
|
||||
var path = require('path');
|
||||
var requireIndex = require('requireindex');
|
||||
var plugins = requireIndex(path.join(__dirname,'..', 'plugins'));
|
||||
var Player=require("../player");
|
||||
var Command = require('../command');
|
||||
|
||||
module.exports.server=function(serv,options)
|
||||
|
|
@ -13,7 +12,7 @@ module.exports.server=function(serv,options)
|
|||
client.on('error',error => serv.emit('clientError',client,error)));
|
||||
|
||||
serv._server.on('login', async (client) => {
|
||||
var player=new Player();
|
||||
var player = serv.initEntity('player', null, serv.overworld, new Vec3(0,0,0));
|
||||
player._client=client;
|
||||
player.commands = new Command({});
|
||||
Object.keys(plugins)
|
||||
|
|
@ -34,14 +33,10 @@ module.exports.player=function(player,serv)
|
|||
{
|
||||
function addPlayer()
|
||||
{
|
||||
player.entity=serv.initEntity('player', null, serv.overworld, new Vec3(0,0,0));
|
||||
player.entity.type = 'player';
|
||||
player.entity.player=player;
|
||||
player.entity.health = 20;
|
||||
player.entity.food = 20;
|
||||
player.entity.crouching = false; // Needs added in prismarine-entity later
|
||||
player.view=10;
|
||||
player.world=serv.overworld;
|
||||
player.type = 'player';
|
||||
player.health = 20;
|
||||
player.food = 20;
|
||||
player.crouching = false; // Needs added in prismarine-entity later
|
||||
player.username=player._client.username;
|
||||
serv.players.push(player);
|
||||
serv.uuidToPlayer[player._client.uuid] = player;
|
||||
|
|
@ -51,8 +46,8 @@ module.exports.player=function(player,serv)
|
|||
function sendPlayersWhenMove()
|
||||
{
|
||||
player.on("positionChanged",() => {
|
||||
if(player.entity.position.distanceTo(player.lastPositionPlayersUpdated)>2*32)
|
||||
player.entity.updateAndSpawn();
|
||||
if(player.position.distanceTo(player.lastPositionPlayersUpdated)>2*32)
|
||||
player.updateAndSpawn();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -60,7 +55,7 @@ module.exports.player=function(player,serv)
|
|||
{
|
||||
// send init data so client will start rendering world
|
||||
player._client.write('login', {
|
||||
entityId: player.entity.id,
|
||||
entityId: player.id,
|
||||
levelType: 'default',
|
||||
gameMode: player.gameMode,
|
||||
dimension: 0,
|
||||
|
|
@ -68,13 +63,13 @@ module.exports.player=function(player,serv)
|
|||
reducedDebugInfo: false,
|
||||
maxPlayers: serv._server.maxPlayers
|
||||
});
|
||||
player.entity.position=player.spawnPoint.toFixedPosition();
|
||||
player.position=player.spawnPoint.toFixedPosition();
|
||||
}
|
||||
|
||||
function sendChunkWhenMove()
|
||||
{
|
||||
player.on("positionChanged", () => {
|
||||
if(!player.sendingChunks && player.entity.position.distanceTo(player.lastPositionChunkUpdated)>16*32)
|
||||
if(!player.sendingChunks && player.position.distanceTo(player.lastPositionChunkUpdated)>16*32)
|
||||
player.sendRestMap();
|
||||
});
|
||||
}
|
||||
|
|
@ -150,7 +145,7 @@ module.exports.player=function(player,serv)
|
|||
|
||||
|
||||
player.login = async () =>
|
||||
{
|
||||
{
|
||||
if (serv.uuidToPlayer[player._client.uuid]) {
|
||||
player._client.end("You are already connected");
|
||||
return;
|
||||
|
|
@ -165,12 +160,12 @@ module.exports.player=function(player,serv)
|
|||
await player.sendMap();
|
||||
player.sendSpawnPosition();
|
||||
player.sendPosition();
|
||||
player.updateHealth(player.entity.health);
|
||||
player.updateHealth(player.health);
|
||||
|
||||
|
||||
updateTime();
|
||||
fillTabList();
|
||||
player.entity.updateAndSpawn();
|
||||
player.updateAndSpawn();
|
||||
|
||||
announceJoin();
|
||||
player.emit("spawned");
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ module.exports.player=function(player,serv)
|
|||
{
|
||||
player.despawnPlayers = despawnedPlayers => {
|
||||
player._client.write('entity_destroy', {
|
||||
'entityIds': despawnedPlayers.map(p => p.entity.id)
|
||||
'entityIds': despawnedPlayers.map(p => p.id)
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -11,7 +11,7 @@ module.exports.player=function(player,serv)
|
|||
});
|
||||
|
||||
player._client.on('end', () => {
|
||||
if(player.entity) {
|
||||
if(player) {
|
||||
serv.broadcast(player.username + ' quit the game.', "yellow");
|
||||
player._writeOthers('player_info', {
|
||||
action: 4,
|
||||
|
|
@ -20,7 +20,7 @@ module.exports.player=function(player,serv)
|
|||
}]
|
||||
});
|
||||
player.nearbyPlayers().forEach(otherPlayer => otherPlayer.despawnPlayers([player]));
|
||||
delete serv.entities[player.entity.id];
|
||||
delete serv.entities[player.id];
|
||||
player.emit('disconnected');
|
||||
var index = serv.players.indexOf(player);
|
||||
if (index > -1) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
var Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
var dir = require("node-dir");
|
||||
var fs = require("fs");
|
||||
|
||||
|
|
@ -61,15 +61,15 @@ function modpeApi() {
|
|||
}
|
||||
|
||||
function getPlayerX() {
|
||||
return player.entity.position.x/32;
|
||||
return player.position.x/32;
|
||||
}
|
||||
|
||||
function getPlayerY() {
|
||||
return player.entity.position.y/32;
|
||||
return player.position.y/32;
|
||||
}
|
||||
|
||||
function getPlayerZ() {
|
||||
return player.entity.position.z/32;
|
||||
return player.position.z/32;
|
||||
}
|
||||
|
||||
function getPlayerEnt() {
|
||||
|
|
|
|||
|
|
@ -1,29 +1,27 @@
|
|||
var Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
|
||||
module.exports.server=function(serv) {
|
||||
serv.emitParticle = (particle, world, position, {whitelist,blacklist=[],radius=32*32,longDistance,size,count}={}) => {
|
||||
serv.emitParticle = (particle, world, position, {whitelist,blacklist=[],radius=32*32,longDistance=true,size,count=1}={}) => {
|
||||
var players = (typeof whitelist != 'undefined' ? (typeof whitelist == 'array' ? whitelist : [whitelist]) : serv.getNearby({
|
||||
world: world,
|
||||
position: position.scaled(32).floored(),
|
||||
radius: radius // 32 blocks, fixed position
|
||||
}));
|
||||
if (!size) size = new Vec3(1.0, 1.0, 1.0);
|
||||
players.filter(player => blacklist.indexOf(player) == -1)
|
||||
.forEach(player => {
|
||||
player._client.write('world_particles', {
|
||||
particleId: particle,
|
||||
longDistance: longDistance || true,
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
z: position.z,
|
||||
offsetX: size.x,
|
||||
offsetY: size.y,
|
||||
offsetZ: size.z,
|
||||
particleData: 1.0,
|
||||
particles: count || 1,
|
||||
data: []
|
||||
});
|
||||
});
|
||||
|
||||
serv._writeArray('world_particles', {
|
||||
particleId: particle,
|
||||
longDistance: longDistance,
|
||||
x: position.x,
|
||||
y: position.y,
|
||||
z: position.z,
|
||||
offsetX: size.x,
|
||||
offsetY: size.y,
|
||||
offsetZ: size.z,
|
||||
particleData: 1.0,
|
||||
particles: count,
|
||||
data: []
|
||||
}, players.filter(p => blacklist.indexOf(p) == -1));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -47,7 +45,7 @@ module.exports.player=function(player,serv){
|
|||
return;
|
||||
}
|
||||
player.chat('Emitting "' + particle + '" (count: ' + amount + ', size: ' + size.toString() + ')');
|
||||
serv.emitParticle(particle, player.world, player.entity.position.scaled(1/32), {count: amount,size: size});
|
||||
serv.emitParticle(particle, player.world, player.position.scaled(1/32), {count: amount,size: size});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
var blocks=require("minecraft-data")(require("../version")).blocks;
|
||||
var Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
|
||||
module.exports.entity=function(entity){
|
||||
entity.calculatePhysics = async (delta) => {
|
||||
|
|
|
|||
|
|
@ -1,31 +1,36 @@
|
|||
var Vec3 = require("vec3").Vec3;
|
||||
|
||||
module.exports.player=function(player,serv)
|
||||
{
|
||||
|
||||
player.updateHealth = (health) => {
|
||||
player.entity.health = health;
|
||||
player.health = health;
|
||||
player._client.write('update_health', {
|
||||
food: player.entity.food,
|
||||
food: player.food,
|
||||
foodSaturation: 0.0,
|
||||
health: player.entity.health
|
||||
health: player.health
|
||||
});
|
||||
};
|
||||
|
||||
function attackEntity(entityId)
|
||||
{
|
||||
if (!serv.entities[entityId]) return; // ?????
|
||||
var attackedPlayer = serv.entities[entityId].player;
|
||||
if(!attackedPlayer || attackedPlayer.gameMode!=0) return;
|
||||
attackedPlayer.updateHealth(attackedPlayer.entity.health - 1);
|
||||
serv.playSound('game.player.hurt', player.world, attackedPlayer.entity.position.scaled(1/32));
|
||||
var attackedEntity = serv.entities[entityId];
|
||||
if(!attackedEntity || (attackedEntity.gameMode != 0 && attackedEntity.type == 'player')) return;
|
||||
|
||||
if(attackedPlayer.entity.health==0)
|
||||
attackedPlayer._writeOthers('entity_status',{
|
||||
entityId:attackedPlayer.entity.id,
|
||||
attackedEntity.updateHealth(attackedEntity.health - 1);
|
||||
serv.playSound('game.player.hurt', 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(attackVelocity, new Vec3(4, 4, 4));
|
||||
|
||||
if(attackedEntity.health<=0)
|
||||
attackedEntity._writeOthers('entity_status',{
|
||||
entityId:attackedEntity.id,
|
||||
entityStatus:3
|
||||
});
|
||||
else
|
||||
attackedPlayer._writeOthers('animation',{
|
||||
entityId:attackedPlayer.entity.id,
|
||||
attackedEntity._writeOthers('animation',{
|
||||
entityId:attackedEntity.id,
|
||||
animation:1
|
||||
});
|
||||
}
|
||||
|
|
@ -35,4 +40,13 @@ module.exports.player=function(player,serv)
|
|||
attackEntity(target);
|
||||
});
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
module.exports.entity=function(entity,serv)
|
||||
{
|
||||
if (entity.type != 'player') {
|
||||
entity.updateHealth = (health) => {
|
||||
entity.health = health;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -10,8 +10,8 @@ module.exports.player=function(player)
|
|||
});
|
||||
player.sendPosition();
|
||||
player.updateHealth(20);
|
||||
player.entity.nearbyEntities=[];
|
||||
player.entity.updateAndSpawn();
|
||||
player.nearbyEntities=[];
|
||||
player.updateAndSpawn();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
@ -9,7 +9,7 @@ module.exports.server=function(serv) {
|
|||
}));
|
||||
players.filter(player => blacklist.indexOf(player) == -1)
|
||||
.forEach(player => {
|
||||
var pos = (position || player.entity.position.scaled(1/32)).scaled(8).floored();
|
||||
var pos = (position || player.position.scaled(1/32)).scaled(8).floored();
|
||||
player._client.write('named_sound_effect', {
|
||||
soundName: sound,
|
||||
x: pos.x,
|
||||
|
|
@ -39,7 +39,7 @@ module.exports.player=function(player,serv) {
|
|||
};
|
||||
|
||||
player._client.on('block_place', ({location}={}) => {
|
||||
if (player.entity.crouching) return;
|
||||
if (player.crouching) return;
|
||||
var pos=new Vec3(location.x,location.y,location.z);
|
||||
player.world.getBlockType(pos).then((id) => {
|
||||
if (id != 25) return;
|
||||
|
|
@ -99,7 +99,13 @@ module.exports.player=function(player,serv) {
|
|||
},
|
||||
action({sound_name,volume,pitch}) {
|
||||
player.chat('Playing "'+sound_name+'" (volume: ' + volume + ', pitch: ' + pitch + ')');
|
||||
serv.playSound(sound_name, player.world, player.entity.position.scaled(1/32), {volume: volume,pitch: pitch});
|
||||
serv.playSound(sound_name, player.world, player.position.scaled(1/32), {volume: volume,pitch: pitch});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.entity=function(entity,serv) {
|
||||
entity.playSoundAtSelf = (sound, opt={}) => {
|
||||
serv.playSound(sound, entity.world, entity.position.scaled(1/32), opt);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
var Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
|
||||
Vec3.prototype.toFixedPosition=function() {
|
||||
return this.scaled(32).floored();
|
||||
|
|
@ -19,18 +19,18 @@ module.exports.player=function(player)
|
|||
{
|
||||
var convYaw=conv(yaw);
|
||||
var convPitch=conv(pitch);
|
||||
if (convYaw == player.entity.yaw && convPitch == player.entity.pitch) return;
|
||||
if (convYaw == player.yaw && convPitch == player.pitch) return;
|
||||
player._writeOthersNearby("entity_look", {
|
||||
entityId: player.entity.id,
|
||||
entityId: player.id,
|
||||
yaw: convYaw,
|
||||
pitch: convPitch,
|
||||
onGround: onGround
|
||||
});
|
||||
player.entity.yaw = convYaw;
|
||||
player.entity.pitch = convPitch;
|
||||
player.entity.onGround = onGround;
|
||||
player.yaw = convYaw;
|
||||
player.pitch = convPitch;
|
||||
player.onGround = onGround;
|
||||
player._writeOthersNearby("entity_head_rotation", {
|
||||
entityId: player.entity.id,
|
||||
entityId: player.id,
|
||||
headYaw: convYaw
|
||||
});
|
||||
}
|
||||
|
|
@ -44,23 +44,23 @@ module.exports.player=function(player)
|
|||
});
|
||||
|
||||
function sendRelativePositionChange(newPosition, onGround) {
|
||||
if (player.entity.position.distanceTo(new Vec3(0, 0, 0)) != 0) {
|
||||
var diff = newPosition.minus(player.entity.position);
|
||||
if (player.position.distanceTo(new Vec3(0, 0, 0)) != 0) {
|
||||
var diff = newPosition.minus(player.position);
|
||||
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
||||
{
|
||||
player._writeOthersNearby('entity_teleport', {
|
||||
entityId:player.entity.id,
|
||||
entityId:player.id,
|
||||
x: newPosition.x,
|
||||
y: newPosition.y,
|
||||
z: newPosition.z,
|
||||
yaw: player.entity.yaw,
|
||||
pitch: player.entity.pitch,
|
||||
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.entity.id,
|
||||
entityId: player.id,
|
||||
dX: diff.x,
|
||||
dY: diff.y,
|
||||
dZ: diff.z,
|
||||
|
|
@ -68,18 +68,18 @@ module.exports.player=function(player)
|
|||
});
|
||||
}
|
||||
}
|
||||
player.entity.position = newPosition;
|
||||
player.entity.onGround = onGround;
|
||||
player.position = newPosition;
|
||||
player.onGround = onGround;
|
||||
player.emit("positionChanged");
|
||||
}
|
||||
|
||||
player.sendPosition = () => {
|
||||
player._client.write('position', {
|
||||
x: player.entity.position.x/32,
|
||||
y: player.entity.position.y/32,
|
||||
z: player.entity.position.z/32,
|
||||
yaw: player.entity.yaw,
|
||||
pitch: player.entity.pitch,
|
||||
x: player.position.x/32,
|
||||
y: player.position.y/32,
|
||||
z: player.position.z/32,
|
||||
yaw: player.yaw,
|
||||
pitch: player.pitch,
|
||||
flags: 0x00
|
||||
});
|
||||
};
|
||||
|
|
@ -90,7 +90,7 @@ module.exports.entity=function(entity,serv){
|
|||
var diff = entity.position.minus(oldPos);
|
||||
|
||||
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
|
||||
serv._writeNearby('entity_teleport', {
|
||||
entity._writeOthersNearby('entity_teleport', {
|
||||
entityId: entity.id,
|
||||
x: entity.position.x,
|
||||
y: entity.position.y,
|
||||
|
|
@ -98,7 +98,7 @@ module.exports.entity=function(entity,serv){
|
|||
yaw: entity.yaw,
|
||||
pitch: entity.pitch,
|
||||
onGround: onGround
|
||||
}, entity);
|
||||
});
|
||||
else if (diff.distanceTo(new Vec3(0, 0, 0)) != 0) serv._writeNearby('rel_entity_move', {
|
||||
entityId: entity.id,
|
||||
dX: diff.x,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
var Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
var spiralloop = require('spiralloop');
|
||||
|
||||
var Chunk = require('prismarine-chunk')(require("../version"));
|
||||
|
|
@ -56,7 +56,7 @@ module.exports.player=function(player,serv) {
|
|||
player.spawnEntity = entity => {
|
||||
player._client.write(entity.spawnPacketName, entity.getSpawnPacket());
|
||||
if (typeof entity.itemId != 'undefined') {
|
||||
entity.setMetadata([{
|
||||
entity.sendMetadata([{
|
||||
"key": 10,
|
||||
"type": 5,
|
||||
"value": {
|
||||
|
|
@ -91,9 +91,9 @@ module.exports.player=function(player,serv) {
|
|||
|
||||
player.sendNearbyChunks = (view,group) =>
|
||||
{
|
||||
player.lastPositionChunkUpdated=player.entity.position;
|
||||
var playerChunkX=Math.floor(player.entity.position.x/16/32);
|
||||
var playerChunkZ=Math.floor(player.entity.position.z/16/32);
|
||||
player.lastPositionChunkUpdated=player.position;
|
||||
var playerChunkX=Math.floor(player.position.x/16/32);
|
||||
var playerChunkZ=Math.floor(player.position.z/16/32);
|
||||
|
||||
return spiral([view*2,view*2])
|
||||
.map(t => ({
|
||||
|
|
@ -145,7 +145,7 @@ module.exports.player=function(player,serv) {
|
|||
if(player.world == world) return Promise.resolve();
|
||||
opt = opt || {};
|
||||
player.world = world;
|
||||
player.entity.world = world;
|
||||
player.world = world;
|
||||
player.loadedChunks={};
|
||||
if (typeof opt.gamemode != 'undefined') player.gameMode = opt.gamemode;
|
||||
player._client.write("respawn",{
|
||||
|
|
@ -154,9 +154,9 @@ module.exports.player=function(player,serv) {
|
|||
gamemode: opt.gamemode || player.gameMode,
|
||||
levelType:'default'
|
||||
});
|
||||
player.entity.position=player.spawnPoint.toFixedPosition();
|
||||
player.position=player.spawnPoint.toFixedPosition();
|
||||
player.sendSpawnPosition();
|
||||
player.entity.updateAndSpawn();
|
||||
player.updateAndSpawn();
|
||||
|
||||
await player.sendMap();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue