diff --git a/DepthsBelow/DepthsBelow/Core.cs b/DepthsBelow/DepthsBelow/Core.cs old mode 100644 new mode 100755 index f4fddb8..257f514 --- a/DepthsBelow/DepthsBelow/Core.cs +++ b/DepthsBelow/DepthsBelow/Core.cs @@ -20,10 +20,11 @@ namespace DepthsBelow SpriteBatch spriteBatch; public static AStar.PathFinderFast PathFinder; + public Camera Camera; - public Camera camera; MouseInput mouseInput; - public Soldier soldier; + //public Soldier soldier; + public List Squad; private Map map; public Core() @@ -51,7 +52,9 @@ namespace DepthsBelow protected override void Initialize() { // TODO: Add your initialization logic here - camera = new Camera(this); + Camera = new Camera(this); + Squad = new List(); + base.Initialize(); } @@ -65,6 +68,8 @@ namespace DepthsBelow spriteBatch = new SpriteBatch(GraphicsDevice); map = Content.Load("maps/Cave.Level1"); + map.ParseObjects(this); + PathFinder = new AStar.PathFinderFast(map.GetCollisionMap()); PathFinder.Formula = AStar.HeuristicFormula.Manhattan; PathFinder.Diagonals = false; @@ -78,7 +83,7 @@ namespace DepthsBelow // TODO: use this.Content to load your game content here Soldier.LoadContent(this); - soldier = new Soldier(this); + //soldier = new Soldier(this); mouseInput = new MouseInput(this); mouseInput.LoadContent(); @@ -104,11 +109,13 @@ namespace DepthsBelow if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); - camera.Update(gameTime); + Camera.Update(gameTime); mouseInput.Update(gameTime); // TODO: Add your update logic here - soldier.Update(gameTime); + //soldier.Update(gameTime); + foreach (var soldier in Squad) + soldier.Update(gameTime); base.Update(gameTime); } @@ -121,17 +128,23 @@ namespace DepthsBelow { GraphicsDevice.Clear(Color.Black); - // Start drawing using the camera transform - spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, camera.Transform); + // Start drawing using the Camera transform + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, + Camera.Transform); // Draw the level map.Draw(spriteBatch); // Draw units // TODO: Foreach etc... - Component.SpriteRenderer rc = soldier.GetComponent(); - if (rc != null) - rc.Draw(spriteBatch); - // Draw mouse input visuals + foreach (var soldier in Squad) + { + var sr = soldier.GetComponent(); + Console.WriteLine("Drawing soldier at: " + soldier.pixelTransform.X + "," + soldier.pixelTransform.Y); + if (sr != null) + sr.Draw(spriteBatch); + } + + // Draw mouse input visuals mouseInput.Draw(spriteBatch); spriteBatch.End(); diff --git a/DepthsBelow/DepthsBelow/Entity.cs b/DepthsBelow/DepthsBelow/Entity.cs index 673958d..b35f26e 100644 --- a/DepthsBelow/DepthsBelow/Entity.cs +++ b/DepthsBelow/DepthsBelow/Entity.cs @@ -11,6 +11,25 @@ namespace DepthsBelow List Components; public Component.PixelTransform pixelTransform; 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; public Entity(Core core) diff --git a/DepthsBelow/DepthsBelow/Map.cs b/DepthsBelow/DepthsBelow/Map.cs index 3a99368..ca04e54 100755 --- a/DepthsBelow/DepthsBelow/Map.cs +++ b/DepthsBelow/DepthsBelow/Map.cs @@ -5,7 +5,7 @@ using Microsoft.Xna.Framework.Graphics; namespace DepthsBelow { - public class Tile + public class MapTile { public int LocalId; public Texture2D Texture; @@ -13,42 +13,107 @@ namespace DepthsBelow 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 Points; + } + + public class MapLayer { public int Width; public int Height; public string Name; - public Tile[] Tiles; + } + + public class MapTileLayer : MapLayer + { + public MapTile[] Tiles; + } + + public class MapObjectLayer : MapLayer + { + public MapObject[] Objects; } public class Map { public int TileWidth; public int TileHeight; - public List Layers = new List(); + public List Layers = new List(); + + 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) { foreach (var layer in Layers) { - if (layer.Name == "Collision") + var tileLayer = layer as MapTileLayer; + + if (tileLayer == null) 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]; - if (tile != null) + MapTile mapTile = tileLayer.Tiles[y * layer.Width + x]; + if (mapTile != null) { spriteBatch.Draw( - tile.Texture, + mapTile.Texture, new Rectangle(x * TileWidth, y * TileHeight, TileWidth, TileHeight), - tile.SourceRectangle, + mapTile.SourceRectangle, Color.White, 0, Vector2.Zero, - tile.SpriteEffects, + mapTile.SpriteEffects, 0); } } diff --git a/DepthsBelow/DepthsBelow/MouseInput.cs b/DepthsBelow/DepthsBelow/MouseInput.cs old mode 100644 new mode 100755 index 6dcb770..1aa25f2 --- a/DepthsBelow/DepthsBelow/MouseInput.cs +++ b/DepthsBelow/DepthsBelow/MouseInput.cs @@ -39,7 +39,7 @@ namespace DepthsBelow public void Update(GameTime gameTime) { 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(); // Selection rectangle @@ -55,18 +55,24 @@ namespace DepthsBelow selectionRectangle.Height = (int)mouseWorldPos.Y - selectionRectangle.Y; } } + // When the mouse is released if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty) { + // Deselect all units if (!ks.IsKeyDown(Keys.LeftControl)) { - core.soldier.Selected = false; + foreach (var unit in core.Squad) + unit.Selected = false; } - if (selectionRectangle.Intersects(core.soldier.GetComponent().Rectangle)) + // Select all units in the rectangle + foreach (var unit in core.Squad) { - core.soldier.Selected = true; + if (selectionRectangle.Intersects(unit.GetComponent().Rectangle)) + unit.Selected = true; } - + + // Hide the selection rectangle selectionRectangle = Rectangle.Empty; } diff --git a/DepthsBelow/DepthsBelow/Soldier.cs b/DepthsBelow/DepthsBelow/Soldier.cs index bc1f7ed..b3c2286 100644 --- a/DepthsBelow/DepthsBelow/Soldier.cs +++ b/DepthsBelow/DepthsBelow/Soldier.cs @@ -9,6 +9,7 @@ namespace DepthsBelow public class Soldier : Entity { public static Texture2D Texture; + public Color Color; public static Point Origin; private bool _selected; @@ -23,6 +24,7 @@ namespace DepthsBelow gridTransform.Position = new Point(7, 3); pixelTransform.Position = gridTransform.ToWorld(); + this.Color = Color.White; var rc = new SpriteRenderer(this) {Texture = Texture, Color = Color.White}; AddComponent(rc); @@ -40,7 +42,7 @@ namespace DepthsBelow set { _selected = value; - GetComponent().Color = (value) ? Color.Blue : Color.HotPink; + GetComponent().Color = (value) ? Color.Blue : this.Color; } } diff --git a/DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj b/DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj index dfd0e74..3a6f708 100644 --- a/DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj +++ b/DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj @@ -89,6 +89,7 @@ Cave.Level1 MapProcessor TmxImporter + Designer diff --git a/DepthsBelow/DepthsBelowContent/maps/Cave.Level1.tmx b/DepthsBelow/DepthsBelowContent/maps/Cave.Level1.tmx index d0dae50..b2db441 100755 --- a/DepthsBelow/DepthsBelowContent/maps/Cave.Level1.tmx +++ b/DepthsBelow/DepthsBelowContent/maps/Cave.Level1.tmx @@ -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 + + + + + + + diff --git a/DepthsBelow/DepthsBelowContentPipeline/MapProcessor.cs b/DepthsBelow/DepthsBelowContentPipeline/MapProcessor.cs index e24f0b4..880de60 100755 --- a/DepthsBelow/DepthsBelowContentPipeline/MapProcessor.cs +++ b/DepthsBelow/DepthsBelowContentPipeline/MapProcessor.cs @@ -12,7 +12,7 @@ using TiledLib; namespace DepthsBelowContentPipeline { // Each tile has a texture, source rect, and sprite effects. - [ContentSerializerRuntimeType("DepthsBelow.Tile, DepthsBelow")] + [ContentSerializerRuntimeType("DepthsBelow.MapTile, DepthsBelow")] public class MapTileContent { public int LocalId; @@ -21,15 +21,34 @@ namespace DepthsBelowContentPipeline public SpriteEffects SpriteEffects; } + [ContentSerializerRuntimeType("DepthsBelow.MapObject, DepthsBelow")] + public class MapObjectContent + { + //public MapObjectType ObjectType; + public string Name; + public string Type; + public Rectangle Bounds; + public List Points; + } + // 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 int Width; public int Height; public string Name; + } + [ContentSerializerRuntimeType("DepthsBelow.MapTileLayer, DepthsBelow")] + public class MapTileLayerContent : MapLayerContent + { 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. [ContentSerializerRuntimeType("DepthsBelow.Map, DepthsBelow")] @@ -45,8 +64,6 @@ namespace DepthsBelowContentPipeline { public override MapContent Process(TiledLib.MapContent input, ContentProcessorContext context) { - //System.Diagnostics.Debugger.Launch(); - // build the textures TiledHelpers.BuildTileSetTextures(input, context, "maps"); @@ -57,18 +74,49 @@ namespace DepthsBelowContentPipeline MapContent output = new MapContent { TileWidth = input.TileWidth, - TileHeight = input.TileHeight + TileHeight = input.TileHeight, }; // iterate all the layers of the input 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 TileLayerContent tlc = layer as TileLayerContent; + if (tlc != null) { // create the new layer - MapLayerContent outLayer = new MapLayerContent + MapTileLayerContent outLayer = new MapTileLayerContent { Width = tlc.Width, Height = tlc.Height,