diff --git a/DangerousD/GameCore/GUI/AbstractGui.cs b/DangerousD/GameCore/GUI/AbstractGui.cs index 24af331..7f7b212 100644 --- a/DangerousD/GameCore/GUI/AbstractGui.cs +++ b/DangerousD/GameCore/GUI/AbstractGui.cs @@ -17,9 +17,9 @@ public abstract class AbstractGui : IDrawableObject protected abstract void CreateUI(); private GraphicsDevice graphicsDevice; - public virtual void Initialize(GraphicsDevice graphicsDevice) + public virtual void Initialize() { - Manager.Initialize(graphicsDevice); + Manager.Initialize(AppManager.Instance.GraphicsDevice); this.graphicsDevice = graphicsDevice; CreateUI(); } diff --git a/DangerousD/GameCore/GameObjects/Entity.cs b/DangerousD/GameCore/GameObjects/Entity.cs index cf2caa9..9388d31 100644 --- a/DangerousD/GameCore/GameObjects/Entity.cs +++ b/DangerousD/GameCore/GameObjects/Entity.cs @@ -7,12 +7,8 @@ using Microsoft.Xna.Framework.Graphics; namespace DangerousD.GameCore.GameObjects { public abstract class Entity : GameObject - { - - + { public Entity(Vector2 position) : base(position) {} - - } } diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index 078a850..47ae72e 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -22,10 +22,12 @@ namespace DangerousD.GameCore protected abstract GraphicsComponent GraphicsComponent { get; } public GameObject(Vector2 pos) { + Initialize(); _pos = pos; Width = 500; Height = 101; //Animator = new GraphicsComponent(new() { "playerIdle" }); + LoadContent(); AppManager.Instance.GameManager.Register(this); } @@ -34,7 +36,7 @@ namespace DangerousD.GameCore { } - public virtual void Initialize(GraphicsDevice graphicsDevice) + public virtual void Initialize() { } diff --git a/DangerousD/GameCore/GameObjects/IDrawableObject.cs b/DangerousD/GameCore/GameObjects/IDrawableObject.cs index f157dcb..1d9ca5c 100644 --- a/DangerousD/GameCore/GameObjects/IDrawableObject.cs +++ b/DangerousD/GameCore/GameObjects/IDrawableObject.cs @@ -6,7 +6,7 @@ namespace DangerousD.GameCore.GUI { interface IDrawableObject { - void Initialize(GraphicsDevice graphicsDevice); + void Initialize(); void LoadContent(); void Update(GameTime gameTime); void Draw(SpriteBatch spriteBatch); diff --git a/DangerousD/GameCore/GameObjects/MapObject.cs b/DangerousD/GameCore/GameObjects/MapObject.cs index d4180d0..c12598b 100644 --- a/DangerousD/GameCore/GameObjects/MapObject.cs +++ b/DangerousD/GameCore/GameObjects/MapObject.cs @@ -1,13 +1,29 @@ -using Microsoft.Xna.Framework; +using System.Collections.Generic; +using Microsoft.Xna.Framework; using System.Security.Cryptography.X509Certificates; +using DangerousD.GameCore.Graphics; +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.Sprites; namespace DangerousD.GameCore.GameObjects; public abstract class MapObject : GameObject { public bool IsColliderOn; - public MapObject(Vector2 position) : base(position) + private Rectangle _sourceRectangle; + protected override GraphicsComponent GraphicsComponent { get; } = new("map"); + public MapObject(Vector2 position, Rectangle sourceRectangle) : base(position) + { + _sourceRectangle = sourceRectangle; + } + + public override void Initialize() { } + + public void Draw(SpriteBatch spriteBatch) + { + GraphicsComponent.DrawAnimation(Rectangle, spriteBatch, _sourceRectangle); + } } \ No newline at end of file diff --git a/DangerousD/GameCore/GameObjects/MapObjects/Platform.cs b/DangerousD/GameCore/GameObjects/MapObjects/Platform.cs index 2739422..999a009 100644 --- a/DangerousD/GameCore/GameObjects/MapObjects/Platform.cs +++ b/DangerousD/GameCore/GameObjects/MapObjects/Platform.cs @@ -5,7 +5,7 @@ namespace DangerousD.GameCore.GameObjects.MapObjects; public class Platform : MapObject { - public Platform(Vector2 position, string texture) : base(position) + public Platform(Vector2 position, Rectangle sourceRectangle) : base(position, sourceRectangle) { IsColliderOn = true; } diff --git a/DangerousD/GameCore/GameObjects/MapObjects/StopTile.cs b/DangerousD/GameCore/GameObjects/MapObjects/StopTile.cs index 9032ed3..e73eaaf 100644 --- a/DangerousD/GameCore/GameObjects/MapObjects/StopTile.cs +++ b/DangerousD/GameCore/GameObjects/MapObjects/StopTile.cs @@ -5,7 +5,7 @@ namespace DangerousD.GameCore.GameObjects.MapObjects; public class StopTile : MapObject { - public StopTile(Vector2 position) : base(position) + public StopTile(Vector2 position, Rectangle sourceRectangle) : base(position, sourceRectangle) { IsColliderOn = true; } diff --git a/DangerousD/GameCore/GameObjects/MapObjects/Tile.cs b/DangerousD/GameCore/GameObjects/MapObjects/Tile.cs index 640dc9b..5776344 100644 --- a/DangerousD/GameCore/GameObjects/MapObjects/Tile.cs +++ b/DangerousD/GameCore/GameObjects/MapObjects/Tile.cs @@ -5,7 +5,7 @@ namespace DangerousD.GameCore.GameObjects.MapObjects; public class Tile : MapObject { - public Tile(Vector2 position) : base(position) + public Tile(Vector2 position, Rectangle sourceRectangle) : base(position, sourceRectangle) { IsColliderOn = false; } diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index f769129..2891dbf 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -45,9 +45,9 @@ namespace DangerousD.GameCore protected override void Initialize() { AnimationBuilder.LoadAnimations(); - MenuGUI.Initialize(GraphicsDevice); - LoginGUI.Initialize(GraphicsDevice); - LobbyGUI.Initialize(GraphicsDevice); + MenuGUI.Initialize(); + LoginGUI.Initialize(); + LobbyGUI.Initialize(); base.Initialize(); } diff --git a/DangerousD/GameCore/Managers/GameManager.cs b/DangerousD/GameCore/Managers/GameManager.cs index f017120..a6840d6 100644 --- a/DangerousD/GameCore/Managers/GameManager.cs +++ b/DangerousD/GameCore/Managers/GameManager.cs @@ -28,7 +28,6 @@ namespace DangerousD.GameCore players = new List(); mapManager = new MapManager(); physicsManager = new PhysicsManager(); - mapManager.Init(); } internal void Register(GameObject gameObject) diff --git a/DangerousD/GameCore/Managers/MapManager.cs b/DangerousD/GameCore/Managers/MapManager.cs index 711f12e..c566cf9 100644 --- a/DangerousD/GameCore/Managers/MapManager.cs +++ b/DangerousD/GameCore/Managers/MapManager.cs @@ -1,51 +1,60 @@ -using DangerousD.GameCore.GameObjects; -using DangerousD.GameCore.Graphics; -using DangerousD.GameCore.Levels; -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Net.Security; -using System.Text; -using System.Threading.Tasks; using System.Xml; using DangerousD.GameCore.GameObjects.MapObjects; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; - namespace DangerousD.GameCore.Managers { public class MapManager { - private Texture2D _texture; - public void Init() - { - } //Level public void LoadLevel(string level) { XmlDocument xml = new(); xml.Load($"{level}.tmx"); - XmlNode mapNode = xml.DocumentElement[].SelectSingleNode("//layer[@type='collidable']"); Vector2 tileSize = new(int.Parse(xml.DocumentElement.Attributes["tilewidth"].Value), int.Parse(xml.DocumentElement.Attributes["tileheight"].Value)); - foreach (XmlNode chunk in mapNode.ChildNodes) + XmlNodeList layers = xml.DocumentElement.SelectNodes("//layer"); + + foreach (XmlNode layer in layers) { - Vector2 chunkSize = new(int.Parse(chunk.Attributes["width"].Value), int.Parse(chunk.Attributes["height"].Value)) + InstantiateTiles(layer, tileSize); + } + } + + private void InstantiateTiles(XmlNode layer, Vector2 tileSize) + { + string tileType = layer.Attributes["class"].Value; + + foreach (XmlNode chunk in layer.ChildNodes) + { + Vector2 chunkSize = new(int.Parse(chunk.Attributes["width"].Value), + int.Parse(chunk.Attributes["height"].Value)); Vector2 chunkPos = new(int.Parse(chunk.Attributes["x"].Value), int.Parse(chunk.Attributes["y"].Value)); List tiles = chunk.Value.Split(',').Select(int.Parse).ToList(); for (int i = 0; i < tiles.Count; i++) { - new StopTile(chunk) - } - } - } + Vector2 pos = new((chunkPos.Y + i % chunkSize.X) * tileSize.Y, + (chunkPos.Y + i / chunkSize.X) * tileSize.Y); + Rectangle sourceRect = new(pos.ToPoint(), tileSize.ToPoint()); - private void CreateTiles(T d) - { - + switch (tileType) + { + case "collidable": + new StopTile(pos, sourceRect); + break; + case "platform": + new Platform(pos, sourceRect); + break; + case "non_collidable": + new Tile(pos, sourceRect); + break; + }} + } } } }