global Content, automatic LoadContent for GameObject

This commit is contained in:
Ivan Filipenkov 2023-08-15 11:23:51 +03:00
parent f298d83793
commit a70367eb27
11 changed files with 107 additions and 94 deletions

View file

@ -23,9 +23,9 @@ public abstract class AbstractGui : IDrawableObject
CreateUI();
}
public virtual void LoadContent(ContentManager content)
public virtual void LoadContent()
{
Manager.LoadContent(content);
Manager.LoadContent(AppManager.Instance.Content);
}
public virtual void Update(GameTime gameTime)

View file

@ -13,9 +13,7 @@ internal class MenuGUI : AbstractGui
Elements.Add(but);
but.LeftButtonPressed += () =>
{
AppManager.AppManagerInstance.ChangeGameState(GameState.Game);
GameManager.mapManager.LoadLevel("");
AppManager.Instance.ChangeGameState(GameState.Game);
};
}
}

View file

@ -15,33 +15,39 @@ namespace DangerousD.GameCore
public int Width { get; protected set; }
public int Height { get; protected set; }
public Rectangle Rectangle => new Rectangle((int)Pos.X, (int)Pos.Y, Width, Height);
protected GraphicsComponent graphicsComponent;
protected abstract GraphicsComponent GraphicsComponent { get; }
public GameObject(Vector2 pos)
{
Pos = pos;
Width = 128;
Height = 128;
Width = 500;
Height = 101;
//Animator = new GraphicsComponent(new() { "playerIdle" });
GameManager.Register(this);
LoadContent();
AppManager.Instance.GameManager.Register(this);
}
public virtual void OnCollision()
{
}
public abstract void Initialize(GraphicsDevice graphicsDevice);
public virtual void LoadContent(ContentManager content)
public virtual void Initialize(GraphicsDevice graphicsDevice)
{
graphicsComponent.LoadContent();
}
public virtual void Update(GameTime gameTime) { graphicsComponent.Update(); }
public void LoadContent()
{
GraphicsComponent.LoadContent();
}
public virtual void Update(GameTime gameTime)
{
GraphicsComponent.Update();
}
public virtual void Draw(SpriteBatch spriteBatch)
{
graphicsComponent.DrawAnimation(Rectangle, spriteBatch);
GraphicsComponent.DrawAnimation(Rectangle, spriteBatch);
}
}
}

View file

@ -7,7 +7,7 @@ namespace DangerousD.GameCore.GUI
interface IDrawableObject
{
void Initialize(GraphicsDevice graphicsDevice);
void LoadContent(ContentManager content);
void LoadContent();
void Update(GameTime gameTime);
void Draw(SpriteBatch spriteBatch);
}

View file

@ -12,20 +12,17 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
{
public class Enemy1 : LivingEntity
{
protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> { "IDLE", "WALK" }, "IDLE");
public Enemy1(Vector2 position) : base(position)
{
}
public override void Initialize(GraphicsDevice graphicsDevice)
{
graphicsComponent = new GraphicsComponent(new List<string>() { "IDLE", "WALK" }, "IDLE");
}
public override void Update(GameTime gameTime)
{
if (graphicsComponent.GetCurrentAnimation!="WALK")
graphicsComponent.StartAnimation("WALK");
if (GraphicsComponent.GetCurrentAnimation!="WALK")
GraphicsComponent.StartAnimation("WALK");
base.Update(gameTime);
}

View file

@ -6,29 +6,18 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DangerousD.GameCore.Graphics;
namespace DangerousD.GameCore.GameObjects.MapObjects
{
internal class GrassBlock : MapObject
{
protected override GraphicsComponent GraphicsComponent { get; } = new("wall");
public GrassBlock(Vector2 position) : base(position)
{
Width = 32;
Height = 32;
}
public override void Initialize(GraphicsDevice graphicsDevice)
{
}
public override void LoadContent(ContentManager content)
{
graphicsComponent = new Graphics.GraphicsComponent(content.Load<Texture2D>("wall"));
base.LoadContent(content);
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
}
}
}

View file

@ -8,10 +8,10 @@ namespace DangerousD.GameCore.Graphics
{
public class AnimationBuilder
{
public List<AnimationContainer> animations;
public List<AnimationContainer> Animations { get; private set; }
void LoadAnimations(string nameOfMainFile)
{
animations = new List<AnimationContainer>();
Animations = new List<AnimationContainer>();
List<string> animationFilesNames = new List<string>();
StreamReader reader = new StreamReader(nameOfMainFile);
while (reader.Peek() != -1)
@ -24,7 +24,7 @@ namespace DangerousD.GameCore.Graphics
reader = new StreamReader(fileName);
string json = reader.ReadToEnd();
AnimationContainer animation = JsonConvert.DeserializeObject<AnimationContainer>(json);
animations.Add(animation);
Animations.Add(animation);
}
}
}

View file

@ -14,7 +14,12 @@ namespace DangerousD.GameCore.Graphics
private List<Texture2D> textures;
private List<string> texturesNames;
private AnimationContainer currentAnimation;
public string GetCurrentAnimation { get { return currentAnimation.Id; } }
public string GetCurrentAnimation
{
get { return currentAnimation.Id; }
}
private AnimationContainer neitralAnimation;
//private SpriteBatch _spriteBatch;
@ -22,11 +27,7 @@ namespace DangerousD.GameCore.Graphics
private int interval;
private int lastInterval;
private Rectangle sourceRectangle;
public static ContentManager contentManager;
public static void LoadGraphicsComponent(ContentManager _contentManager)
{
contentManager = _contentManager;
}
public GraphicsComponent(List<string> animationsId, string neitralAnimationId)
{
//this._spriteBatch = _spriteBatch;
@ -34,13 +35,13 @@ namespace DangerousD.GameCore.Graphics
lastInterval = 1;
LoadAnimations(animationsId, neitralAnimationId);
}
public GraphicsComponent(Texture2D texture)
public GraphicsComponent(string textureName)
{
animations = new List<AnimationContainer>();
textures = new List<Texture2D>();
var texture = AppManager.Instance.Content.Load<Texture2D>(textureName);
textures.Add(texture);
AnimationContainer animationContainer = new AnimationContainer();
animationContainer.StartSpriteRectangle = new Rectangle(0, 0, texture.Width, texture.Height);
@ -54,18 +55,20 @@ namespace DangerousD.GameCore.Graphics
neitralAnimation = animationContainer;
animations.Add(animationContainer);
}
private void LoadAnimations(List<string> animationsId, string neitralAnimationId)
{
animations = new List<AnimationContainer>();
foreach (var id in animationsId)
{
animations.Add( GameManager.builder.animations.Find(x => x.Id == id));
animations.Add(AppManager.Instance.AnimationBuilder.Animations.Find(x => x.Id == id));
if (id == neitralAnimationId)
{
neitralAnimation = animations.Last();
}
}
}
public void LoadContent()
{
textures = new List<Texture2D>();
@ -76,11 +79,11 @@ namespace DangerousD.GameCore.Graphics
if (!texturesNames.Contains(animation.TextureName))
{
texturesNames.Add(animation.TextureName);
textures.Add(contentManager.Load<Texture2D>(animation.TextureName));
textures.Add(AppManager.Instance.Content.Load<Texture2D>(animation.TextureName));
}
}
}
}
}
}
public void StartAnimation(string startedanimationId)
{
currentFrame = 0;
@ -89,6 +92,7 @@ namespace DangerousD.GameCore.Graphics
buildSourceRectangle();
SetInterval();
}
public void StopAnimation()
{
currentFrame = 0;
@ -96,8 +100,8 @@ namespace DangerousD.GameCore.Graphics
currentAnimation = neitralAnimation;
buildSourceRectangle();
SetInterval();
}
public void Update()
{
if (interval == 0)
@ -109,27 +113,33 @@ namespace DangerousD.GameCore.Graphics
{
currentAnimation = neitralAnimation;
}
currentFrame = 0;
}
buildSourceRectangle();
SetInterval();
}
interval--;
}
public void DrawAnimation(Rectangle destinationRectangle, SpriteBatch _spriteBatch)
{
_spriteBatch.Draw(textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)], destinationRectangle, sourceRectangle, Color.White);
_spriteBatch.Draw(textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)],
destinationRectangle, sourceRectangle, Color.White);
}
private void buildSourceRectangle()
{
sourceRectangle = new Rectangle();
sourceRectangle.X = currentAnimation.StartSpriteRectangle.X + currentFrame * (currentAnimation.StartSpriteRectangle.Width + currentAnimation.TextureFrameInterval);
sourceRectangle.X = currentAnimation.StartSpriteRectangle.X + currentFrame *
(currentAnimation.StartSpriteRectangle.Width + currentAnimation.TextureFrameInterval);
sourceRectangle.Y = currentAnimation.StartSpriteRectangle.Y;
sourceRectangle.Height = currentAnimation.StartSpriteRectangle.Height;
sourceRectangle.Width = currentAnimation.StartSpriteRectangle.Width;
}
private void SetInterval()
{
Tuple<int, int> i = currentAnimation.FrameTime.Find(x => x.Item1 == currentFrame);
@ -144,5 +154,4 @@ namespace DangerousD.GameCore.Graphics
}
}
}
}

View file

@ -13,37 +13,38 @@ namespace DangerousD.GameCore
public enum GameState { Menu, Options, Lobby, Game }
public class AppManager : Game
{
public static AppManager AppManagerInstance { get; private set; }
public static AppManager Instance { get; private set; }
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
GameState gameState;
IDrawableObject MenuGUI;
IDrawableObject OptionsGUI;
IDrawableObject LobbyGUI;
public GameManager GameManager { get; private set; }
public AnimationBuilder AnimationBuilder { get; private set; } = new AnimationBuilder();
public AppManager()
{
AppManagerInstance = this;
Instance = this;
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
TargetElapsedTime = TimeSpan.FromMilliseconds(1000 / 30);
GameManager = new GameManager();
gameState = GameState.Menu;
MenuGUI = new MenuGUI();
}
protected override void Initialize()
{
GameManager.Init();
MenuGUI.Initialize(GraphicsDevice);
base.Initialize();
}
protected override void LoadContent()
{
GraphicsComponent.LoadGraphicsComponent(Content);
_spriteBatch = new SpriteBatch(GraphicsDevice);
MenuGUI.LoadContent(Content);
MenuGUI.LoadContent();
}
protected override void Update(GameTime gameTime)
@ -102,6 +103,20 @@ namespace DangerousD.GameCore
public void ChangeGameState(GameState gameState)
{
this.gameState = gameState;
switch (this.gameState)
{
case GameState.Menu:
break;
case GameState.Options:
break;
case GameState.Lobby:
break;
case GameState.Game:
GameManager.mapManager.LoadLevel("");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}

View file

@ -9,23 +9,14 @@ using System.Text;
namespace DangerousD.GameCore
{
static class GameManager
public class GameManager
{
static List<LivingEntity> livingEntities;
static List<Entity> entities;
static List<MapObject> mapObjects;
public static AnimationBuilder builder;
public static MapManager mapManager;
internal static void Register(GameObject gameObject)
{
if (gameObject is LivingEntity)
livingEntities.Add(gameObject as LivingEntity);
if (gameObject is Entity)
entities.Add(gameObject as Entity);
if (gameObject is MapObject)
mapObjects.Add(gameObject as MapObject);
}
public static void Init()
List<LivingEntity> livingEntities;
List<Entity> entities;
List<MapObject> mapObjects;
public MapManager mapManager;
public GameManager()
{
livingEntities = new List<LivingEntity>();
mapObjects = new List<MapObject>();
@ -34,7 +25,17 @@ namespace DangerousD.GameCore
mapManager.Init();
}
public static void Draw(SpriteBatch _spriteBatch)
internal void Register(GameObject gameObject)
{
if (gameObject is LivingEntity)
livingEntities.Add(gameObject as LivingEntity);
if (gameObject is Entity)
entities.Add(gameObject as Entity);
if (gameObject is MapObject)
mapObjects.Add(gameObject as MapObject);
}
public void Draw(SpriteBatch _spriteBatch)
{
foreach (var item in mapObjects)
item.Draw(_spriteBatch);
@ -43,7 +44,8 @@ namespace DangerousD.GameCore
foreach (var item in livingEntities)
item.Draw(_spriteBatch);
}
public static void Update(GameTime gameTime)
public void Update(GameTime gameTime)
{
foreach (var item in mapObjects)
item.Update(gameTime);
@ -51,8 +53,6 @@ namespace DangerousD.GameCore
item.Update(gameTime);
foreach (var item in livingEntities)
item.Update(gameTime);
}
}
}

View file

@ -20,7 +20,6 @@ namespace DangerousD.GameCore.Managers
public void InitLevel()
{
var Трава = new GrassBlock(new Vector2(0,128));
Трава.LoadContent(GraphicsComponent.contentManager);
}
}
public class MapManager