Skip to content

Commit

Permalink
[300] NETZWERK!!!! Wohoooo
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwendel committed Dec 14, 2015
1 parent c8f37ea commit 9040e46
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 45 deletions.
7 changes: 6 additions & 1 deletion OctoAwesome/OctoAwesome.Client/Components/ActorProxy.cs
Expand Up @@ -18,6 +18,11 @@ public ActorProxy(IConnection client)
{
this.client = client;
Connected = true;

foreach (var definition in DefinitionManager.GetBlockDefinitions())
{
inventory.Add(new InventorySlot() { Definition = definition, Amount = int.MaxValue });
}
}

public void Close(string reason)
Expand Down Expand Up @@ -132,7 +137,7 @@ public void Apply(Index3 blockIndex, InventorySlot tool, OrientationFlags orient
{
try
{
client.Apply(blockIndex, tool, orientation);
client.Apply(blockIndex, tool.Definition.GetType().FullName, orientation);
}
catch (Exception ex)
{
Expand Down
52 changes: 51 additions & 1 deletion OctoAwesome/OctoAwesome.Client/Components/ChunkRenderer.cs
Expand Up @@ -27,6 +27,9 @@ internal sealed class ChunkRenderer : IDisposable
private int indexCount;
private int lastReset;
private ILocalChunkCache _manager;
private VertexPositionColor[] playerModel;
private short[] playerIndeces;
private BasicEffect playerEffect;

/// <summary>
/// Adresse des aktuellen Chunks
Expand All @@ -39,7 +42,31 @@ public ChunkRenderer(Effect simpleShader, GraphicsDevice graphicsDevice, Matrix
this.textures = textures;
this.lastReset = -1;

playerModel = new[]
{
new VertexPositionColor(new Vector3(-0.75f, +0.75f, +2.00f), Microsoft.Xna.Framework.Color.Beige),
new VertexPositionColor(new Vector3(+0.75f, +0.75f, +2.00f), Microsoft.Xna.Framework.Color.Brown),
new VertexPositionColor(new Vector3(-0.75f, -0.75f, +2.00f), Microsoft.Xna.Framework.Color.Beige),
new VertexPositionColor(new Vector3(+0.75f, -0.75f, +2.00f), Microsoft.Xna.Framework.Color.Brown),
new VertexPositionColor(new Vector3(-0.75f, +0.75f, -0.00f), Microsoft.Xna.Framework.Color.Beige),
new VertexPositionColor(new Vector3(+0.75f, +0.75f, -0.00f), Microsoft.Xna.Framework.Color.Beige),
new VertexPositionColor(new Vector3(-0.75f, -0.75f, -0.00f), Microsoft.Xna.Framework.Color.Beige),
new VertexPositionColor(new Vector3(+0.75f, -0.75f, -0.00f), Microsoft.Xna.Framework.Color.Beige),
};

playerIndeces = new short[]
{
0, 1, 2, 1, 3, 2, //oben
3, 1, 7, 1, 5, 7, //rechts
2, 3, 6, 3, 7, 6, // vorne
1, 0, 5, 0, 4, 5, // hinten
0, 2, 4, 2, 6, 4, // links
6, 7, 4, 7, 5, 4
};

simple = simpleShader;
playerEffect = new BasicEffect(graphicsDevice);
playerEffect.VertexColorEnabled = true;
}

public void SetChunk(ILocalChunkCache manager, int x, int y, int z)
Expand Down Expand Up @@ -100,6 +127,29 @@ public void Draw(Matrix view, Matrix projection, Index3 shift)
graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexCount, 0, indexCount / 3);
}
}

// Draw Other player
foreach (Player player in chunk.Entities.ToArray())
{
// Index3 offset = player.ActorHost.Position.ChunkIndex * Chunk.CHUNKSIZE;
//Index3 offset = camera.CameraChunk * Chunk.CHUNKSIZE;
//Index3 planetSize = planet.Size * Chunk.CHUNKSIZE;
//Index3 relativePosition = new Index3(
// Index2.ShortestDistanceOnAxis(offset.X, player.Position.GlobalBlockIndex.X, planetSize.X),
// Index2.ShortestDistanceOnAxis(offset.Y, player.Position.GlobalBlockIndex.Y, planetSize.Y),
// player.Position.GlobalBlockIndex.Z - offset.Z);

Vector3 x = new Vector3(shift.X * Chunk.CHUNKSIZE_X, shift.Y * Chunk.CHUNKSIZE_Y, shift.Z * Chunk.CHUNKSIZE_Z);

playerEffect.World = Matrix.CreateRotationZ(MathHelper.TwoPi - player.Angle) * Matrix.CreateTranslation(x + player.Position.LocalPosition);
playerEffect.View = view;
playerEffect.Projection = projection;
foreach (var pass in playerEffect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, playerModel, 0, 8, playerIndeces, 0, 12);
}
}
}

public void RegenerateVertexBuffer()
Expand All @@ -123,7 +173,7 @@ public void RegenerateVertexBuffer()
float textureWidth = 1f / textureColumns;
float texelSize = 1f / SceneControl.TEXTURESIZE;
float textureSizeGap = texelSize;
float textureGap = texelSize/2;
float textureGap = texelSize / 2;
// BlockTypes sammlen
Dictionary<IBlockDefinition, int> textureOffsets = new Dictionary<IBlockDefinition, int>();
// Dictionary<Type, BlockDefinition> definitionMapping = new Dictionary<Type, BlockDefinition>();
Expand Down
61 changes: 48 additions & 13 deletions OctoAwesome/OctoAwesome.Client/Components/ClientComponent.cs
Expand Up @@ -152,14 +152,26 @@ public void SendPlayerLeave(Guid client)
otherPlayers.Remove(c);
}

public void SendBlockRemove(int planet, int chunkX, int chunkY, int chunkZ, int blockX, int blockY, int blockZ)
public void SendBlockRemove(PlanetIndex3 chunkIndex, Index3 blockIndex)
{
throw new NotImplementedException();
var chunk = ResourceManager.Instance.GlobalChunkCache.GetChunk(chunkIndex);
if (chunk == null)
return;

chunk.SetBlock(blockIndex, 0);
}

public void SendBlockInsert(int planet, int chunkX, int chunkY, int chunkZ, int blockX, int blockY, int blockZ, string fullName, int metaData)
public void SendBlockInsert(PlanetIndex3 chunkIndex, Index3 blockIndex, string fullName, int metaData)
{
throw new NotImplementedException();
var chunk = ResourceManager.Instance.GlobalChunkCache.GetChunk(chunkIndex);
if (chunk == null)
return;

var definition = DefinitionManager.GetBlockDefinitions().SingleOrDefault(d => d.GetType().FullName.Equals(fullName));
if (definition == null)
return;

chunk.SetBlock(blockIndex, DefinitionManager.GetBlockDefinitionIndex(definition), metaData);
}

public void SendEntityInsert(PlanetIndex3 index, Guid id, string assemblyName, string fullName, byte[] data)
Expand All @@ -168,7 +180,8 @@ public void SendEntityInsert(PlanetIndex3 index, Guid id, string assemblyName, s

var chunk = ResourceManager.Instance.GlobalChunkCache.GetChunk(index);
if (chunk == null)
throw new Exception("Chunk noch nicht geladen. Das sollte nicht passieren.");
return;
// throw new Exception("Chunk noch nicht geladen. Das sollte nicht passieren.");

// TODO: Mehr Checks!
Entity entity = (Entity)Activator.CreateInstance(assemblyName, fullName).Unwrap();
Expand All @@ -185,15 +198,18 @@ public void SendEntityRemove(Guid id)

PlanetIndex3 chunkIndex;
if (!entityChunks.TryGetValue(id, out chunkIndex))
throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");
return;
// throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");

var chunk = ResourceManager.Instance.GlobalChunkCache.GetChunk(chunkIndex);
if (chunk == null)
throw new Exception("Chunk nicht gefunden. Auch das sollte nicht passieren");
return;
// throw new Exception("Chunk nicht gefunden. Auch das sollte nicht passieren");

Entity entity = chunk.Entities.SingleOrDefault(e => e.Id == id);
if (entity == null)
throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");
return;
// throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");

chunk.Entities.Remove(entity);
entityChunks.Remove(id);
Expand All @@ -207,30 +223,49 @@ public void SendEntityMove(Guid id, PlanetIndex3 index)
PlanetIndex3 chunkIndex;
Entity entity = null;
if (!entityChunks.TryGetValue(id, out chunkIndex))
throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");
return;
// throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");

var chunk = ResourceManager.Instance.GlobalChunkCache.GetChunk(chunkIndex);
if (chunk == null)
throw new Exception("Chunk nicht gefunden. Auch das sollte nicht passieren");
return;
// throw new Exception("Chunk nicht gefunden. Auch das sollte nicht passieren");

entity = chunk.Entities.SingleOrDefault(e => e.Id == id);
if (entity == null)
throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");
return;
// throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");

chunk.Entities.Remove(entity);
entityChunks.Remove(id);

IChunk destinationChunk = ResourceManager.Instance.GlobalChunkCache.GetChunk(index);
if (destinationChunk == null)
throw new Exception("Chunk nicht gefunden. Auch das sollte nicht passieren");
return;
// throw new Exception("Chunk nicht gefunden. Auch das sollte nicht passieren");

destinationChunk.Entities.Add(entity);
entityChunks.Add(id, index);
}

public void SendEntityUpdate(Guid id, byte[] data)
{
throw new NotImplementedException();
PlanetIndex3 chunkIndex;
if (!entityChunks.TryGetValue(id, out chunkIndex))
return;
// throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");

var chunk = ResourceManager.Instance.GlobalChunkCache.GetChunk(chunkIndex);
if (chunk == null)
return;
// throw new Exception("Chunk nicht gefunden. Auch das sollte nicht passieren");

Entity entity = chunk.Entities.SingleOrDefault(e => e.Id == id);
if (entity == null)
return;
// throw new Exception("Entity nicht gefunden. Das sollte nicht passieren.");

entity.SetData(data);
}

public delegate void DisconnectDelegate(string reason);
Expand Down
2 changes: 2 additions & 0 deletions OctoAwesome/OctoAwesome.Runtime/ActorHost.cs
Expand Up @@ -231,6 +231,7 @@ public void Update(GameTime frameTime)
{
ushort lastBlock = localChunkCache.GetBlock(lastInteract.Value);
localChunkCache.SetBlock(lastInteract.Value, 0);
Server.Instance.RemoveBlock(Player.Position.Planet, lastInteract.Value);

if (lastBlock != 0)
{
Expand Down Expand Up @@ -272,6 +273,7 @@ public void Update(GameTime frameTime)
{
IBlockDefinition definition = lastTool.Definition as IBlockDefinition;
localChunkCache.SetBlock(lastApply.Value + add, DefinitionManager.GetBlockDefinitionIndex(definition));
Server.Instance.AddBlock(Player.Position.Planet, lastApply.Value + add, definition, 0);

lastTool.Amount--;
if (lastTool.Amount <= 0)
Expand Down
9 changes: 7 additions & 2 deletions OctoAwesome/OctoAwesome.Runtime/Client.cs
Expand Up @@ -154,9 +154,14 @@ public void SetMove(Vector2 value)
}

[OperationBehavior]
public void Apply(Index3 blockIndex, InventorySlot tool, OrientationFlags orientation)
public void Apply(Index3 blockIndex, string definitionName, OrientationFlags orientation)
{
ActorHost.Apply(blockIndex, tool, orientation);
var definition = DefinitionManager.GetBlockDefinitions().SingleOrDefault(d => d.GetType().FullName.Equals(definitionName));
if (definition == null) return;

InventorySlot slot = new InventorySlot() { Amount = 2, Definition = definition };

ActorHost.Apply(blockIndex, slot, orientation);
}

[OperationBehavior]
Expand Down
2 changes: 1 addition & 1 deletion OctoAwesome/OctoAwesome.Runtime/IConnection.cs
Expand Up @@ -35,7 +35,7 @@ public interface IConnection
void SetMove(Vector2 value);

[OperationContract(IsInitiating = false, IsTerminating = false, IsOneWay = true)]
void Apply(Index3 blockIndex, InventorySlot tool, OrientationFlags orientation);
void Apply(Index3 blockIndex, string definitionName, OrientationFlags orientation);

[OperationContract(IsInitiating = false, IsTerminating = false, IsOneWay = true)]
void Interact(Index3 blockIndex);
Expand Down
20 changes: 2 additions & 18 deletions OctoAwesome/OctoAwesome.Runtime/IConnectionCallback.cs
Expand Up @@ -58,30 +58,14 @@ public interface IConnectionCallback
/// <summary>
/// Informiert den Client über das Entfernen eines einzelnen Blocks
/// </summary>
/// <param name="planet"></param>
/// <param name="chunkX"></param>
/// <param name="chunkY"></param>
/// <param name="chunkZ"></param>
/// <param name="blockX"></param>
/// <param name="blockY"></param>
/// <param name="blockZ"></param>
[OperationContract(IsInitiating = false, IsTerminating = false, IsOneWay = true)]
void SendBlockRemove(int planet, int chunkX, int chunkY, int chunkZ, int blockX, int blockY, int blockZ);
void SendBlockRemove(PlanetIndex3 chunkIndex, Index3 blockIndex);

/// <summary>
/// Informiert den Client über das Einfügen eines Blocks
/// </summary>
/// <param name="planet"></param>
/// <param name="chunkX"></param>
/// <param name="chunkY"></param>
/// <param name="chunkZ"></param>
/// <param name="blockX"></param>
/// <param name="blockY"></param>
/// <param name="blockZ"></param>
/// <param name="fullName"></param>
/// <param name="metaData"></param>
[OperationContract(IsInitiating = false, IsTerminating = false, IsOneWay = true)]
void SendBlockInsert(int planet, int chunkX, int chunkY, int chunkZ, int blockX, int blockY, int blockZ, string fullName, int metaData);
void SendBlockInsert(PlanetIndex3 chunkIndex, Index3 blockIndex, string fullName, int metaData);

/// <summary>
/// Informiert den Player über das einfügen einer Entität (Items, player,...)
Expand Down

0 comments on commit 9040e46

Please sign in to comment.