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(); 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) public virtual void Update(GameTime gameTime)

View file

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

View file

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

View file

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

View file

@ -6,29 +6,18 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using DangerousD.GameCore.Graphics;
namespace DangerousD.GameCore.GameObjects.MapObjects namespace DangerousD.GameCore.GameObjects.MapObjects
{ {
internal class GrassBlock : MapObject internal class GrassBlock : MapObject
{ {
protected override GraphicsComponent GraphicsComponent { get; } = new("wall");
public GrassBlock(Vector2 position) : base(position) public GrassBlock(Vector2 position) : base(position)
{ {
Width = 32; Width = 32;
Height = 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 class AnimationBuilder
{ {
public List<AnimationContainer> animations; public List<AnimationContainer> Animations { get; private set; }
void LoadAnimations(string nameOfMainFile) void LoadAnimations(string nameOfMainFile)
{ {
animations = new List<AnimationContainer>(); Animations = new List<AnimationContainer>();
List<string> animationFilesNames = new List<string>(); List<string> animationFilesNames = new List<string>();
StreamReader reader = new StreamReader(nameOfMainFile); StreamReader reader = new StreamReader(nameOfMainFile);
while (reader.Peek() != -1) while (reader.Peek() != -1)
@ -24,7 +24,7 @@ namespace DangerousD.GameCore.Graphics
reader = new StreamReader(fileName); reader = new StreamReader(fileName);
string json = reader.ReadToEnd(); string json = reader.ReadToEnd();
AnimationContainer animation = JsonConvert.DeserializeObject<AnimationContainer>(json); AnimationContainer animation = JsonConvert.DeserializeObject<AnimationContainer>(json);
animations.Add(animation); Animations.Add(animation);
} }
} }
} }

View file

@ -8,13 +8,18 @@ using System.Text;
namespace DangerousD.GameCore.Graphics namespace DangerousD.GameCore.Graphics
{ {
public class GraphicsComponent public class GraphicsComponent
{ {
private List<AnimationContainer> animations; private List<AnimationContainer> animations;
private List<Texture2D> textures; private List<Texture2D> textures;
private List<string> texturesNames; private List<string> texturesNames;
private AnimationContainer currentAnimation; private AnimationContainer currentAnimation;
public string GetCurrentAnimation { get { return currentAnimation.Id; } }
public string GetCurrentAnimation
{
get { return currentAnimation.Id; }
}
private AnimationContainer neitralAnimation; private AnimationContainer neitralAnimation;
//private SpriteBatch _spriteBatch; //private SpriteBatch _spriteBatch;
@ -22,50 +27,48 @@ namespace DangerousD.GameCore.Graphics
private int interval; private int interval;
private int lastInterval; private int lastInterval;
private Rectangle sourceRectangle; private Rectangle sourceRectangle;
public static ContentManager contentManager;
public static void LoadGraphicsComponent(ContentManager _contentManager)
{
contentManager = _contentManager;
}
public GraphicsComponent(List<string> animationsId, string neitralAnimationId) public GraphicsComponent(List<string> animationsId, string neitralAnimationId)
{ {
//this._spriteBatch = _spriteBatch; //this._spriteBatch = _spriteBatch;
currentFrame = 0; currentFrame = 0;
lastInterval = 1; lastInterval = 1;
LoadAnimations(animationsId,neitralAnimationId); LoadAnimations(animationsId, neitralAnimationId);
} }
public GraphicsComponent(Texture2D texture)
public GraphicsComponent(string textureName)
{ {
animations = new List<AnimationContainer>(); animations = new List<AnimationContainer>();
textures = new List<Texture2D>(); textures = new List<Texture2D>();
var texture = AppManager.Instance.Content.Load<Texture2D>(textureName);
textures.Add(texture); textures.Add(texture);
AnimationContainer animationContainer = new AnimationContainer(); AnimationContainer animationContainer = new AnimationContainer();
animationContainer.StartSpriteRectangle = new Rectangle(0, 0, texture.Width, texture.Height); animationContainer.StartSpriteRectangle = new Rectangle(0, 0, texture.Width, texture.Height);
animationContainer.TextureFrameInterval = 0; animationContainer.TextureFrameInterval = 0;
animationContainer.TextureName=texture.Name; animationContainer.TextureName = texture.Name;
animationContainer.IsCycle = true; animationContainer.IsCycle = true;
animationContainer.FramesCount = 1; animationContainer.FramesCount = 1;
animationContainer.FrameTime = new List<Tuple<int, int>>() { new Tuple<int, int>(0, 10) }; animationContainer.FrameTime = new List<Tuple<int, int>>() { new Tuple<int, int>(0, 10) };
animationContainer.Id = texture.Name; animationContainer.Id = texture.Name;
currentAnimation= animationContainer; currentAnimation = animationContainer;
neitralAnimation = animationContainer; neitralAnimation = animationContainer;
animations.Add(animationContainer); animations.Add(animationContainer);
} }
private void LoadAnimations(List<string> animationsId, string neitralAnimationId) private void LoadAnimations(List<string> animationsId, string neitralAnimationId)
{ {
animations = new List<AnimationContainer>(); animations = new List<AnimationContainer>();
foreach (var id in animationsId) 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) if (id == neitralAnimationId)
{ {
neitralAnimation = animations.Last(); neitralAnimation = animations.Last();
} }
} }
} }
public void LoadContent() public void LoadContent()
{ {
textures = new List<Texture2D>(); textures = new List<Texture2D>();
@ -76,11 +79,11 @@ namespace DangerousD.GameCore.Graphics
if (!texturesNames.Contains(animation.TextureName)) if (!texturesNames.Contains(animation.TextureName))
{ {
texturesNames.Add(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) public void StartAnimation(string startedanimationId)
{ {
currentFrame = 0; currentFrame = 0;
@ -89,6 +92,7 @@ namespace DangerousD.GameCore.Graphics
buildSourceRectangle(); buildSourceRectangle();
SetInterval(); SetInterval();
} }
public void StopAnimation() public void StopAnimation()
{ {
currentFrame = 0; currentFrame = 0;
@ -96,8 +100,8 @@ namespace DangerousD.GameCore.Graphics
currentAnimation = neitralAnimation; currentAnimation = neitralAnimation;
buildSourceRectangle(); buildSourceRectangle();
SetInterval(); SetInterval();
} }
public void Update() public void Update()
{ {
if (interval == 0) if (interval == 0)
@ -109,27 +113,33 @@ namespace DangerousD.GameCore.Graphics
{ {
currentAnimation = neitralAnimation; currentAnimation = neitralAnimation;
} }
currentFrame = 0; currentFrame = 0;
} }
buildSourceRectangle(); buildSourceRectangle();
SetInterval(); SetInterval();
} }
interval--; interval--;
} }
public void DrawAnimation(Rectangle destinationRectangle, SpriteBatch _spriteBatch)
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() private void buildSourceRectangle()
{ {
sourceRectangle = new Rectangle(); 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.Y = currentAnimation.StartSpriteRectangle.Y;
sourceRectangle.Height = currentAnimation.StartSpriteRectangle.Height; sourceRectangle.Height = currentAnimation.StartSpriteRectangle.Height;
sourceRectangle.Width = currentAnimation.StartSpriteRectangle.Width; sourceRectangle.Width = currentAnimation.StartSpriteRectangle.Width;
} }
private void SetInterval() private void SetInterval()
{ {
Tuple<int, int> i = currentAnimation.FrameTime.Find(x => x.Item1 == currentFrame); 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 enum GameState { Menu, Options, Lobby, Game }
public class AppManager : Game public class AppManager : Game
{ {
public static AppManager AppManagerInstance { get; private set; } public static AppManager Instance { get; private set; }
private GraphicsDeviceManager _graphics; private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch; private SpriteBatch _spriteBatch;
GameState gameState; GameState gameState;
IDrawableObject MenuGUI; IDrawableObject MenuGUI;
IDrawableObject OptionsGUI; IDrawableObject OptionsGUI;
IDrawableObject LobbyGUI; IDrawableObject LobbyGUI;
public GameManager GameManager { get; private set; }
public AnimationBuilder AnimationBuilder { get; private set; } = new AnimationBuilder();
public AppManager() public AppManager()
{ {
AppManagerInstance = this; Instance = this;
_graphics = new GraphicsDeviceManager(this); _graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content"; Content.RootDirectory = "Content";
IsMouseVisible = true; IsMouseVisible = true;
TargetElapsedTime = TimeSpan.FromMilliseconds(1000 / 30); TargetElapsedTime = TimeSpan.FromMilliseconds(1000 / 30);
GameManager = new GameManager();
gameState = GameState.Menu; gameState = GameState.Menu;
MenuGUI = new MenuGUI(); MenuGUI = new MenuGUI();
} }
protected override void Initialize() protected override void Initialize()
{ {
GameManager.Init();
MenuGUI.Initialize(GraphicsDevice); MenuGUI.Initialize(GraphicsDevice);
base.Initialize(); base.Initialize();
} }
protected override void LoadContent() protected override void LoadContent()
{ {
GraphicsComponent.LoadGraphicsComponent(Content);
_spriteBatch = new SpriteBatch(GraphicsDevice); _spriteBatch = new SpriteBatch(GraphicsDevice);
MenuGUI.LoadContent(Content); MenuGUI.LoadContent();
} }
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)
@ -102,6 +103,20 @@ namespace DangerousD.GameCore
public void ChangeGameState(GameState gameState) public void ChangeGameState(GameState gameState)
{ {
this.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,14 +9,23 @@ using System.Text;
namespace DangerousD.GameCore namespace DangerousD.GameCore
{ {
static class GameManager public class GameManager
{ {
static List<LivingEntity> livingEntities; List<LivingEntity> livingEntities;
static List<Entity> entities; List<Entity> entities;
static List<MapObject> mapObjects; List<MapObject> mapObjects;
public static AnimationBuilder builder; public MapManager mapManager;
public static MapManager mapManager;
internal static void Register(GameObject gameObject) public GameManager()
{
livingEntities = new List<LivingEntity>();
mapObjects = new List<MapObject>();
entities = new List<Entity>();
mapManager = new MapManager();
mapManager.Init();
}
internal void Register(GameObject gameObject)
{ {
if (gameObject is LivingEntity) if (gameObject is LivingEntity)
livingEntities.Add(gameObject as LivingEntity); livingEntities.Add(gameObject as LivingEntity);
@ -25,16 +34,8 @@ namespace DangerousD.GameCore
if (gameObject is MapObject) if (gameObject is MapObject)
mapObjects.Add(gameObject as MapObject); mapObjects.Add(gameObject as MapObject);
} }
public static void Init()
{
livingEntities = new List<LivingEntity>();
mapObjects = new List<MapObject>();
entities = new List<Entity>();
mapManager =new MapManager();
mapManager.Init();
}
public static void Draw(SpriteBatch _spriteBatch) public void Draw(SpriteBatch _spriteBatch)
{ {
foreach (var item in mapObjects) foreach (var item in mapObjects)
item.Draw(_spriteBatch); item.Draw(_spriteBatch);
@ -43,7 +44,8 @@ namespace DangerousD.GameCore
foreach (var item in livingEntities) foreach (var item in livingEntities)
item.Draw(_spriteBatch); item.Draw(_spriteBatch);
} }
public static void Update(GameTime gameTime)
public void Update(GameTime gameTime)
{ {
foreach (var item in mapObjects) foreach (var item in mapObjects)
item.Update(gameTime); item.Update(gameTime);
@ -51,8 +53,6 @@ namespace DangerousD.GameCore
item.Update(gameTime); item.Update(gameTime);
foreach (var item in livingEntities) foreach (var item in livingEntities)
item.Update(gameTime); item.Update(gameTime);
} }
} }
} }

View file

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