add logout to its own file, expose sendInitialPosition and spawn, implement respawn, fix #16

This commit is contained in:
Romain Beaumont 2015-08-27 23:26:12 +02:00
parent ec2ba6696d
commit caedd14632
4 changed files with 66 additions and 30 deletions

View file

@ -58,6 +58,8 @@
- [player.others()](#playerothers)
- [player.chat(message)](#playerchatmessage)
- [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype)
- [player.sendInitialPosition()](#playersendinitialposition)
- [player.spawn()](#playerspawn)
- [Low level properties](#low-level-properties)
- [player._client](#player_client)
- [Low level methods](#low-level-methods)
@ -268,6 +270,15 @@ sends `message` to the player
change the block at position `position` to `blockType`
#### player.sendInitialPosition()
send its initial position to the player
#### player.spawn()
tell everybody else that the player spawned
### Low level properties
#### player._client

View file

@ -15,8 +15,6 @@ function toFixedPosition(p)
function inject(serv,player)
{
player.login=login;
function addPlayer()
{
serv.entityMaxId++;
@ -54,7 +52,7 @@ function inject(serv,player)
});
}
function sendSpawn()
function sendSpawnPosition()
{
console.log("setting spawn at "+player.spawnPoint);
player._client.write('spawn_position',{
@ -122,7 +120,8 @@ function inject(serv,player)
});
}
function spawn()
function spawnOthers()
{
player.getOthers().forEach(function (otherPlayer) {
var spawnPoint=toFixedPosition(otherPlayer.spawnPoint);
@ -140,6 +139,10 @@ function inject(serv,player)
});
});
}
function spawn()
{
var spawnPoint=toFixedPosition(player.spawnPoint);
player._writeOthers('named_entity_spawn',{
entityId: player.entity.id,
@ -154,6 +157,7 @@ function inject(serv,player)
});
}
function announceJoin()
{
serv.broadcast(player.username + ' joined the game.', "yellow");
@ -169,7 +173,7 @@ function inject(serv,player)
addPlayer();
sendLogin();
sendMap();
sendSpawn();
sendSpawnPosition();
sendInitialPosition();
player.emit("spawned");
@ -177,32 +181,14 @@ function inject(serv,player)
updateTime();
updateGameState();
fillTabList();
spawnOthers();
spawn();
announceJoin();
player._client.on('end', function () {
serv.broadcast(player.username + ' quit the game.', "yellow");
player._writeOthers('player_info', {
action: 4,
data: [{
UUID: transformUuid(player._client.uuid)
}]
});
player._writeOthers('entity_destroy', {'entityIds': [player.entity.id]});
delete serv.entities[player.entity.id]
player.emit('disconnect');
var index = serv.players.indexOf(player);
if (index > -1) {
serv.players.splice(index,1);
}
delete serv.uuidToPlayer[player._client.uuid];
});
player._client.on('error', function (error) {
player.emit('error',error);
});
}
player.login=login;
player.sendInitialPosition=sendInitialPosition;
player.spawn=spawn;
}

View file

@ -0,0 +1,32 @@
module.exports=inject;
function transformUuid(s)
{
return s.split("-").map(function(item) { return parseInt(item, 16); });
}
function inject(serv,player)
{
player._client.on('end', function () {
serv.broadcast(player.username + ' quit the game.', "yellow");
player._writeOthers('player_info', {
action: 4,
data: [{
UUID: transformUuid(player._client.uuid)
}]
});
player._writeOthers('entity_destroy', {'entityIds': [player.entity.id]});
delete serv.entities[player.entity.id]
player.emit('disconnect');
var index = serv.players.indexOf(player);
if (index > -1) {
serv.players.splice(index,1);
}
delete serv.uuidToPlayer[player._client.uuid];
});
player._client.on('error', function (error) {
player.emit('error',error);
});
}

View file

@ -4,7 +4,14 @@ function inject(serv, player)
{
player._client.on("client_command", function(packet) {
if(packet.payload == 0) {
// respawn the player
player._client.write("respawn",{
dimension:0,
difficulty:0,
gamemode:player.gameMode,
levelType:'default'
});
player.sendInitialPosition();
player.spawn();
}
});
}