player can shoot

This commit is contained in:
bmvolf 2023-08-18 01:44:29 +03:00
parent e6d0f2b8a0
commit 24919cf07f
6 changed files with 104 additions and 19 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

View file

@ -0,0 +1 @@
{"id":"playerReload","textureName":"playerAnimation","startSpriteRectangle":{"X":101,"Y":67,"Width":24,"Height":32},"frameSecond":[{"Item1":0,"Item2":12}],"textureFrameInterval":1,"framesCount":2,"isCycle":false,"offset":"0, 0"}

View file

@ -0,0 +1 @@
{"id":"playerShootLeft","textureName":"playerAnimation","startSpriteRectangle":{"X":201,"Y":34,"Width":32,"Height":32},"frameSecond":[{"Item1":0,"Item2":12}],"textureFrameInterval":1,"framesCount":2,"isCycle":false,"offset":"4, 0"}

View file

@ -0,0 +1 @@
{"id":"playerShootRight","textureName":"playerAnimation","startSpriteRectangle":{"X":201,"Y":1,"Width":32,"Height":32},"frameSecond":[{"Item1":0,"Item2":12}],"textureFrameInterval":1,"framesCount":2,"isCycle":false,"offset":"4, 0"}

View file

@ -24,8 +24,9 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
Height = 40; Height = 40;
monster_speed = 3; monster_speed = 3;
name = "Zombie"; name = "Zombie";
leftBorder = (int)position.X - 50; monster_health = 2;
rightBorder = (int)position.X + 50; leftBorder = (int)position.X - 100;
rightBorder = (int)position.X + 100;
physicsManager = new PhysicsManager(); physicsManager = new PhysicsManager();
Random random = new Random(); Random random = new Random();
if(random.Next(0, 2) == 0) if(random.Next(0, 2) == 0)
@ -74,7 +75,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
public override void Death() public override void Death()
{ {
AppManager.Instance.GameManager.Remove(this);
} }
public override void Move(GameTime gameTime) public override void Move(GameTime gameTime)
@ -151,5 +152,15 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities.Monsters
public override void Attack(GameTime gameTime) public override void Attack(GameTime gameTime)
{} {}
public void TakeDamage()
{
monster_health--;
GraphicsComponent.StartAnimation("ZombieRightAttack");
if (monster_health <= 0)
{
Death();
}
}
} }
} }

View file

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using DangerousD.GameCore.GameObjects.PlayerDeath; using DangerousD.GameCore.GameObjects.PlayerDeath;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using DangerousD.GameCore.GameObjects.LivingEntities.Monsters;
namespace DangerousD.GameCore.GameObjects.LivingEntities namespace DangerousD.GameCore.GameObjects.LivingEntities
{ {
@ -24,7 +25,9 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
public int leftBorder; public int leftBorder;
public bool isVisible = true; public bool isVisible = true;
private bool isAttacked = false; private bool isAttacked = false;
private bool isShooting = false;
public GameObject objectAttack; public GameObject objectAttack;
private int bullets;
public Player(Vector2 position) : base(position) public Player(Vector2 position) : base(position)
{ {
@ -35,17 +38,30 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
AppManager.Instance.InputManager.MovEventJump += Jump; AppManager.Instance.InputManager.MovEventJump += Jump;
AppManager.Instance.InputManager.MovEventDown += MoveDown; AppManager.Instance.InputManager.MovEventDown += MoveDown;
AppManager.Instance.InputManager.ShootEvent += Shoot;
velocity = new Vector2(0, 0); velocity = new Vector2(0, 0);
rightBorder = (int)position.X + 100; rightBorder = (int)position.X + 100;
leftBorder = (int)position.X - 100; leftBorder = (int)position.X - 100;
bullets = 5;
this.GraphicsComponent.actionOfAnimationEnd += (a) =>
{
if (a == "playerShootLeft" || a == "playerShootRight")
{
isShooting = false;
}
if (a == "playerReload")
{
bullets++;
}
};
} }
public bool IsAlive { get { return isAlive; } } public bool IsAlive { get { return isAlive; } }
protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> { "playerMoveLeft", "playerMoveRight", "DeathFromZombie", "playerRightStay", "playerStayLeft", protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> { "playerMoveLeft", "playerMoveRight", "DeathFromZombie", "playerRightStay", "playerStayLeft",
"playerJumpRight" , "playerJumpLeft"}, "playerStayLeft"); "playerJumpRight" , "playerJumpLeft", "playerShootLeft", "playerShootRight", "playerReload"}, "playerReload");
public void Attack() public void Attack()
{ {
@ -53,6 +69,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
{ {
isVisible = false; isVisible = false;
} }
} }
public override void OnCollision(GameObject gameObject) public override void OnCollision(GameObject gameObject)
{ {
@ -95,9 +112,47 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
} }
public void Shoot() public void Shoot()
{ {
if (bullets > 0)
{
if (!isShooting)
{
isShooting = true;
bullets--;
if (isRight)
{
if (GraphicsComponent.GetCurrentAnimation != "playerShootRight")
{
GraphicsComponent.StartAnimation("playerShootRight");
}
var targets = AppManager.Instance.GameManager.physicsManager.CheckRectangle(new Rectangle((int)Pos.X, (int)(Pos.Y - 10f), 100, 10), typeof(Zombie));
if (targets != null)
{
foreach (var target in targets)
{
Zombie targetZombie = (Zombie)target;
targetZombie.TakeDamage();
}
}
}
else
{
if (GraphicsComponent.GetCurrentAnimation != "playerShootRight")
{
GraphicsComponent.StartAnimation("playerShootRight");
}
var targets = AppManager.Instance.GameManager.physicsManager.CheckRectangle(new Rectangle((int)Pos.X, (int)(Pos.Y - 10f), -100, 10), typeof(Zombie));
if (targets != null)
{
foreach (var target in targets)
{
Zombie targetZombie = (Zombie)target;
targetZombie.TakeDamage();
}
}
}
}
}
} }
public override void Update(GameTime gameTime) public override void Update(GameTime gameTime)
{ {
GraphicsComponent.CameraPosition = (_pos-new Vector2(200, 350)).ToPoint(); GraphicsComponent.CameraPosition = (_pos-new Vector2(200, 350)).ToPoint();
@ -116,25 +171,41 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
{ {
float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
velocity.X = 5 * AppManager.Instance.InputManager.VectorMovementDirection.X; velocity.X = 5 * AppManager.Instance.InputManager.VectorMovementDirection.X;
if (AppManager.Instance.InputManager.VectorMovementDirection.X > 0) if (GraphicsComponent.GetCurrentAnimation != "playerShootLeft" && GraphicsComponent.GetCurrentAnimation != "playerShootRight")
{ {
if (GraphicsComponent.GetCurrentAnimation != "playerMoveRight")//идёт направо if (AppManager.Instance.InputManager.VectorMovementDirection.X > 0)
{ {
GraphicsComponent.StartAnimation("playerMoveRight"); isRight = true;
if (GraphicsComponent.GetCurrentAnimation != "playerMoveRight")//идёт направо
{
GraphicsComponent.StartAnimation("playerMoveRight");
}
} }
} else if (AppManager.Instance.InputManager.VectorMovementDirection.X < 0)//идёт налево
else if (AppManager.Instance.InputManager.VectorMovementDirection.X < 0)//идёт налево
{
if (GraphicsComponent.GetCurrentAnimation != "playerMoveLeft")
{ {
GraphicsComponent.StartAnimation("playerMoveLeft"); isRight = false;
if (GraphicsComponent.GetCurrentAnimation != "playerMoveLeft")
{
GraphicsComponent.StartAnimation("playerMoveLeft");
}
} }
} else if (AppManager.Instance.InputManager.VectorMovementDirection.X == 0)//стоит
else if(AppManager.Instance.InputManager.VectorMovementDirection.X == 0)//стоит
{
if (GraphicsComponent.GetCurrentAnimation != "ZombieMoveLeft")
{ {
GraphicsComponent.StartAnimation("ZombieMoveLeft"); if(bullets < 5)
{
if (GraphicsComponent.GetCurrentAnimation != "playerReload")
{
GraphicsComponent.StartAnimation("playerReload");
}
}
else if (isRight)
{
GraphicsComponent.StartAnimation("playerRightStay");
}
else if (!isRight)
{
GraphicsComponent.StartAnimation("playerStayLeft");
}
} }
} }
} }