This repository has been archived on 2024-06-13. You can view files and clone it, but cannot push or open issues or pull requests.
TrueCraft/TrueCraft.Client/Modules/HighlightModule.cs
2015-09-27 17:14:04 -04:00

91 lines
3.4 KiB
C#

using System;
using Microsoft.Xna.Framework.Graphics;
using TrueCraft.API;
using TrueCraft.Client.Rendering;
using Microsoft.Xna.Framework;
using XVector3 = Microsoft.Xna.Framework.Vector3;
using TVector3 = TrueCraft.API.Vector3;
using TRay = TrueCraft.API.Ray;
namespace TrueCraft.Client.Modules
{
public class HighlightModule : IGraphicalModule
{
public TrueCraftGame Game { get; set; }
private Coordinates3D HighlightedBlock { get; set; }
private BasicEffect HighlightEffect { get; set; }
private static readonly VertexPositionColor[] CubeVerticies;
private static readonly short[] CubeIndicies;
static HighlightModule()
{
var color = Color.Black;
CubeVerticies = new[]
{
new VertexPositionColor(new XVector3(0, 0, 1), color),
new VertexPositionColor(new XVector3(1, 0, 1), color),
new VertexPositionColor(new XVector3(1, 1, 1), color),
new VertexPositionColor(new XVector3(0, 1, 1), color),
new VertexPositionColor(new XVector3(0, 0, 0), color),
new VertexPositionColor(new XVector3(1, 0, 0), color),
new VertexPositionColor(new XVector3(1, 1, 0), color),
new VertexPositionColor(new XVector3(0, 1, 0), color)
};
CubeIndicies = new short[]
{
0, 1, 1, 2, 2, 3, 3, 0,
0, 4, 4, 7, 7, 6, 6, 2,
1, 5, 5, 4, 3, 7, 6, 5
};
}
public HighlightModule(TrueCraftGame game)
{
Game = game;
HighlightEffect = new BasicEffect(Game.GraphicsDevice);
HighlightEffect.VertexColorEnabled = true;
}
public void Update(GameTime gameTime)
{
var direction = Microsoft.Xna.Framework.Vector3.Transform(
-Microsoft.Xna.Framework.Vector3.UnitZ,
Matrix.CreateRotationX(MathHelper.ToRadians(Game.Client.Pitch)) *
Matrix.CreateRotationY(MathHelper.ToRadians(Game.Client.Yaw)));
var cast = VoxelCast.Cast(Game.Client.World,
new TRay(Game.Camera.Position, new TVector3(direction.X, direction.Y, direction.Z)),
Game.BlockRepository, (int)TrueCraftGame.Reach);
if (cast == null)
HighlightedBlock = -Coordinates3D.One;
else
{
HighlightedBlock = cast.Item1;
HighlightEffect.World =
Matrix.CreateTranslation(new XVector3(-0.5f)) *
Matrix.CreateScale(1.01f) *
Matrix.CreateTranslation(new XVector3(0.5f)) *
Matrix.CreateTranslation(new XVector3(cast.Item1.X, cast.Item1.Y, cast.Item1.Z));
}
}
public void Draw(GameTime gameTime)
{
Game.Camera.ApplyTo(HighlightEffect);
if (HighlightedBlock != -Coordinates3D.One)
{
foreach (var pass in HighlightEffect.CurrentTechnique.Passes)
{
pass.Apply();
HighlightEffect.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
PrimitiveType.LineList, CubeVerticies, 0,
CubeVerticies.Length, CubeIndicies, 0, CubeIndicies.Length / 2);
}
}
}
}
}