Merge branch 'master' of github.com:sippeangelo/DepthsBelow

Conflicts:
	DepthsBelow/DepthsBelow/Core.cs
This commit is contained in:
Simon Holmberg
2012-10-09 19:20:13 +02:00
5 changed files with 390 additions and 294 deletions
+16
View File
@@ -23,6 +23,7 @@ namespace DepthsBelow
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;
@@ -86,6 +87,8 @@ namespace DepthsBelow
MouseInput.LoadContent(); MouseInput.LoadContent();
KeyboardInput = new KeyboardInput(this); KeyboardInput = new KeyboardInput(this);
TestMonster.X = 12;
TestMonster.Y = 4;
// Test GUI frames // Test GUI frames
var frame = new GUI.Button() var frame = new GUI.Button()
{ {
@@ -134,11 +137,24 @@ namespace DepthsBelow
foreach (var body in Swarm) foreach (var body in Swarm)
body.Update(gameTime); body.Update(gameTime);
TestMonster.Update(gameTime);
GUIManager.Update(gameTime); GUIManager.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.
/// </summary> /// </summary>
+47 -4
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,13 +25,54 @@ 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.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<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;
} }
} }
} }
} }
+184 -179
View File
@@ -1,179 +1,184 @@
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 Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Media;
namespace DepthsBelow namespace DepthsBelow
{ {
public class MouseInput public class MouseInput
{ {
Core core; Core core;
MouseState lastMouseState; MouseState lastMouseState;
Rectangle selectionRectangle; Rectangle selectionRectangle;
Texture2D selectionTexture; Texture2D selectionTexture;
Rectangle gridRectangle; Rectangle gridRectangle;
Texture2D gridTexture; Texture2D gridTexture;
Vector2 directionStart; Vector2 directionStart;
Vector2 directionNow; Vector2 directionNow;
bool checkingDirection = false; bool checkingDirection = false;
bool readjust = false; bool readjust = false;
public MouseInput(Core core) public MouseInput(Core core)
{ {
this.core = core; this.core = core;
selectionRectangle = Rectangle.Empty; selectionRectangle = Rectangle.Empty;
gridRectangle = Rectangle.Empty; gridRectangle = Rectangle.Empty;
} }
public void LoadContent() public void LoadContent()
{ {
selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1); selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1);
selectionTexture.SetData(new Color[] { Color.White }); selectionTexture.SetData(new Color[] { Color.White });
gridTexture = new Texture2D(core.GraphicsDevice, 1, 1); gridTexture = new Texture2D(core.GraphicsDevice, 1, 1);
gridTexture.SetData(new Color[] { Color.White }); gridTexture.SetData(new Color[] { Color.White });
} }
public void 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));
KeyboardState ks = Keyboard.GetState(); KeyboardState ks = Keyboard.GetState();
if (core.PlayerTurn == true)
// Selection rectangle {
if (ms.LeftButton == ButtonState.Pressed) // Selection rectangle
{ if (ms.LeftButton == ButtonState.Pressed)
{
if (selectionRectangle == Rectangle.Empty)
{ if (selectionRectangle == Rectangle.Empty)
directionStart = mouseWorldPos; {
selectionRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 0, 0); directionStart = mouseWorldPos;
readjust = false; selectionRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 0, 0);
} 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 rectangleY = (int)mouseWorldPos.Y; int rectangleX = (int)mouseWorldPos.X - ((int)mouseWorldPos.X - (int)directionStart.X);
int rectangleWidth = (int)mouseWorldPos.X - (int)directionStart.X; int rectangleY = (int)mouseWorldPos.Y;
int rectangleHeight = (int)directionStart.Y - (int)mouseWorldPos.Y; int rectangleWidth = (int)mouseWorldPos.X - (int)directionStart.X;
selectionRectangle = new Rectangle(rectangleX, rectangleY, rectangleWidth, rectangleHeight); int rectangleHeight = (int)directionStart.Y - (int)mouseWorldPos.Y;
readjust = true; selectionRectangle = new Rectangle(rectangleX, rectangleY, rectangleWidth, rectangleHeight);
} 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.Height = (int)mouseWorldPos.Y - selectionRectangle.Y; selectionRectangle.Width = (int)mouseWorldPos.X - selectionRectangle.X;
readjust = false; selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y;
} readjust = false;
} }
} }
// When the mouse is released }
if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty) // When the mouse is released
{ if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty)
// Deselect all units {
if (!ks.IsKeyDown(Keys.LeftControl)) // Deselect all units
{ if (!ks.IsKeyDown(Keys.LeftControl))
foreach (var unit in core.Squad) {
unit.Selected = false; foreach (var unit in core.Squad)
} unit.Selected = false;
}
// Select all units in the rectangle
foreach (var unit in core.Squad) // Select all units in the rectangle
{ foreach (var unit in core.Squad)
if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle)) {
unit.Selected = true; if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
} unit.Selected = true;
}
// Hide the selection rectangle
selectionRectangle = Rectangle.Empty; // Hide the selection rectangle
} selectionRectangle = Rectangle.Empty;
}
// Send orders with right click
if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed) // Send orders with right click
{ if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
foreach (var unit in core.Squad) {
if (unit.Selected) if (checkingDirection == false)
{ {
unit.GetComponent<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos); 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)) if (ms.RightButton == ButtonState.Pressed)
{ {
checkingDirection = true; foreach (var unit in core.Squad)
directionStart = unit.Transform.World + unit.Transform.World.Origin; {
} if (unit.Selected && gridRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
} {
} checkingDirection = true;
if (checkingDirection == true && ms.RightButton == ButtonState.Pressed) directionStart = unit.Transform.World + unit.Transform.World.Origin;
{ }
foreach (var unit in core.Squad) }
if (unit.Selected) }
{ if (checkingDirection == true && 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; if (unit.Selected)
float directionY = mouseWorldPos.Y - unit.Transform.World.Y; {
var mouseDirection = mouseWorldPos; directionNow.X = mouseWorldPos.X;
mouseDirection.Normalize(); directionNow.Y = mouseWorldPos.Y;
//float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi; float directionX = mouseWorldPos.X - unit.Transform.World.X;
/*if (directionX < 0) float directionY = mouseWorldPos.Y - unit.Transform.World.Y;
{ var mouseDirection = mouseWorldPos;
directionX *= -1; mouseDirection.Normalize();
unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX); //float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi;
} /*if (directionX < 0)
else {
{ directionX *= -1;
*/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);/* unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX);
}*/ }
//Console.WriteLine(angle); else
//unit.GetComponent<Component.Transform>().World.Rotation = angle; {
} */
} 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);/*
else }*/
{ //Console.WriteLine(angle);
checkingDirection = false; //unit.GetComponent<Component.Transform>().World.Rotation = angle;
} }
}
// Grid highlighting else
Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos); {
Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos); checkingDirection = false;
gridRectangle = new Rectangle((int)rectWorldPos.X, (int)rectWorldPos.Y, Grid.TileSize, Grid.TileSize); }
}
lastMouseState = ms; // Grid highlighting
} Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos);
Vector2 rectWorldPos = Grid.GridToWorld(mouseGridPos);
public void Draw(SpriteBatch spriteBatch) gridRectangle = new Rectangle((int)rectWorldPos.X, (int)rectWorldPos.Y, Grid.TileSize, Grid.TileSize);
{
spriteBatch.Draw(selectionTexture, selectionRectangle, Color.Red * 0.5f); lastMouseState = ms;
spriteBatch.Draw(gridTexture, gridRectangle, Color.Yellow * 0.3f); }
if (checkingDirection == true) public void Draw(SpriteBatch spriteBatch)
{ {
RenderHelpers.DrawLine(spriteBatch, Color.White, 2, directionStart, directionNow); 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);
} }
}
}
}
+141 -106
View File
@@ -1,107 +1,142 @@
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 int numberOfSteps = 5;
: base(core) public int currentStep = 0;
{
if (Texture == null) public int step
LoadContent(core); {
get { return currentStep; }
this.Swarm = swarm; set
{
Transform.World.Origin = new Vector2(20, 16); currentStep = value;
}
this.Color = Color.White; }
var rc = new SpriteRenderer(this) { Texture = Texture, Color = Color.White };
AddComponent(rc); public SmallEnemy(Core core, ref List<SmallEnemy> swarm)
: base(core)
var cc = new Collision(this, 32, 32); {
AddComponent(cc); if (Texture == null)
LoadContent(core);
var pfc = new PathFinder(this);
AddComponent(pfc); this.Swarm = swarm;
}
Transform.World.Origin = new Vector2(20, 16);
public bool Selected
{ this.Color = Color.White;
get { return _selected; } var rc = new SpriteRenderer(this) { Texture = Texture, Color = Color.White };
set AddComponent(rc);
{
_selected = value; var cc = new Collision(this, 32, 32);
GetComponent<SpriteRenderer>().Color = (value) ? Color.Blue : this.Color; AddComponent(cc);
}
} var pfc = new PathFinder(this);
AddComponent(pfc);
public static void LoadContent(Core core) }
{
Texture = core.Content.Load<Texture2D>("images/Monster"); public bool Selected
} {
get { return _selected; }
public override void Update(GameTime gameTime) set
{ {
base.Update(gameTime); _selected = value;
GetComponent<SpriteRenderer>().Color = (value) ? Color.Blue : this.Color;
/*float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; }
}
if (nextNode == null)
nextNode = GetComponent<PathFinder>().Next(); public static void LoadContent(Core core)
{
if (nextNode != null) Texture = core.Content.Load<Texture2D>("images/Monster");
{ }
List<Point> soldierCollisions = new List<Point>();
public override void Update(GameTime gameTime)
foreach (var body in Swarm) {
{ base.Update(gameTime);
if (body != this)
{ float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
if (body.Transform.Grid == nextNode.Position)
{ if (nextNode == null)
soldierCollisions.Add(body.Transform.Grid); nextNode = GetComponent<PathFinder>().Next();
GetComponent<PathFinder>().RecreatePath(soldierCollisions);
nextNode = GetComponent<PathFinder>().Next(); if (nextNode != null)
break; {
} List<Point> soldierCollisions = new List<Point>();
}
} foreach (var body in Swarm)
{
if (body != this)
{
if (nextNode != null) if (body.Transform.Grid == nextNode.Position)
{ {
var nodeWorldPos = Grid.GridToWorld(nextNode.Position); soldierCollisions.Add(body.Transform.Grid);
// HACK: Make this work properly with other speeds... GetComponent<PathFinder>().RecreatePath(soldierCollisions);
float speed = 4f; nextNode = GetComponent<PathFinder>().Next();
if (Transform.World.X < nodeWorldPos.X) break;
Transform.World.X += speed; }
if (Transform.World.X > nodeWorldPos.X) }
Transform.World.X -= speed; }
if (Transform.World.Y < nodeWorldPos.Y)
Transform.World.Y += speed; foreach (var soldier in core.Squad)
if (Transform.World.Y > nodeWorldPos.Y) {
Transform.World.Y -= speed; var pathFinder = soldier.GetComponent<PathFinder>();
var position = nextNode.Position;
if (Transform.World == nodeWorldPos) if (soldier.Transform.Grid == position)
nextNode = GetComponent<PathFinder>().Next(); {
} soldierCollisions.Add(soldier.Transform.Grid);
GetComponent<PathFinder>().RecreatePath(soldierCollisions);
}*/ nextNode = GetComponent<PathFinder>().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<PathFinder>().Next();
}
else
{
GetComponent<PathFinder>().Stop();
}
}
}
}
}
}
} }
+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();