mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-05 03:00:46 +00:00
add player.spawnAPlayer, player.despawnPlayers, player.updateAndSpawnNearbyPlayers, automatically spawn and despawn players when getting closer or far
This commit is contained in:
parent
c2b1424743
commit
2a3b8a157b
7 changed files with 81 additions and 53 deletions
28
doc/api.md
28
doc/api.md
|
|
@ -29,7 +29,6 @@
|
||||||
- [serv.createLog()](#servcreatelog)
|
- [serv.createLog()](#servcreatelog)
|
||||||
- [serv.log(message)](#servlogmessage)
|
- [serv.log(message)](#servlogmessage)
|
||||||
- [serv.broadcast(message[,color])](#servbroadcastmessagecolor)
|
- [serv.broadcast(message[,color])](#servbroadcastmessagecolor)
|
||||||
- [serv.setBlock(position,blockType)](#servsetblockpositionblocktype)
|
|
||||||
- [serv.getPlayer(username)](#servgetplayerusername)
|
- [serv.getPlayer(username)](#servgetplayerusername)
|
||||||
- [serv.getNearby(loc)](#servgetnearbyloc)
|
- [serv.getNearby(loc)](#servgetnearbyloc)
|
||||||
- [server.banUsername(username,reason,callback)](#serverbanusernameusernamereasoncallback)
|
- [server.banUsername(username,reason,callback)](#serverbanusernameusernamereasoncallback)
|
||||||
|
|
@ -49,6 +48,7 @@
|
||||||
- [player.username](#playerusername)
|
- [player.username](#playerusername)
|
||||||
- [player.view](#playerview)
|
- [player.view](#playerview)
|
||||||
- [player.world](#playerworld)
|
- [player.world](#playerworld)
|
||||||
|
- [player.nearbyPlayers](#playernearbyplayers)
|
||||||
- [Events](#events-1)
|
- [Events](#events-1)
|
||||||
- ["connected"](#connected)
|
- ["connected"](#connected)
|
||||||
- ["spawned"](#spawned)
|
- ["spawned"](#spawned)
|
||||||
|
|
@ -66,11 +66,14 @@
|
||||||
- [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype)
|
- [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype)
|
||||||
- [player.sendBlock(position,blockType)](#playersendblockpositionblocktype)
|
- [player.sendBlock(position,blockType)](#playersendblockpositionblocktype)
|
||||||
- [player.sendInitialPosition()](#playersendinitialposition)
|
- [player.sendInitialPosition()](#playersendinitialposition)
|
||||||
- [player.spawn()](#playerspawn)
|
|
||||||
- [player.setGameMode(gameMode)](#playersetgamemodegamemode)
|
- [player.setGameMode(gameMode)](#playersetgamemodegamemode)
|
||||||
- [player.handleCommand(command)](#playerhandlecommandcommand)
|
- [player.handleCommand(command)](#playerhandlecommandcommand)
|
||||||
|
- [player.setBlock(position,blockType)](#playersetblockpositionblocktype)
|
||||||
- [player.updateHealth(health)](#playerupdatehealthhealth)
|
- [player.updateHealth(health)](#playerupdatehealthhealth)
|
||||||
- [player.changeWorld(world, opt)](#playerchangeworldworld-opt)
|
- [player.changeWorld(world, opt)](#playerchangeworldworld-opt)
|
||||||
|
- [player.spawnAPlayer(spawnedPlayer)](#playerspawnaplayerspawnedplayer)
|
||||||
|
- [player.despawnPlayers(despawnedPlayers)](#playerdespawnplayersdespawnedplayers)
|
||||||
|
- [player.updateAndSpawnNearbyPlayers()](#playerupdateandspawnnearbyplayers)
|
||||||
- [Low level properties](#low-level-properties)
|
- [Low level properties](#low-level-properties)
|
||||||
- [player._client](#player_client)
|
- [player._client](#player_client)
|
||||||
- [Low level methods](#low-level-methods-1)
|
- [Low level methods](#low-level-methods-1)
|
||||||
|
|
@ -264,6 +267,10 @@ The view size of the player, for example 8 for 16x16
|
||||||
|
|
||||||
The world which the player is in.
|
The world which the player is in.
|
||||||
|
|
||||||
|
#### player.nearbyPlayers
|
||||||
|
|
||||||
|
Nearby players.
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
#### "connected"
|
#### "connected"
|
||||||
|
|
@ -333,10 +340,6 @@ this will not make any changes on the server's world and only sends it to the us
|
||||||
|
|
||||||
send its initial position to the player
|
send its initial position to the player
|
||||||
|
|
||||||
#### player.spawn()
|
|
||||||
|
|
||||||
tell everybody else that the player spawned
|
|
||||||
|
|
||||||
#### player.setGameMode(gameMode)
|
#### player.setGameMode(gameMode)
|
||||||
|
|
||||||
set player gameMode to `gameMode`
|
set player gameMode to `gameMode`
|
||||||
|
|
@ -364,6 +367,19 @@ The world object which the player is in (use serv.overworld, serv.netherworld, s
|
||||||
- yaw: Yaw in which they spawn, default is 0
|
- yaw: Yaw in which they spawn, default is 0
|
||||||
- pitch: Pitch in which they spawn, default is 0
|
- pitch: Pitch in which they spawn, default is 0
|
||||||
|
|
||||||
|
|
||||||
|
#### player.spawnAPlayer(spawnedPlayer)
|
||||||
|
|
||||||
|
Spawn `spawnedPlayer` for `player`.
|
||||||
|
|
||||||
|
#### player.despawnPlayers(despawnedPlayers)
|
||||||
|
|
||||||
|
Despawn `despawnedPlayers` for `player`.
|
||||||
|
|
||||||
|
#### player.updateAndSpawnNearbyPlayers()
|
||||||
|
|
||||||
|
Spawn and despawn the correct players depending on distance for `player`.
|
||||||
|
|
||||||
### Low level properties
|
### Low level properties
|
||||||
|
|
||||||
#### player._client
|
#### player._client
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ function inject(serv,player)
|
||||||
|
|
||||||
function setBlock(position,blockType)
|
function setBlock(position,blockType)
|
||||||
{
|
{
|
||||||
player.getNearby().forEach(function(player){
|
serv.players.filter(p => p.world==player.world).forEach(function(player){
|
||||||
player.sendBlock(position, blockType);
|
player.sendBlock(position, blockType);
|
||||||
});
|
});
|
||||||
return player.world.setBlockType(position,blockType);
|
return player.world.setBlockType(position,blockType);
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ function inject(serv,player)
|
||||||
};
|
};
|
||||||
|
|
||||||
player._writeOthersNearby = function(packetName, packetFields) {
|
player._writeOthersNearby = function(packetName, packetFields) {
|
||||||
serv._writeArray(packetName, packetFields, player.getNearby());
|
serv._writeArray(packetName, packetFields, player.nearbyPlayers);
|
||||||
};
|
};
|
||||||
|
|
||||||
player.getOthers = function() {
|
player.getOthers = function() {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,33 @@ function inject(serv,player)
|
||||||
serv.players.push(player);
|
serv.players.push(player);
|
||||||
serv.uuidToPlayer[player._client.uuid] = player;
|
serv.uuidToPlayer[player._client.uuid] = player;
|
||||||
player.loadedChunks={};
|
player.loadedChunks={};
|
||||||
|
player.nearbyPlayers=[];
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateAndSpawnNearbyPlayers()
|
||||||
|
{
|
||||||
|
player.lastPositionPlayersUpdated=player.entity.position;
|
||||||
|
var updatedPlayers=player.getNearby();
|
||||||
|
var playersToAdd=updatedPlayers.filter(p => player.nearbyPlayers.indexOf(p)==-1);
|
||||||
|
var playersToRemove=player.nearbyPlayers.filter(p => updatedPlayers.indexOf(p)==-1);
|
||||||
|
player.despawnPlayers(playersToRemove);
|
||||||
|
playersToAdd.forEach(player.spawnAPlayer);
|
||||||
|
|
||||||
|
playersToRemove.forEach(p => p.despawnPlayers([player]));
|
||||||
|
playersToRemove.forEach(p => p.nearbyPlayers=p.getNearby());
|
||||||
|
playersToAdd.forEach(p => p.spawnAPlayer(player));
|
||||||
|
playersToAdd.forEach(p => p.nearbyPlayers=p.getNearby());
|
||||||
|
|
||||||
|
player.nearbyPlayers=updatedPlayers;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendPlayersWhenMove()
|
||||||
|
{
|
||||||
|
player.on("positionChanged",function(){
|
||||||
|
if(player.entity.position.distanceTo(player.lastPositionPlayersUpdated)>2*32)
|
||||||
|
player.updateAndSpawnNearbyPlayers();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendLogin()
|
function sendLogin()
|
||||||
|
|
@ -41,7 +68,7 @@ function inject(serv,player)
|
||||||
{
|
{
|
||||||
player.on("positionChanged",function(){
|
player.on("positionChanged",function(){
|
||||||
if(!player.sendingChunks && player.entity.position.distanceTo(player.lastPositionChunkUpdated)>16*32)
|
if(!player.sendingChunks && player.entity.position.distanceTo(player.lastPositionChunkUpdated)>16*32)
|
||||||
player.sendMap();
|
player.sendRestMap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,22 +135,23 @@ function inject(serv,player)
|
||||||
sendLogin();
|
sendLogin();
|
||||||
await player.sendMap();
|
await player.sendMap();
|
||||||
player.sendSpawnPosition();
|
player.sendSpawnPosition();
|
||||||
|
player.sendPosition();
|
||||||
player.updateHealth(player.entity.health);
|
player.updateHealth(player.entity.health);
|
||||||
|
|
||||||
|
|
||||||
updateTime();
|
updateTime();
|
||||||
setGameMode(player.gameMode);
|
setGameMode(player.gameMode);
|
||||||
fillTabList();
|
fillTabList();
|
||||||
player.spawnForOthers();
|
player.updateAndSpawnNearbyPlayers();
|
||||||
player.sendNearbyPlayers();
|
|
||||||
|
|
||||||
player.spawn();
|
|
||||||
announceJoin();
|
announceJoin();
|
||||||
player.emit("spawned");
|
player.emit("spawned");
|
||||||
|
sendPlayersWhenMove();
|
||||||
|
|
||||||
setTimeout(function(){player.sendRestMap();sendChunkWhenMove();},100);
|
setTimeout(function(){player.sendRestMap();sendChunkWhenMove();},100);
|
||||||
}
|
}
|
||||||
|
|
||||||
player.setGameMode=setGameMode;
|
player.setGameMode=setGameMode;
|
||||||
player.login=login;
|
player.login=login;
|
||||||
|
player.updateAndSpawnNearbyPlayers=updateAndSpawnNearbyPlayers;
|
||||||
}
|
}
|
||||||
|
|
@ -2,6 +2,12 @@ module.exports=inject;
|
||||||
|
|
||||||
function inject(serv,player)
|
function inject(serv,player)
|
||||||
{
|
{
|
||||||
|
function despawnPlayers(despawnedPlayers) {
|
||||||
|
player._client.write('entity_destroy', {
|
||||||
|
'entityIds': despawnedPlayers.map(p => p.entity.id)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
player._client.on('end', function () {
|
player._client.on('end', function () {
|
||||||
if(player.entity) {
|
if(player.entity) {
|
||||||
serv.broadcast(player.username + ' quit the game.', "yellow");
|
serv.broadcast(player.username + ' quit the game.', "yellow");
|
||||||
|
|
@ -11,7 +17,7 @@ function inject(serv,player)
|
||||||
UUID: player._client.uuid
|
UUID: player._client.uuid
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
player._writeOthersNearby('entity_destroy', {'entityIds': [player.entity.id]});
|
player.nearbyPlayers.forEach(otherPlayer => otherPlayer.despawnPlayers([player]));
|
||||||
delete serv.entities[player.entity.id];
|
delete serv.entities[player.entity.id];
|
||||||
player.emit('disconnected');
|
player.emit('disconnected');
|
||||||
var index = serv.players.indexOf(player);
|
var index = serv.players.indexOf(player);
|
||||||
|
|
@ -26,4 +32,6 @@ function inject(serv,player)
|
||||||
player._client.on('error', function (error) {
|
player._client.on('error', function (error) {
|
||||||
player.emit('error',error);
|
player.emit('error',error);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
player.despawnPlayers=despawnPlayers;
|
||||||
}
|
}
|
||||||
|
|
@ -10,9 +10,9 @@ function inject(serv, player)
|
||||||
gamemode:player.gameMode,
|
gamemode:player.gameMode,
|
||||||
levelType:'default'
|
levelType:'default'
|
||||||
});
|
});
|
||||||
player.sendInitialPosition();
|
player.sendPosition();
|
||||||
player.updateHealth(20);
|
player.updateHealth(20);
|
||||||
player.spawn();
|
player.updateAndSpawnNearbyPlayers();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -4,37 +4,18 @@ var spiralloop = require('spiralloop');
|
||||||
module.exports = inject;
|
module.exports = inject;
|
||||||
|
|
||||||
function inject(serv, player) {
|
function inject(serv, player) {
|
||||||
function spawn() {
|
|
||||||
player.sendPosition();
|
|
||||||
}
|
|
||||||
|
|
||||||
function spawnForOthers() {
|
function spawnAPlayer(spawnedPlayer) {
|
||||||
player._writeOthersNearby('named_entity_spawn',{
|
player._client.write('named_entity_spawn', {
|
||||||
entityId: player.entity.id,
|
entityId: spawnedPlayer.entity.id,
|
||||||
playerUUID: player._client.uuid,
|
playerUUID: spawnedPlayer._client.uuid,
|
||||||
x: player.entity.position.x,
|
x: spawnedPlayer.entity.position.x,
|
||||||
y: player.entity.position.y,
|
y: spawnedPlayer.entity.position.y,
|
||||||
z: player.entity.position.z,
|
z: spawnedPlayer.entity.position.z,
|
||||||
yaw: player.entity.yaw,
|
yaw: spawnedPlayer.entity.yaw,
|
||||||
pitch: player.entity.pitch,
|
pitch: spawnedPlayer.entity.pitch,
|
||||||
currentItem: 0,
|
currentItem: 0,
|
||||||
metadata: player.entity.metadata
|
metadata: spawnedPlayer.entity.metadata
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function sendNearbyPlayers() {
|
|
||||||
player.getNearby().forEach(function (otherPlayer) {
|
|
||||||
player._client.write('named_entity_spawn', {
|
|
||||||
entityId: otherPlayer.entity.id,
|
|
||||||
playerUUID: otherPlayer._client.uuid,
|
|
||||||
x: otherPlayer.entity.position.x,
|
|
||||||
y: otherPlayer.entity.position.y,
|
|
||||||
z: otherPlayer.entity.position.z,
|
|
||||||
yaw: otherPlayer.entity.yaw,
|
|
||||||
pitch: otherPlayer.entity.pitch,
|
|
||||||
currentItem: 0,
|
|
||||||
metadata: otherPlayer.entity.metadata
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,9 +40,8 @@ function inject(serv, player) {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendNearbyChunks(view, world)
|
function sendNearbyChunks(view)
|
||||||
{
|
{
|
||||||
world = world || player.world;
|
|
||||||
player.lastPositionChunkUpdated=player.entity.position;
|
player.lastPositionChunkUpdated=player.entity.position;
|
||||||
var playerChunkX=Math.floor(player.entity.position.x/16/32);
|
var playerChunkX=Math.floor(player.entity.position.x/16/32);
|
||||||
var playerChunkZ=Math.floor(player.entity.position.z/16/32);
|
var playerChunkZ=Math.floor(player.entity.position.z/16/32);
|
||||||
|
|
@ -80,7 +60,7 @@ function inject(serv, player) {
|
||||||
.reduce((acc,{chunkX,chunkZ})=>
|
.reduce((acc,{chunkX,chunkZ})=>
|
||||||
acc
|
acc
|
||||||
//.then(() => sleep(100))
|
//.then(() => sleep(100))
|
||||||
.then(() => world.getColumn(chunkX,chunkZ))
|
.then(() => player.world.getColumn(chunkX,chunkZ))
|
||||||
.then((column) => player.sendChunk(chunkX,chunkZ,column))
|
.then((column) => player.sendChunk(chunkX,chunkZ,column))
|
||||||
,Promise.resolve());
|
,Promise.resolve());
|
||||||
}
|
}
|
||||||
|
|
@ -112,7 +92,6 @@ function inject(serv, player) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function changeWorld(world, opt) {
|
async function changeWorld(world, opt) {
|
||||||
player._writeOthersNearby('entity_destroy', {'entityIds': [player.entity.id]});
|
|
||||||
|
|
||||||
opt = opt || {};
|
opt = opt || {};
|
||||||
player.world = world;
|
player.world = world;
|
||||||
|
|
@ -126,9 +105,8 @@ function inject(serv, player) {
|
||||||
});
|
});
|
||||||
player.entity.position=player.spawnPoint.toFixedPosition();
|
player.entity.position=player.spawnPoint.toFixedPosition();
|
||||||
player.sendSpawnPosition();
|
player.sendSpawnPosition();
|
||||||
|
player.updateAndSpawnNearbyPlayers();
|
||||||
|
|
||||||
player.spawnForOthers();
|
|
||||||
player.sendNearbyPlayers();
|
|
||||||
await player.sendMap();
|
await player.sendMap();
|
||||||
setTimeout(player.sendRestMap,100);
|
setTimeout(player.sendRestMap,100);
|
||||||
player.sendPosition();
|
player.sendPosition();
|
||||||
|
|
@ -136,15 +114,13 @@ function inject(serv, player) {
|
||||||
player.emit('change_world');
|
player.emit('change_world');
|
||||||
}
|
}
|
||||||
|
|
||||||
player.spawn = spawn;
|
|
||||||
player.spawnForOthers = spawnForOthers;
|
|
||||||
player.sendNearbyPlayers = sendNearbyPlayers;
|
|
||||||
player.sendNearbyChunks = sendNearbyChunks;
|
player.sendNearbyChunks = sendNearbyChunks;
|
||||||
player.changeWorld = changeWorld;
|
player.changeWorld = changeWorld;
|
||||||
player.sendChunk = sendChunk;
|
player.sendChunk = sendChunk;
|
||||||
player.sendMap = sendMap;
|
player.sendMap = sendMap;
|
||||||
player.sendRestMap = sendRestMap;
|
player.sendRestMap = sendRestMap;
|
||||||
player.sendSpawnPosition = sendSpawnPosition;
|
player.sendSpawnPosition = sendSpawnPosition;
|
||||||
|
player.spawnAPlayer = spawnAPlayer;
|
||||||
|
|
||||||
player.on('chat', function(message) {
|
player.on('chat', function(message) {
|
||||||
if (message == 'world') {
|
if (message == 'world') {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue