mirror of
https://github.com/danbulant/flying-squid
synced 2026-06-11 10:31:08 +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)
|
||||
{
|
||||
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
|
||||
.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.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.viewDistance*32) => player.nearbyEntities
|
||||
.filter(e => e.type == 'player');
|
||||
};
|
||||
entity.nearbyPlayers = (radius=entity.viewDistance*32) => entity.nearbyEntities
|
||||
.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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ 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;
|
||||
|
|
@ -49,7 +49,7 @@ 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.metadata = metadata;
|
||||
|
||||
|
|
@ -57,11 +57,8 @@ module.exports.server=function(serv,options) {
|
|||
};
|
||||
|
||||
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];
|
||||
};
|
||||
|
|
@ -174,10 +171,10 @@ module.exports.entity=function(entity,serv){
|
|||
});
|
||||
|
||||
entity.sendMetadata = (data) => {
|
||||
serv._writeNearby('entity_metadata', {
|
||||
entity._writeOthersNearby('entity_metadata', {
|
||||
entityId: entity.id,
|
||||
metadata: data
|
||||
}, entity);
|
||||
});
|
||||
};
|
||||
|
||||
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 = () => {
|
||||
var updatedEntities=entity.getNearby();
|
||||
var entitiesToAdd=updatedEntities.filter(e => entity.nearbyEntities.indexOf(e)==-1);
|
||||
|
|
@ -269,11 +258,37 @@ module.exports.entity=function(entity,serv){
|
|||
entity.collect = (collectEntity) => {
|
||||
if (entity.type != 'player') serv.emit('error', 'Non-player entity (ttype ' + entity.type + ') cannot collect another entity');
|
||||
else {
|
||||
serv._writeNearby('collect', {
|
||||
collectEntity._writeOthersNearby('collect', {
|
||||
collectedEntityId: collectEntity.id,
|
||||
collectorEntityId: entity.id
|
||||
}, collectEntity);
|
||||
});
|
||||
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 Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
|
||||
var path = require('path');
|
||||
var requireIndex = require('requireindex');
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
var Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
var dir = require("node-dir");
|
||||
var fs = require("fs");
|
||||
|
||||
|
|
|
|||
|
|
@ -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._writeNearby('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));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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,3 +1,5 @@
|
|||
var Vec3 = require("vec3").Vec3;
|
||||
|
||||
module.exports.player=function(player,serv)
|
||||
{
|
||||
|
||||
|
|
@ -12,20 +14,23 @@ module.exports.player=function(player,serv)
|
|||
|
||||
function attackEntity(entityId)
|
||||
{
|
||||
if (!serv.entities[entityId]) return; // ?????
|
||||
var attackedPlayer = serv.entities[entityId].player;
|
||||
if(!attackedPlayer || attackedPlayer.gameMode!=0) return;
|
||||
attackedPlayer.updateHealth(attackedplayer.health - 1);
|
||||
serv.playSound('game.player.hurt', player.world, attackedplayer.position.scaled(1/32));
|
||||
var attackedEntity = serv.entities[entityId];
|
||||
if(!attackedEntity || (attackedEntity.gameMode != 0 && attackedEntity.type == 'player')) return;
|
||||
|
||||
if(attackedplayer.health==0)
|
||||
attackedPlayer._writeOthers('entity_status',{
|
||||
entityId:attackedplayer.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.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
var Vec3 = require("vec3").Vec3
|
||||
var Vec3 = require("vec3").Vec3;
|
||||
|
||||
Vec3.prototype.toFixedPosition=function() {
|
||||
return this.scaled(32).floored();
|
||||
|
|
@ -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"));
|
||||
|
|
|
|||
Loading…
Reference in a new issue