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