2015-09-24 21:20:36 -04:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using TrueCraft.Client.Input;
|
2015-09-27 22:03:15 -04:00
|
|
|
using TVector3 = TrueCraft.API.Vector3;
|
|
|
|
using XVector3 = Microsoft.Xna.Framework.Vector3;
|
|
|
|
using TrueCraft.API;
|
2015-09-27 22:32:07 -04:00
|
|
|
using TrueCraft.Core.Logic;
|
|
|
|
using TrueCraft.Core.Networking.Packets;
|
2015-09-24 21:20:36 -04:00
|
|
|
|
|
|
|
namespace TrueCraft.Client.Modules
|
|
|
|
{
|
|
|
|
public class PlayerControlModule : IInputModule
|
|
|
|
{
|
|
|
|
private TrueCraftGame Game { get; set; }
|
2015-09-27 22:32:07 -04:00
|
|
|
private DateTime NextAnimation { get; set; }
|
2015-09-27 22:03:15 -04:00
|
|
|
private XVector3 Delta { get; set; }
|
2015-09-27 17:14:04 -04:00
|
|
|
private bool Capture { get; set; }
|
2015-09-24 21:20:36 -04:00
|
|
|
|
|
|
|
public PlayerControlModule(TrueCraftGame game)
|
|
|
|
{
|
|
|
|
Game = game;
|
2015-09-27 17:14:04 -04:00
|
|
|
Capture = true;
|
2015-09-29 20:39:11 -04:00
|
|
|
Game.StartDigging = DateTime.MinValue;
|
|
|
|
Game.EndDigging = DateTime.MaxValue;
|
|
|
|
Game.TargetBlock = -Coordinates3D.One;
|
2015-09-27 22:32:07 -04:00
|
|
|
NextAnimation = DateTime.MaxValue;
|
2015-09-24 21:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool KeyDown(GameTime gameTime, KeyboardKeyEventArgs e)
|
|
|
|
{
|
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
// Exit game
|
|
|
|
case Keys.Escape:
|
|
|
|
Process.GetCurrentProcess().Kill();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Take a screenshot.
|
|
|
|
case Keys.F2:
|
|
|
|
Game.TakeScreenshot();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Move to the left.
|
|
|
|
case Keys.A:
|
|
|
|
case Keys.Left:
|
2015-09-27 22:03:15 -04:00
|
|
|
Delta += XVector3.Left;
|
2015-09-24 21:20:36 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Move to the right.
|
|
|
|
case Keys.D:
|
|
|
|
case Keys.Right:
|
2015-09-27 22:03:15 -04:00
|
|
|
Delta += XVector3.Right;
|
2015-09-24 21:20:36 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Move forwards.
|
|
|
|
case Keys.W:
|
|
|
|
case Keys.Up:
|
2015-09-27 22:03:15 -04:00
|
|
|
Delta += XVector3.Forward;
|
2015-09-24 21:20:36 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Move backwards.
|
|
|
|
case Keys.S:
|
|
|
|
case Keys.Down:
|
2015-09-27 22:03:15 -04:00
|
|
|
Delta += XVector3.Backward;
|
2015-09-24 21:20:36 -04:00
|
|
|
return true;
|
|
|
|
|
2015-09-27 17:14:04 -04:00
|
|
|
case Keys.I:
|
|
|
|
Game.Client.Position = Game.Client.Position.Floor();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Keys.Tab:
|
|
|
|
Capture = !Capture;
|
|
|
|
return true;
|
|
|
|
|
2015-09-24 21:20:36 -04:00
|
|
|
case Keys.Space:
|
|
|
|
if (Math.Floor(Game.Client.Position.Y) == Game.Client.Position.Y)
|
|
|
|
Game.Client.Velocity += TrueCraft.API.Vector3.Up * 0.3;
|
|
|
|
return true;
|
2015-09-30 15:28:38 -04:00
|
|
|
|
|
|
|
case Keys.NumPad1:
|
2015-09-30 15:56:09 -04:00
|
|
|
Game.Client.HotbarSelection = 0;
|
2015-09-30 15:28:38 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Keys.NumPad2:
|
2015-09-30 15:56:09 -04:00
|
|
|
Game.Client.HotbarSelection = 1;
|
2015-09-30 15:28:38 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Keys.NumPad3:
|
2015-09-30 15:56:09 -04:00
|
|
|
Game.Client.HotbarSelection = 2;
|
2015-09-30 15:28:38 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Keys.NumPad4:
|
2015-09-30 15:56:09 -04:00
|
|
|
Game.Client.HotbarSelection = 3;
|
2015-09-30 15:28:38 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Keys.NumPad5:
|
2015-09-30 15:56:09 -04:00
|
|
|
Game.Client.HotbarSelection = 4;
|
2015-09-30 15:28:38 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Keys.NumPad6:
|
2015-09-30 15:56:09 -04:00
|
|
|
Game.Client.HotbarSelection = 5;
|
2015-09-30 15:28:38 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Keys.NumPad7:
|
2015-09-30 15:56:09 -04:00
|
|
|
Game.Client.HotbarSelection = 6;
|
2015-09-30 15:28:38 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Keys.NumPad8:
|
2015-09-30 15:56:09 -04:00
|
|
|
Game.Client.HotbarSelection = 7;
|
2015-09-30 15:28:38 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Keys.NumPad9:
|
2015-09-30 15:56:09 -04:00
|
|
|
Game.Client.HotbarSelection = 8;
|
2015-09-30 15:28:38 -04:00
|
|
|
return true;
|
2015-09-24 21:20:36 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool KeyUp(GameTime gameTime, KeyboardKeyEventArgs e)
|
|
|
|
{
|
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
// Stop moving to the left.
|
|
|
|
case Keys.A:
|
|
|
|
case Keys.Left:
|
2015-09-27 22:03:15 -04:00
|
|
|
Delta -= XVector3.Left;
|
2015-09-24 21:20:36 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Stop moving to the right.
|
|
|
|
case Keys.D:
|
|
|
|
case Keys.Right:
|
2015-09-27 22:03:15 -04:00
|
|
|
Delta -= XVector3.Right;
|
2015-09-24 21:20:36 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Stop moving forwards.
|
|
|
|
case Keys.W:
|
|
|
|
case Keys.Up:
|
2015-09-27 22:03:15 -04:00
|
|
|
Delta -= XVector3.Forward;
|
2015-09-24 21:20:36 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Stop moving backwards.
|
|
|
|
case Keys.S:
|
|
|
|
case Keys.Down:
|
2015-09-27 22:03:15 -04:00
|
|
|
Delta -= XVector3.Backward;
|
2015-09-24 21:20:36 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void MouseMove(GameTime gameTime, MouseMoveEventArgs e)
|
|
|
|
{
|
2015-09-27 17:14:04 -04:00
|
|
|
if (!Capture)
|
|
|
|
return;
|
2015-09-24 21:20:36 -04:00
|
|
|
var centerX = Game.GraphicsDevice.Viewport.Width / 2;
|
|
|
|
var centerY = Game.GraphicsDevice.Viewport.Height / 2;
|
|
|
|
Mouse.SetPosition(centerX, centerY);
|
|
|
|
|
|
|
|
var look = new Vector2((centerX - e.X), (centerY - e.Y))
|
|
|
|
* (float)(gameTime.ElapsedGameTime.TotalSeconds * 30);
|
|
|
|
|
2015-09-27 22:03:15 -04:00
|
|
|
Game.Client.Yaw -= look.X;
|
|
|
|
Game.Client.Pitch -= look.Y;
|
2015-09-24 21:20:36 -04:00
|
|
|
Game.Client.Yaw %= 360;
|
|
|
|
Game.Client.Pitch = MathHelper.Clamp(Game.Client.Pitch, -89.9f, 89.9f);
|
|
|
|
}
|
|
|
|
|
2015-09-27 22:03:15 -04:00
|
|
|
public bool MouseButtonDown(GameTime gameTime, MouseButtonEventArgs e)
|
|
|
|
{
|
|
|
|
switch (e.Button)
|
|
|
|
{
|
|
|
|
case MouseButton.Left:
|
2015-09-29 20:39:11 -04:00
|
|
|
if (Game.StartDigging == DateTime.MinValue) // Would like to start digging a block
|
2015-09-27 22:32:07 -04:00
|
|
|
{
|
|
|
|
var target = Game.HighlightedBlock;
|
|
|
|
if (target != -Coordinates3D.One)
|
|
|
|
BeginDigging(target);
|
|
|
|
}
|
|
|
|
else // Currently digging a block
|
|
|
|
{
|
|
|
|
var target = Game.HighlightedBlock;
|
|
|
|
if (target == -Coordinates3D.One) // Cancel
|
|
|
|
{
|
2015-09-29 20:39:11 -04:00
|
|
|
Game.StartDigging = DateTime.MinValue;
|
|
|
|
Game.EndDigging = DateTime.MaxValue;
|
|
|
|
Game.TargetBlock = -Coordinates3D.One;
|
2015-09-27 22:32:07 -04:00
|
|
|
}
|
2015-09-29 20:39:11 -04:00
|
|
|
else if (target != Game.TargetBlock) // Change target
|
2015-09-27 22:32:07 -04:00
|
|
|
BeginDigging(target);
|
|
|
|
}
|
2015-09-27 22:03:15 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-27 22:32:07 -04:00
|
|
|
private void BeginDigging(Coordinates3D target)
|
|
|
|
{
|
2015-09-29 20:39:11 -04:00
|
|
|
// TODO: Adjust digging time to compensate for latency
|
2015-09-27 22:32:07 -04:00
|
|
|
var block = Game.Client.World.GetBlockID(target);
|
2015-09-29 20:39:11 -04:00
|
|
|
Game.TargetBlock = target;
|
|
|
|
Game.StartDigging = DateTime.UtcNow;
|
2015-09-27 22:32:07 -04:00
|
|
|
short damage;
|
2015-09-29 20:39:11 -04:00
|
|
|
Game.EndDigging = Game.StartDigging.AddMilliseconds(
|
2015-09-27 22:32:07 -04:00
|
|
|
BlockProvider.GetHarvestTime(block, 0, out damage));
|
|
|
|
Game.Client.QueuePacket(new PlayerDiggingPacket(
|
|
|
|
PlayerDiggingPacket.Action.StartDigging,
|
2015-09-29 20:39:11 -04:00
|
|
|
Game.TargetBlock.X, (sbyte)Game.TargetBlock.Y, Game.TargetBlock.Z,
|
2015-09-27 22:32:07 -04:00
|
|
|
Game.HighlightedBlockFace));
|
|
|
|
NextAnimation = DateTime.UtcNow.AddSeconds(0.25);
|
|
|
|
}
|
|
|
|
|
2015-09-27 22:03:15 -04:00
|
|
|
public bool MouseButtonUp(GameTime gameTime, MouseButtonEventArgs e)
|
|
|
|
{
|
|
|
|
switch (e.Button)
|
|
|
|
{
|
|
|
|
case MouseButton.Left:
|
2015-09-29 20:39:11 -04:00
|
|
|
Game.StartDigging = DateTime.MinValue;
|
|
|
|
Game.EndDigging = DateTime.MaxValue;
|
|
|
|
Game.TargetBlock = -Coordinates3D.One;
|
2015-09-27 22:03:15 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-24 21:20:36 -04:00
|
|
|
public void Update(GameTime gameTime)
|
|
|
|
{
|
2015-09-27 22:03:15 -04:00
|
|
|
if (Delta != XVector3.Zero)
|
2015-09-24 21:20:36 -04:00
|
|
|
{
|
2015-09-27 22:03:15 -04:00
|
|
|
var lookAt = XVector3.Transform(-Delta,
|
2015-09-27 22:32:07 -04:00
|
|
|
Matrix.CreateRotationY(MathHelper.ToRadians(-(Game.Client.Yaw - 180) + 180)));
|
2015-09-24 21:20:36 -04:00
|
|
|
|
|
|
|
lookAt.X *= (float)(gameTime.ElapsedGameTime.TotalSeconds * 4.3717);
|
|
|
|
lookAt.Z *= (float)(gameTime.ElapsedGameTime.TotalSeconds * 4.3717);
|
|
|
|
|
|
|
|
Game.Bobbing += Math.Max(Math.Abs(lookAt.X), Math.Abs(lookAt.Z));
|
|
|
|
|
2015-09-27 22:03:15 -04:00
|
|
|
Game.Client.Velocity = new TVector3(lookAt.X, Game.Client.Velocity.Y, lookAt.Z);
|
2015-09-24 21:20:36 -04:00
|
|
|
}
|
|
|
|
else
|
2015-09-27 22:03:15 -04:00
|
|
|
Game.Client.Velocity *= new TVector3(0, 1, 0);
|
2015-09-29 20:39:11 -04:00
|
|
|
if (Game.EndDigging != DateTime.MaxValue)
|
2015-09-27 22:32:07 -04:00
|
|
|
{
|
|
|
|
if (NextAnimation < DateTime.UtcNow)
|
|
|
|
{
|
|
|
|
NextAnimation = DateTime.UtcNow.AddSeconds(0.25);
|
|
|
|
Game.Client.QueuePacket(new AnimationPacket(Game.Client.EntityID,
|
|
|
|
AnimationPacket.PlayerAnimation.SwingArm));
|
|
|
|
}
|
2015-09-29 20:39:11 -04:00
|
|
|
if (DateTime.UtcNow > Game.EndDigging && Game.HighlightedBlock == Game.TargetBlock)
|
2015-09-27 22:32:07 -04:00
|
|
|
{
|
|
|
|
Game.Client.QueuePacket(new PlayerDiggingPacket(
|
|
|
|
PlayerDiggingPacket.Action.StopDigging,
|
2015-09-29 20:39:11 -04:00
|
|
|
Game.TargetBlock.X, (sbyte)Game.TargetBlock.Y, Game.TargetBlock.Z,
|
2015-09-27 22:32:07 -04:00
|
|
|
Game.HighlightedBlockFace));
|
2015-09-29 20:39:11 -04:00
|
|
|
Game.EndDigging = DateTime.MaxValue;
|
2015-09-27 22:32:07 -04:00
|
|
|
}
|
|
|
|
}
|
2015-09-24 21:20:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|