Enemy hunt closest soldier on enter

This commit is contained in:
Niklas Ulf Anders Cullberg
2012-10-09 19:01:14 +02:00
parent ff4e874d47
commit 29288df309
5 changed files with 268 additions and 211 deletions
+23 -5
View File
@@ -19,8 +19,9 @@ namespace DepthsBelow
public static GraphicsDeviceManager GraphicsDeviceManager; public static GraphicsDeviceManager GraphicsDeviceManager;
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
public Camera Camera; public Camera Camera;
public bool PlayerTurn = true;
public static MouseInput MouseInput; public static MouseInput MouseInput;
public static KeyboardInput KeyboardInput; public static KeyboardInput KeyboardInput;
public List<Soldier> Squad; public List<Soldier> Squad;
@@ -82,7 +83,10 @@ namespace DepthsBelow
MouseInput = new MouseInput(this); MouseInput = new MouseInput(this);
MouseInput.LoadContent(); MouseInput.LoadContent();
KeyboardInput = new KeyboardInput(this); KeyboardInput = new KeyboardInput(this);
TestMonster.X = 12;
TestMonster.Y = 4;
} }
/// <summary> /// <summary>
@@ -114,10 +118,24 @@ namespace DepthsBelow
soldier.Update(gameTime); soldier.Update(gameTime);
foreach (var body in Swarm) foreach (var body in Swarm)
body.Update(gameTime); body.Update(gameTime);
TestMonster.Update(gameTime);
base.Update(gameTime); base.Update(gameTime);
} }
public int FindDistance(Vector2 target, Vector2 start)
{
Vector2 combine = new Vector2(target.X - start.X, target.Y - start.Y);
int result = 0;
int firstRestult = (int)Math.Sqrt((int)combine.X^2 + (int)combine.Y^2);
result = (int)Vector2.Distance(target, start);
return result;
}
/// <summary> /// <summary>
/// This is called when the game should draw itself. /// This is called when the game should draw itself.
+45 -3
View File
@@ -13,6 +13,8 @@ namespace DepthsBelow
public class KeyboardInput public class KeyboardInput
{ {
Core core; Core core;
bool Enter = false;
bool turn;
public KeyboardInput(Core core) public KeyboardInput(Core core)
{ {
@@ -23,12 +25,52 @@ namespace DepthsBelow
{ {
KeyboardState ks = Keyboard.GetState(); KeyboardState ks = Keyboard.GetState();
if (ks.IsKeyDown(Keys.Enter)) if (ks.IsKeyUp(Keys.Enter) && Enter == true)
{ {
foreach (var unit in core.Squad) Enter = false;
}
if (ks.IsKeyDown(Keys.Enter) && Enter == false)
{
Enter = true;
if (core.PlayerTurn == false)
{ {
unit.step = 0; core.PlayerTurn = true;
foreach (var unit in core.Squad)
{
unit.step = 0;
}
} }
else
{
core.PlayerTurn = false;
Point target = Point.Zero;
int distance = 7000;
foreach (var unit in core.Squad)
{
Vector2 soldier = new Vector2(unit.Transform.Grid.X, unit.Transform.Grid.Y);
Vector2 monster = new Vector2(core.TestMonster.Transform.Grid.X, core.TestMonster.Transform.Grid.Y);
int newDistance = core.FindDistance(soldier, monster);
if (newDistance < distance)
{
target = unit.Transform.Grid;
distance = newDistance;
}
}
core.TestMonster.GetComponent<Component.PathFinder>().Goal = target;
distance = 7000;
}
}
if (ks.IsKeyDown(Keys.A))
{
/*foreach (var unit in core.Swarm)
{
unit.X = 200;
unit.Y = 200;
}*/
core.TestMonster.X = 12;
core.TestMonster.Y = 4;
} }
} }
} }
+94 -92
View File
@@ -49,115 +49,117 @@ namespace DepthsBelow
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));
KeyboardState ks = Keyboard.GetState(); KeyboardState ks = Keyboard.GetState();
if (core.PlayerTurn == true)
{
// Selection rectangle
if (ms.LeftButton == ButtonState.Pressed)
{
// Selection rectangle if (selectionRectangle == Rectangle.Empty)
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); directionStart = mouseWorldPos;
int rectangleY = (int)mouseWorldPos.Y; selectionRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 0, 0);
int rectangleWidth = (int)mouseWorldPos.X - (int)directionStart.X; readjust = false;
int rectangleHeight = (int)directionStart.Y - (int)mouseWorldPos.Y;
selectionRectangle = new Rectangle(rectangleX, rectangleY, rectangleWidth, rectangleHeight);
readjust = true;
} }
else else
{ {
if (readjust == true) if (mouseWorldPos.X > directionStart.X && mouseWorldPos.Y < directionStart.Y || mouseWorldPos.X < directionStart.X && mouseWorldPos.Y > directionStart.Y)
{ {
selectionRectangle = new Rectangle((int)directionStart.X, (int)directionStart.Y, 0, 0); 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;
} }
selectionRectangle.Width = (int)mouseWorldPos.X - selectionRectangle.X;
selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y;
readjust = false;
} }
} }
} // When the mouse is released
// When the mouse is released if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty)
if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty) {
{ // Deselect all units
// Deselect all units if (!ks.IsKeyDown(Keys.LeftControl))
if (!ks.IsKeyDown(Keys.LeftControl)) {
{ foreach (var unit in core.Squad)
foreach (var unit in core.Squad) unit.Selected = false;
unit.Selected = false; }
}
// Select all units in the rectangle // Select all units in the rectangle
foreach (var unit in core.Squad) foreach (var unit in core.Squad)
{ {
if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle)) if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
unit.Selected = true; unit.Selected = true;
} }
// Hide the selection rectangle // Hide the selection rectangle
selectionRectangle = Rectangle.Empty; selectionRectangle = Rectangle.Empty;
} }
// Send orders with right click // Send orders with right click
if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed) if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
{ {
if (checkingDirection == false) 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) foreach (var unit in core.Squad)
if (unit.Selected) if (unit.Selected)
{ {
unit.GetComponent<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos); 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;
if (ms.RightButton == ButtonState.Pressed) mouseDirection.Normalize();
{ //float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi;
foreach (var unit in core.Squad) /*if (directionX < 0)
{ {
if (unit.Selected && gridRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle)) directionX *= -1;
{ unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX);
checkingDirection = true; }
directionStart = unit.Transform.World + unit.Transform.World.Origin; 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);/*
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); //Console.WriteLine(angle);
//unit.GetComponent<Component.Transform>().World.Rotation = angle; //unit.GetComponent<Component.Transform>().World.Rotation = angle;
} }
}
else
{
checkingDirection = false;
}
} }
else
{
checkingDirection = false;
}
// Grid highlighting // Grid highlighting
Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos); Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos);
Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos); Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos);
+104 -106
View File
@@ -1,107 +1,105 @@
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.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
namespace DepthsBelow namespace DepthsBelow
{ {
public class SmallEnemy : Entity public class SmallEnemy : Entity
{ {
public static Texture2D Texture; public static Texture2D Texture;
public Color Color; public Color Color;
public static Point Origin; public static Point Origin;
private bool _selected; //I guess as in "currently doing something" private bool _selected; //I guess as in "currently doing something"
public List<SmallEnemy> Swarm; public List<SmallEnemy> Swarm;
private PathFinder.Node nextNode; private PathFinder.Node nextNode;
public SmallEnemy(Core core, ref List<SmallEnemy> swarm) public SmallEnemy(Core core, ref List<SmallEnemy> swarm)
: base(core) : base(core)
{ {
if (Texture == null) if (Texture == null)
LoadContent(core); LoadContent(core);
this.Swarm = swarm; this.Swarm = swarm;
Transform.World.Origin = new Vector2(20, 16); Transform.World.Origin = new Vector2(20, 16);
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);
var cc = new Collision(this, 32, 32); var cc = new Collision(this, 32, 32);
AddComponent(cc); AddComponent(cc);
var pfc = new PathFinder(this); var pfc = new PathFinder(this);
AddComponent(pfc); AddComponent(pfc);
} }
public bool Selected public bool Selected
{ {
get { return _selected; } get { return _selected; }
set set
{ {
_selected = value; _selected = value;
GetComponent<SpriteRenderer>().Color = (value) ? Color.Blue : this.Color; GetComponent<SpriteRenderer>().Color = (value) ? Color.Blue : this.Color;
} }
} }
public static void LoadContent(Core core) public static void LoadContent(Core core)
{ {
Texture = core.Content.Load<Texture2D>("images/Monster"); Texture = core.Content.Load<Texture2D>("images/Monster");
} }
public override void Update(GameTime gameTime) public override void Update(GameTime gameTime)
{ {
base.Update(gameTime); base.Update(gameTime);
/*float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
if (nextNode == null) if (nextNode == null)
nextNode = GetComponent<PathFinder>().Next(); nextNode = GetComponent<PathFinder>().Next();
if (nextNode != null) if (nextNode != null)
{ {
List<Point> soldierCollisions = new List<Point>(); List<Point> soldierCollisions = new List<Point>();
foreach (var body in Swarm) foreach (var body in Swarm)
{ {
if (body != this) if (body != this)
{ {
if (body.Transform.Grid == nextNode.Position) if (body.Transform.Grid == nextNode.Position)
{ {
soldierCollisions.Add(body.Transform.Grid); soldierCollisions.Add(body.Transform.Grid);
GetComponent<PathFinder>().RecreatePath(soldierCollisions); GetComponent<PathFinder>().RecreatePath(soldierCollisions);
nextNode = GetComponent<PathFinder>().Next(); nextNode = GetComponent<PathFinder>().Next();
break; break;
} }
} }
} }
if (nextNode != null)
{
if (nextNode != null) var nodeWorldPos = Grid.GridToWorld(nextNode.Position);
{ // HACK: Make this work properly with other speeds...
var nodeWorldPos = Grid.GridToWorld(nextNode.Position); float speed = 4f;
// HACK: Make this work properly with other speeds... if (Transform.World.X < nodeWorldPos.X)
float speed = 4f; Transform.World.X += speed;
if (Transform.World.X < nodeWorldPos.X) if (Transform.World.X > nodeWorldPos.X)
Transform.World.X += speed; Transform.World.X -= speed;
if (Transform.World.X > nodeWorldPos.X) if (Transform.World.Y < nodeWorldPos.Y)
Transform.World.X -= speed; Transform.World.Y += speed;
if (Transform.World.Y < nodeWorldPos.Y) if (Transform.World.Y > nodeWorldPos.Y)
Transform.World.Y += speed; Transform.World.Y -= speed;
if (Transform.World.Y > nodeWorldPos.Y)
Transform.World.Y -= speed; if (Transform.World == nodeWorldPos)
nextNode = GetComponent<PathFinder>().Next();
if (Transform.World == nodeWorldPos) }
nextNode = GetComponent<PathFinder>().Next();
} }
}
}*/ }
}
}
} }
+2 -5
View File
@@ -25,10 +25,7 @@ namespace DepthsBelow
public int step public int step
{ {
get get { return currentStep; }
{
return currentStep;
}
set set
{ {
currentStep = value; currentStep = value;
@@ -75,7 +72,7 @@ namespace DepthsBelow
base.Update(gameTime); base.Update(gameTime);
float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
if (nextNode == null) if (nextNode == null)
nextNode = GetComponent<PathFinder>().Next(); nextNode = GetComponent<PathFinder>().Next();