3 hours later

This commit is contained in:
Simon Holmberg
2012-09-28 18:59:47 +02:00
parent adfdf963d8
commit cf100c95f1
16 changed files with 1672 additions and 10 deletions
+55
View File
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace DepthsBelow.Component
{
class PathFinder : Component
{
public Point Start { get; private set; }
public Point Goal
{
get { return goal; }
set
{
this.goal = value;
this.Start = this.Parent.GetComponent<GridTransform>().Position;
this.FindPath(this.Start, value);
}
}
private Point goal;
private List<AStar.PathFinderNode> path;
public PathFinder(Entity parent)
: base(parent)
{
}
public override void Update(GameTime gameTime)
{
if (path != null && path.Count != 0)
{
var nextNode = path.Last();
var nodePos = new Point(nextNode.X, nextNode.Y);
Parent.gridTransform.Position = nodePos;
if (Parent.pixelTransform.Position == Parent.gridTransform.ToWorld())
path.Remove(nextNode);
}
base.Update(gameTime);
}
public void FindPath(Point start, Point goal)
{
var _start = new System.Drawing.Point(start.X, start.Y);
var _goal = new System.Drawing.Point(goal.X, goal.Y);
path = Core.PathFinder.FindPath(_start, _goal);
if (path == null)
Console.WriteLine("Path not found!");
}
}
}
+13 -1
View File
@@ -19,6 +19,8 @@ namespace DepthsBelow
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public static AStar.PathFinderFast PathFinder;
public Camera camera;
MouseInput mouseInput;
public Soldier soldier;
@@ -33,7 +35,7 @@ namespace DepthsBelow
graphics.PreferredBackBufferHeight = 720;
graphics.IsFullScreen = false;
//graphics.SynchronizeWithVerticalRetrace = false;
graphics.SynchronizeWithVerticalRetrace = false;
this.IsFixedTimeStep = false;
graphics.ApplyChanges();
@@ -63,6 +65,16 @@ namespace DepthsBelow
spriteBatch = new SpriteBatch(GraphicsDevice);
map = Content.Load<Map>("maps/Cave.Level1");
PathFinder = new AStar.PathFinderFast(map.GetCollisionMap());
PathFinder.Formula = AStar.HeuristicFormula.Manhattan;
PathFinder.Diagonals = false;
PathFinder.HeavyDiagonals = false;
PathFinder.HeuristicEstimate = 2;
PathFinder.PunishChangeDirection = true;
PathFinder.TieBreaker = false;
PathFinder.SearchLimit = 50000;
PathFinder.DebugProgress = false;
PathFinder.DebugFoundPath = false;
// TODO: use this.Content to load your game content here
Soldier.LoadContent(this);
+8 -1
View File
@@ -106,6 +106,7 @@
<Reference Include="System">
<Private>False</Private>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Xml">
<Private>False</Private>
</Reference>
@@ -121,6 +122,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Camera.cs" />
<Compile Include="Component\PathFinder.cs" />
<Compile Include="Grid.cs" />
<Compile Include="Component\Collision.cs" />
<Compile Include="Component\Component.cs" />
@@ -140,9 +142,14 @@
<Content Include="GameThumbnail.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\AStar\AStar.csproj">
<Project>{CD3F949D-54AA-4D38-99DB-92905A375D84}</Project>
<Name>AStar</Name>
</ProjectReference>
<ProjectReference Include="..\DepthsBelowContent\DepthsBelowContent.contentproj">
<Name>DepthsBelowContent</Name>
<Name>DepthsBelowContent %28Content%29</Name>
<XnaReferenceType>Content</XnaReferenceType>
<Project>{433A77A2-DE52-4875-A4EF-232CF59C2DD3}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
+32 -1
View File
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@@ -6,6 +7,7 @@ namespace DepthsBelow
{
public class Tile
{
public int LocalId;
public Texture2D Texture;
public Rectangle SourceRectangle;
public SpriteEffects SpriteEffects;
@@ -53,5 +55,34 @@ namespace DepthsBelow
}
}
}
public byte[,] GetCollisionMap()
{
byte[,] collisionMap = new byte[1024, 1024];
foreach (var layer in Layers)
{
if (layer.Name == "Collision")
{
for (int y = 0; y < layer.Height; y++)
{
for (int x = 0; x < layer.Width; x++)
{
Tile tile = layer.Tiles[y*layer.Width + x];
if (tile == null || tile.LocalId == 0)
collisionMap[x, y] = 1;
else
collisionMap[x, y] = 0;
}
}
break;
}
}
//if (collisionMap == null)
// throw new Exception("Map lacks a Collision layer!");
return collisionMap;
}
}
}
+2
View File
@@ -58,7 +58,9 @@ namespace DepthsBelow
if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty)
{
if (!ks.IsKeyDown(Keys.LeftControl))
{
core.soldier.Selected = false;
}
if (selectionRectangle.Intersects(core.soldier.GetComponent<Component.Collision>().Rectangle))
{
+10 -4
View File
@@ -20,12 +20,18 @@ namespace DepthsBelow
LoadContent(core);
pixelTransform.Origin = new Vector2(16, 16);
gridTransform.Position = new Point(7, 3);
pixelTransform.Position = gridTransform.ToWorld();
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);
pfc.Goal = new Point(8, 39);
AddComponent(pfc);
}
public bool Selected
@@ -70,14 +76,14 @@ namespace DepthsBelow
}
lastKeyboardState = ks;
int speed = 2;
int speed = 1;
if (pixelTransform.X < gridTransform.ToWorld().X)
pixelTransform.X += speed;
else if (pixelTransform.X > gridTransform.ToWorld().X)
if (pixelTransform.X > gridTransform.ToWorld().X)
pixelTransform.X -= speed;
else if (pixelTransform.Y < gridTransform.ToWorld().Y)
if (pixelTransform.Y < gridTransform.ToWorld().Y)
pixelTransform.Y += speed;
else if (pixelTransform.Y > gridTransform.ToWorld().Y)
if (pixelTransform.Y > gridTransform.ToWorld().Y)
pixelTransform.Y -= speed;
}
}
File diff suppressed because one or more lines are too long
@@ -15,6 +15,7 @@ namespace DepthsBelowContentPipeline
[ContentSerializerRuntimeType("DepthsBelow.Tile, DepthsBelow")]
public class MapTileContent
{
public int LocalId;
public ExternalReference<Texture2DContent> Texture;
public Rectangle SourceRectangle;
public SpriteEffects SpriteEffects;
@@ -113,6 +114,7 @@ namespace DepthsBelowContentPipeline
// now insert the tile into our output
outLayer.Tiles[i] = new MapTileContent
{
LocalId = tileIndex,
Texture = textureContent,
SourceRectangle = sourceRect,
SpriteEffects = spriteEffects