Merge branch 'master' of github.com:sippeangelo/DepthsBelow
Conflicts: DepthsBelow/DepthsBelow/KeyboardInput.cs DepthsBelow/DepthsBelow/MouseInput.cs DepthsBelow/DepthsBelow/Soldier.cs DepthsBelow/DepthsBelow/Utility.cs
This commit is contained in:
@@ -42,7 +42,13 @@ namespace DepthsBelow.Component
|
||||
public int GetAim
|
||||
{
|
||||
get { return soldierAim + weaponAccuracy; }
|
||||
set { soldierAim = value; }
|
||||
set { weaponAccuracy = value; }
|
||||
}
|
||||
|
||||
public int GetStep
|
||||
{
|
||||
get { return stepsTaken; }
|
||||
set { stepsTaken = value; }
|
||||
}
|
||||
|
||||
public int GetDodge
|
||||
@@ -100,7 +106,8 @@ namespace DepthsBelow.Component
|
||||
int penalty = 0;
|
||||
if (distance >= penaltyRange * 3)
|
||||
{
|
||||
penalty = 100;
|
||||
distance -= penaltyRange * 3;
|
||||
penalty = penaltyRange + penaltyRange * 3 + penaltyRange * 5 + distance * 10;
|
||||
}
|
||||
else if (distance >= penaltyRange * 2)
|
||||
{
|
||||
|
||||
Regular → Executable
+116
-116
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework;
|
||||
@@ -13,7 +13,7 @@ namespace DepthsBelow
|
||||
{
|
||||
public class KeyboardInput
|
||||
{
|
||||
Core core;
|
||||
Core core;
|
||||
private KeyboardState lastKeyboardState;
|
||||
|
||||
int checkerSpeed = 2;
|
||||
@@ -21,126 +21,126 @@ namespace DepthsBelow
|
||||
public KeyboardInput(Core core)
|
||||
{
|
||||
this.core = core;
|
||||
}
|
||||
|
||||
/*public List<List<Soldier>> GetGroups(List<Soldier> soldiers)
|
||||
{
|
||||
var ungrouped = soldiers.ToList();
|
||||
var groups = new List<List<Soldier>>();
|
||||
|
||||
for (int index = 0; index < soldiers.Count; index++)
|
||||
{
|
||||
var checkingSoldier = soldiers[index];
|
||||
var group = new List<Soldier>();
|
||||
|
||||
for (int i = 0; i < soldiers.Count; i++)
|
||||
{
|
||||
var soldier = soldiers[i];
|
||||
if (ungrouped.Contains(soldier))
|
||||
{
|
||||
float distance = Vector2.Distance(checkingSoldier.Transform.World + checkingSoldier.Transform.World.Origin,
|
||||
soldier.Transform.World + soldier.Transform.World.Origin);
|
||||
if (distance <= ((float) Grid.TileSize * 1f))
|
||||
{
|
||||
ungrouped.Remove(soldier);
|
||||
group.Add(soldier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (group.Count > 0)
|
||||
groups.Add(group);
|
||||
}
|
||||
|
||||
return groups;
|
||||
}*/
|
||||
|
||||
// Get a list of dynamic soldier groups
|
||||
public List<List<Soldier>> GetGroups(List<Soldier> soldiers, float maxRange)
|
||||
{
|
||||
var alreadyGrouped = new List<Soldier>();
|
||||
var groups = new List<List<Soldier>>();
|
||||
|
||||
for (int index = 0; index < soldiers.Count; index++)
|
||||
{
|
||||
var soldier = soldiers[index];
|
||||
|
||||
if (!alreadyGrouped.Contains(soldier))
|
||||
{
|
||||
var group = new List<Soldier>();
|
||||
GetNearChain(ref group, soldiers, soldier, maxRange);
|
||||
alreadyGrouped.AddRange(group);
|
||||
|
||||
if (group.Count > 0)
|
||||
groups.Add(group);
|
||||
}
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
// Get all soldiers who are close to each other in a chain
|
||||
public void GetNearChain(ref List<Soldier> group, List<Soldier> soldiers, Soldier soldier, float maxRange)
|
||||
{
|
||||
for (int index = 0; index < soldiers.Count; index++)
|
||||
{
|
||||
var nearSoldier = soldiers[index];
|
||||
|
||||
if (!group.Contains(nearSoldier))
|
||||
{
|
||||
float distance = Vector2.Distance(nearSoldier.Transform.World + nearSoldier.Transform.World.Origin,
|
||||
soldier.Transform.World + soldier.Transform.World.Origin);
|
||||
if (distance <= maxRange)
|
||||
{
|
||||
group.Add(nearSoldier);
|
||||
GetNearChain(ref group, soldiers, nearSoldier, maxRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private DynamicGroupManager groupManager;
|
||||
|
||||
}
|
||||
|
||||
/*public List<List<Soldier>> GetGroups(List<Soldier> soldiers)
|
||||
{
|
||||
var ungrouped = soldiers.ToList();
|
||||
var groups = new List<List<Soldier>>();
|
||||
|
||||
for (int index = 0; index < soldiers.Count; index++)
|
||||
{
|
||||
var checkingSoldier = soldiers[index];
|
||||
var group = new List<Soldier>();
|
||||
|
||||
for (int i = 0; i < soldiers.Count; i++)
|
||||
{
|
||||
var soldier = soldiers[i];
|
||||
if (ungrouped.Contains(soldier))
|
||||
{
|
||||
float distance = Vector2.Distance(checkingSoldier.Transform.World + checkingSoldier.Transform.World.Origin,
|
||||
soldier.Transform.World + soldier.Transform.World.Origin);
|
||||
if (distance <= ((float) Grid.TileSize * 1f))
|
||||
{
|
||||
ungrouped.Remove(soldier);
|
||||
group.Add(soldier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (group.Count > 0)
|
||||
groups.Add(group);
|
||||
}
|
||||
|
||||
return groups;
|
||||
}*/
|
||||
|
||||
// Get a list of dynamic soldier groups
|
||||
public List<List<Soldier>> GetGroups(List<Soldier> soldiers, float maxRange)
|
||||
{
|
||||
var alreadyGrouped = new List<Soldier>();
|
||||
var groups = new List<List<Soldier>>();
|
||||
|
||||
for (int index = 0; index < soldiers.Count; index++)
|
||||
{
|
||||
var soldier = soldiers[index];
|
||||
|
||||
if (!alreadyGrouped.Contains(soldier))
|
||||
{
|
||||
var group = new List<Soldier>();
|
||||
GetNearChain(ref group, soldiers, soldier, maxRange);
|
||||
alreadyGrouped.AddRange(group);
|
||||
|
||||
if (group.Count > 0)
|
||||
groups.Add(group);
|
||||
}
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
// Get all soldiers who are close to each other in a chain
|
||||
public void GetNearChain(ref List<Soldier> group, List<Soldier> soldiers, Soldier soldier, float maxRange)
|
||||
{
|
||||
for (int index = 0; index < soldiers.Count; index++)
|
||||
{
|
||||
var nearSoldier = soldiers[index];
|
||||
|
||||
if (!group.Contains(nearSoldier))
|
||||
{
|
||||
float distance = Vector2.Distance(nearSoldier.Transform.World + nearSoldier.Transform.World.Origin,
|
||||
soldier.Transform.World + soldier.Transform.World.Origin);
|
||||
if (distance <= maxRange)
|
||||
{
|
||||
group.Add(nearSoldier);
|
||||
GetNearChain(ref group, soldiers, nearSoldier, maxRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private DynamicGroupManager groupManager;
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
KeyboardState ks = Keyboard.GetState();
|
||||
|
||||
// Lua reload scripts
|
||||
if (ks.IsKeyUp(Keys.R) && lastKeyboardState.IsKeyDown(Keys.R))
|
||||
{
|
||||
core.Lua.Reload();
|
||||
}
|
||||
|
||||
// Debug test: grouping
|
||||
if (ks.IsKeyUp(Keys.K) && lastKeyboardState.IsKeyDown(Keys.K))
|
||||
{
|
||||
if (groupManager == null)
|
||||
groupManager = new DynamicGroupManager(core.Squad.Cast<Entity>().ToList(), Grid.TileSize * 1.5f);
|
||||
groupManager.UpdateGroups();
|
||||
//var groups = GetGroups(core.Squad, (float)Grid.TileSize * 1.5f);
|
||||
var groups = groupManager.Groups;
|
||||
foreach (var group in groups)
|
||||
{
|
||||
Debug.WriteLine("Group #" + groups.IndexOf(group) + " Panic: " + group.Panic);
|
||||
foreach (var entity in group.Entities)
|
||||
{
|
||||
var soldier = (Soldier)entity;
|
||||
Debug.WriteLine(soldier.Name + " " + soldier.GetHashCode());
|
||||
}
|
||||
}
|
||||
// Lua reload scripts
|
||||
if (ks.IsKeyUp(Keys.R) && lastKeyboardState.IsKeyDown(Keys.R))
|
||||
{
|
||||
core.Lua.Reload();
|
||||
}
|
||||
|
||||
// Debug test: grouping
|
||||
if (ks.IsKeyUp(Keys.K) && lastKeyboardState.IsKeyDown(Keys.K))
|
||||
{
|
||||
if (groupManager == null)
|
||||
groupManager = new DynamicGroupManager(core.Squad.Cast<Entity>().ToList(), Grid.TileSize * 1.5f);
|
||||
groupManager.UpdateGroups();
|
||||
//var groups = GetGroups(core.Squad, (float)Grid.TileSize * 1.5f);
|
||||
var groups = groupManager.Groups;
|
||||
foreach (var group in groups)
|
||||
{
|
||||
Debug.WriteLine("Group #" + groups.IndexOf(group) + " Panic: " + group.Panic);
|
||||
foreach (var entity in group.Entities)
|
||||
{
|
||||
var soldier = (Soldier)entity;
|
||||
Debug.WriteLine(soldier.Name + " " + soldier.GetHashCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Next turn
|
||||
if (ks.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Enter))
|
||||
{
|
||||
{
|
||||
if (core.TurnManager.CurrentTurn == core.TurnManager["Player"])
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
{
|
||||
unit.step = 0;
|
||||
unit.Fired = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
core.TurnManager.EndTurn();
|
||||
}
|
||||
|
||||
@@ -163,8 +163,8 @@ namespace DepthsBelow
|
||||
}
|
||||
}
|
||||
core.TestMonster.GetComponent<Component.PathFinder>().Goal = target;
|
||||
distance = 7000;
|
||||
|
||||
distance = 7000;
|
||||
|
||||
core.TurnManager.EndTurn();
|
||||
}
|
||||
}
|
||||
@@ -184,7 +184,7 @@ namespace DepthsBelow
|
||||
{
|
||||
if (soldier.Selected == true && soldier.Fired == false)
|
||||
{
|
||||
core.Volley.Add(new Shot(core.EntityManager));
|
||||
core.Volley.Add(new Shot(core.EntityManager, soldier.GetComponent<Component.Stat>()));
|
||||
soldier.Fired = true;
|
||||
foreach (var shot in core.Volley)
|
||||
{
|
||||
@@ -241,7 +241,7 @@ namespace DepthsBelow
|
||||
current = 0;
|
||||
if (soldier.Selected == true && soldier.Fired == false && hit == true)
|
||||
{
|
||||
core.Volley.Add(new Shot(core.EntityManager));
|
||||
core.Volley.Add(new Shot(core.EntityManager, soldier.GetComponent<Component.Stat>()));
|
||||
soldier.Fired = true;
|
||||
foreach (var shot in core.Volley)
|
||||
{
|
||||
@@ -263,9 +263,9 @@ namespace DepthsBelow
|
||||
hit = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lastKeyboardState = ks;
|
||||
}
|
||||
|
||||
lastKeyboardState = ks;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,273 +1,288 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using Microsoft.Xna.Framework.Media;
|
||||
|
||||
namespace DepthsBelow
|
||||
{
|
||||
public class MouseInput
|
||||
{
|
||||
Core core;
|
||||
MouseState lastMouseState;
|
||||
Rectangle selectionRectangle;
|
||||
Texture2D selectionTexture;
|
||||
Rectangle gridRectangle;
|
||||
Texture2D gridTexture;
|
||||
|
||||
Vector2 directionStart;
|
||||
Vector2 directionNow;
|
||||
|
||||
bool checkingDirection = false;
|
||||
bool readjust = false;
|
||||
|
||||
private Point pressLocation;
|
||||
|
||||
public MouseInput(Core core)
|
||||
{
|
||||
this.core = core;
|
||||
|
||||
selectionRectangle = Rectangle.Empty;
|
||||
gridRectangle = Rectangle.Empty;
|
||||
}
|
||||
|
||||
public void LoadContent()
|
||||
{
|
||||
selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1);
|
||||
selectionTexture.SetData(new Color[] { Color.White });
|
||||
|
||||
gridTexture = new Texture2D(core.GraphicsDevice, 1, 1);
|
||||
gridTexture.SetData(new Color[] { Color.White });
|
||||
}
|
||||
|
||||
public void OnPress(MouseState ms, GameTime gameTime)
|
||||
{
|
||||
pressLocation = new Point(ms.X, ms.Y);
|
||||
var mouseEvent = new GUIManager.MouseEventArgs()
|
||||
{
|
||||
MouseState = ms,
|
||||
Time = gameTime.TotalGameTime
|
||||
};
|
||||
bool handledByUI = GUIManager.RaiseAt("OnPress", mouseEvent, pressLocation);
|
||||
|
||||
if (handledByUI)
|
||||
return;
|
||||
|
||||
// If the mouse event is above a GUI frame that handles mouse events, do nothing
|
||||
//if (GUIManager.FramesIntersectingPoint(mousePos).Any(frame => frame.MouseEnabled))
|
||||
// return;
|
||||
}
|
||||
|
||||
public void OnRelease(MouseState ms, GameTime gameTime)
|
||||
{
|
||||
var mouseEvent = new GUIManager.MouseEventArgs()
|
||||
{
|
||||
MouseState = ms,
|
||||
Time = gameTime.TotalGameTime
|
||||
};
|
||||
|
||||
bool handledByUI = GUIManager.RaiseAt("OnRelease", mouseEvent, pressLocation);
|
||||
|
||||
if (handledByUI)
|
||||
return;
|
||||
}
|
||||
|
||||
public void OnClick(MouseState ms, GameTime gameTime)
|
||||
{
|
||||
var mouseEvent = new GUIManager.MouseEventArgs()
|
||||
{
|
||||
MouseState = ms,
|
||||
Time = gameTime.TotalGameTime
|
||||
};
|
||||
|
||||
bool handledByUI = GUIManager.RaiseAt("OnClick", mouseEvent, new Point(ms.X, ms.Y));
|
||||
|
||||
if (handledByUI)
|
||||
return;
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
MouseState ms = Mouse.GetState();
|
||||
Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y));
|
||||
Rectangle mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1);
|
||||
KeyboardState ks = Keyboard.GetState();
|
||||
|
||||
// OnPress event
|
||||
if (
|
||||
(ms.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released)
|
||||
|| (ms.RightButton == ButtonState.Pressed && lastMouseState.RightButton == ButtonState.Released)
|
||||
)
|
||||
OnPress(ms, gameTime);
|
||||
|
||||
// OnRelease event
|
||||
if (
|
||||
(ms.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed)
|
||||
|| (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
|
||||
)
|
||||
{
|
||||
OnClick(ms, gameTime);
|
||||
OnRelease(ms, gameTime);
|
||||
}
|
||||
|
||||
|
||||
// Tooltip stuff
|
||||
var tooltip = core.Lua.GetObject<GUI.Frame>("ToolTip");
|
||||
if (tooltip != null)
|
||||
{
|
||||
tooltip.Visible = false;
|
||||
foreach(var entity in core.Squad)
|
||||
{
|
||||
if (mouseWorldRectangle.Intersects(entity.GetComponent<Component.Collision>().Rectangle))
|
||||
{
|
||||
tooltip.Visible = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (tooltip.Visible)
|
||||
{
|
||||
tooltip.X = ms.X + 10;
|
||||
tooltip.Y = ms.Y + 15;
|
||||
}
|
||||
}
|
||||
|
||||
if (core.TurnManager.CurrentTurn == core.TurnManager["Player"])
|
||||
{
|
||||
// Selection rectangle
|
||||
/*if (ms.LeftButton == ButtonState.Pressed)
|
||||
{
|
||||
|
||||
if (selectionRectangle == Rectangle.Empty)
|
||||
{
|
||||
directionStart = mouseWorldPos;
|
||||
selectionRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 0, 0);
|
||||
readjust = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
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 rectangleY = (int)mouseWorldPos.Y;
|
||||
int rectangleWidth = (int)mouseWorldPos.X - (int)directionStart.X;
|
||||
int rectangleHeight = (int)directionStart.Y - (int)mouseWorldPos.Y;
|
||||
selectionRectangle = new Rectangle(rectangleX, rectangleY, rectangleWidth, rectangleHeight);
|
||||
readjust = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (readjust == true)
|
||||
{
|
||||
selectionRectangle = new Rectangle((int)directionStart.X, (int)directionStart.Y, 0, 0);
|
||||
}
|
||||
selectionRectangle.Width = (int)mouseWorldPos.X - selectionRectangle.X;
|
||||
selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y;
|
||||
readjust = false;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
// When the mouse is released
|
||||
/*if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty)
|
||||
{
|
||||
// Deselect all units
|
||||
if (!ks.IsKeyDown(Keys.LeftControl))
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
unit.Selected = false;
|
||||
}
|
||||
|
||||
// Select all units in the rectangle
|
||||
foreach (var unit in core.Squad)
|
||||
{
|
||||
if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
|
||||
unit.Selected = true;
|
||||
}
|
||||
|
||||
// Hide the selection rectangle
|
||||
selectionRectangle = Rectangle.Empty;
|
||||
}*/
|
||||
|
||||
// Send orders with right click
|
||||
if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
if (checkingDirection == false)
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
if (unit.Selected)
|
||||
{
|
||||
unit.GetComponent<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (ms.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
{
|
||||
if (unit.Selected && gridRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
|
||||
{
|
||||
checkingDirection = true;
|
||||
directionStart = unit.Transform.World + unit.Transform.World.Origin;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (checkingDirection == true && ms.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
if (unit.Selected)
|
||||
{
|
||||
directionNow.X = mouseWorldPos.X;
|
||||
directionNow.Y = mouseWorldPos.Y;
|
||||
float directionX = mouseWorldPos.X - unit.Transform.World.X;
|
||||
float directionY = mouseWorldPos.Y - unit.Transform.World.Y;
|
||||
var mouseDirection = mouseWorldPos;
|
||||
mouseDirection.Normalize();
|
||||
//float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi;
|
||||
/*if (directionX < 0)
|
||||
{
|
||||
directionX *= -1;
|
||||
unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX);
|
||||
}
|
||||
else
|
||||
{
|
||||
*/
|
||||
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);/*
|
||||
}*/
|
||||
//Console.WriteLine(angle);
|
||||
//unit.GetComponent<Component.Transform>().World.Rotation = angle;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
checkingDirection = false;
|
||||
}
|
||||
}
|
||||
// Grid highlighting
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using Microsoft.Xna.Framework.Media;
|
||||
|
||||
namespace DepthsBelow
|
||||
{
|
||||
public class MouseInput
|
||||
{
|
||||
Core core;
|
||||
MouseState lastMouseState;
|
||||
Rectangle selectionRectangle;
|
||||
Texture2D selectionTexture;
|
||||
Rectangle gridRectangle;
|
||||
Texture2D gridTexture;
|
||||
|
||||
Vector2 directionStart;
|
||||
Vector2 directionNow;
|
||||
|
||||
bool checkingDirection = false;
|
||||
bool readjust = false;
|
||||
|
||||
private Point pressLocation;
|
||||
|
||||
public MouseInput(Core core)
|
||||
{
|
||||
this.core = core;
|
||||
|
||||
selectionRectangle = Rectangle.Empty;
|
||||
gridRectangle = Rectangle.Empty;
|
||||
}
|
||||
|
||||
public void LoadContent()
|
||||
{
|
||||
selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1);
|
||||
selectionTexture.SetData(new Color[] { Color.White });
|
||||
|
||||
gridTexture = new Texture2D(core.GraphicsDevice, 1, 1);
|
||||
gridTexture.SetData(new Color[] { Color.White });
|
||||
}
|
||||
|
||||
public void OnPress(MouseState ms, GameTime gameTime)
|
||||
{
|
||||
pressLocation = new Point(ms.X, ms.Y);
|
||||
var mouseEvent = new GUIManager.MouseEventArgs()
|
||||
{
|
||||
MouseState = ms,
|
||||
Time = gameTime.TotalGameTime
|
||||
};
|
||||
bool handledByUI = GUIManager.RaiseAt("OnPress", mouseEvent, pressLocation);
|
||||
|
||||
if (handledByUI)
|
||||
return;
|
||||
|
||||
// If the mouse event is above a GUI frame that handles mouse events, do nothing
|
||||
//if (GUIManager.FramesIntersectingPoint(mousePos).Any(frame => frame.MouseEnabled))
|
||||
// return;
|
||||
}
|
||||
|
||||
public void OnRelease(MouseState ms, GameTime gameTime)
|
||||
{
|
||||
var mouseEvent = new GUIManager.MouseEventArgs()
|
||||
{
|
||||
MouseState = ms,
|
||||
Time = gameTime.TotalGameTime
|
||||
};
|
||||
|
||||
bool handledByUI = GUIManager.RaiseAt("OnRelease", mouseEvent, pressLocation);
|
||||
|
||||
if (handledByUI)
|
||||
return;
|
||||
}
|
||||
|
||||
public void OnClick(MouseState ms, GameTime gameTime)
|
||||
{
|
||||
var mouseEvent = new GUIManager.MouseEventArgs()
|
||||
{
|
||||
MouseState = ms,
|
||||
Time = gameTime.TotalGameTime
|
||||
};
|
||||
|
||||
bool handledByUI = GUIManager.RaiseAt("OnClick", mouseEvent, new Point(ms.X, ms.Y));
|
||||
|
||||
if (handledByUI)
|
||||
return;
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
MouseState ms = Mouse.GetState();
|
||||
Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y));
|
||||
Rectangle mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1);
|
||||
KeyboardState ks = Keyboard.GetState();
|
||||
|
||||
// OnPress event
|
||||
if (
|
||||
(ms.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released)
|
||||
|| (ms.RightButton == ButtonState.Pressed && lastMouseState.RightButton == ButtonState.Released)
|
||||
)
|
||||
OnPress(ms, gameTime);
|
||||
|
||||
// OnRelease event
|
||||
if (
|
||||
(ms.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed)
|
||||
|| (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
|
||||
)
|
||||
{
|
||||
OnClick(ms, gameTime);
|
||||
OnRelease(ms, gameTime);
|
||||
}
|
||||
|
||||
|
||||
// Tooltip stuff
|
||||
var tooltip = core.Lua.GetObject<GUI.Frame>("ToolTip");
|
||||
if (tooltip != null)
|
||||
{
|
||||
tooltip.Visible = false;
|
||||
foreach(var entity in core.Squad)
|
||||
{
|
||||
if (mouseWorldRectangle.Intersects(entity.GetComponent<Component.Collision>().Rectangle))
|
||||
{
|
||||
tooltip.Visible = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (tooltip.Visible)
|
||||
{
|
||||
tooltip.X = ms.X + 10;
|
||||
tooltip.Y = ms.Y + 15;
|
||||
}
|
||||
}
|
||||
|
||||
if (core.TurnManager.CurrentTurn == core.TurnManager["Player"])
|
||||
{
|
||||
// Selection rectangle
|
||||
/*if (ms.LeftButton == ButtonState.Pressed)
|
||||
{
|
||||
|
||||
if (selectionRectangle == Rectangle.Empty)
|
||||
{
|
||||
directionStart = mouseWorldPos;
|
||||
selectionRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 0, 0);
|
||||
readjust = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
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 rectangleY = (int)mouseWorldPos.Y;
|
||||
int rectangleWidth = (int)mouseWorldPos.X - (int)directionStart.X;
|
||||
int rectangleHeight = (int)directionStart.Y - (int)mouseWorldPos.Y;
|
||||
selectionRectangle = new Rectangle(rectangleX, rectangleY, rectangleWidth, rectangleHeight);
|
||||
readjust = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (readjust == true)
|
||||
{
|
||||
selectionRectangle = new Rectangle((int)directionStart.X, (int)directionStart.Y, 0, 0);
|
||||
}
|
||||
selectionRectangle.Width = (int)mouseWorldPos.X - selectionRectangle.X;
|
||||
selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y;
|
||||
readjust = false;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
//Calculating chance to hit if holding cursor over enemy
|
||||
foreach (var unit in core.Squad)
|
||||
{
|
||||
if (unit.Selected == true && unit.Fired == false)
|
||||
{
|
||||
foreach (var enemy in core.Swarm)
|
||||
{
|
||||
if (gridRectangle.Intersects(enemy.GetComponent<Component.Collision>().Rectangle))
|
||||
{
|
||||
Console.WriteLine(Utility.CalculateHitChance(unit, enemy));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When the mouse is released
|
||||
/*if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty)
|
||||
{
|
||||
// Deselect all units
|
||||
if (!ks.IsKeyDown(Keys.LeftControl))
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
unit.Selected = false;
|
||||
}
|
||||
|
||||
// Select all units in the rectangle
|
||||
foreach (var unit in core.Squad)
|
||||
{
|
||||
if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
|
||||
unit.Selected = true;
|
||||
}
|
||||
|
||||
// Hide the selection rectangle
|
||||
selectionRectangle = Rectangle.Empty;
|
||||
}*/
|
||||
|
||||
// Send orders with right click
|
||||
if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
if (checkingDirection == false)
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
if (unit.Selected)
|
||||
{
|
||||
unit.GetComponent<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (ms.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
{
|
||||
if (unit.Selected && gridRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
|
||||
{
|
||||
checkingDirection = true;
|
||||
directionStart = unit.Transform.World + unit.Transform.World.Origin;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (checkingDirection == true && ms.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
if (unit.Selected)
|
||||
{
|
||||
directionNow.X = mouseWorldPos.X;
|
||||
directionNow.Y = mouseWorldPos.Y;
|
||||
float directionX = mouseWorldPos.X - unit.Transform.World.X;
|
||||
float directionY = mouseWorldPos.Y - unit.Transform.World.Y;
|
||||
var mouseDirection = mouseWorldPos;
|
||||
mouseDirection.Normalize();
|
||||
//float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi;
|
||||
/*if (directionX < 0)
|
||||
{
|
||||
directionX *= -1;
|
||||
unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX);
|
||||
}
|
||||
else
|
||||
{
|
||||
*/
|
||||
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);/*
|
||||
}*/
|
||||
//Console.WriteLine(angle);
|
||||
//unit.GetComponent<Component.Transform>().World.Rotation = angle;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
checkingDirection = false;
|
||||
}
|
||||
}
|
||||
// Grid highlighting
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DepthsBelow.Component;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
@@ -14,6 +14,14 @@ namespace DepthsBelow
|
||||
public Color Color;
|
||||
public static Point Origin;
|
||||
|
||||
Stat soldierStat;
|
||||
|
||||
public Stat Stat
|
||||
{
|
||||
get { return soldierStat; }
|
||||
set { soldierStat = value; }
|
||||
}
|
||||
|
||||
private bool _selected = false; //I guess as in "currently doing something"
|
||||
|
||||
public bool select
|
||||
@@ -29,7 +37,7 @@ namespace DepthsBelow
|
||||
|
||||
public Vector2 direction = Vector2.Zero;
|
||||
|
||||
public Shot(EntityManager entityManager)
|
||||
public Shot(EntityManager entityManager, Stat _soldierStat)
|
||||
: base(entityManager)
|
||||
{
|
||||
if (Texture == null)
|
||||
@@ -37,6 +45,8 @@ namespace DepthsBelow
|
||||
|
||||
Transform.World.Origin = new Vector2(16, 16);
|
||||
|
||||
Stat = _soldierStat;
|
||||
|
||||
this.Color = Color.White;
|
||||
var rc = new SpriteRenderer(this) { Texture = Texture, Color = Color.White };
|
||||
AddComponent(rc);
|
||||
@@ -46,7 +56,7 @@ namespace DepthsBelow
|
||||
|
||||
}
|
||||
public static void LoadContent()
|
||||
{
|
||||
{
|
||||
Texture = GameServices.GetService<ContentManager>().Load<Texture2D>("images/Shot");
|
||||
}
|
||||
public override void Update(GameTime gameTime)
|
||||
|
||||
Regular → Executable
+21
-20
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DepthsBelow.Component;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace DepthsBelow
|
||||
public class Soldier : Entity
|
||||
{
|
||||
public static Texture2D Texture;
|
||||
public Color Color;
|
||||
public Color Color;
|
||||
public string Name;
|
||||
private bool _selected;
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace DepthsBelow
|
||||
}
|
||||
|
||||
public int numberOfSteps = 14;
|
||||
public int currentStep = 0;
|
||||
int currentStep = 0;
|
||||
|
||||
public int step
|
||||
{
|
||||
@@ -41,10 +41,11 @@ namespace DepthsBelow
|
||||
set
|
||||
{
|
||||
currentStep = value;
|
||||
GetComponent<Component.Stat>().GetStep = currentStep;
|
||||
}
|
||||
}
|
||||
|
||||
public Soldier(EntityManager entityManager, ref List<Soldier> squad)
|
||||
}
|
||||
|
||||
public Soldier(EntityManager entityManager, ref List<Soldier> squad)
|
||||
: base(entityManager)
|
||||
{
|
||||
if (Texture == null)
|
||||
@@ -62,17 +63,17 @@ namespace DepthsBelow
|
||||
AddComponent(cc);
|
||||
|
||||
var pfc = new PathFinder(this);
|
||||
AddComponent(pfc);
|
||||
|
||||
var stat = new Component.Stat(this)
|
||||
{
|
||||
MaxHP = 100,
|
||||
//Life = 10,
|
||||
Defence = 10,
|
||||
Strength = 10,
|
||||
GetAim = 10,
|
||||
GetDodge = 20
|
||||
};
|
||||
AddComponent(pfc);
|
||||
|
||||
var stat = new Component.Stat(this)
|
||||
{
|
||||
MaxHP = 100,
|
||||
//Life = 10,
|
||||
Defence = 10,
|
||||
Strength = 10,
|
||||
GetAim = 40,
|
||||
GetDodge = 20
|
||||
};
|
||||
AddComponent(stat);
|
||||
}
|
||||
|
||||
@@ -146,9 +147,9 @@ namespace DepthsBelow
|
||||
|
||||
if (Transform.World == nodeWorldPos)
|
||||
{
|
||||
if (currentStep < numberOfSteps)
|
||||
if (step < numberOfSteps)
|
||||
{
|
||||
currentStep++;
|
||||
step++;
|
||||
lastLastNode = lastNode;
|
||||
lastNode = nextNode;
|
||||
nextNode = GetComponent<PathFinder>().Next();
|
||||
|
||||
Regular → Executable
+71
-69
@@ -1,23 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using DepthsBelow.Component;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
using System.Text;
|
||||
using DepthsBelow.Component;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace DepthsBelow
|
||||
{
|
||||
/// <summary>
|
||||
/// Various global utility functions.
|
||||
/// <summary>
|
||||
/// Various global utility functions.
|
||||
/// </summary>
|
||||
static class Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Calculate the chance of an entity with a Stat component to hit another unit.
|
||||
/// </summary>
|
||||
/// <param name="attacker">The attacking unit.</param>
|
||||
/// <param name="defender">The recieving unit.</param>
|
||||
/// <summary>
|
||||
/// Calculate the chance of an entity with a Stat component to hit another unit.
|
||||
/// </summary>
|
||||
/// <param name="attacker">The attacking unit.</param>
|
||||
/// <param name="defender">The recieving unit.</param>
|
||||
/// <returns>Returns a hit chance percentage.</returns>
|
||||
public static int CalculateHitChance(Entity attacker, Entity defender)
|
||||
{
|
||||
@@ -29,7 +29,9 @@ namespace DepthsBelow
|
||||
}
|
||||
int baseHitChance = shooting.GetAim;
|
||||
int baseDodgeChance = dodging.GetDodge;
|
||||
int basePenalty = shooting.Penalty((int)Vector2.Distance(attacker.GetComponent<Transform>().World.Position, defender.GetComponent<Transform>().World.Position) / Grid.TileSize);
|
||||
int distance = (int)Vector2.Distance(attacker.GetComponent<Transform>().World.Position, defender.GetComponent<Transform>().World.Position);
|
||||
int basePenalty = shooting.Penalty(distance / (Grid.TileSize));
|
||||
Console.WriteLine("Penalty = " + basePenalty);
|
||||
int chanceToHit = baseHitChance - baseDodgeChance - basePenalty;
|
||||
//Console.WriteLine(chanceToHit);
|
||||
return chanceToHit;
|
||||
@@ -45,61 +47,61 @@ namespace DepthsBelow
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}*/
|
||||
private static Texture2D blank;
|
||||
|
||||
public static void DrawLine(SpriteBatch batch, Color color, float width, Vector2 start, Vector2 end)
|
||||
{
|
||||
if (blank == null)
|
||||
{
|
||||
blank = new Texture2D(Core.GraphicsDeviceManager.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
|
||||
blank.SetData(new[] { color });
|
||||
}
|
||||
|
||||
float angle = (float)Math.Atan2(end.Y - start.Y, end.X - start.X);
|
||||
float length = Vector2.Distance(start, end);
|
||||
|
||||
batch.Draw(blank, start, null, color,
|
||||
angle, Vector2.Zero, new Vector2(length, width),
|
||||
SpriteEffects.None, 0);
|
||||
}
|
||||
|
||||
private static Texture2D solidTexture = null;
|
||||
/// <summary>
|
||||
/// Creates a 1x1 solid white XNA texture.
|
||||
/// </summary>
|
||||
/// <returns>Returns a 1x1 solid white Texture2D object.</returns>
|
||||
public static Texture2D GetSolidTexture()
|
||||
{
|
||||
if (solidTexture == null)
|
||||
{
|
||||
solidTexture = new Texture2D(GameServices.GetService<GraphicsDevice>(), 1, 1);
|
||||
solidTexture.SetData(new Color[] { Color.White });
|
||||
}
|
||||
|
||||
return solidTexture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts rectangles with negative sizes to positive sizes with its position offset.
|
||||
/// </summary>
|
||||
/// <param name="rect">The rectangle with potentially negative sizes.</param>
|
||||
/// <returns>Returns a rectangle with positive sizes.</returns>
|
||||
public static Rectangle MakeRectanglePositive(Rectangle rect)
|
||||
{
|
||||
if (rect.Width < 0)
|
||||
{
|
||||
rect.X += rect.Width;
|
||||
rect.Width = -rect.Width;
|
||||
}
|
||||
|
||||
if (rect.Height < 0)
|
||||
{
|
||||
rect.Y += rect.Height;
|
||||
rect.Height = -rect.Height;
|
||||
}
|
||||
|
||||
return rect;
|
||||
}
|
||||
}*/
|
||||
private static Texture2D blank;
|
||||
|
||||
public static void DrawLine(SpriteBatch batch, Color color, float width, Vector2 start, Vector2 end)
|
||||
{
|
||||
if (blank == null)
|
||||
{
|
||||
blank = new Texture2D(Core.GraphicsDeviceManager.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
|
||||
blank.SetData(new[] { color });
|
||||
}
|
||||
|
||||
float angle = (float)Math.Atan2(end.Y - start.Y, end.X - start.X);
|
||||
float length = Vector2.Distance(start, end);
|
||||
|
||||
batch.Draw(blank, start, null, color,
|
||||
angle, Vector2.Zero, new Vector2(length, width),
|
||||
SpriteEffects.None, 0);
|
||||
}
|
||||
|
||||
private static Texture2D solidTexture = null;
|
||||
/// <summary>
|
||||
/// Creates a 1x1 solid white XNA texture.
|
||||
/// </summary>
|
||||
/// <returns>Returns a 1x1 solid white Texture2D object.</returns>
|
||||
public static Texture2D GetSolidTexture()
|
||||
{
|
||||
if (solidTexture == null)
|
||||
{
|
||||
solidTexture = new Texture2D(GameServices.GetService<GraphicsDevice>(), 1, 1);
|
||||
solidTexture.SetData(new Color[] { Color.White });
|
||||
}
|
||||
|
||||
return solidTexture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts rectangles with negative sizes to positive sizes with its position offset.
|
||||
/// </summary>
|
||||
/// <param name="rect">The rectangle with potentially negative sizes.</param>
|
||||
/// <returns>Returns a rectangle with positive sizes.</returns>
|
||||
public static Rectangle MakeRectanglePositive(Rectangle rect)
|
||||
{
|
||||
if (rect.Width < 0)
|
||||
{
|
||||
rect.X += rect.Width;
|
||||
rect.Width = -rect.Width;
|
||||
}
|
||||
|
||||
if (rect.Height < 0)
|
||||
{
|
||||
rect.Y += rect.Height;
|
||||
rect.Height = -rect.Height;
|
||||
}
|
||||
|
||||
return rect;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user