diff --git a/DepthsBelow/DepthsBelow/Core.cs b/DepthsBelow/DepthsBelow/Core.cs index 7dd9cf8..60d9975 100755 --- a/DepthsBelow/DepthsBelow/Core.cs +++ b/DepthsBelow/DepthsBelow/Core.cs @@ -23,6 +23,7 @@ namespace DepthsBelow public Camera Camera; + public bool PlayerTurn = true; public static MouseInput MouseInput; public static KeyboardInput KeyboardInput; public List Squad; @@ -86,6 +87,8 @@ namespace DepthsBelow MouseInput.LoadContent(); KeyboardInput = new KeyboardInput(this); + TestMonster.X = 12; + TestMonster.Y = 4; // Test GUI frames var frame = new GUI.Button() { @@ -134,11 +137,24 @@ namespace DepthsBelow foreach (var body in Swarm) body.Update(gameTime); + TestMonster.Update(gameTime); GUIManager.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; + } + /// /// This is called when the game should draw itself. /// diff --git a/DepthsBelow/DepthsBelow/KeyboardInput.cs b/DepthsBelow/DepthsBelow/KeyboardInput.cs index 41ec90c..71db3e1 100644 --- a/DepthsBelow/DepthsBelow/KeyboardInput.cs +++ b/DepthsBelow/DepthsBelow/KeyboardInput.cs @@ -13,6 +13,8 @@ namespace DepthsBelow public class KeyboardInput { Core core; + bool Enter = false; + bool turn; public KeyboardInput(Core core) { @@ -23,13 +25,54 @@ namespace DepthsBelow { 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.TestMonster.step = 0; + 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().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; } } } -} +} \ No newline at end of file diff --git a/DepthsBelow/DepthsBelow/MouseInput.cs b/DepthsBelow/DepthsBelow/MouseInput.cs index 691a918..34211d0 100755 --- a/DepthsBelow/DepthsBelow/MouseInput.cs +++ b/DepthsBelow/DepthsBelow/MouseInput.cs @@ -1,179 +1,184 @@ -using System; -using System.Collections.Generic; -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; - - - 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 Update(GameTime gameTime) - { - MouseState ms = Mouse.GetState(); - Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y)); - KeyboardState ks = Keyboard.GetState(); - - // 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().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) - { - foreach (var unit in core.Squad) - if (unit.Selected) - { - unit.GetComponent().Goal = Grid.WorldToGrid(mouseWorldPos); - } - - } - if (ms.RightButton == ButtonState.Pressed) - { - foreach (var unit in core.Squad) - { - if (unit.Selected && gridRectangle.Intersects(unit.GetComponent().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().World.Rotation = (float)Math.Atan(directionY / directionX); - } - else - { - */unit.GetComponent().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().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) - { - RenderHelpers.DrawLine(spriteBatch, Color.White, 2, directionStart, directionNow); - } - - } - - } -} +using System; +using System.Collections.Generic; +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; + + + 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 Update(GameTime gameTime) + { + MouseState ms = Mouse.GetState(); + Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y)); + KeyboardState ks = Keyboard.GetState(); + if (core.PlayerTurn == true) + { + // 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().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().Goal = Grid.WorldToGrid(mouseWorldPos); + } + } + + } + if (ms.RightButton == ButtonState.Pressed) + { + foreach (var unit in core.Squad) + { + if (unit.Selected && gridRectangle.Intersects(unit.GetComponent().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().World.Rotation = (float)Math.Atan(directionY / directionX); + } + else + { + */ + unit.GetComponent().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().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) + { + RenderHelpers.DrawLine(spriteBatch, Color.White, 2, directionStart, directionNow); + } + + } + + } +} diff --git a/DepthsBelow/DepthsBelow/SmallEnemy.cs b/DepthsBelow/DepthsBelow/SmallEnemy.cs index 11e72fe..b1b4aa2 100644 --- a/DepthsBelow/DepthsBelow/SmallEnemy.cs +++ b/DepthsBelow/DepthsBelow/SmallEnemy.cs @@ -1,107 +1,142 @@ -using System; -using System.Collections.Generic; -using DepthsBelow.Component; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; - -namespace DepthsBelow -{ - public class SmallEnemy : Entity - { - public static Texture2D Texture; - public Color Color; - public static Point Origin; - private bool _selected; //I guess as in "currently doing something" - - public List Swarm; - - private PathFinder.Node nextNode; - - public SmallEnemy(Core core, ref List swarm) - : base(core) - { - if (Texture == null) - LoadContent(core); - - this.Swarm = swarm; - - Transform.World.Origin = new Vector2(20, 16); - - this.Color = Color.White; - var rc = new SpriteRenderer(this) { Texture = Texture, Color = Color.White }; - AddComponent(rc); - - var cc = new Collision(this, 32, 32); - AddComponent(cc); - - var pfc = new PathFinder(this); - AddComponent(pfc); - } - - public bool Selected - { - get { return _selected; } - set - { - _selected = value; - GetComponent().Color = (value) ? Color.Blue : this.Color; - } - } - - public static void LoadContent(Core core) - { - Texture = core.Content.Load("images/Monster"); - } - - public override void Update(GameTime gameTime) - { - base.Update(gameTime); - - /*float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; - - if (nextNode == null) - nextNode = GetComponent().Next(); - - if (nextNode != null) - { - List soldierCollisions = new List(); - - foreach (var body in Swarm) - { - if (body != this) - { - if (body.Transform.Grid == nextNode.Position) - { - soldierCollisions.Add(body.Transform.Grid); - GetComponent().RecreatePath(soldierCollisions); - nextNode = GetComponent().Next(); - break; - } - } - } - - - - if (nextNode != null) - { - var nodeWorldPos = Grid.GridToWorld(nextNode.Position); - // HACK: Make this work properly with other speeds... - float speed = 4f; - if (Transform.World.X < nodeWorldPos.X) - Transform.World.X += speed; - if (Transform.World.X > nodeWorldPos.X) - Transform.World.X -= speed; - if (Transform.World.Y < nodeWorldPos.Y) - Transform.World.Y += speed; - if (Transform.World.Y > nodeWorldPos.Y) - Transform.World.Y -= speed; - - if (Transform.World == nodeWorldPos) - nextNode = GetComponent().Next(); - } - - }*/ - } - } +using System; +using System.Collections.Generic; +using DepthsBelow.Component; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; + +namespace DepthsBelow +{ + public class SmallEnemy : Entity + { + public static Texture2D Texture; + public Color Color; + public static Point Origin; + private bool _selected; //I guess as in "currently doing something" + + public List Swarm; + + private PathFinder.Node nextNode; + + public int numberOfSteps = 5; + public int currentStep = 0; + + public int step + { + get { return currentStep; } + set + { + currentStep = value; + } + } + + public SmallEnemy(Core core, ref List swarm) + : base(core) + { + if (Texture == null) + LoadContent(core); + + this.Swarm = swarm; + + Transform.World.Origin = new Vector2(20, 16); + + this.Color = Color.White; + var rc = new SpriteRenderer(this) { Texture = Texture, Color = Color.White }; + AddComponent(rc); + + var cc = new Collision(this, 32, 32); + AddComponent(cc); + + var pfc = new PathFinder(this); + AddComponent(pfc); + } + + public bool Selected + { + get { return _selected; } + set + { + _selected = value; + GetComponent().Color = (value) ? Color.Blue : this.Color; + } + } + + public static void LoadContent(Core core) + { + Texture = core.Content.Load("images/Monster"); + } + + public override void Update(GameTime gameTime) + { + base.Update(gameTime); + + float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; + + if (nextNode == null) + nextNode = GetComponent().Next(); + + if (nextNode != null) + { + List soldierCollisions = new List(); + + foreach (var body in Swarm) + { + if (body != this) + { + if (body.Transform.Grid == nextNode.Position) + { + soldierCollisions.Add(body.Transform.Grid); + GetComponent().RecreatePath(soldierCollisions); + nextNode = GetComponent().Next(); + break; + } + } + } + + foreach (var soldier in core.Squad) + { + var pathFinder = soldier.GetComponent(); + var position = nextNode.Position; + if (soldier.Transform.Grid == position) + { + soldierCollisions.Add(soldier.Transform.Grid); + GetComponent().RecreatePath(soldierCollisions); + nextNode = GetComponent().Next(); + break; + } + } + + if (nextNode != null) + { + var nodeWorldPos = Grid.GridToWorld(nextNode.Position); + // HACK: Make this work properly with other speeds... + float speed = 4f; + if (Transform.World.X < nodeWorldPos.X) + Transform.World.X += speed; + if (Transform.World.X > nodeWorldPos.X) + Transform.World.X -= speed; + if (Transform.World.Y < nodeWorldPos.Y) + Transform.World.Y += speed; + if (Transform.World.Y > nodeWorldPos.Y) + Transform.World.Y -= speed; + + if (Transform.World == nodeWorldPos) + { + if (currentStep < numberOfSteps) + { + currentStep++; + //lastLastNode = lastNode; + //lastNode = nextNode; + nextNode = GetComponent().Next(); + } + else + { + GetComponent().Stop(); + } + } + } + + } + } + } } \ No newline at end of file diff --git a/DepthsBelow/DepthsBelow/Soldier.cs b/DepthsBelow/DepthsBelow/Soldier.cs index 86af12f..3c5a4c4 100644 --- a/DepthsBelow/DepthsBelow/Soldier.cs +++ b/DepthsBelow/DepthsBelow/Soldier.cs @@ -25,10 +25,7 @@ namespace DepthsBelow public int step { - get - { - return currentStep; - } + get { return currentStep; } set { currentStep = value; @@ -75,7 +72,7 @@ namespace DepthsBelow base.Update(gameTime); float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; - + if (nextNode == null) nextNode = GetComponent().Next();