Merge remote-tracking branch 'origin/livingEntities'

# Conflicts:
#	DangerousD/GameCore/GameObjects/Entity.cs
This commit is contained in:
SergoDobro 2023-08-16 09:58:18 +03:00
commit e944a972d6
16 changed files with 202 additions and 39 deletions

View file

@ -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<Tuple<int, int>>();
container.FrameTime.Add(new Tuple<int, int>(0, interval));

View file

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View file

@ -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"}

View file

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

View file

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

View file

@ -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"}

View file

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

View file

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

View file

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

View file

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

View file

@ -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<string> { "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
}
}
}

View file

@ -12,13 +12,18 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
{
public Player(Vector2 position) : base(position)
{
}
protected override GraphicsComponent GraphicsComponent => throw new NotImplementedException();
public void Kill()
{
}
public void Death(string monsterName)
{
//анимация по имени монстра
}
}
}

View file

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

View file

@ -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);
_spriteBatch.Draw(textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)],
}
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(texture,
destinationRectangle, sourceRectangle, Color.White);
}

View file

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