From a70367eb270eeffe29b1311c7dce5690f47d83d3 Mon Sep 17 00:00:00 2001 From: Ivan Filipenkov Date: Tue, 15 Aug 2023 11:23:51 +0300 Subject: [PATCH] global Content, automatic LoadContent for GameObject --- DangerousD/GameCore/GUI/AbstractGui.cs | 4 +- DangerousD/GameCore/GUI/MenuGUI.cs | 4 +- DangerousD/GameCore/GameObjects/GameObject.cs | 28 +++++---- .../GameCore/GameObjects/IDrawableObject.cs | 2 +- .../GameObjects/LivingEntities/Enemy1.cs | 13 ++-- .../GameObjects/MapObjects/GrassBlock.cs | 17 +----- .../GameCore/Graphics/AnimationBuilder.cs | 6 +- .../GameCore/Graphics/GraphicsComponent.cs | 61 +++++++++++-------- DangerousD/GameCore/Managers/AppManager.cs | 25 ++++++-- DangerousD/GameCore/Managers/GameManager.cs | 40 ++++++------ DangerousD/GameCore/Managers/MapManager.cs | 1 - 11 files changed, 107 insertions(+), 94 deletions(-) diff --git a/DangerousD/GameCore/GUI/AbstractGui.cs b/DangerousD/GameCore/GUI/AbstractGui.cs index 802981e..755f1cc 100644 --- a/DangerousD/GameCore/GUI/AbstractGui.cs +++ b/DangerousD/GameCore/GUI/AbstractGui.cs @@ -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) diff --git a/DangerousD/GameCore/GUI/MenuGUI.cs b/DangerousD/GameCore/GUI/MenuGUI.cs index 376ccc5..2103562 100644 --- a/DangerousD/GameCore/GUI/MenuGUI.cs +++ b/DangerousD/GameCore/GUI/MenuGUI.cs @@ -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); }; } } \ No newline at end of file diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index 54ee8c6..83bc3f8 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -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); } } -} +} \ No newline at end of file diff --git a/DangerousD/GameCore/GameObjects/IDrawableObject.cs b/DangerousD/GameCore/GameObjects/IDrawableObject.cs index 4c25e3f..61df454 100644 --- a/DangerousD/GameCore/GameObjects/IDrawableObject.cs +++ b/DangerousD/GameCore/GameObjects/IDrawableObject.cs @@ -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); } diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Enemy1.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Enemy1.cs index 9e46a52..ccebca0 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Enemy1.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Enemy1.cs @@ -12,20 +12,17 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities { public class Enemy1 : LivingEntity { + + protected override GraphicsComponent GraphicsComponent { get; } = new(new List { "IDLE", "WALK" }, "IDLE"); + public Enemy1(Vector2 position) : base(position) { } - public override void Initialize(GraphicsDevice graphicsDevice) - { - graphicsComponent = new GraphicsComponent(new List() { "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); } diff --git a/DangerousD/GameCore/GameObjects/MapObjects/GrassBlock.cs b/DangerousD/GameCore/GameObjects/MapObjects/GrassBlock.cs index 801ba54..3ada6f5 100644 --- a/DangerousD/GameCore/GameObjects/MapObjects/GrassBlock.cs +++ b/DangerousD/GameCore/GameObjects/MapObjects/GrassBlock.cs @@ -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("wall")); - base.LoadContent(content); - } - - public override void Draw(SpriteBatch spriteBatch) - { - base.Draw(spriteBatch); - } } } diff --git a/DangerousD/GameCore/Graphics/AnimationBuilder.cs b/DangerousD/GameCore/Graphics/AnimationBuilder.cs index 9b68161..9a64339 100644 --- a/DangerousD/GameCore/Graphics/AnimationBuilder.cs +++ b/DangerousD/GameCore/Graphics/AnimationBuilder.cs @@ -8,10 +8,10 @@ namespace DangerousD.GameCore.Graphics { public class AnimationBuilder { - public List animations; + public List Animations { get; private set; } void LoadAnimations(string nameOfMainFile) { - animations = new List(); + Animations = new List(); List animationFilesNames = new List(); 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(json); - animations.Add(animation); + Animations.Add(animation); } } } diff --git a/DangerousD/GameCore/Graphics/GraphicsComponent.cs b/DangerousD/GameCore/Graphics/GraphicsComponent.cs index e5cf1cf..fad85b5 100644 --- a/DangerousD/GameCore/Graphics/GraphicsComponent.cs +++ b/DangerousD/GameCore/Graphics/GraphicsComponent.cs @@ -8,64 +8,67 @@ using System.Text; namespace DangerousD.GameCore.Graphics { - public class GraphicsComponent + public class GraphicsComponent { private List animations; private List textures; private List texturesNames; private AnimationContainer currentAnimation; - public string GetCurrentAnimation { get { return currentAnimation.Id; } } + + public string GetCurrentAnimation + { + get { return currentAnimation.Id; } + } + private AnimationContainer neitralAnimation; //private SpriteBatch _spriteBatch; - + private int currentFrame; private int interval; private int lastInterval; private Rectangle sourceRectangle; - public static ContentManager contentManager; - public static void LoadGraphicsComponent(ContentManager _contentManager) - { - contentManager = _contentManager; - } + public GraphicsComponent(List animationsId, string neitralAnimationId) { //this._spriteBatch = _spriteBatch; currentFrame = 0; lastInterval = 1; - - LoadAnimations(animationsId,neitralAnimationId); - + LoadAnimations(animationsId, neitralAnimationId); } - public GraphicsComponent(Texture2D texture) + + public GraphicsComponent(string textureName) { animations = new List(); textures = new List(); + var texture = AppManager.Instance.Content.Load(textureName); textures.Add(texture); AnimationContainer animationContainer = new AnimationContainer(); animationContainer.StartSpriteRectangle = new Rectangle(0, 0, texture.Width, texture.Height); animationContainer.TextureFrameInterval = 0; - animationContainer.TextureName=texture.Name; + animationContainer.TextureName = texture.Name; animationContainer.IsCycle = true; animationContainer.FramesCount = 1; animationContainer.FrameTime = new List>() { new Tuple(0, 10) }; animationContainer.Id = texture.Name; - currentAnimation= animationContainer; + currentAnimation = animationContainer; neitralAnimation = animationContainer; animations.Add(animationContainer); } + private void LoadAnimations(List animationsId, string neitralAnimationId) { animations = new List(); foreach (var id in animationsId) { - animations.Add( GameManager.builder.animations.Find(x => x.Id == id)); - if (id==neitralAnimationId) + animations.Add(AppManager.Instance.AnimationBuilder.Animations.Find(x => x.Id == id)); + if (id == neitralAnimationId) { neitralAnimation = animations.Last(); } } } + public void LoadContent() { textures = new List(); @@ -76,19 +79,20 @@ namespace DangerousD.GameCore.Graphics if (!texturesNames.Contains(animation.TextureName)) { texturesNames.Add(animation.TextureName); - textures.Add(contentManager.Load(animation.TextureName)); - + textures.Add(AppManager.Instance.Content.Load(animation.TextureName)); } } } + public void StartAnimation(string startedanimationId) { currentFrame = 0; currentAnimation = animations.Find(x => x.Id == startedanimationId); - + 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) + + 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 i = currentAnimation.FrameTime.Find(x => x.Item1 == currentFrame); @@ -144,5 +154,4 @@ namespace DangerousD.GameCore.Graphics } } } - -} +} \ No newline at end of file diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index d0873a2..68cd6c6 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -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(); + } } } diff --git a/DangerousD/GameCore/Managers/GameManager.cs b/DangerousD/GameCore/Managers/GameManager.cs index 7c36c57..25021cd 100644 --- a/DangerousD/GameCore/Managers/GameManager.cs +++ b/DangerousD/GameCore/Managers/GameManager.cs @@ -9,14 +9,23 @@ using System.Text; namespace DangerousD.GameCore { - static class GameManager + public class GameManager { - static List livingEntities; - static List entities; - static List mapObjects; - public static AnimationBuilder builder; - public static MapManager mapManager; - internal static void Register(GameObject gameObject) + List livingEntities; + List entities; + List mapObjects; + public MapManager mapManager; + + public GameManager() + { + livingEntities = new List(); + mapObjects = new List(); + entities = new List(); + mapManager = new MapManager(); + mapManager.Init(); + } + + internal void Register(GameObject gameObject) { if (gameObject is LivingEntity) livingEntities.Add(gameObject as LivingEntity); @@ -25,16 +34,8 @@ namespace DangerousD.GameCore if (gameObject is MapObject) mapObjects.Add(gameObject as MapObject); } - public static void Init() - { - livingEntities = new List(); - mapObjects = new List(); - entities = new List(); - mapManager =new MapManager(); - mapManager.Init(); - } - public static void Draw(SpriteBatch _spriteBatch) + 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); - - } } -} +} \ No newline at end of file diff --git a/DangerousD/GameCore/Managers/MapManager.cs b/DangerousD/GameCore/Managers/MapManager.cs index 4c5f736..6b85e53 100644 --- a/DangerousD/GameCore/Managers/MapManager.cs +++ b/DangerousD/GameCore/Managers/MapManager.cs @@ -20,7 +20,6 @@ namespace DangerousD.GameCore.Managers public void InitLevel() { var Трава = new GrassBlock(new Vector2(0,128)); - Трава.LoadContent(GraphicsComponent.contentManager); } } public class MapManager