Split GraphicsComponent.cs to animated and static
This commit is contained in:
parent
89978f0fc3
commit
fd3c8305f2
11 changed files with 432 additions and 323 deletions
|
@ -12,7 +12,7 @@ namespace ZoFo.GameCore.GameObjects.Entities
|
|||
{
|
||||
|
||||
//public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List<string> { "тут пишите название анимации" }, "сдублируйте " +
|
||||
public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List<string> { "player_running_top_rotate" }, "player_running_top_rotate");
|
||||
public override AnimatedGraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "player_running_top_rotate" }, "player_running_top_rotate");
|
||||
public EntittyForAnimationTests(Vector2 position) : base(position)
|
||||
{
|
||||
graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,16*12, 16 * 16);
|
||||
|
|
|
@ -6,7 +6,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables;
|
|||
|
||||
public class Wood : Collectable
|
||||
{
|
||||
public override GraphicsComponent graphicsComponent { get; } = new(new List<string> { "Wood" }, "Wood");
|
||||
public override StaticGraphicsComponent graphicsComponent { get; } = new("Wood");
|
||||
|
||||
public Wood(Vector2 position) : base(position)
|
||||
{
|
||||
|
|
|
@ -9,16 +9,16 @@ public class Door : Interactable
|
|||
{
|
||||
public bool isOpened;
|
||||
|
||||
public override GraphicsComponent graphicsComponent { get; } = new(new List<string> { "DoorInteraction" }, "DoorInteraction");
|
||||
public override StaticGraphicsComponent graphicsComponent { get; } = new("DoorClosed");
|
||||
|
||||
public Door(Vector2 position) : base(position)
|
||||
{
|
||||
graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };
|
||||
//graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };
|
||||
}
|
||||
|
||||
public override void OnInteraction(object sender, CollisionComponent e)
|
||||
{
|
||||
graphicsComponent.AnimationSelect("DoorInteraction", isOpened);
|
||||
graphicsComponent.AnimationStep();
|
||||
//graphicsComponent.AnimationSelect("DoorInteraction", isOpened);
|
||||
//graphicsComponent.AnimationStep();
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -21,7 +21,7 @@ public class LivingEntity : Entity
|
|||
inputManager = new InputManager();
|
||||
}
|
||||
|
||||
public override GraphicsComponent graphicsComponent { get; } = null;
|
||||
public override AnimatedGraphicsComponent graphicsComponent { get; } = null;
|
||||
|
||||
#region Server side
|
||||
/*public override void Update()
|
||||
|
|
|
@ -20,7 +20,7 @@ public class Player : LivingEntity
|
|||
public bool IsTryingToShoot { get; set; }
|
||||
private float speed;
|
||||
private int health;
|
||||
public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List<string> { "player_running_top_rotate" }, "player_running_top_rotate");
|
||||
public override AnimatedGraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "player_running_top_rotate" }, "player_running_top_rotate");
|
||||
public Player(Vector2 position) : base(position)
|
||||
{
|
||||
//InputWeaponRotation = new Vector2(0, 0);
|
||||
|
|
|
@ -80,7 +80,7 @@ public abstract class GameObject
|
|||
/// </summary>
|
||||
public virtual void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
graphicsComponent.DrawAnimation(graphicsComponent.ObjectDrawRectangle, spriteBatch);
|
||||
graphicsComponent.Draw(graphicsComponent.ObjectDrawRectangle, spriteBatch);
|
||||
//debug
|
||||
if (AppManager.Instance.InputManager.CollisionsCheat)
|
||||
DrawDebugRectangle(spriteBatch, graphicsComponent.ObjectDrawRectangle);
|
||||
|
|
|
@ -16,7 +16,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();
|
||||
public override StaticGraphicsComponent graphicsComponent { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Создается простой объект на карте - no animations, только где, насколько крупно рисовать, по какой сорс ректанглу рисовать и из какой текстуры
|
||||
|
@ -35,7 +35,7 @@ namespace ZoFo.GameCore.GameObjects.MapObjects
|
|||
}
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
graphicsComponent.DrawAnimation(graphicsComponent.ObjectDrawRectangle, spriteBatch, sourceRectangle);
|
||||
graphicsComponent.Draw(graphicsComponent.ObjectDrawRectangle, spriteBatch, sourceRectangle);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
310
ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs
Normal file
310
ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs
Normal file
|
@ -0,0 +1,310 @@
|
|||
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
|
||||
{
|
||||
|
||||
public class AnimatedGraphicsComponent : GraphicsComponent
|
||||
{
|
||||
public Rectangle ObjectDrawRectangle;
|
||||
|
||||
|
||||
|
||||
public event Action<string> OnAnimationEnd;
|
||||
private List<AnimationContainer> animations;
|
||||
private List<Texture2D> textures;
|
||||
public List<string> texturesNames; //rethink public and following that errors
|
||||
private AnimationContainer currentAnimation;
|
||||
|
||||
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 AnimatedGraphicsComponent(List<string> animationsId, string neitralAnimationId)
|
||||
{
|
||||
//this._spriteBatch = _spriteBatch;
|
||||
currentFrame = 0;
|
||||
lastInterval = 1;
|
||||
LoadAnimations(animationsId, neitralAnimationId);
|
||||
currentAnimation = idleAnimation;
|
||||
SetInterval();
|
||||
buildSourceRectangle();
|
||||
}
|
||||
|
||||
|
||||
public AnimatedGraphicsComponent(string textureName)
|
||||
{
|
||||
BuildComponent(textureName);
|
||||
}
|
||||
public AnimatedGraphicsComponent()
|
||||
{
|
||||
}
|
||||
public void BuildComponent(string textureName)
|
||||
{
|
||||
mainTextureName = textureName;
|
||||
//texturesNames.Add(textureName);//Added by SD
|
||||
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);
|
||||
animationContainer.TextureFrameInterval = 0;
|
||||
animationContainer.TextureName = texture.Name;
|
||||
animationContainer.IsCycle = true;
|
||||
animationContainer.FramesCount = 1;
|
||||
animationContainer.FrameTime = new List<Tuple<int, int>>() { new Tuple<int, int>(0, 10) };
|
||||
animationContainer.Id = texture.Name;
|
||||
currentAnimation = animationContainer;
|
||||
idleAnimation = animationContainer;
|
||||
animations.Add(animationContainer);
|
||||
}
|
||||
|
||||
private void LoadAnimations(List<string> animationsId, string neitralAnimationId)
|
||||
{
|
||||
animations = new List<AnimationContainer>();
|
||||
foreach (var id in animationsId)
|
||||
{
|
||||
animations.Add(AppManager.Instance.animationBuilder.Animations.Find(x => x.Id == id));
|
||||
if (id == neitralAnimationId)
|
||||
{
|
||||
idleAnimation = animations.Last();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void LoadContent()
|
||||
{
|
||||
textures = new List<Texture2D>();
|
||||
texturesNames = new List<string>();
|
||||
|
||||
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<Texture2D>(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 override void Update()
|
||||
{
|
||||
if (currentAnimation.FramesCount <= currentFrame || currentFrame < 0)
|
||||
{
|
||||
AnimationEnd();
|
||||
}
|
||||
|
||||
if (!animating)
|
||||
return;
|
||||
|
||||
if (interval == 0)
|
||||
{
|
||||
currentFrame += step;
|
||||
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 = 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<int, int> 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);
|
||||
}
|
||||
}
|
|
@ -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<string> OnAnimationEnd;
|
||||
private List<AnimationContainer> animations;
|
||||
private List<Texture2D> textures;
|
||||
public List<string> 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<string> 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<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);
|
||||
animationContainer.TextureFrameInterval = 0;
|
||||
animationContainer.TextureName = texture.Name;
|
||||
animationContainer.IsCycle = true;
|
||||
animationContainer.FramesCount = 1;
|
||||
animationContainer.FrameTime = new List<Tuple<int, int>>() { new Tuple<int, int>(0, 10) };
|
||||
animationContainer.Id = texture.Name;
|
||||
currentAnimation = animationContainer;
|
||||
idleAnimation = animationContainer;
|
||||
animations.Add(animationContainer);
|
||||
}
|
||||
|
||||
private void LoadAnimations(List<string> animationsId, string neitralAnimationId)
|
||||
{
|
||||
animations = new List<AnimationContainer>();
|
||||
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<Texture2D>();
|
||||
texturesNames = new List<string>();
|
||||
|
||||
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<Texture2D>(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<int, int> 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);
|
||||
}
|
61
ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs
Normal file
61
ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs
Normal file
|
@ -0,0 +1,61 @@
|
|||
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
|
||||
{
|
||||
|
||||
public class StaticGraphicsComponent : GraphicsComponent
|
||||
{
|
||||
private Texture2D texture;
|
||||
private string textureName;
|
||||
|
||||
|
||||
public StaticGraphicsComponent()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public StaticGraphicsComponent(string textureName)
|
||||
{
|
||||
BuildComponent(textureName);
|
||||
}
|
||||
|
||||
public void BuildComponent(string textureName)
|
||||
{
|
||||
this.textureName = textureName;
|
||||
}
|
||||
|
||||
|
||||
public override void LoadContent()
|
||||
{
|
||||
texture = AppManager.Instance.Content.Load<Texture2D>(textureName);
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch)
|
||||
{
|
||||
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)
|
||||
{
|
||||
destinationRectangle.X -= CameraPosition.X;
|
||||
destinationRectangle.Y -= CameraPosition.Y;
|
||||
|
||||
destinationRectangle = Scaling(destinationRectangle);
|
||||
_spriteBatch.Draw(texture,
|
||||
destinationRectangle, sourceRectangle, Color.White);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue