Squad list. Squad unit spawns handled by map objects.

This commit is contained in:
Simon Holmberg
2012-09-29 00:32:35 +02:00
parent adfdf963d8
commit 11b2af46ff
8 changed files with 300 additions and 40 deletions
+24 -12
View File
@@ -19,9 +19,10 @@ namespace DepthsBelow
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Camera camera;
public Camera Camera;
MouseInput mouseInput;
public Soldier soldier;
//public Soldier soldier;
public List<Soldier> Squad;
private Map map;
public Core()
@@ -49,7 +50,9 @@ namespace DepthsBelow
protected override void Initialize()
{
// TODO: Add your initialization logic here
camera = new Camera(this);
Camera = new Camera(this);
Squad = new List<Soldier>();
base.Initialize();
}
@@ -63,10 +66,11 @@ namespace DepthsBelow
spriteBatch = new SpriteBatch(GraphicsDevice);
map = Content.Load<Map>("maps/Cave.Level1");
map.ParseObjects(this);
// 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();
@@ -92,11 +96,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);
}
@@ -109,17 +115,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<Component.SpriteRenderer>();
if (rc != null)
rc.Draw(spriteBatch);
// Draw mouse input visuals
foreach (var soldier in Squad)
{
var sr = soldier.GetComponent<Component.SpriteRenderer>();
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();
+19
View File
@@ -11,6 +11,25 @@ namespace DepthsBelow
List<Component.Component> 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)
+79 -13
View File
@@ -1,52 +1,118 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace DepthsBelow
{
public class Tile
public class MapTile
{
public Texture2D Texture;
public Rectangle SourceRectangle;
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 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<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)
{
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);
}
}
+11 -5
View File
@@ -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,16 +55,22 @@ 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<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;
}
+3 -1
View File
@@ -9,6 +9,7 @@ namespace DepthsBelow
public class Soldier : Entity
{
public static Texture2D Texture;
public Color Color;
public static Point Origin;
private bool _selected;
@@ -21,6 +22,7 @@ namespace DepthsBelow
pixelTransform.Origin = new Vector2(16, 16);
this.Color = Color.White;
var rc = new SpriteRenderer(this) {Texture = Texture, Color = Color.White};
AddComponent(rc);
@@ -34,7 +36,7 @@ namespace DepthsBelow
set
{
_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>
<Processor>MapProcessor</Processor>
<Importer>TmxImporter</Importer>
<SubType>Designer</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
File diff suppressed because one or more lines are too long
@@ -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 ExternalReference<Texture2DContent> Texture;
@@ -20,15 +20,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<Vector2> 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")]
@@ -44,8 +63,6 @@ namespace DepthsBelowContentPipeline
{
public override MapContent Process(TiledLib.MapContent input, ContentProcessorContext context)
{
//System.Diagnostics.Debugger.Launch();
// build the textures
TiledHelpers.BuildTileSetTextures(input, context, "maps");
@@ -56,18 +73,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,