implement looking, thanks @Gjum and @mhsjlw

This commit is contained in:
Romain Beaumont 2015-08-27 22:21:54 +02:00
parent ef632d0919
commit 2c6e8c18a7
3 changed files with 56 additions and 22 deletions

View file

@ -16,6 +16,7 @@ function Entity(id) {
this.equipment = new Array(5);
this.heldItem = this.equipment[0]; // shortcut to equipment[0]
this.isValid = true;
this.metadata = [];
}
Entity.prototype.setEquipment = function(index, item) {

View file

@ -64,12 +64,15 @@ function inject(serv,player)
function sendInitialPosition()
{
player.entity.position=player.spawnPoint;
player.entity.yaw=0;
player.entity.pitch=0;
player._client.write('position', {
x: player.spawnPoint.x,
y: player.spawnPoint.y,
z: player.spawnPoint.z,
yaw: 0,
pitch: 0,
x: player.entity.position.x,
y: player.entity.position.y,
z: player.entity.position.z,
yaw: player.entity.yaw,
pitch: player.entity.pitch,
flags: 0x00
});
}
@ -125,18 +128,16 @@ function inject(serv,player)
function spawn()
{
player.getOthers().forEach(function (otherPlayer) {
var spawnPoint=toFixedPosition(otherPlayer.spawnPoint);
var pos = otherPlayer.entity.position;
player._client.write('named_entity_spawn', {
entityId: otherPlayer.entity.id,
playerUUID: transformUuid(otherPlayer._client.uuid),
x: pos ? pos.x : spawnPoint.x,
y: pos ? pos.y : spawnPoint.y,
z: pos ? pos.z : spawnPoint.z,
yaw: 0,
pitch: 0,
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: []
metadata: otherPlayer.entity.metadata
});
});
@ -144,13 +145,13 @@ function inject(serv,player)
player._writeOthers('named_entity_spawn',{
entityId: player.entity.id,
playerUUID: transformUuid(player._client.uuid),
x: spawnPoint.x,
y: spawnPoint.y,
z: spawnPoint.z,
yaw: 0,
pitch: 0,
x: player.entity.position.x,
y: player.entity.position.y,
z: player.entity.position.z,
yaw: player.entity.yaw,
pitch: player.entity.pitch,
currentItem: 0,
metadata: []
metadata: player.entity.metadata
});
}

View file

@ -9,6 +9,37 @@ function toFixedPosition(p)
function inject(serv,player)
{
player._client.on('look', function(packet) {
sendLook(packet.yaw,packet.pitch,packet.onGround)
});
// float (degrees) --> byte (1/256 "degrees")
function conv(f){
var b = (f % 360) * 256 / 360;
if (b < -128) b += 256;
else if (b > 127) b -= 256;
return Math.floor(b);
}
function sendLook(yaw,pitch,onGround)
{
var convYaw=conv(yaw);
var convPitch=conv(pitch);
if (convYaw == player.entity.yaw && convPitch == player.entity.pitch) return;
player._writeOthers("entity_look", {
entityId: player.entity.id,
yaw: convYaw,
pitch: convPitch,
onGround: onGround
});
player.entity.yaw = convYaw;
player.entity.pitch = convPitch;
player.entity.onGround = onGround;
player._writeOthers("entity_head_rotation", {
entityId: player.entity.id,
headYaw: convYaw
});
}
player._client.on('position', function (packet) {
var position = new vec3(packet.x, packet.y, packet.z);
var onGround = packet.onGround;
@ -19,20 +50,21 @@ function inject(serv,player)
var position = new vec3(packet.x, packet.y, packet.z);
var onGround = packet.onGround;
sendRelativePositionChange(toFixedPosition(position), onGround);
sendLook(packet.yaw,packet.pitch,packet.onGround);
});
function sendRelativePositionChange(newPosition, onGround) {
if (player.entity.position.distanceTo(new vec3(0, 0, 0)) != 0) {
var diff = newPosition.minus(player.entity.position);
if(diff.abs().x>256 || diff.abs().y>256 || diff.abs().z>256)
if(diff.abs().x>127 || diff.abs().y>127 || diff.abs().z>127)
{
player._writeOthers('entity_teleport', {
entityId:player.entity.id,
x: newPosition.x,
y: newPosition.y,
z: newPosition.z,
yaw: 0,
pitch: 0,
yaw: player.entity.yaw,
pitch: player.entity.pitch,
onGround: onGround
});
}