BIG MapManager commit

This commit is contained in:
Mootfrost777 2023-08-16 23:06:13 +03:00
parent e7e18468f4
commit 368d08cb41
11 changed files with 63 additions and 41 deletions

View file

@ -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();
}

View file

@ -8,11 +8,7 @@ namespace DangerousD.GameCore.GameObjects
{
public abstract class Entity : GameObject
{
public Entity(Vector2 position) : base(position) {}
}
}

View file

@ -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()
{
}

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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();
}

View file

@ -28,7 +28,6 @@ namespace DangerousD.GameCore
players = new List<Player>();
mapManager = new MapManager();
physicsManager = new PhysicsManager();
mapManager.Init();
}
internal void Register(GameObject gameObject)

View file

@ -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<int> 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;
}}
}
}
}
}