mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-04 10:40:42 +00:00
Fix a lot of communication things, added lots of semicolons, started on entity knockback (hehe, doesn't work well, needs improvement)
This commit is contained in:
parent
2a376626e6
commit
8215985f7f
11 changed files with 106 additions and 71 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
var Vec3 = require("vec3").Vec3;
|
||||||
|
|
||||||
module.exports.player=function(player,serv)
|
module.exports.player=function(player,serv)
|
||||||
{
|
{
|
||||||
player.changeBlock=async (position,blockType,blockData) =>
|
player.changeBlock=async (position,blockType,blockData) =>
|
||||||
|
|
|
||||||
|
|
@ -22,24 +22,30 @@ module.exports.server=function(serv)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.player=function(player,serv)
|
module.exports.entity=function(entity,serv)
|
||||||
{
|
{
|
||||||
player._writeOthers= (packetName, packetFields) =>
|
|
||||||
player
|
entity.getNearby = () => serv
|
||||||
.getOthers()
|
.getNearbyEntities({
|
||||||
.forEach((otherPlayer) => otherPlayer._client.write(packetName, packetFields));
|
world: entity.world,
|
||||||
|
position: entity.position,
|
||||||
|
radius: entity.viewDistance*32
|
||||||
|
})
|
||||||
|
.filter((e) => e != entity);
|
||||||
|
|
||||||
player._writeOthersNearby = (packetName, packetFields) =>
|
entity.getOtherPlayers = () => serv.players.filter((p) => p != entity);
|
||||||
serv._writeArray(packetName, packetFields, player.nearbyPlayers());
|
|
||||||
|
|
||||||
player.getOthers = () => serv.players.filter((otherPlayer) => otherPlayer != player);
|
entity.getOthers = () => serv.entities.filter((e) => e != entity);
|
||||||
|
|
||||||
player.getNearbyPlayers = (radius=player.viewDistance*32) => serv.getNearby({
|
entity.getNearbyPlayers = (radius=entity.viewDistance*32) => entity.getNearby()
|
||||||
world: player.world,
|
.filter((e) => e.type == 'player');
|
||||||
position: player.position,
|
|
||||||
radius: radius
|
|
||||||
});
|
|
||||||
|
|
||||||
player.nearbyPlayers = (radius=player.viewDistance*32) => player.nearbyEntities
|
entity.nearbyPlayers = (radius=entity.viewDistance*32) => entity.nearbyEntities
|
||||||
.filter(e => e.type == 'player');
|
.filter(e => e.type == '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)
|
module.exports.player=function(player,serv)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ module.exports.server=function(serv,options) {
|
||||||
object.yaw = yaw;
|
object.yaw = yaw;
|
||||||
object.gravity = new Vec3(0, -20*32, 0);
|
object.gravity = new Vec3(0, -20*32, 0);
|
||||||
object.terminalvelocity = new Vec3(27*32, 27*32, 27*32);
|
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.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.deathTime = 60*1000; // 60 seconds
|
||||||
object.pickupTime = 200;
|
object.pickupTime = 200;
|
||||||
|
|
@ -49,7 +49,7 @@ module.exports.server=function(serv,options) {
|
||||||
mob.yaw = yaw;
|
mob.yaw = yaw;
|
||||||
mob.gravity = new Vec3(0, -20*32, 0);
|
mob.gravity = new Vec3(0, -20*32, 0);
|
||||||
mob.terminalvelocity = new Vec3(27*32, 27*32, 27*32);
|
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.size = new Vec3(0.75, 1.75, 0.75);
|
||||||
mob.metadata = metadata;
|
mob.metadata = metadata;
|
||||||
|
|
||||||
|
|
@ -57,11 +57,8 @@ module.exports.server=function(serv,options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
serv.destroyEntity = entity => {
|
serv.destroyEntity = entity => {
|
||||||
serv._writeNearby('entity_destroy', {
|
entity._writeOthersNearby('entity_destroy', {
|
||||||
entityIds: [entity.id]
|
entityIds: [entity.id]
|
||||||
}, {
|
|
||||||
position: entity.position,
|
|
||||||
world: entity.world
|
|
||||||
});
|
});
|
||||||
delete serv.entities[entity.id];
|
delete serv.entities[entity.id];
|
||||||
};
|
};
|
||||||
|
|
@ -174,10 +171,10 @@ module.exports.entity=function(entity,serv){
|
||||||
});
|
});
|
||||||
|
|
||||||
entity.sendMetadata = (data) => {
|
entity.sendMetadata = (data) => {
|
||||||
serv._writeNearby('entity_metadata', {
|
entity._writeOthersNearby('entity_metadata', {
|
||||||
entityId: entity.id,
|
entityId: entity.id,
|
||||||
metadata: data
|
metadata: data
|
||||||
}, entity);
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
entity.setAndUpdateMetadata = (data) => {
|
entity.setAndUpdateMetadata = (data) => {
|
||||||
|
|
@ -237,14 +234,6 @@ 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 = () => {
|
entity.updateAndSpawn = () => {
|
||||||
var updatedEntities=entity.getNearby();
|
var updatedEntities=entity.getNearby();
|
||||||
var entitiesToAdd=updatedEntities.filter(e => entity.nearbyEntities.indexOf(e)==-1);
|
var entitiesToAdd=updatedEntities.filter(e => entity.nearbyEntities.indexOf(e)==-1);
|
||||||
|
|
@ -269,11 +258,37 @@ module.exports.entity=function(entity,serv){
|
||||||
entity.collect = (collectEntity) => {
|
entity.collect = (collectEntity) => {
|
||||||
if (entity.type != 'player') serv.emit('error', 'Non-player entity (ttype ' + entity.type + ') cannot collect another entity');
|
if (entity.type != 'player') serv.emit('error', 'Non-player entity (ttype ' + entity.type + ') cannot collect another entity');
|
||||||
else {
|
else {
|
||||||
serv._writeNearby('collect', {
|
collectEntity._writeOthersNearby('collect', {
|
||||||
collectedEntityId: collectEntity.id,
|
collectedEntityId: collectEntity.id,
|
||||||
collectorEntityId: entity.id
|
collectorEntityId: entity.id
|
||||||
}, collectEntity);
|
});
|
||||||
entity.playSoundAtSelf('random.pop');
|
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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
var Entity=require("prismarine-entity");
|
var Entity=require("prismarine-entity");
|
||||||
var Vec3 = require("vec3").Vec3
|
var Vec3 = require("vec3").Vec3;
|
||||||
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var requireIndex = require('requireindex');
|
var requireIndex = require('requireindex');
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
var Vec3 = require("vec3").Vec3
|
var Vec3 = require("vec3").Vec3;
|
||||||
var dir = require("node-dir");
|
var dir = require("node-dir");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,27 @@
|
||||||
var Vec3 = require("vec3").Vec3
|
var Vec3 = require("vec3").Vec3;
|
||||||
|
|
||||||
module.exports.server=function(serv) {
|
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({
|
var players = (typeof whitelist != 'undefined' ? (typeof whitelist == 'array' ? whitelist : [whitelist]) : serv.getNearby({
|
||||||
world: world,
|
world: world,
|
||||||
position: position.scaled(32).floored(),
|
position: position.scaled(32).floored(),
|
||||||
radius: radius // 32 blocks, fixed position
|
radius: radius // 32 blocks, fixed position
|
||||||
}));
|
}));
|
||||||
if (!size) size = new Vec3(1.0, 1.0, 1.0);
|
if (!size) size = new Vec3(1.0, 1.0, 1.0);
|
||||||
players.filter(player => blacklist.indexOf(player) == -1)
|
|
||||||
.forEach(player => {
|
serv._writeNearby('world_particles', {
|
||||||
player._client.write('world_particles', {
|
particleId: particle,
|
||||||
particleId: particle,
|
longDistance: longDistance,
|
||||||
longDistance: longDistance || true,
|
x: position.x,
|
||||||
x: position.x,
|
y: position.y,
|
||||||
y: position.y,
|
z: position.z,
|
||||||
z: position.z,
|
offsetX: size.x,
|
||||||
offsetX: size.x,
|
offsetY: size.y,
|
||||||
offsetY: size.y,
|
offsetZ: size.z,
|
||||||
offsetZ: size.z,
|
particleData: 1.0,
|
||||||
particleData: 1.0,
|
particles: count,
|
||||||
particles: count || 1,
|
data: []
|
||||||
data: []
|
}, players.filter(p => blacklist.indexOf(p) == -1));
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
var blocks=require("minecraft-data")(require("../version")).blocks;
|
var blocks=require("minecraft-data")(require("../version")).blocks;
|
||||||
var Vec3 = require("vec3").Vec3
|
var Vec3 = require("vec3").Vec3;
|
||||||
|
|
||||||
module.exports.entity=function(entity){
|
module.exports.entity=function(entity){
|
||||||
entity.calculatePhysics = async (delta) => {
|
entity.calculatePhysics = async (delta) => {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
var Vec3 = require("vec3").Vec3;
|
||||||
|
|
||||||
module.exports.player=function(player,serv)
|
module.exports.player=function(player,serv)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -12,20 +14,23 @@ module.exports.player=function(player,serv)
|
||||||
|
|
||||||
function attackEntity(entityId)
|
function attackEntity(entityId)
|
||||||
{
|
{
|
||||||
if (!serv.entities[entityId]) return; // ?????
|
var attackedEntity = serv.entities[entityId];
|
||||||
var attackedPlayer = serv.entities[entityId].player;
|
if(!attackedEntity || (attackedEntity.gameMode != 0 && attackedEntity.type == 'player')) return;
|
||||||
if(!attackedPlayer || attackedPlayer.gameMode!=0) return;
|
|
||||||
attackedPlayer.updateHealth(attackedplayer.health - 1);
|
|
||||||
serv.playSound('game.player.hurt', player.world, attackedplayer.position.scaled(1/32));
|
|
||||||
|
|
||||||
if(attackedplayer.health==0)
|
attackedEntity.updateHealth(attackedEntity.health - 1);
|
||||||
attackedPlayer._writeOthers('entity_status',{
|
serv.playSound('game.player.hurt', player.world, attackedEntity.position.scaled(1/32));
|
||||||
entityId:attackedplayer.id,
|
|
||||||
|
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
|
entityStatus:3
|
||||||
});
|
});
|
||||||
else
|
else
|
||||||
attackedPlayer._writeOthers('animation',{
|
attackedEntity._writeOthers('animation',{
|
||||||
entityId:attackedplayer.id,
|
entityId:attackedEntity.id,
|
||||||
animation:1
|
animation:1
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -35,4 +40,13 @@ module.exports.player=function(player,serv)
|
||||||
attackEntity(target);
|
attackEntity(target);
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.entity=function(entity,serv)
|
||||||
|
{
|
||||||
|
if (entity.type != 'player') {
|
||||||
|
entity.updateHealth = (health) => {
|
||||||
|
entity.health = health;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
var Vec3 = require("vec3").Vec3
|
var Vec3 = require("vec3").Vec3;
|
||||||
|
|
||||||
Vec3.prototype.toFixedPosition=function() {
|
Vec3.prototype.toFixedPosition=function() {
|
||||||
return this.scaled(32).floored();
|
return this.scaled(32).floored();
|
||||||
|
|
@ -90,7 +90,7 @@ module.exports.entity=function(entity,serv){
|
||||||
var diff = entity.position.minus(oldPos);
|
var diff = entity.position.minus(oldPos);
|
||||||
|
|
||||||
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)
|
||||||
serv._writeNearby('entity_teleport', {
|
entity._writeOthersNearby('entity_teleport', {
|
||||||
entityId: entity.id,
|
entityId: entity.id,
|
||||||
x: entity.position.x,
|
x: entity.position.x,
|
||||||
y: entity.position.y,
|
y: entity.position.y,
|
||||||
|
|
@ -98,7 +98,7 @@ module.exports.entity=function(entity,serv){
|
||||||
yaw: entity.yaw,
|
yaw: entity.yaw,
|
||||||
pitch: entity.pitch,
|
pitch: entity.pitch,
|
||||||
onGround: onGround
|
onGround: onGround
|
||||||
}, entity);
|
});
|
||||||
else if (diff.distanceTo(new Vec3(0, 0, 0)) != 0) serv._writeNearby('rel_entity_move', {
|
else if (diff.distanceTo(new Vec3(0, 0, 0)) != 0) serv._writeNearby('rel_entity_move', {
|
||||||
entityId: entity.id,
|
entityId: entity.id,
|
||||||
dX: diff.x,
|
dX: diff.x,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
var Vec3 = require("vec3").Vec3
|
var Vec3 = require("vec3").Vec3;
|
||||||
var spiralloop = require('spiralloop');
|
var spiralloop = require('spiralloop');
|
||||||
|
|
||||||
var Chunk = require('prismarine-chunk')(require("../version"));
|
var Chunk = require('prismarine-chunk')(require("../version"));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue