Send cilents more chunks over time

This also implements movement between chunks
This commit is contained in:
Drew DeVault 2014-12-27 22:53:22 -07:00
parent 6f87efd519
commit 557a6ffe4c
6 changed files with 71 additions and 12 deletions

View File

@ -81,13 +81,6 @@ namespace TrueCraft.Entities
{
}
public event EventHandler Despawn;
protected internal virtual void OnDespawn()
{
if (Despawn != null) Despawn(this, new EventArgs());
}
protected bool EnablePropertyChange { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected internal virtual void OnPropertyChanged(string property)

View File

@ -3,6 +3,10 @@ using TrueCraft.API.Server;
using TrueCraft.API.World;
using TrueCraft.API.Entities;
using TrueCraft.API.Networking;
using System.ComponentModel;
using TrueCraft.Entities;
using System.Linq;
using System.Threading.Tasks;
namespace TrueCraft
{
@ -23,6 +27,44 @@ namespace TrueCraft
public void SpawnEntity(IEntity entity)
{
entity.EntityID = NextEntityID++;
entity.PropertyChanged -= HandlePropertyChanged;
entity.PropertyChanged += HandlePropertyChanged;
}
void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
var entity = sender as IEntity;
if (entity == null)
throw new InvalidCastException("Attempted to handle property changes for non-entity");
if (entity is PlayerEntity)
{
switch (e.PropertyName)
{
case "Position":
UpdatePlayerPosition(entity as PlayerEntity);
break;
}
}
}
private void UpdatePlayerPosition(PlayerEntity entity)
{
var self = GetClientForEntity(entity);
for (int i = 0, ServerClientsCount = Server.Clients.Count; i < ServerClientsCount; i++)
{
var client = Server.Clients[i];
if (client == self) continue;
// TODO: Send updates about
}
if (self is RemoteClient)
{
Task.Factory.StartNew(() => (self as RemoteClient).UpdateChunks());
}
}
IRemoteClient GetClientForEntity(PlayerEntity entity)
{
return Server.Clients.SingleOrDefault(c => c.Entity.EntityID == entity.EntityID);
}
public void DespawnEntity(IEntity entity)

View File

@ -14,11 +14,27 @@ namespace TrueCraft.Handlers
HandlePlayerMovement(_client, new Vector3(packet.X, packet.Y, packet.Z), _client.Entity.Yaw, _client.Entity.Pitch);
}
public static void HandlePlayerLookPacket(IPacket _packet, IRemoteClient _client, IMultiplayerServer server)
{
var packet = (PlayerLookPacket)_packet;
HandlePlayerMovement(_client, _client.Entity.Position, packet.Yaw, packet.Pitch);
}
public static void HandlePlayerPositionAndLookPacket(IPacket _packet, IRemoteClient _client, IMultiplayerServer server)
{
var packet = (PlayerPositionAndLookPacket)_packet;
HandlePlayerMovement(_client, new Vector3(packet.X, packet.Y, packet.Z), packet.Yaw, packet.Pitch);
}
public static void HandlePlayerMovement(IRemoteClient client, Vector3 position, float yaw, float pitch)
{
if (client.Entity.Position.DistanceTo(position) > 3)
if (client.Entity.Position.DistanceTo(position) > 10)
{
client.QueuePacket(new DisconnectPacket("Client moved to fast (hacking?)"));
//client.QueuePacket(new DisconnectPacket("Client moved to fast (hacking?) " + client.Entity.Position.DistanceTo(position)));
// TODO: Figure out a good way of knowing when the client is going to stop BSing us about its position
client.Entity.Position = position;
client.Entity.Yaw = yaw;
client.Entity.Pitch = pitch;
}
else
{

View File

@ -36,12 +36,13 @@ namespace TrueCraft.Handlers
client.World = server.Worlds[0];
server.GetEntityManagerForWorld(client.World).SpawnEntity(client.Entity);
client.QueuePacket(new LoginResponsePacket(0, 0, Dimension.Overworld));
client.ChunkRadius = 4;
client.ChunkRadius = 2;
client.UpdateChunks();
client.QueuePacket(new SpawnPositionPacket(0, 16, 0));
client.QueuePacket(new SetPlayerPositionPacket(0, 16, 17, 0, 0, 0, true));
client.QueuePacket(new ChatMessagePacket(string.Format("Welcome to the server, {0}!", client.Username)));
server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(10), client.SendKeepAlive);
server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(1), client.ExpandChunkRadius);
}
}
}

View File

@ -120,7 +120,6 @@ namespace TrueCraft
IPacket packet;
while (!client.PacketQueue.TryDequeue(out packet)) { }
PacketReader.WritePacket(client.MinecraftStream, packet);
Console.WriteLine("Sent {0}", packet.GetType().Name);
if (packet is DisconnectPacket)
{
DisconnectClient(client);
@ -130,7 +129,6 @@ namespace TrueCraft
if (client.DataAvailable)
{
var packet = PacketReader.ReadPacket(client.MinecraftStream);
Console.WriteLine("Got {0}", packet.GetType().Name);
if (PacketHandlers[packet.ID] != null)
{
try

View File

@ -51,6 +51,15 @@ namespace TrueCraft
PacketQueue.Enqueue(packet);
}
internal void ExpandChunkRadius(IMultiplayerServer server)
{
if (ChunkRadius < 5)
{
ChunkRadius++;
server.Scheduler.ScheduleEvent(DateTime.Now.AddSeconds(1), ExpandChunkRadius);
}
}
internal void SendKeepAlive(IMultiplayerServer server)
{
QueuePacket(new KeepAlivePacket());