diff --git a/AnimationsFileCreator/Program.cs b/AnimationsFileCreator/Program.cs index 1287e6f..dc2bf5b 100644 --- a/AnimationsFileCreator/Program.cs +++ b/AnimationsFileCreator/Program.cs @@ -40,6 +40,7 @@ namespace AnimationsFileCreator string id = Console.ReadLine(); Console.WriteLine("Введите 1 если анимация зациклена, и 0 если нет"); AnimationContainer container = new AnimationContainer(); + int a = int.Parse(Console.ReadLine()); if (a==1) { @@ -49,6 +50,10 @@ namespace AnimationsFileCreator { container.IsCycle = false; } + Console.WriteLine("Введите отклонение анимации от стандартной (сначала X, потом enter, потом Y): "); + int otklx = int.Parse(Console.ReadLine()); + int otkly = int.Parse(Console.ReadLine()); + container.Offset =new Vector2(otklx,otkly); container.FramesCount = framesCount; container.FrameTime = new System.Collections.Generic.List>(); container.FrameTime.Add(new Tuple(0, interval)); diff --git a/DangerousD/Content/Content.mgcb b/DangerousD/Content/Content.mgcb index 18f7ea3..b32dbb2 100644 --- a/DangerousD/Content/Content.mgcb +++ b/DangerousD/Content/Content.mgcb @@ -13,6 +13,18 @@ #---------------------------------- Content ---------------------------------# +#begin ../../../animation1.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:../../../animation1.png;animation1.png + #begin File.spritefont /importer:FontDescriptionImporter /processor:FontDescriptionProcessor diff --git a/DangerousD/Content/animation1.png b/DangerousD/Content/animation1.png new file mode 100644 index 0000000..01d185c Binary files /dev/null and b/DangerousD/Content/animation1.png differ diff --git a/DangerousD/Content/animations/ZombieLeftAttack b/DangerousD/Content/animations/ZombieLeftAttack new file mode 100644 index 0000000..a5eae6c --- /dev/null +++ b/DangerousD/Content/animations/ZombieLeftAttack @@ -0,0 +1 @@ +{"id":"ZombieLeftAttack","textureName":"animation1","startSpriteRectangle":{"X":126,"Y":50,"Width":50,"Height":40},"frameSecond":[{"Item1":0,"Item2":25}],"textureFrameInterval":1,"framesCount":3,"isCycle":false,"offset":"16, 0"} diff --git a/DangerousD/Content/animations/ZombieMoveLeft b/DangerousD/Content/animations/ZombieMoveLeft new file mode 100644 index 0000000..2f41b50 --- /dev/null +++ b/DangerousD/Content/animations/ZombieMoveLeft @@ -0,0 +1 @@ +{"id":"ZombieMoveLeft","textureName":"animation1","startSpriteRectangle":{"X":1,"Y":50,"Width":24,"Height":40},"frameSecond":[{"Item1":0,"Item2":12}],"textureFrameInterval":1,"framesCount":4,"isCycle":true} diff --git a/DangerousD/Content/animations/ZombieMoveRight b/DangerousD/Content/animations/ZombieMoveRight new file mode 100644 index 0000000..c59cd6f --- /dev/null +++ b/DangerousD/Content/animations/ZombieMoveRight @@ -0,0 +1 @@ +{"id":"ZombieMoveRight","textureName":"animation1","startSpriteRectangle":{"X":1,"Y":9,"Width":24,"Height":40},"frameSecond":[{"Item1":0,"Item2":12}],"textureFrameInterval":1,"framesCount":4,"isCycle":true} diff --git a/DangerousD/Content/animations/ZombieRightAttack b/DangerousD/Content/animations/ZombieRightAttack new file mode 100644 index 0000000..d2e6456 --- /dev/null +++ b/DangerousD/Content/animations/ZombieRightAttack @@ -0,0 +1 @@ +{"id":"ZombieRightAttack","textureName":"animation1","startSpriteRectangle":{"X":126,"Y":9,"Width":50,"Height":40},"frameSecond":[{"Item1":0,"Item2":25}],"textureFrameInterval":1,"framesCount":3,"isCycle":false,"offset":"16, 0"} diff --git a/DangerousD/GameCore/GameObjects/Entity.cs b/DangerousD/GameCore/GameObjects/Entity.cs index c7ea2f3..eb865e5 100644 --- a/DangerousD/GameCore/GameObjects/Entity.cs +++ b/DangerousD/GameCore/GameObjects/Entity.cs @@ -13,6 +13,17 @@ namespace DangerousD.GameCore.GameObjects public Entity(Vector2 position) : base(position) {} - + public void SetPosition(Vector2 position) { targetPosition = position; } + + public override void Update(GameTime gameTime) + { + if (Vector2.Distance(Pos, targetPosition) > 0.5f) + { + Vector2 dir = targetPosition - Pos; + dir.Normalize(); + _pos += dir * speed; + } + base.Update(gameTime); + } } } diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index 82648d4..52c96c1 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -12,14 +12,17 @@ namespace DangerousD.GameCore { public abstract class GameObject : IDrawableObject { - public Vector2 Pos { get; protected set; } + protected Vector2 _pos; + public Vector2 Pos => _pos; public int Width { get; protected set; } public int Height { get; protected set; } public Rectangle Rectangle => new Rectangle((int)Pos.X, (int)Pos.Y, Width, Height); + public Vector2 velocity; + public Vector2 acceleration; protected abstract GraphicsComponent GraphicsComponent { get; } public GameObject(Vector2 pos) { - Pos = pos; + _pos = pos; Width = 500; Height = 101; //Animator = new GraphicsComponent(new() { "playerIdle" }); diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/CoreEnemy.cs b/DangerousD/GameCore/GameObjects/LivingEntities/CoreEnemy.cs new file mode 100644 index 0000000..c5b7230 --- /dev/null +++ b/DangerousD/GameCore/GameObjects/LivingEntities/CoreEnemy.cs @@ -0,0 +1,40 @@ +using DangerousD.GameCore.Graphics; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DangerousD.GameCore.GameObjects.LivingEntities +{ + public abstract class CoreEnemy : LivingEntity + { + protected int monster_health; + protected int monster_speed; + protected string name; + protected bool isAlive = true; + + public CoreEnemy(Vector2 position) : base(position) + { + //здесь я не понял + } + + public virtual void Update(GameTime gameTime, Player player) + { + if (monster_health <= 0) + { + Death(); + isAlive = false; + } + base.Update(gameTime); + } + public abstract void Death(); + + public abstract void Attack(); + + public abstract void Move(GameTime gameTime); + } +} diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Enemy1.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Enemy1.cs deleted file mode 100644 index ccebca0..0000000 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Enemy1.cs +++ /dev/null @@ -1,30 +0,0 @@ -using DangerousD.GameCore.Graphics; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; -using Microsoft.Xna.Framework.Graphics; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DangerousD.GameCore.GameObjects.LivingEntities -{ - public class Enemy1 : LivingEntity - { - - protected override GraphicsComponent GraphicsComponent { get; } = new(new List { "IDLE", "WALK" }, "IDLE"); - - public Enemy1(Vector2 position) : base(position) - { - } - - public override void Update(GameTime gameTime) - { - if (GraphicsComponent.GetCurrentAnimation!="WALK") - GraphicsComponent.StartAnimation("WALK"); - - base.Update(gameTime); - } - } -} diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs new file mode 100644 index 0000000..68e2881 --- /dev/null +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Monsters/Zombie.cs @@ -0,0 +1,92 @@ +using DangerousD.GameCore.Graphics; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters +{ + public class Zombie : CoreEnemy + { + private bool isGoRight = true; + int leftBorder; + int rightBorder; + public Zombie(Vector2 position) : base(position) + { + Width = 72; + Height = 120; + monster_speed = 20; + GraphicsComponent.StartAnimation("ZombieLeftAttack"); + name = "Zombie"; + leftBorder = (int)position.X; + rightBorder = (int)position.X + 200; + } + protected override GraphicsComponent GraphicsComponent { get; } = new(new List { "ZombieMoveRight", "ZombieMoveLeft", "ZombieRightAttack", "ZombieLeftAttack" }, "ZombieMoveLeft"); + + public override void Update(GameTime gameTime, Player player) + { + Move(gameTime); + + if(Pos.X + 20 <= player.Pos.X || Pos.X - 20 >= player.Pos.X) + { + Attack(); + player.Death(name); + } + + base.Update(gameTime); + } + + public override void Attack() + { + if (isGoRight) + { + GraphicsComponent.StopAnimation(); + GraphicsComponent.StartAnimation("ZombieRightAttack"); + AppManager.Instance.GameManager.Player.Death(name); + } + else if (!isGoRight) + { + GraphicsComponent.StopAnimation(); + GraphicsComponent.StartAnimation("ZombieLeftAttack"); + AppManager.Instance.GameManager.Player.Death(name); + } + } + + public override void Death() + { + + } + + public override void Move(GameTime gameTime) + { + double delta = gameTime.ElapsedGameTime.TotalSeconds; + if (isGoRight) + { + if (GraphicsComponent.GetCurrentAnimation != "ZombieMoveRight") + { + GraphicsComponent.StartAnimation("ZombieMoveRight"); + velocity = new Vector2(monster_speed, 0); + } + } + + else if (!isGoRight) + { + if(GraphicsComponent.GetCurrentAnimation != "ZombieMoveLeft") + { + GraphicsComponent.StartAnimation("ZombieMoveLeft"); + velocity = new Vector2(-monster_speed, 0); + } + } + } + + public void TakeDamage(int damage) + { + monster_health -= damage; + //play take damage animation + } + } +} diff --git a/DangerousD/GameCore/GameObjects/LivingEntities/Player.cs b/DangerousD/GameCore/GameObjects/LivingEntities/Player.cs index a36ee64..dd0e96e 100644 --- a/DangerousD/GameCore/GameObjects/LivingEntities/Player.cs +++ b/DangerousD/GameCore/GameObjects/LivingEntities/Player.cs @@ -10,15 +10,20 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities { public class Player : LivingEntity { - public Player(Vector2 position): base(position) + public Player(Vector2 position) : base(position) { - } + protected override GraphicsComponent GraphicsComponent => throw new NotImplementedException(); public void Kill() { } + + public void Death(string monsterName) + { + //анимация по имени монстра + } } } diff --git a/DangerousD/GameCore/Graphics/AnimationContainer.cs b/DangerousD/GameCore/Graphics/AnimationContainer.cs index 863f260..c50cb2b 100644 --- a/DangerousD/GameCore/Graphics/AnimationContainer.cs +++ b/DangerousD/GameCore/Graphics/AnimationContainer.cs @@ -23,6 +23,10 @@ namespace DangerousD.GameCore.Graphics public int FramesCount { get; set; } [JsonProperty("isCycle")] public bool IsCycle { get; set; } + [JsonProperty("offset")] + public Vector2 Offset { get; set; } + + } } diff --git a/DangerousD/GameCore/Graphics/GraphicsComponent.cs b/DangerousD/GameCore/Graphics/GraphicsComponent.cs index 3dfed09..c5834ab 100644 --- a/DangerousD/GameCore/Graphics/GraphicsComponent.cs +++ b/DangerousD/GameCore/Graphics/GraphicsComponent.cs @@ -110,7 +110,7 @@ namespace DangerousD.GameCore.Graphics if (interval == 0) { currentFrame++; - if (currentAnimation.FramesCount - 1 <= currentFrame) + if (currentAnimation.FramesCount <= currentFrame) { if (!currentAnimation.IsCycle) { @@ -129,8 +129,24 @@ namespace DangerousD.GameCore.Graphics 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); + } + - _spriteBatch.Draw(textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)], + _spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, Color.White); } diff --git a/DangerousD/GameCore/Levels/Level1.cs b/DangerousD/GameCore/Levels/Level1.cs index 781bb24..1716044 100644 --- a/DangerousD/GameCore/Levels/Level1.cs +++ b/DangerousD/GameCore/Levels/Level1.cs @@ -1,6 +1,8 @@ using DangerousD.GameCore.GameObjects.LivingEntities; using DangerousD.GameCore.GameObjects.MapObjects; using Microsoft.Xna.Framework; +using DangerousD.GameCore.GameObjects.LivingEntities.Monsters; + namespace DangerousD.GameCore.Levels { @@ -8,10 +10,9 @@ namespace DangerousD.GameCore.Levels { public void InitLevel() { - //new Player(); var Трава = new GrassBlock(new Vector2(0, 128)); var Death = new TestAnimationDeath(new Vector2(128, 128)); - + var Zombie = new Zombie(new Vector2(256, 128)); } } }