Inserted calculation if hover over enemy
Though text is not displayed, only the calculation is done.
This commit is contained in:
@@ -1,219 +1,233 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Content;
|
using Microsoft.Xna.Framework.Content;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
using Microsoft.Xna.Framework.Media;
|
using Microsoft.Xna.Framework.Media;
|
||||||
|
|
||||||
namespace DepthsBelow
|
namespace DepthsBelow
|
||||||
{
|
{
|
||||||
public class MouseInput
|
public class MouseInput
|
||||||
{
|
{
|
||||||
Core core;
|
Core core;
|
||||||
MouseState lastMouseState;
|
MouseState lastMouseState;
|
||||||
Rectangle selectionRectangle;
|
Rectangle selectionRectangle;
|
||||||
Texture2D selectionTexture;
|
Texture2D selectionTexture;
|
||||||
Rectangle gridRectangle;
|
Rectangle gridRectangle;
|
||||||
Texture2D gridTexture;
|
Texture2D gridTexture;
|
||||||
|
|
||||||
Vector2 directionStart;
|
Vector2 directionStart;
|
||||||
Vector2 directionNow;
|
Vector2 directionNow;
|
||||||
|
|
||||||
bool checkingDirection = false;
|
bool checkingDirection = false;
|
||||||
bool readjust = false;
|
bool readjust = false;
|
||||||
|
|
||||||
public MouseInput(Core core)
|
public MouseInput(Core core)
|
||||||
{
|
{
|
||||||
this.core = core;
|
this.core = core;
|
||||||
|
|
||||||
selectionRectangle = Rectangle.Empty;
|
selectionRectangle = Rectangle.Empty;
|
||||||
gridRectangle = Rectangle.Empty;
|
gridRectangle = Rectangle.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LoadContent()
|
public void LoadContent()
|
||||||
{
|
{
|
||||||
selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1);
|
selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1);
|
||||||
selectionTexture.SetData(new Color[] { Color.White });
|
selectionTexture.SetData(new Color[] { Color.White });
|
||||||
|
|
||||||
gridTexture = new Texture2D(core.GraphicsDevice, 1, 1);
|
gridTexture = new Texture2D(core.GraphicsDevice, 1, 1);
|
||||||
gridTexture.SetData(new Color[] { Color.White });
|
gridTexture.SetData(new Color[] { Color.White });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnClick(MouseState ms, GameTime gameTime)
|
public void OnClick(MouseState ms, GameTime gameTime)
|
||||||
{
|
{
|
||||||
GUIManager.Click(new Point(ms.X, ms.Y), gameTime.TotalGameTime);
|
GUIManager.Click(new Point(ms.X, ms.Y), gameTime.TotalGameTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(GameTime gameTime)
|
public void Update(GameTime gameTime)
|
||||||
{
|
{
|
||||||
MouseState ms = Mouse.GetState();
|
MouseState ms = Mouse.GetState();
|
||||||
Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y));
|
Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y));
|
||||||
Rectangle mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1);
|
Rectangle mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1);
|
||||||
KeyboardState ks = Keyboard.GetState();
|
KeyboardState ks = Keyboard.GetState();
|
||||||
|
|
||||||
// OnClick event
|
// OnClick event
|
||||||
if (
|
if (
|
||||||
(ms.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed)
|
(ms.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed)
|
||||||
|| (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
|
|| (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
|
||||||
)
|
)
|
||||||
OnClick(ms, gameTime);
|
OnClick(ms, gameTime);
|
||||||
|
|
||||||
|
|
||||||
// Tooltip stuff
|
// Tooltip stuff
|
||||||
var tooltip = core.Lua.GetObject<GUI.Frame>("ToolTip");
|
var tooltip = core.Lua.GetObject<GUI.Frame>("ToolTip");
|
||||||
if (tooltip != null)
|
if (tooltip != null)
|
||||||
{
|
{
|
||||||
tooltip.Visible = false;
|
tooltip.Visible = false;
|
||||||
foreach(var entity in core.Squad)
|
foreach(var entity in core.Squad)
|
||||||
{
|
{
|
||||||
if (mouseWorldRectangle.Intersects(entity.GetComponent<Component.Collision>().Rectangle))
|
if (mouseWorldRectangle.Intersects(entity.GetComponent<Component.Collision>().Rectangle))
|
||||||
{
|
{
|
||||||
tooltip.Visible = true;
|
tooltip.Visible = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tooltip.Visible)
|
if (tooltip.Visible)
|
||||||
{
|
{
|
||||||
tooltip.X = ms.X + 10;
|
tooltip.X = ms.X + 10;
|
||||||
tooltip.Y = ms.Y + 15;
|
tooltip.Y = ms.Y + 15;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (core.TurnManager.CurrentTurn == core.TurnManager["Player"])
|
if (core.TurnManager.CurrentTurn == core.TurnManager["Player"])
|
||||||
{
|
{
|
||||||
// Selection rectangle
|
// Selection rectangle
|
||||||
if (ms.LeftButton == ButtonState.Pressed)
|
if (ms.LeftButton == ButtonState.Pressed)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (selectionRectangle == Rectangle.Empty)
|
if (selectionRectangle == Rectangle.Empty)
|
||||||
{
|
{
|
||||||
directionStart = mouseWorldPos;
|
directionStart = mouseWorldPos;
|
||||||
selectionRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 0, 0);
|
selectionRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 0, 0);
|
||||||
readjust = false;
|
readjust = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (mouseWorldPos.X > directionStart.X && mouseWorldPos.Y < directionStart.Y || mouseWorldPos.X < directionStart.X && mouseWorldPos.Y > directionStart.Y)
|
if (mouseWorldPos.X > directionStart.X && mouseWorldPos.Y < directionStart.Y || mouseWorldPos.X < directionStart.X && mouseWorldPos.Y > directionStart.Y)
|
||||||
{
|
{
|
||||||
int rectangleX = (int)mouseWorldPos.X - ((int)mouseWorldPos.X - (int)directionStart.X);
|
int rectangleX = (int)mouseWorldPos.X - ((int)mouseWorldPos.X - (int)directionStart.X);
|
||||||
int rectangleY = (int)mouseWorldPos.Y;
|
int rectangleY = (int)mouseWorldPos.Y;
|
||||||
int rectangleWidth = (int)mouseWorldPos.X - (int)directionStart.X;
|
int rectangleWidth = (int)mouseWorldPos.X - (int)directionStart.X;
|
||||||
int rectangleHeight = (int)directionStart.Y - (int)mouseWorldPos.Y;
|
int rectangleHeight = (int)directionStart.Y - (int)mouseWorldPos.Y;
|
||||||
selectionRectangle = new Rectangle(rectangleX, rectangleY, rectangleWidth, rectangleHeight);
|
selectionRectangle = new Rectangle(rectangleX, rectangleY, rectangleWidth, rectangleHeight);
|
||||||
readjust = true;
|
readjust = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (readjust == true)
|
if (readjust == true)
|
||||||
{
|
{
|
||||||
selectionRectangle = new Rectangle((int)directionStart.X, (int)directionStart.Y, 0, 0);
|
selectionRectangle = new Rectangle((int)directionStart.X, (int)directionStart.Y, 0, 0);
|
||||||
}
|
}
|
||||||
selectionRectangle.Width = (int)mouseWorldPos.X - selectionRectangle.X;
|
selectionRectangle.Width = (int)mouseWorldPos.X - selectionRectangle.X;
|
||||||
selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y;
|
selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y;
|
||||||
readjust = false;
|
readjust = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
//Calculating chance to hit if holding cursor over enemy
|
||||||
// When the mouse is released
|
foreach (var unit in core.Squad)
|
||||||
if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty)
|
{
|
||||||
{
|
if (unit.Selected == true && unit.Fired == false)
|
||||||
// Deselect all units
|
{
|
||||||
if (!ks.IsKeyDown(Keys.LeftControl))
|
foreach (var enemy in core.Swarm)
|
||||||
{
|
{
|
||||||
foreach (var unit in core.Squad)
|
if (gridRectangle.Intersects(enemy.GetComponent<Component.Collision>().Rectangle))
|
||||||
unit.Selected = false;
|
{
|
||||||
}
|
Utility.CalculateHitChance(unit, enemy);
|
||||||
|
}
|
||||||
// Select all units in the rectangle
|
}
|
||||||
foreach (var unit in core.Squad)
|
}
|
||||||
{
|
}
|
||||||
if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
|
}
|
||||||
unit.Selected = true;
|
// When the mouse is released
|
||||||
}
|
if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty)
|
||||||
|
{
|
||||||
// Hide the selection rectangle
|
// Deselect all units
|
||||||
selectionRectangle = Rectangle.Empty;
|
if (!ks.IsKeyDown(Keys.LeftControl))
|
||||||
}
|
{
|
||||||
|
foreach (var unit in core.Squad)
|
||||||
// Send orders with right click
|
unit.Selected = false;
|
||||||
if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
|
}
|
||||||
{
|
|
||||||
if (checkingDirection == false)
|
// Select all units in the rectangle
|
||||||
{
|
foreach (var unit in core.Squad)
|
||||||
foreach (var unit in core.Squad)
|
{
|
||||||
if (unit.Selected)
|
if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
|
||||||
{
|
unit.Selected = true;
|
||||||
unit.GetComponent<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos);
|
}
|
||||||
}
|
|
||||||
}
|
// Hide the selection rectangle
|
||||||
|
selectionRectangle = Rectangle.Empty;
|
||||||
}
|
}
|
||||||
if (ms.RightButton == ButtonState.Pressed)
|
|
||||||
{
|
// Send orders with right click
|
||||||
foreach (var unit in core.Squad)
|
if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
|
||||||
{
|
{
|
||||||
if (unit.Selected && gridRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
|
if (checkingDirection == false)
|
||||||
{
|
{
|
||||||
checkingDirection = true;
|
foreach (var unit in core.Squad)
|
||||||
directionStart = unit.Transform.World + unit.Transform.World.Origin;
|
if (unit.Selected)
|
||||||
}
|
{
|
||||||
}
|
unit.GetComponent<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos);
|
||||||
}
|
}
|
||||||
if (checkingDirection == true && ms.RightButton == ButtonState.Pressed)
|
}
|
||||||
{
|
|
||||||
foreach (var unit in core.Squad)
|
}
|
||||||
if (unit.Selected)
|
if (ms.RightButton == ButtonState.Pressed)
|
||||||
{
|
{
|
||||||
directionNow.X = mouseWorldPos.X;
|
foreach (var unit in core.Squad)
|
||||||
directionNow.Y = mouseWorldPos.Y;
|
{
|
||||||
float directionX = mouseWorldPos.X - unit.Transform.World.X;
|
if (unit.Selected && gridRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
|
||||||
float directionY = mouseWorldPos.Y - unit.Transform.World.Y;
|
{
|
||||||
var mouseDirection = mouseWorldPos;
|
checkingDirection = true;
|
||||||
mouseDirection.Normalize();
|
directionStart = unit.Transform.World + unit.Transform.World.Origin;
|
||||||
//float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi;
|
}
|
||||||
/*if (directionX < 0)
|
}
|
||||||
{
|
}
|
||||||
directionX *= -1;
|
if (checkingDirection == true && ms.RightButton == ButtonState.Pressed)
|
||||||
unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX);
|
{
|
||||||
}
|
foreach (var unit in core.Squad)
|
||||||
else
|
if (unit.Selected)
|
||||||
{
|
{
|
||||||
*/
|
directionNow.X = mouseWorldPos.X;
|
||||||
unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan2(mouseWorldPos.Y - unit.Transform.World.Y - unit.Transform.World.Origin.Y, mouseWorldPos.X - unit.Transform.World.X - unit.Transform.World.Origin.X);/*
|
directionNow.Y = mouseWorldPos.Y;
|
||||||
}*/
|
float directionX = mouseWorldPos.X - unit.Transform.World.X;
|
||||||
//Console.WriteLine(angle);
|
float directionY = mouseWorldPos.Y - unit.Transform.World.Y;
|
||||||
//unit.GetComponent<Component.Transform>().World.Rotation = angle;
|
var mouseDirection = mouseWorldPos;
|
||||||
}
|
mouseDirection.Normalize();
|
||||||
}
|
//float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi;
|
||||||
else
|
/*if (directionX < 0)
|
||||||
{
|
{
|
||||||
checkingDirection = false;
|
directionX *= -1;
|
||||||
}
|
unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX);
|
||||||
}
|
}
|
||||||
// Grid highlighting
|
else
|
||||||
Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos);
|
{
|
||||||
Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos);
|
*/
|
||||||
gridRectangle = new Rectangle((int)rectWorldPos.X, (int)rectWorldPos.Y, Grid.TileSize, Grid.TileSize);
|
unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan2(mouseWorldPos.Y - unit.Transform.World.Y - unit.Transform.World.Origin.Y, mouseWorldPos.X - unit.Transform.World.X - unit.Transform.World.Origin.X);/*
|
||||||
|
}*/
|
||||||
lastMouseState = ms;
|
//Console.WriteLine(angle);
|
||||||
}
|
//unit.GetComponent<Component.Transform>().World.Rotation = angle;
|
||||||
|
}
|
||||||
public void Draw(SpriteBatch spriteBatch)
|
}
|
||||||
{
|
else
|
||||||
spriteBatch.Draw(selectionTexture, selectionRectangle, Color.Red * 0.5f);
|
{
|
||||||
spriteBatch.Draw(gridTexture, gridRectangle, Color.Yellow * 0.3f);
|
checkingDirection = false;
|
||||||
|
}
|
||||||
if (checkingDirection == true)
|
}
|
||||||
{
|
// Grid highlighting
|
||||||
Utility.DrawLine(spriteBatch, Color.White, 2, directionStart, directionNow);
|
Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos);
|
||||||
}
|
Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos);
|
||||||
|
gridRectangle = new Rectangle((int)rectWorldPos.X, (int)rectWorldPos.Y, Grid.TileSize, Grid.TileSize);
|
||||||
}
|
|
||||||
|
lastMouseState = ms;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public void Draw(SpriteBatch spriteBatch)
|
||||||
|
{
|
||||||
|
spriteBatch.Draw(selectionTexture, selectionRectangle, Color.Red * 0.5f);
|
||||||
|
spriteBatch.Draw(gridTexture, gridRectangle, Color.Yellow * 0.3f);
|
||||||
|
|
||||||
|
if (checkingDirection == true)
|
||||||
|
{
|
||||||
|
Utility.DrawLine(spriteBatch, Color.White, 2, directionStart, directionNow);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user