diff --git a/.run/ZoFo.run.xml b/.run/ZoFo.run.xml new file mode 100644 index 0000000..315a66b --- /dev/null +++ b/.run/ZoFo.run.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file diff --git a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs index 54d71fd..57cc891 100644 --- a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs +++ b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs @@ -12,7 +12,9 @@ namespace ZoFo.GameCore.GameObjects.Entities { //public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "тут пишите название анимации" }, "сдублируйте " + - public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "player_idle_top-right_noweapon" }, "player_idle_top-right_noweapon"); + + public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_idle_rotate_weapon" }, "player_idle_rotate_weapon"); + public EntittyForAnimationTests(Vector2 position) : base(position) { graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,16*12, 16 * 16); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Ammo.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Ammo.cs index fe3fb67..ba4f1bc 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Ammo.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Ammo.cs @@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { class Ammo:Collectable { - public override GraphicsComponent graphicsComponent { get; } = new("Textures/icons/8"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("Textures/icons/8"); public Ammo(Vector2 position) : base(position) { graphicsComponent.ObjectDrawRectangle.Width = 20; diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Antiradine.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Antiradine.cs index e575538..01f36a9 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Antiradine.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Antiradine.cs @@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { class Antiradine:Collectable { - public override GraphicsComponent graphicsComponent { get; } = new(new List { "Antiradine" }, "Antiradine"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("Antiradine"); public Antiradine(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/BottleOfWater.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/BottleOfWater.cs index 4b51206..528f614 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/BottleOfWater.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/BottleOfWater.cs @@ -14,7 +14,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { public class BottleOfWater : Collectable { - public override GraphicsComponent graphicsComponent { get; } = new(new List { "BottleOfWater" }, "BottleOfWater"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("BottleOfWater"); public BottleOfWater(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Peeble.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Peeble.cs index 8623b76..5525cfd 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Peeble.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Peeble.cs @@ -14,7 +14,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { public class Peeble:Collectable { - public override GraphicsComponent graphicsComponent { get; } = new(new List { "Peeble" }, "Peeble"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("Peeble"); public Peeble(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/PureBottleOfWater.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/PureBottleOfWater.cs index 2a55323..1523f6e 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/PureBottleOfWater.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/PureBottleOfWater.cs @@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { class PureBottleOfWater:Collectable { - public override GraphicsComponent graphicsComponent { get; } = new(new List { "PureBottleOfWater" }, "PureBottleOfWater"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("PureBottleOfWater"); public PureBottleOfWater(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/RottenFlesh.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/RottenFlesh.cs index 5649f5a..147f30d 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/RottenFlesh.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/RottenFlesh.cs @@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { class RottenFlesh:Collectable { - public override GraphicsComponent graphicsComponent { get; } = new(new List { "RottenFlesh" }, "RottenFlesh"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("RottenFlesh"); public RottenFlesh(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Steel.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Steel.cs index 41d73ed..5618236 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Steel.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Steel.cs @@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables { class Steel:Collectable { - public override GraphicsComponent graphicsComponent { get; } = new(new List { "Steel" }, "Steel"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("Steel"); public Steel(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs index e79ba5c..85af71b 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs @@ -9,7 +9,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables; public class Wood : Collectable { - public override GraphicsComponent graphicsComponent { get; } = new(new List { "Wood" }, "Wood"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("Wood"); public Wood(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs index 06e7e8b..23ce44f 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs @@ -9,16 +9,16 @@ public class Door : Interactable { public bool isOpened; - public override GraphicsComponent graphicsComponent { get; } = new(new List { "DoorInteraction" }, "DoorInteraction"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("DoorInteraction"); public Door(Vector2 position) : base(position) { - graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };//, - SD + //graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };//���������, ��� ����� ������ ������������� - SD } public override void OnInteraction(object sender, CollisionComponent e) { - graphicsComponent.AnimationSelect("DoorInteraction", isOpened); - graphicsComponent.AnimationStep(); + //graphicsComponent.AnimationSelect("DoorInteraction", isOpened); + //graphicsComponent.AnimationStep(); } } \ No newline at end of file diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs index 2701d49..270b986 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs @@ -9,7 +9,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables; public class Interactable : Entity { - public override GraphicsComponent graphicsComponent => throw new System.NotImplementedException(); + public override StaticGraphicsComponent graphicsComponent => throw new System.NotImplementedException(); public Interactable(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index 883ce9e..3e21338 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -1,8 +1,8 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; using System; using System.Collections.Generic; +using Microsoft.Xna.Framework.Input; using ZoFo.GameCore.GameManagers; using ZoFo.GameCore.GameManagers.CollisionManager; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer; @@ -21,15 +21,14 @@ public class Player : LivingEntity public bool IsTryingToShoot { get; set; } private float speed; private int health; + public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); private LootData lootData; - public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); public Player(Vector2 position) : base(position) { //InputWeaponRotation = new Vector2(0, 0); //InputPlayerRotation = new Vector2(0, 0); - + graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 100, 100); collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100); - graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,100,100); } diff --git a/ZoFo/GameCore/GameObjects/GameObject.cs b/ZoFo/GameCore/GameObjects/GameObject.cs index 7a4eb43..85189ea 100644 --- a/ZoFo/GameCore/GameObjects/GameObject.cs +++ b/ZoFo/GameCore/GameObjects/GameObject.cs @@ -13,7 +13,7 @@ public abstract class GameObject public Vector2 position; public Vector2 rotation; //вектор направления объекта - public abstract GraphicsComponent graphicsComponent { get; } + public virtual GraphicsComponent graphicsComponent { get; } #region ServerSide public GameObject(Vector2 position) @@ -81,7 +81,7 @@ public abstract class GameObject /// public virtual void Draw(SpriteBatch spriteBatch) { - graphicsComponent.DrawAnimation(graphicsComponent.ObjectDrawRectangle, spriteBatch); + graphicsComponent.Draw(graphicsComponent.ObjectDrawRectangle, spriteBatch); //debug DrawDebugRectangle(spriteBatch, graphicsComponent.ObjectDrawRectangle); diff --git a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs index 1c2b5b3..e55976d 100644 --- a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs +++ b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs @@ -16,7 +16,8 @@ namespace ZoFo.GameCore.GameObjects.MapObjects { public virtual bool IsColliderOn { get; protected set; } = true;//Who added that? public Rectangle sourceRectangle; - public override GraphicsComponent graphicsComponent { get; } = new(); + public override GraphicsComponent graphicsComponent { get; } + = new StaticGraphicsComponent(); /// /// Создается простой объект на карте - no animations, только где, насколько крупно рисовать, по какой сорс ректанглу рисовать и из какой текстуры @@ -27,17 +28,16 @@ namespace ZoFo.GameCore.GameObjects.MapObjects /// public MapObject(Vector2 position, Vector2 size, Rectangle sourceRectangle, string textureName) : base(position) { + (graphicsComponent as StaticGraphicsComponent)._textureName = textureName; + (graphicsComponent as StaticGraphicsComponent).BuildComponent(textureName); + (graphicsComponent as StaticGraphicsComponent).ObjectDrawRectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y); + (graphicsComponent as StaticGraphicsComponent).LoadContent(); this.sourceRectangle = sourceRectangle; - graphicsComponent.ObjectDrawRectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y); - graphicsComponent.BuildComponent(textureName); - graphicsComponent.LoadContent(); } public override void Draw(SpriteBatch spriteBatch) { - graphicsComponent.DrawAnimation(graphicsComponent.ObjectDrawRectangle, spriteBatch, sourceRectangle); - - + graphicsComponent.Draw(graphicsComponent.ObjectDrawRectangle, spriteBatch, sourceRectangle); } } diff --git a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs new file mode 100644 index 0000000..d590462 --- /dev/null +++ b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs @@ -0,0 +1,247 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using ZoFo.GameCore.GameManagers; +using ZoFo.GameCore.GUI; + +namespace ZoFo.GameCore.Graphics +{ + + public class AnimatedGraphicsComponent : GraphicsComponent + { + + public event Action actionOfAnimationEnd; + public List animations; + private List textures; + private List texturesNames; + private AnimationContainer currentAnimation; + static public int Camera_XW=800; + static public int Camera_YH = 400; + static public Vector2 CameraSize = new Vector2(1800, 960); + public int parentId; + public AnimationContainer CurrentAnimation + { + get + { + return currentAnimation; + } + } + public string LastAnimation { get; set; } + public string GetCurrentAnimation + { + get { return currentAnimation.Id; } + } + + private AnimationContainer neitralAnimation; + //private SpriteBatch _spriteBatch; + + private int currentFrame; + public int CurrentFrame + { + get + { + return currentFrame; + } + } + + // Needed to ckeck whether the frame has changed since last update call + private int lastUpdateCallFrame; + public int LastUpdateCallFrame + { + get + { + return lastUpdateCallFrame; + } + } + public int CurrentFrameInterval { get => interval; } + public void Force_Set_CurrentFrameInterval(int newFrameInterval) { } + private int interval; + private int lastInterval; + private Rectangle sourceRectangle; + public AnimatedGraphicsComponent(List animationsId, string neitralAnimationId) + { + //this._spriteBatch = _spriteBatch; + currentFrame = 0; + lastInterval = 1; + LoadAnimations(animationsId, neitralAnimationId); + currentAnimation = neitralAnimation; + SetInterval(); + buildSourceRectangle(); + } + + public AnimatedGraphicsComponent(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.IsCycle = true; + animationContainer.FramesCount = 1; + animationContainer.FrameTime = new List>() { new Tuple(0, 10) }; + animationContainer.Id = texture.Name; + currentAnimation = animationContainer; + neitralAnimation = animationContainer; + animations.Add(animationContainer); + } + + private void LoadAnimations(List animationsId, string neitralAnimationId) + { + animations = new List(); + foreach (var id in animationsId) + { + animations.Add(AppManager.Instance.animationBuilder.Animations.Find(x => x.Id == id)); + if (id == neitralAnimationId) + { + neitralAnimation = animations.Last(); + } + } + } + + public override void LoadContent() + { + textures = new List(); + texturesNames = new List(); + + foreach (var animation in animations) + { + if (!texturesNames.Contains(animation.TextureName)) + { + texturesNames.Add(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; + interval = 0; + currentAnimation = neitralAnimation; + buildSourceRectangle(); + SetInterval(); + } + + public override void Update() + { + lastUpdateCallFrame = currentFrame; + if (interval == 0) + { + currentFrame++; + if (currentAnimation.FramesCount <= currentFrame) + { + if (!currentAnimation.IsCycle) + { + if (actionOfAnimationEnd != null) + { + actionOfAnimationEnd(currentAnimation.Id); + } + currentAnimation = neitralAnimation; + + } + + currentFrame = 0; + + } + + buildSourceRectangle(); + SetInterval(); + } + + interval--; + } + + public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) + { + Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; + float scale; + if (currentAnimation.Offset.X != 0) + { + destinationRectangle.X -= (int)currentAnimation.Offset.X; + scale = destinationRectangle.Height / sourceRectangle.Height; + destinationRectangle.Width = (int)(sourceRectangle.Width * scale); + + } + else if (currentAnimation.Offset.Y != 0) + { + destinationRectangle.Y -= (int)currentAnimation.Offset.Y; + scale = destinationRectangle.Width / sourceRectangle.Width; + destinationRectangle.Height = (int)(sourceRectangle.Height * scale); + } + + destinationRectangle.X -= CameraPosition.X; + destinationRectangle.Y -= CameraPosition.Y; + + destinationRectangle = Scaling(destinationRectangle); + _spriteBatch.Draw(texture, + destinationRectangle, sourceRectangle, Color.White); + } + public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) + { + Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; + float scale; + if (currentAnimation.Offset.X != 0) + { + destinationRectangle.X -= (int)currentAnimation.Offset.X; + scale = destinationRectangle.Height / sourceRectangle.Height; + destinationRectangle.Width = (int)(sourceRectangle.Width * scale); + + } + else if (currentAnimation.Offset.Y != 0) + { + destinationRectangle.Y -= (int)currentAnimation.Offset.Y; + scale = destinationRectangle.Width / sourceRectangle.Width; + destinationRectangle.Height = (int)(sourceRectangle.Height * scale); + } + + destinationRectangle.X -= CameraPosition.X; + destinationRectangle.Y -= CameraPosition.Y; + + destinationRectangle = Scaling(destinationRectangle); + _spriteBatch.Draw(texture, + destinationRectangle, sourceRectangle, Color.White); + } + private void buildSourceRectangle() + { + sourceRectangle = new Rectangle(); + if (currentAnimation == null) + { + currentAnimation = neitralAnimation; + } + 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); + if (i != null) + { + interval = i.Item2; + lastInterval = interval; + } + else + { + interval = lastInterval; + } + } + + } +} diff --git a/ZoFo/GameCore/Graphics/AnimationComponent.cs b/ZoFo/GameCore/Graphics/AnimationComponent.cs deleted file mode 100644 index 084b912..0000000 --- a/ZoFo/GameCore/Graphics/AnimationComponent.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace ZoFo.GameCore.Graphics; - -public class AnimationComponent -{ - -} \ No newline at end of file diff --git a/ZoFo/GameCore/Graphics/GraphicsComponent.cs b/ZoFo/GameCore/Graphics/GraphicsComponent.cs index e8f9f17..c71b0c3 100644 --- a/ZoFo/GameCore/Graphics/GraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/GraphicsComponent.cs @@ -1,318 +1,56 @@ -using System; -using System.Collections.Generic; -using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using ZoFo.GameCore.GameManagers; -namespace ZoFo.GameCore.Graphics +namespace ZoFo.GameCore.Graphics; + +public abstract class GraphicsComponent { + public Rectangle ObjectDrawRectangle; + public static int scaling = 1; + public string mainTextureName;//TODO костыль - пофиксить - public class GraphicsComponent + public abstract void LoadContent(); + public abstract void Update(); + public abstract void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch); + public abstract void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle); + + protected Rectangle Scaling(Rectangle destinationRectangle) { - public Rectangle ObjectDrawRectangle; - - - - public event Action OnAnimationEnd; - private List animations; - private List textures; - public List texturesNames; //rethink public and following that errors - private AnimationContainer currentAnimation; - public static int scaling = 1; - - public bool animating = true; - private int step = 1; - - public AnimationContainer CurrentAnimation - { - get - { - return currentAnimation; - } - } - public string LastAnimation { get; set; } - public string GetCurrentAnimation - { - get { return currentAnimation.Id; } - } - - private AnimationContainer idleAnimation; - //private SpriteBatch _spriteBatch; - - private int currentFrame; - public int CurrentFrame - { - get - { - return currentFrame; - } - } - private int interval; - private int lastInterval; - private Rectangle sourceRectangle; - public GraphicsComponent(List animationsId, string neitralAnimationId) - { - //this._spriteBatch = _spriteBatch; - currentFrame = 0; - lastInterval = 1; - LoadAnimations(animationsId, neitralAnimationId); - currentAnimation = idleAnimation; - SetInterval(); - buildSourceRectangle(); - } - - public string mainTextureName;//TODO костыль - пофиксить - public GraphicsComponent(string textureName) - { - BuildComponent(textureName); - } - public GraphicsComponent() - { - } - public void BuildComponent(string textureName) - { - mainTextureName = textureName; - //texturesNames.Add(textureName);//Added by SD - 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.IsCycle = true; - animationContainer.FramesCount = 1; - animationContainer.FrameTime = new List>() { new Tuple(0, 10) }; - animationContainer.Id = texture.Name; - currentAnimation = animationContainer; - idleAnimation = animationContainer; - animations.Add(animationContainer); - } - - private void LoadAnimations(List animationsId, string neitralAnimationId) - { - animations = new List(); - foreach (var id in animationsId) - { - animations.Add(AppManager.Instance.animationBuilder.Animations.Find(x => x.Id == id)); - if (id == neitralAnimationId) - { - idleAnimation = animations.Last(); - } - } - } - - public void LoadContent() - { - textures = new List(); - texturesNames = new List(); - - if (animations is null) - { - return; - } - - foreach (var animation in animations) - { - if (!texturesNames.Contains(animation.TextureName)) - { - texturesNames.Add(animation.TextureName); - textures.Add(AppManager.Instance.Content.Load(animation.TextureName)); - } - } - } - - public void AnimationSelect(string animationId, bool reverse = false) - { - currentAnimation = animations.Find(x => x.Id == animationId); - if (reverse) - { - currentFrame = currentAnimation.FramesCount; - step = -1; - } - else - { - step = 1; - currentFrame = 1; - } - buildSourceRectangle(); - SetInterval(); - } - - public void StartAnimation() - { - animating = true; - } - - public void AnimationStep() - { - currentFrame += step; - } - - public void SetFrame(int frame) - { - currentFrame = frame; - } - - public void StopAnimation() - { - currentFrame = 0; - interval = 0; - currentAnimation = idleAnimation; - buildSourceRectangle(); - SetInterval(); - } - - private void AnimationEnd() - { - if (!currentAnimation.IsCycle) - { - if (OnAnimationEnd != null) - { - OnAnimationEnd(currentAnimation.Id); - } - currentAnimation = idleAnimation; - animating = false; - } - currentFrame = 0; - } - - public void Update() - { - if (currentAnimation.FramesCount <= currentFrame || currentFrame < 0) - { - AnimationEnd(); - } - - if (!animating) - return; - - if (interval == 0) - { - currentFrame += step; - buildSourceRectangle(); - SetInterval(); - } - - interval--; - } - - public void DrawAnimation(Rectangle destinationRectangle, SpriteBatch _spriteBatch) - { - Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; - float scale; - if (currentAnimation.Offset.X != 0) - { - destinationRectangle.X -= (int)currentAnimation.Offset.X; - scale = destinationRectangle.Height / sourceRectangle.Height; - destinationRectangle.Width = (int)(sourceRectangle.Width * scale); - - } - else if (currentAnimation.Offset.Y != 0) - { - destinationRectangle.Y -= (int)currentAnimation.Offset.Y; - scale = destinationRectangle.Width / sourceRectangle.Width; - destinationRectangle.Height = (int)(sourceRectangle.Height * scale); - } - - destinationRectangle.X -= CameraPosition.X; - destinationRectangle.Y -= CameraPosition.Y; - - destinationRectangle = Scaling(destinationRectangle); - _spriteBatch.Draw(texture, - destinationRectangle, sourceRectangle, Color.White); - } - public void DrawAnimation(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) - { - Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; - float scale; - if (currentAnimation.Offset.X != 0) - { - destinationRectangle.X -= (int)currentAnimation.Offset.X; - scale = destinationRectangle.Height / sourceRectangle.Height; - destinationRectangle.Width = (int)(sourceRectangle.Width * scale); - - } - else if (currentAnimation.Offset.Y != 0) - { - destinationRectangle.Y -= (int)currentAnimation.Offset.Y; - scale = destinationRectangle.Width / sourceRectangle.Width; - destinationRectangle.Height = (int)(sourceRectangle.Height * scale); - } - - destinationRectangle.X -= CameraPosition.X; - destinationRectangle.Y -= CameraPosition.Y; - - destinationRectangle = Scaling(destinationRectangle); - _spriteBatch.Draw(texture, - destinationRectangle, sourceRectangle, Color.White); - } - private Rectangle Scaling(Rectangle destinationRectangle) - { - destinationRectangle.X *= scaling; - destinationRectangle.Y *= scaling; - destinationRectangle.Width *= scaling; - destinationRectangle.Height *= scaling; - return destinationRectangle; - } - private void buildSourceRectangle() - { - sourceRectangle = new Rectangle(); - if (currentAnimation == null) - { - currentAnimation = idleAnimation; - } - 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); - if (i != null) - { - interval = i.Item2; - lastInterval = interval; - } - else - { - interval = lastInterval; - } - } - public static void SetCameraPosition(Vector2 playerPosition) - { - CameraPosition = (playerPosition).ToPoint(); - CameraPosition.X -= 200; - CameraPosition.Y -= 120; - - // TODO - /* - if (CameraPosition.X > AppManager.Instance.GameManager.CameraBorder.Y - 460) - { - CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.Y - 460; - } - - if (CameraPosition.Y < AppManager.Instance.GameManager.CameraBorder.Z) - { - CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.Z; - } - if (CameraPosition.X < AppManager.Instance.GameManager.CameraBorder.X) - { - CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.X; - } - if (CameraPosition.Y > AppManager.Instance.GameManager.CameraBorder.W - 240) - { - CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.W - 240; - } - - AppManager.Instance.DebugHUD.Set("CameraPosition", $"{CameraPosition.X}, {CameraPosition.Y}"); - */ - } - public static Point CameraPosition = new Point(0, 0); + destinationRectangle.X *= scaling; + destinationRectangle.Y *= scaling; + destinationRectangle.Width *= scaling; + destinationRectangle.Height *= scaling; + return destinationRectangle; } -} + + public static void SetCameraPosition(Vector2 playerPosition) + { + CameraPosition = (playerPosition).ToPoint(); + CameraPosition.X -= 200; + CameraPosition.Y -= 120; + + // TODO + /* + if (CameraPosition.X > AppManager.Instance.GameManager.CameraBorder.Y - 460) + { + CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.Y - 460; + } + + if (CameraPosition.Y < AppManager.Instance.GameManager.CameraBorder.Z) + { + CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.Z; + } + if (CameraPosition.X < AppManager.Instance.GameManager.CameraBorder.X) + { + CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.X; + } + if (CameraPosition.Y > AppManager.Instance.GameManager.CameraBorder.W - 240) + { + CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.W - 240; + } + + AppManager.Instance.DebugHUD.Set("CameraPosition", $"{CameraPosition.X}, {CameraPosition.Y}"); + */ + } + public static Point CameraPosition = new Point(0, 0); +} \ No newline at end of file diff --git a/ZoFo/GameCore/Graphics/IGraphicsComponent.cs b/ZoFo/GameCore/Graphics/IGraphicsComponent.cs new file mode 100644 index 0000000..50e2424 --- /dev/null +++ b/ZoFo/GameCore/Graphics/IGraphicsComponent.cs @@ -0,0 +1,16 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace ZoFo.GameCore.Graphics; + +public interface IGraphicsComponent +{ + public Rectangle ObjectDrawRectangle { get; set; } + public static int scaling = 1; + public string mainTextureName { get; set; }//TODO костыль - пофиксить + + public abstract void LoadContent(); + public abstract void Update(); + public abstract void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch); + public abstract void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle); +} \ No newline at end of file diff --git a/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs b/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs new file mode 100644 index 0000000..3e2fd6c --- /dev/null +++ b/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using ZoFo.GameCore.GameManagers; +using ZoFo.GameCore.GUI; + +namespace ZoFo.GameCore.Graphics +{ + + public class StaticGraphicsComponent : GraphicsComponent + { + private Texture2D texture; + public string _textureName; + + public StaticGraphicsComponent() + { + LoadContent(); + } + + public StaticGraphicsComponent(string textureName) + { + BuildComponent(textureName); + LoadContent(); + } + + public void BuildComponent(string textureName) + { + _textureName = textureName; + } + + + public override void LoadContent() + { + if (_textureName is null) + { + return; + } + + texture = AppManager.Instance.Content.Load(_textureName); + } + + public override void Update() + { + + } + + public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) + { + DebugHUD.Instance.Log("draw "); + + destinationRectangle.X -= CameraPosition.X; + destinationRectangle.Y -= CameraPosition.Y; + destinationRectangle = Scaling(destinationRectangle); + _spriteBatch.Draw(texture, destinationRectangle, Color.White); + } + + public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) + { + DebugHUD.Instance.Log("draw "); + + destinationRectangle.X -= CameraPosition.X; + destinationRectangle.Y -= CameraPosition.Y; + + destinationRectangle = Scaling(destinationRectangle); + _spriteBatch.Draw(texture, + destinationRectangle, sourceRectangle, Color.White); + } + } +} diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index d0272db..2cf8625 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -20,6 +20,7 @@ using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies; using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; using ZoFo.GameCore.GameObjects.MapObjects; using ZoFo.GameCore.GameObjects.MapObjects.StopObjects; +using ZoFo.GameCore.Graphics; namespace ZoFo.GameCore { @@ -124,7 +125,7 @@ namespace ZoFo.GameCore entities = new List(); new MapManager().LoadMap(); - AppManager.Instance.server.RegisterGameObject(new EntittyForAnimationTests(new Vector2(40, 40))); + AppManager.Instance.server.RegisterGameObject(new EntittyForAnimationTests(new Vector2(0, 0))); AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(740, 140))); AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(140, 440))); AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(240, 440))); @@ -175,7 +176,7 @@ namespace ZoFo.GameCore sourceRectangle = (gameObject as StopObject).sourceRectangle, Size = (gameObject as StopObject).graphicsComponent.ObjectDrawRectangle.Size, collisions = (gameObject as StopObject).collisionComponents.Select(x=>x.stopRectangle).ToArray(), - tileSetName = (gameObject as StopObject).graphicsComponent.mainTextureName + tileSetName = ((gameObject as StopObject).graphicsComponent as StaticGraphicsComponent)._textureName });//TODO foreach (var item in (gameObject as StopObject).collisionComponents) { @@ -191,7 +192,7 @@ namespace ZoFo.GameCore Position = (gameObject as MapObject).position, sourceRectangle = (gameObject as MapObject).sourceRectangle, Size = (gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size, - tileSetName = (gameObject as MapObject).graphicsComponent.mainTextureName + tileSetName = ((gameObject as MapObject).graphicsComponent as StaticGraphicsComponent)._textureName });//TODO return; }