void UpdateAvatarPosition()
{
var keyboardState = Keyboard.GetState();
var currentState = GamePad.GetState(PlayerIndex.One);
var mouseState = Mouse.GetState();
//if (Console.IsOpen()) return;
if(keyboardState.IsKeyDown(Keys.P))
{
System.Console.WriteLine("Open!");
}
if (keyboardState.IsKeyDown(Keys.A) || (currentState.DPad.Left == ButtonState.Pressed))
{
// move left
//avatarYaw += RotationSpeed;
var forwardMovement = Matrix.CreateRotationY(avatarYaw);
var v = new Vector3(1, 0, 0);
v = Vector3.Transform(v, forwardMovement);
avatarPosition.X += v.X;
avatarPosition.Y += v.Y;
avatarPosition.Z += v.Z;
}
if (keyboardState.IsKeyDown(Keys.D) || (currentState.DPad.Right == ButtonState.Pressed))
{
// move right
//avatarYaw -= RotationSpeed;
var forwardMovement = Matrix.CreateRotationY(avatarYaw);
var v = new Vector3(1, 0, 0);
avatarPosition -= Vector3.Transform(v, forwardMovement);
}
if (keyboardState.IsKeyDown(Keys.W) || (currentState.DPad.Up == ButtonState.Pressed))
{
//var forwardMovement = Matrix.CreateRotationY(avatarYaw);
//var v = new Vector3(0, 0, ForwardSpeed);
//v = Vector3.Transform(v, forwardMovement);
//avatarPosition.Z += v.Z;
//avatarPosition.X += v.X;
var horizontal = avatarPitch.Cos();
var vertical = -avatarPitch.Sin();
var v = new Vector3(avatarYaw.Sin() * horizontal, vertical, avatarYaw.Cos() * horizontal);
v.Normalize();
avatarPosition += v * ForwardSpeed;
}
if (keyboardState.IsKeyDown(Keys.S) || (currentState.DPad.Down == ButtonState.Pressed))
{
var horizontal = avatarPitch.Cos();
var vertical = -avatarPitch.Sin();
var v = new Vector3(avatarYaw.Sin() * horizontal, vertical, avatarYaw.Cos() * horizontal);
v.Normalize();
avatarPosition -= v * ForwardSpeed;
}
if (keyboardState.IsKeyDown(Keys.F))
{
avatarPosition.Y = avatarPosition.Y - 1;
}
if (keyboardState.IsKeyDown(Keys.R) || keyboardState.IsKeyDown(Keys.Space))
{
avatarPosition.Y = avatarPosition.Y + 1;
}
if (keyboardState.IsKeyDown(Keys.T))
{
_thirdPersonReference.Y = _thirdPersonReference.Y - 0.25f;
}
if (keyboardState.IsKeyDown(Keys.G))
{
_thirdPersonReference.Y = _thirdPersonReference.Y + 0.25f;
}
// adjust speed
if (keyboardState.IsKeyDown(Keys.OemPlus))
{
ForwardSpeed = Math.Min(ForwardSpeed + 0.1f, 15);
}
else if (keyboardState.IsKeyDown(Keys.OemMinus))
{
ForwardSpeed = Math.Max(ForwardSpeed - 0.1f, 0.1f);
}
if (mouseState.LeftButton == ButtonState.Pressed && !mouseLeftButtonDown)
{
// select polygon
mouseLeftButtonDown = true;
var width = _graphics.GraphicsDevice.Viewport.Width;
var height = _graphics.GraphicsDevice.Viewport.Height;
var x = mouseState.X;
var y = mouseState.Y;
if (x < 0 || y < 0 || x > width || y > height) return;
var add = (keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift));
var startPos = new Vector3(x, y, 0);
var endPos = new Vector3(x, y, 1);
var near = _graphics.GraphicsDevice.Viewport.Unproject(startPos, _proj, _view, Matrix.Identity).ToWCell();
var far = _graphics.GraphicsDevice.Viewport.Unproject(endPos, _proj, _view, Matrix.Identity).ToWCell();
var dir = (far - near).NormalizedCopy();
var ray = new Ray(near, dir);
TerrainProgram.TerrainManager.SelectedTriangleManager.UpdateSelectedTriangle(ray, add);
}
if (mouseState.LeftButton == ButtonState.Released && mouseLeftButtonDown)
{
mouseLeftButtonDown = false;
}
}