From c5ac15987c70255d5382197e2b866cf324f52ac3 Mon Sep 17 00:00:00 2001 From: Mootfrost777 Date: Sat, 17 Aug 2024 10:04:53 +0300 Subject: [PATCH] Add Door, reverse play of animation --- ZoFo/GameCore/GameObjects/Entities/Entity.cs | 2 +- .../Entities/Interactables/Door.cs | 18 ++++++- .../Entities/Interactables/Interactable.cs | 2 +- ZoFo/GameCore/GameObjects/GameObject.cs | 10 ++-- .../GameObjects/MapObjects/MapObject.cs | 2 +- .../ZoFo_grafics/GraphicsComponent.cs | 50 +++++++++++++------ 6 files changed, 61 insertions(+), 23 deletions(-) diff --git a/ZoFo/GameCore/GameObjects/Entities/Entity.cs b/ZoFo/GameCore/GameObjects/Entities/Entity.cs index a9e8670..26eb6c0 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Entity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Entity.cs @@ -10,7 +10,7 @@ namespace ZoFo.GameCore.GameObjects.Entities { public abstract class Entity : GameObject { - public override GraphicsComponent graphicsComponent => null; + protected override GraphicsComponent graphicsComponent => null; public CollisionComponent collisionComponent { get; protected set; } public int Id { get; set; } protected Entity(Vector2 position) : base(position) diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs index 06bdec5..58d61ca 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs @@ -1,8 +1,24 @@ +using System.Collections.Generic; +using Microsoft.Xna.Framework; +using ZoFo.GameCore.ZoFo_graphics; + namespace ZoFo.GameCore.GameObjects.Entities.Interactables; -public class Door +public class Door : Interactable { public bool isOpened; + + protected override GraphicsComponent graphicsComponent { get; } = new(new List { "DoorInteraction" }, "DoorInteraction"); + + public Door(Vector2 position) : base(position) + { + graphicsComponent.actionOfAnimationEnd += _ => { isOpened = !isOpened; }; + } + + public override void OnInteraction() + { + graphicsComponent.StartAnimation("DoorInteraction", isOpened); + } } \ 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 6299155..9bf0fa5 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs @@ -19,7 +19,7 @@ public class Interactable : Entity AppManager.Instance.server.AddData(new UpdateInteractionReady((sender as Player).Id, isReady)); } - public void OnInteraction() + public virtual void OnInteraction() { } diff --git a/ZoFo/GameCore/GameObjects/GameObject.cs b/ZoFo/GameCore/GameObjects/GameObject.cs index 14c306f..6037843 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; } + protected abstract GraphicsComponent graphicsComponent { get; } #region ServerSide public GameObject(Vector2 position) @@ -84,10 +84,10 @@ public abstract class GameObject if (color is null) color = new Color(1, 0, 0, 0.25f); if (color.Value.A == 255) color = new Color(color.Value, 0.25f); //spriteBatch.Draw(debugTexture, - // new Rectangle((_rectangle.X - GraphicsComponent.CameraPosition.X) * GraphicsComponent.scaling, - // (_rectangle.Y - GraphicsComponent.CameraPosition.Y) * GraphicsComponent.scaling, - // _rectangle.Width * GraphicsComponent.scaling, - // _rectangle.Height * GraphicsComponent.scaling), color.Value); + // new Rectangle((_rectangle.X - graphicsComponent.CameraPosition.X) * graphicsComponent.scaling, + // (_rectangle.Y - graphicsComponent.CameraPosition.Y) * graphicsComponent.scaling, + // _rectangle.Width * graphicsComponent.scaling, + // _rectangle.Height * graphicsComponent.scaling), color.Value); //TODO: debugTexture } diff --git a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs index 5a5748c..b551483 100644 --- a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs +++ b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs @@ -15,7 +15,7 @@ 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(); + protected override GraphicsComponent graphicsComponent { get; } = new(); /// /// Создается простой объект на карте - no animations, только где, насколько крупно рисовать, по какой сорс ректанглу рисовать и из какой текстуры diff --git a/ZoFo/GameCore/ZoFo_grafics/GraphicsComponent.cs b/ZoFo/GameCore/ZoFo_grafics/GraphicsComponent.cs index f10b40a..34037ec 100644 --- a/ZoFo/GameCore/ZoFo_grafics/GraphicsComponent.cs +++ b/ZoFo/GameCore/ZoFo_grafics/GraphicsComponent.cs @@ -26,6 +26,7 @@ namespace ZoFo.GameCore.ZoFo_graphics private AnimationContainer currentAnimation; static public int scaling = 5; public int parentId; + public bool reverse; public AnimationContainer CurrentAnimation { get @@ -126,7 +127,7 @@ namespace ZoFo.GameCore.ZoFo_graphics } } - public void StartAnimation(string startedanimationId) + public void StartAnimation(string startedanimationId, bool reverse = false) { /* if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.SinglePlayer) @@ -138,8 +139,13 @@ namespace ZoFo.GameCore.ZoFo_graphics } } */ - currentFrame = 0; + this.reverse = reverse; + currentAnimation = animations.Find(x => x.Id == startedanimationId); + if (reverse) + currentFrame = currentAnimation.FramesCount; + else + currentFrame = 0; buildSourceRectangle(); SetInterval(); @@ -154,6 +160,21 @@ namespace ZoFo.GameCore.ZoFo_graphics SetInterval(); } + private void AnimationEnd() + { + if (!currentAnimation.IsCycle) + { + if (actionOfAnimationEnd != null) + { + actionOfAnimationEnd(currentAnimation.Id); + } + currentAnimation = neitralAnimation; + + } + + currentFrame = 0; + } + public void Update() { if (currentAnimation is null) @@ -162,21 +183,22 @@ namespace ZoFo.GameCore.ZoFo_graphics } if (interval == 0) { - currentFrame++; - if (currentAnimation.FramesCount <= currentFrame) + if (reverse) { - if (!currentAnimation.IsCycle) + currentFrame--; + if (currentFrame <= 0) { - if (actionOfAnimationEnd != null) - { - actionOfAnimationEnd(currentAnimation.Id); - } - currentAnimation = neitralAnimation; - + AnimationEnd(); + reverse = false; + } + } + else + { + currentFrame++; + if (currentAnimation.FramesCount <= currentFrame) + { + AnimationEnd(); } - - currentFrame = 0; - } buildSourceRectangle();