Merge branch 'master' into astar

Conflicts:
	DepthsBelow/DepthsBelow/Core.cs
	DepthsBelow/DepthsBelow/MouseInput.cs
	DepthsBelow/DepthsBelowContent/maps/Cave.Level1.tmx
This commit is contained in:
Simon Holmberg
2012-09-29 00:45:30 +02:00
8 changed files with 197 additions and 36 deletions
Regular → Executable
+25 -12
View File
@@ -20,10 +20,11 @@ namespace DepthsBelow
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
public static AStar.PathFinderFast PathFinder; public static AStar.PathFinderFast PathFinder;
public Camera Camera;
public Camera camera;
MouseInput mouseInput; MouseInput mouseInput;
public Soldier soldier; //public Soldier soldier;
public List<Soldier> Squad;
private Map map; private Map map;
public Core() public Core()
@@ -51,7 +52,9 @@ namespace DepthsBelow
protected override void Initialize() protected override void Initialize()
{ {
// TODO: Add your initialization logic here // TODO: Add your initialization logic here
camera = new Camera(this); Camera = new Camera(this);
Squad = new List<Soldier>();
base.Initialize(); base.Initialize();
} }
@@ -65,6 +68,8 @@ namespace DepthsBelow
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
map = Content.Load<Map>("maps/Cave.Level1"); map = Content.Load<Map>("maps/Cave.Level1");
map.ParseObjects(this);
PathFinder = new AStar.PathFinderFast(map.GetCollisionMap()); PathFinder = new AStar.PathFinderFast(map.GetCollisionMap());
PathFinder.Formula = AStar.HeuristicFormula.Manhattan; PathFinder.Formula = AStar.HeuristicFormula.Manhattan;
PathFinder.Diagonals = false; PathFinder.Diagonals = false;
@@ -78,7 +83,7 @@ namespace DepthsBelow
// TODO: use this.Content to load your game content here // TODO: use this.Content to load your game content here
Soldier.LoadContent(this); Soldier.LoadContent(this);
soldier = new Soldier(this); //soldier = new Soldier(this);
mouseInput = new MouseInput(this); mouseInput = new MouseInput(this);
mouseInput.LoadContent(); mouseInput.LoadContent();
@@ -104,11 +109,13 @@ namespace DepthsBelow
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit(); this.Exit();
camera.Update(gameTime); Camera.Update(gameTime);
mouseInput.Update(gameTime); mouseInput.Update(gameTime);
// TODO: Add your update logic here // TODO: Add your update logic here
soldier.Update(gameTime); //soldier.Update(gameTime);
foreach (var soldier in Squad)
soldier.Update(gameTime);
base.Update(gameTime); base.Update(gameTime);
} }
@@ -121,17 +128,23 @@ namespace DepthsBelow
{ {
GraphicsDevice.Clear(Color.Black); GraphicsDevice.Clear(Color.Black);
// Start drawing using the camera transform // Start drawing using the Camera transform
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, camera.Transform); spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null,
Camera.Transform);
// Draw the level // Draw the level
map.Draw(spriteBatch); map.Draw(spriteBatch);
// Draw units // Draw units
// TODO: Foreach etc... // TODO: Foreach etc...
Component.SpriteRenderer rc = soldier.GetComponent<Component.SpriteRenderer>(); foreach (var soldier in Squad)
if (rc != null) {
rc.Draw(spriteBatch); var sr = soldier.GetComponent<Component.SpriteRenderer>();
// Draw mouse input visuals Console.WriteLine("Drawing soldier at: " + soldier.pixelTransform.X + "," + soldier.pixelTransform.Y);
if (sr != null)
sr.Draw(spriteBatch);
}
// Draw mouse input visuals
mouseInput.Draw(spriteBatch); mouseInput.Draw(spriteBatch);
spriteBatch.End(); spriteBatch.End();
+19
View File
@@ -11,6 +11,25 @@ namespace DepthsBelow
List<Component.Component> Components; List<Component.Component> Components;
public Component.PixelTransform pixelTransform; public Component.PixelTransform pixelTransform;
public Component.GridTransform gridTransform; public Component.GridTransform gridTransform;
public int X
{
get { return this.gridTransform.X; }
set
{
this.gridTransform.X = value;
this.pixelTransform.X = this.gridTransform.ToWorld().X;
}
}
public int Y
{
get { return this.gridTransform.Y; }
set
{
this.gridTransform.Y = value;
this.pixelTransform.Y = this.gridTransform.ToWorld().Y;
}
}
private Core core; private Core core;
public Entity(Core core) public Entity(Core core)
+77 -12
View File
@@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace DepthsBelow namespace DepthsBelow
{ {
public class Tile public class MapTile
{ {
public int LocalId; public int LocalId;
public Texture2D Texture; public Texture2D Texture;
@@ -13,42 +13,107 @@ namespace DepthsBelow
public SpriteEffects SpriteEffects; public SpriteEffects SpriteEffects;
} }
public class Layer public enum MapObjectType : byte
{
Plain,
Tile,
Polygon,
Polyline
}
public class MapObject
{
//public MapObjectType ObjectType;
public string Name;
public string Type;
public Rectangle Bounds;
public List<Vector2> Points;
}
public class MapLayer
{ {
public int Width; public int Width;
public int Height; public int Height;
public string Name; public string Name;
public Tile[] Tiles; }
public class MapTileLayer : MapLayer
{
public MapTile[] Tiles;
}
public class MapObjectLayer : MapLayer
{
public MapObject[] Objects;
} }
public class Map public class Map
{ {
public int TileWidth; public int TileWidth;
public int TileHeight; public int TileHeight;
public List<Layer> Layers = new List<Layer>(); public List<MapLayer> Layers = new List<MapLayer>();
public Map()
{
}
public void ParseObjects(Core core)
{
foreach (var layer in Layers)
{
var objectLayer = layer as MapObjectLayer;
if (objectLayer == null)
continue;
foreach (var mapObject in objectLayer.Objects)
{
Console.WriteLine(mapObject.Type);
Console.WriteLine(mapObject.Bounds.Left + "," + mapObject.Bounds.Top);
if (mapObject.Type == "SquadStart")
{
var soldier = new Soldier(core);
var mapObjectPos = new Vector2(mapObject.Bounds.Left, mapObject.Bounds.Top);
var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
soldier.X = mapObjectGridPos.X;
soldier.Y = mapObjectGridPos.Y;
core.Squad.Add(soldier);
}
}
}
}
public void Draw(SpriteBatch spriteBatch) public void Draw(SpriteBatch spriteBatch)
{ {
foreach (var layer in Layers) foreach (var layer in Layers)
{ {
if (layer.Name == "Collision") var tileLayer = layer as MapTileLayer;
if (tileLayer == null)
continue; continue;
for (int y = 0; y < layer.Height; y++) // Don't draw the collision layer
if (tileLayer.Name == "Collision")
continue;
for (int y = 0; y < tileLayer.Height; y++)
{ {
for (int x = 0; x < layer.Width; x++) for (int x = 0; x < tileLayer.Width; x++)
{ {
Tile tile = layer.Tiles[y*layer.Width + x]; MapTile mapTile = tileLayer.Tiles[y * layer.Width + x];
if (tile != null) if (mapTile != null)
{ {
spriteBatch.Draw( spriteBatch.Draw(
tile.Texture, mapTile.Texture,
new Rectangle(x * TileWidth, y * TileHeight, TileWidth, TileHeight), new Rectangle(x * TileWidth, y * TileHeight, TileWidth, TileHeight),
tile.SourceRectangle, mapTile.SourceRectangle,
Color.White, Color.White,
0, 0,
Vector2.Zero, Vector2.Zero,
tile.SpriteEffects, mapTile.SpriteEffects,
0); 0);
} }
} }
+11 -5
View File
@@ -39,7 +39,7 @@ namespace DepthsBelow
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();
// Selection rectangle // Selection rectangle
@@ -55,18 +55,24 @@ namespace DepthsBelow
selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y; selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y;
} }
} }
// When the mouse is released
if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty) if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty)
{ {
// Deselect all units
if (!ks.IsKeyDown(Keys.LeftControl)) if (!ks.IsKeyDown(Keys.LeftControl))
{ {
core.soldier.Selected = false; foreach (var unit in core.Squad)
unit.Selected = false;
} }
if (selectionRectangle.Intersects(core.soldier.GetComponent<Component.Collision>().Rectangle)) // Select all units in the rectangle
foreach (var unit in core.Squad)
{ {
core.soldier.Selected = true; if (selectionRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
unit.Selected = true;
} }
// Hide the selection rectangle
selectionRectangle = Rectangle.Empty; selectionRectangle = Rectangle.Empty;
} }
+3 -1
View File
@@ -9,6 +9,7 @@ namespace DepthsBelow
public class Soldier : Entity public class Soldier : Entity
{ {
public static Texture2D Texture; public static Texture2D Texture;
public Color Color;
public static Point Origin; public static Point Origin;
private bool _selected; private bool _selected;
@@ -23,6 +24,7 @@ namespace DepthsBelow
gridTransform.Position = new Point(7, 3); gridTransform.Position = new Point(7, 3);
pixelTransform.Position = gridTransform.ToWorld(); pixelTransform.Position = gridTransform.ToWorld();
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);
@@ -40,7 +42,7 @@ namespace DepthsBelow
set set
{ {
_selected = value; _selected = value;
GetComponent<SpriteRenderer>().Color = (value) ? Color.Blue : Color.HotPink; GetComponent<SpriteRenderer>().Color = (value) ? Color.Blue : this.Color;
} }
} }
@@ -89,6 +89,7 @@
<Name>Cave.Level1</Name> <Name>Cave.Level1</Name>
<Processor>MapProcessor</Processor> <Processor>MapProcessor</Processor>
<Importer>TmxImporter</Importer> <Importer>TmxImporter</Importer>
<SubType>Designer</SubType>
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -214,4 +214,11 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data> </data>
</layer> </layer>
<objectgroup name="Object Layer 1" width="100" height="100">
<object name="Spawn1" type="SquadStart" gid="143" x="288" y="256"/>
<object name="Spawn2" type="SquadStart" gid="143" x="288" y="192"/>
<object name="Spawn4" type="SquadStart" gid="143" x="224" y="160"/>
<object name="Spawn3" type="SquadStart" gid="143" x="224" y="224"/>
<object name="Spawn5" type="SquadStart" gid="143" x="160" y="192"/>
</objectgroup>
</map> </map>
@@ -12,7 +12,7 @@ using TiledLib;
namespace DepthsBelowContentPipeline namespace DepthsBelowContentPipeline
{ {
// Each tile has a texture, source rect, and sprite effects. // Each tile has a texture, source rect, and sprite effects.
[ContentSerializerRuntimeType("DepthsBelow.Tile, DepthsBelow")] [ContentSerializerRuntimeType("DepthsBelow.MapTile, DepthsBelow")]
public class MapTileContent public class MapTileContent
{ {
public int LocalId; public int LocalId;
@@ -21,15 +21,34 @@ namespace DepthsBelowContentPipeline
public SpriteEffects SpriteEffects; public SpriteEffects SpriteEffects;
} }
[ContentSerializerRuntimeType("DepthsBelow.MapObject, DepthsBelow")]
public class MapObjectContent
{
//public MapObjectType ObjectType;
public string Name;
public string Type;
public Rectangle Bounds;
public List<Vector2> Points;
}
// For each layer, we store the size of the layer and the tiles. // For each layer, we store the size of the layer and the tiles.
[ContentSerializerRuntimeType("DepthsBelow.Layer, DepthsBelow")] [ContentSerializerRuntimeType("DepthsBelow.MapLayer, DepthsBelow")]
public class MapLayerContent public class MapLayerContent
{ {
public int Width; public int Width;
public int Height; public int Height;
public string Name; public string Name;
}
[ContentSerializerRuntimeType("DepthsBelow.MapTileLayer, DepthsBelow")]
public class MapTileLayerContent : MapLayerContent
{
public MapTileContent[] Tiles; public MapTileContent[] Tiles;
} }
[ContentSerializerRuntimeType("DepthsBelow.MapObjectLayer, DepthsBelow")]
public class MapObjectLayerContent : MapLayerContent
{
public MapObjectContent[] Objects;
}
// For the map itself, we just store the size, tile size, and a list of layers. // For the map itself, we just store the size, tile size, and a list of layers.
[ContentSerializerRuntimeType("DepthsBelow.Map, DepthsBelow")] [ContentSerializerRuntimeType("DepthsBelow.Map, DepthsBelow")]
@@ -45,8 +64,6 @@ namespace DepthsBelowContentPipeline
{ {
public override MapContent Process(TiledLib.MapContent input, ContentProcessorContext context) public override MapContent Process(TiledLib.MapContent input, ContentProcessorContext context)
{ {
//System.Diagnostics.Debugger.Launch();
// build the textures // build the textures
TiledHelpers.BuildTileSetTextures(input, context, "maps"); TiledHelpers.BuildTileSetTextures(input, context, "maps");
@@ -57,18 +74,49 @@ namespace DepthsBelowContentPipeline
MapContent output = new MapContent MapContent output = new MapContent
{ {
TileWidth = input.TileWidth, TileWidth = input.TileWidth,
TileHeight = input.TileHeight TileHeight = input.TileHeight,
}; };
// iterate all the layers of the input // iterate all the layers of the input
foreach (LayerContent layer in input.Layers) foreach (LayerContent layer in input.Layers)
{ {
// Get layer objects
var molc = layer as TiledLib.MapObjectLayerContent;
if (molc != null)
{
//System.Diagnostics.Debugger.Launch();
// create the new layer
MapObjectLayerContent outLayer = new MapObjectLayerContent
{
Width = molc.Width,
Height = molc.Height,
Name = molc.Name
};
outLayer.Objects = new MapObjectContent[molc.Objects.Count];
for (int i = 0; i < molc.Objects.Count; i++)
{
var layerObject = molc.Objects[i];
outLayer.Objects[i] = new MapObjectContent
{
//ObjectType = layerObject.ObjectType,
Name = layerObject.Name,
Type = layerObject.Type,
Bounds = layerObject.Bounds,
Points = layerObject.Points
};
}
output.Layers.Add(outLayer);
}
// we only care about tile layers in our demo // we only care about tile layers in our demo
TileLayerContent tlc = layer as TileLayerContent; TileLayerContent tlc = layer as TileLayerContent;
if (tlc != null) if (tlc != null)
{ {
// create the new layer // create the new layer
MapLayerContent outLayer = new MapLayerContent MapTileLayerContent outLayer = new MapTileLayerContent
{ {
Width = tlc.Width, Width = tlc.Width,
Height = tlc.Height, Height = tlc.Height,