Update client lighting on block change

This commit is contained in:
Drew DeVault 2016-04-06 20:36:41 -04:00
parent 0a3eca2745
commit 9de14e371b
3 changed files with 39 additions and 0 deletions

View File

@ -28,6 +28,8 @@ namespace TrueCraft.Client.Handlers
}
chunk.SetBlockID(adjusted, (byte)packet.BlockID);
chunk.SetMetadata(adjusted, (byte)packet.Metadata);
client.OnBlockChanged(new BlockChangeEventArgs(coordinates, new TrueCraft.API.Logic.BlockDescriptor(),
new TrueCraft.API.Logic.BlockDescriptor()));
client.OnChunkModified(new ChunkEventArgs(new ReadOnlyChunk(chunk)));
}
@ -110,6 +112,7 @@ namespace TrueCraft.Client.Handlers
// TODO: Lighting
}
chunk.UpdateHeightMap();
chunk.TerrainPopulated = true;
client.OnChunkLoaded(new ChunkEventArgs(new ReadOnlyChunk(chunk)));
}
}

View File

@ -9,6 +9,9 @@ using TrueCraft.API;
using System.Linq;
using System.Threading;
using TrueCraft.Client.Events;
using TrueCraft.API.World;
using TrueCraft.Core.Lighting;
using TrueCraft.Core.World;
namespace TrueCraft.Client.Modules
{
@ -21,6 +24,7 @@ namespace TrueCraft.Client.Modules
private HashSet<Coordinates2D> ActiveMeshes { get; set; }
private List<ChunkMesh> ChunkMeshes { get; set; }
private ConcurrentBag<Mesh> IncomingChunks { get; set; }
private WorldLighting WorldLighting { get; set; }
private BasicEffect OpaqueEffect { get; set; }
private AlphaTestEffect TransparentEffect { get; set; }
@ -33,8 +37,10 @@ namespace TrueCraft.Client.Modules
Game.Client.ChunkLoaded += Game_Client_ChunkLoaded;
Game.Client.ChunkUnloaded += (sender, e) => UnloadChunk(e.Chunk);
Game.Client.ChunkModified += Game_Client_ChunkModified;
Game.Client.BlockChanged += Game_Client_BlockChanged;
ChunkRenderer.MeshCompleted += MeshCompleted;
ChunkRenderer.Start();
WorldLighting = new WorldLighting(Game.Client.World.World, Game.BlockRepository);
OpaqueEffect = new BasicEffect(Game.GraphicsDevice);
OpaqueEffect.TextureEnabled = true;
@ -56,6 +62,28 @@ namespace TrueCraft.Client.Modules
ActiveMeshes = new HashSet<Coordinates2D>();
}
void Game_Client_BlockChanged(object sender, BlockChangeEventArgs e)
{
WorldLighting.EnqueueOperation(new TrueCraft.API.BoundingBox(
e.Position, e.Position + Coordinates3D.One), false);
WorldLighting.EnqueueOperation(new TrueCraft.API.BoundingBox(
e.Position, e.Position + Coordinates3D.One), true);
var posA = e.Position;
posA.Y = 0;
var posB = e.Position;
posB.Y = World.Height;
posB.X++;
posB.Z++;
WorldLighting.EnqueueOperation(new TrueCraft.API.BoundingBox(posA, posB), true);
WorldLighting.EnqueueOperation(new TrueCraft.API.BoundingBox(posA, posB), false);
for (int i = 0; i < 100; i++)
{
if (!WorldLighting.TryLightNext())
break;
}
}
private void Game_Client_ChunkModified(object sender, ChunkEventArgs e)
{
ChunkRenderer.Enqueue(e.Chunk, true);
@ -126,6 +154,7 @@ namespace TrueCraft.Client.Modules
}
if (any)
Game.FlushMainThreadActions();
WorldLighting.TryLightNext();
}
private static readonly BlendState ColorWriteDisable = new BlendState

View File

@ -19,6 +19,7 @@ using TrueCraft.Core.Physics;
using TrueCraft.Core.Windows;
using TrueCraft.API.Windows;
using TrueCraft.API.Logic;
using TrueCraft.API.World;
namespace TrueCraft.Client
{
@ -30,6 +31,7 @@ namespace TrueCraft.Client
public event EventHandler<ChunkEventArgs> ChunkModified;
public event EventHandler<ChunkEventArgs> ChunkLoaded;
public event EventHandler<ChunkEventArgs> ChunkUnloaded;
public event EventHandler<BlockChangeEventArgs> BlockChanged;
public event PropertyChangedEventHandler PropertyChanged;
private long connected;
@ -265,6 +267,11 @@ namespace TrueCraft.Client
if (ChunkModified != null) ChunkModified(this, e);
}
protected internal void OnBlockChanged(BlockChangeEventArgs e)
{
if (BlockChanged != null) BlockChanged(this, e);
}
#region IAABBEntity implementation
public const double Width = 0.6;