Merge pull request #56 from progtime-net/GraphicsComponent-refactor

Graphics component refactor
This commit is contained in:
SergoDobro 2024-08-18 13:05:36 +03:00 committed by GitHub
commit f074cc04f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 435 additions and 347 deletions

20
.run/ZoFo.run.xml Normal file
View file

@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="ZoFo" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/ZoFo/bin/Debug/net8.0/ZoFo" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/ZoFo/bin/Debug/net8.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/ZoFo/ZoFo.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>

View file

@ -12,7 +12,9 @@ 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> { "тут пишите название анимации" }, "сдублируйте " +
public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List<string> { "player_idle_top-right_noweapon" }, "player_idle_top-right_noweapon");
public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "player_idle_rotate_weapon" }, "player_idle_rotate_weapon");
public EntittyForAnimationTests(Vector2 position) : base(position) public EntittyForAnimationTests(Vector2 position) : base(position)
{ {
graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,16*12, 16 * 16); graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,16*12, 16 * 16);

View file

@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
class Ammo:Collectable 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) public Ammo(Vector2 position) : base(position)
{ {
graphicsComponent.ObjectDrawRectangle.Width = 20; graphicsComponent.ObjectDrawRectangle.Width = 20;

View file

@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
class Antiradine:Collectable class Antiradine:Collectable
{ {
public override GraphicsComponent graphicsComponent { get; } = new(new List<string> { "Antiradine" }, "Antiradine"); public override StaticGraphicsComponent graphicsComponent { get; } = new("Antiradine");
public Antiradine(Vector2 position) : base(position) public Antiradine(Vector2 position) : base(position)
{ {

View file

@ -14,7 +14,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
public class BottleOfWater : Collectable public class BottleOfWater : Collectable
{ {
public override GraphicsComponent graphicsComponent { get; } = new(new List<string> { "BottleOfWater" }, "BottleOfWater"); public override StaticGraphicsComponent graphicsComponent { get; } = new("BottleOfWater");
public BottleOfWater(Vector2 position) : base(position) public BottleOfWater(Vector2 position) : base(position)
{ {

View file

@ -14,7 +14,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
public class Peeble:Collectable public class Peeble:Collectable
{ {
public override GraphicsComponent graphicsComponent { get; } = new(new List<string> { "Peeble" }, "Peeble"); public override StaticGraphicsComponent graphicsComponent { get; } = new("Peeble");
public Peeble(Vector2 position) : base(position) public Peeble(Vector2 position) : base(position)
{ {

View file

@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
class PureBottleOfWater:Collectable class PureBottleOfWater:Collectable
{ {
public override GraphicsComponent graphicsComponent { get; } = new(new List<string> { "PureBottleOfWater" }, "PureBottleOfWater"); public override StaticGraphicsComponent graphicsComponent { get; } = new("PureBottleOfWater");
public PureBottleOfWater(Vector2 position) : base(position) public PureBottleOfWater(Vector2 position) : base(position)
{ {

View file

@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
class RottenFlesh:Collectable class RottenFlesh:Collectable
{ {
public override GraphicsComponent graphicsComponent { get; } = new(new List<string> { "RottenFlesh" }, "RottenFlesh"); public override StaticGraphicsComponent graphicsComponent { get; } = new("RottenFlesh");
public RottenFlesh(Vector2 position) : base(position) public RottenFlesh(Vector2 position) : base(position)
{ {

View file

@ -13,7 +13,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
class Steel:Collectable class Steel:Collectable
{ {
public override GraphicsComponent graphicsComponent { get; } = new(new List<string> { "Steel" }, "Steel"); public override StaticGraphicsComponent graphicsComponent { get; } = new("Steel");
public Steel(Vector2 position) : base(position) public Steel(Vector2 position) : base(position)
{ {

View file

@ -9,7 +9,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables;
public class Wood : Collectable 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) public Wood(Vector2 position) : base(position)
{ {

View file

@ -9,16 +9,16 @@ public class Door : Interactable
{ {
public bool isOpened; public bool isOpened;
public override GraphicsComponent graphicsComponent { get; } = new(new List<string> { "DoorInteraction" }, "DoorInteraction"); public override StaticGraphicsComponent graphicsComponent { get; } = new("DoorInteraction");
public Door(Vector2 position) : base(position) public Door(Vector2 position) : base(position)
{ {
graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };//ïðèêîëüêî, ÷òî ÷åðåç íèæíåå ïîä÷åðêèâàíèå - SD //graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - SD
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(object sender, CollisionComponent e)
{ {
graphicsComponent.AnimationSelect("DoorInteraction", isOpened); //graphicsComponent.AnimationSelect("DoorInteraction", isOpened);
graphicsComponent.AnimationStep(); //graphicsComponent.AnimationStep();
} }
} }

View file

@ -9,7 +9,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables;
public class Interactable : Entity 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) public Interactable(Vector2 position) : base(position)
{ {

View file

@ -1,8 +1,8 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
using ZoFo.GameCore.GameManagers; using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.CollisionManager; using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
@ -21,15 +21,14 @@ public class Player : LivingEntity
public bool IsTryingToShoot { get; set; } public bool IsTryingToShoot { get; set; }
private float speed; private float speed;
private int health; private int health;
public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List<string> { "player_running_top_rotate" }, "player_running_top_rotate");
private LootData lootData; private LootData lootData;
public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List<string> { "player_running_top_rotate" }, "player_running_top_rotate");
public Player(Vector2 position) : base(position) public Player(Vector2 position) : base(position)
{ {
//InputWeaponRotation = new Vector2(0, 0); //InputWeaponRotation = new Vector2(0, 0);
//InputPlayerRotation = 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); collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100);
graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,100,100);
} }

View file

@ -13,7 +13,7 @@ public abstract class GameObject
public Vector2 position; public Vector2 position;
public Vector2 rotation; //вектор направления объекта public Vector2 rotation; //вектор направления объекта
public abstract GraphicsComponent graphicsComponent { get; } public virtual GraphicsComponent graphicsComponent { get; }
#region ServerSide #region ServerSide
public GameObject(Vector2 position) public GameObject(Vector2 position)
@ -81,7 +81,7 @@ public abstract class GameObject
/// </summary> /// </summary>
public virtual void Draw(SpriteBatch spriteBatch) public virtual void Draw(SpriteBatch spriteBatch)
{ {
graphicsComponent.DrawAnimation(graphicsComponent.ObjectDrawRectangle, spriteBatch); graphicsComponent.Draw(graphicsComponent.ObjectDrawRectangle, spriteBatch);
//debug //debug
DrawDebugRectangle(spriteBatch, graphicsComponent.ObjectDrawRectangle); DrawDebugRectangle(spriteBatch, graphicsComponent.ObjectDrawRectangle);

View file

@ -16,7 +16,8 @@ namespace ZoFo.GameCore.GameObjects.MapObjects
{ {
public virtual bool IsColliderOn { get; protected set; } = true;//Who added that? public virtual bool IsColliderOn { get; protected set; } = true;//Who added that?
public Rectangle sourceRectangle; public Rectangle sourceRectangle;
public override GraphicsComponent graphicsComponent { get; } = new(); public override GraphicsComponent graphicsComponent { get; }
= new StaticGraphicsComponent();
/// <summary> /// <summary>
/// Создается простой объект на карте - no animations, только где, насколько крупно рисовать, по какой сорс ректанглу рисовать и из какой текстуры /// Создается простой объект на карте - no animations, только где, насколько крупно рисовать, по какой сорс ректанглу рисовать и из какой текстуры
@ -27,17 +28,16 @@ namespace ZoFo.GameCore.GameObjects.MapObjects
/// <param name="textureName"></param> /// <param name="textureName"></param>
public MapObject(Vector2 position, Vector2 size, Rectangle sourceRectangle, string textureName) : base(position) 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; 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) public override void Draw(SpriteBatch spriteBatch)
{ {
graphicsComponent.DrawAnimation(graphicsComponent.ObjectDrawRectangle, spriteBatch, sourceRectangle); graphicsComponent.Draw(graphicsComponent.ObjectDrawRectangle, spriteBatch, sourceRectangle);
} }
} }

View file

@ -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<string> actionOfAnimationEnd;
public List<AnimationContainer> animations;
private List<Texture2D> textures;
private List<string> 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<string> animationsId, string neitralAnimationId)
{
//this._spriteBatch = _spriteBatch;
currentFrame = 0;
lastInterval = 1;
LoadAnimations(animationsId, neitralAnimationId);
currentAnimation = neitralAnimation;
SetInterval();
buildSourceRectangle();
}
public AnimatedGraphicsComponent(string textureName)
{
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;
neitralAnimation = 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)
{
neitralAnimation = animations.Last();
}
}
}
public override void LoadContent()
{
textures = new List<Texture2D>();
texturesNames = new List<string>();
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 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<int, int> i = currentAnimation.FrameTime.Find(x => x.Item1 == currentFrame);
if (i != null)
{
interval = i.Item2;
lastInterval = interval;
}
else
{
interval = lastInterval;
}
}
}
}

View file

@ -1,6 +0,0 @@
namespace ZoFo.GameCore.Graphics;
public class AnimationComponent
{
}

View file

@ -1,255 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using ZoFo.GameCore.GameManagers;
namespace ZoFo.GameCore.Graphics namespace ZoFo.GameCore.Graphics;
public abstract class GraphicsComponent
{ {
public class GraphicsComponent
{
public Rectangle ObjectDrawRectangle; 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 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 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) public abstract void LoadContent();
{ public abstract void Update();
animations = new List<AnimationContainer>(); public abstract void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch);
foreach (var id in animationsId) public abstract void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle);
{
animations.Add(AppManager.Instance.animationBuilder.Animations.Find(x => x.Id == id));
if (id == neitralAnimationId)
{
idleAnimation = animations.Last();
}
}
}
public void LoadContent() protected Rectangle Scaling(Rectangle destinationRectangle)
{
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.X *= scaling;
destinationRectangle.Y *= scaling; destinationRectangle.Y *= scaling;
@ -257,33 +22,7 @@ namespace ZoFo.GameCore.Graphics
destinationRectangle.Height *= scaling; destinationRectangle.Height *= scaling;
return destinationRectangle; 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) public static void SetCameraPosition(Vector2 playerPosition)
{ {
CameraPosition = (playerPosition).ToPoint(); CameraPosition = (playerPosition).ToPoint();
@ -314,5 +53,4 @@ namespace ZoFo.GameCore.Graphics
*/ */
} }
public static Point CameraPosition = new Point(0, 0); public static Point CameraPosition = new Point(0, 0);
}
} }

View file

@ -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);
}

View file

@ -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<Texture2D>(_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);
}
}
}

View file

@ -20,6 +20,7 @@ using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
using ZoFo.GameCore.GameObjects.MapObjects; using ZoFo.GameCore.GameObjects.MapObjects;
using ZoFo.GameCore.GameObjects.MapObjects.StopObjects; using ZoFo.GameCore.GameObjects.MapObjects.StopObjects;
using ZoFo.GameCore.Graphics;
namespace ZoFo.GameCore namespace ZoFo.GameCore
{ {
@ -124,7 +125,7 @@ namespace ZoFo.GameCore
entities = new List<Entity>(); entities = new List<Entity>();
new MapManager().LoadMap(); 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 Player(new Vector2(740, 140)));
AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(140, 440))); AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(140, 440)));
AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(240, 440))); AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(240, 440)));
@ -175,7 +176,7 @@ namespace ZoFo.GameCore
sourceRectangle = (gameObject as StopObject).sourceRectangle, sourceRectangle = (gameObject as StopObject).sourceRectangle,
Size = (gameObject as StopObject).graphicsComponent.ObjectDrawRectangle.Size, Size = (gameObject as StopObject).graphicsComponent.ObjectDrawRectangle.Size,
collisions = (gameObject as StopObject).collisionComponents.Select(x=>x.stopRectangle).ToArray(), 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 });//TODO
foreach (var item in (gameObject as StopObject).collisionComponents) foreach (var item in (gameObject as StopObject).collisionComponents)
{ {
@ -191,7 +192,7 @@ namespace ZoFo.GameCore
Position = (gameObject as MapObject).position, Position = (gameObject as MapObject).position,
sourceRectangle = (gameObject as MapObject).sourceRectangle, sourceRectangle = (gameObject as MapObject).sourceRectangle,
Size = (gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size, Size = (gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size,
tileSetName = (gameObject as MapObject).graphicsComponent.mainTextureName tileSetName = ((gameObject as MapObject).graphicsComponent as StaticGraphicsComponent)._textureName
});//TODO });//TODO
return; return;
} }