BulletNetworkSupport

This commit is contained in:
AnloGames 2023-08-18 19:32:20 +03:00
parent b0a384972f
commit 8c258f9e5b
3 changed files with 147 additions and 61 deletions

View file

@ -10,6 +10,7 @@ using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using DangerousD.GameCore.GameObjects.LivingEntities.Monsters;
using DangerousD.GameCore.Network;
using DangerousD.GameCore.GameObjects.MapObjects;
namespace DangerousD.GameCore.GameObjects.LivingEntities
{
@ -55,25 +56,29 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
leftBorder = (int)position.X - 100;
bullets = 5;
this.GraphicsComponent.actionOfAnimationEnd += (a) =>
{
if (a == "playerShootLeft" || a == "playerShootRight")
{
isShooting = false;
}
if (a == "playerReload")
{
bullets++;
}
};
}
this.GraphicsComponent.actionOfAnimationEnd += (a) =>
{
if (a == "playerShootLeft" || a == "playerShootRight")
{
isShooting = false;
}
if (a == "playerReload")
{
bullets++;
}
if (a == "playerShootBoomUpRight" || a == "playerShootBoomUpLeft")
{
isShooting = false;
}
};
}
public bool IsAlive { get { return isAlive; } }
protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> { "playerMoveLeft", "playerMoveRight", "DeathFromZombie", "playerRightStay", "playerStayLeft",
"playerJumpRight" , "playerJumpLeft", "playerShootLeft", "playerShootRight", "playerReload", "smokeAfterShoot", "playerShootUpRight", "playerShootUpLeft"}, "playerReload");
"playerJumpRight" , "playerJumpLeft", "playerShootLeft", "playerShootRight", "playerReload", "smokeAfterShoot", "playerShootUpRight", "playerShootUpLeft", "playerShootBoomUpRight",
"playerShootBoomUpLeft"}, "playerReload");
public void Attack()
{
@ -89,10 +94,10 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
}
public Rectangle GetShootRectangle(bool isRight)
{
if (isRight)
if (isRight)
return new Rectangle((int)Pos.X, (int)(Pos.Y) + 10, shootLength + Width, Height / 2);
else
return new Rectangle((int)Pos.X-shootLength, (int)(Pos.Y) + 10, shootLength, Height / 2);
return new Rectangle((int)Pos.X - shootLength, (int)(Pos.Y) + 10, shootLength, Height / 2);
}
public override void Draw(SpriteBatch spriteBatch)
{
@ -118,7 +123,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
return;
}
isAttacked = true;
if(monsterName == "Zombie")
if (monsterName == "Zombie")
{
AnimationRectangle deathRectangle = new AnimationRectangle(Pos, "DeathFrom" + monsterName);
deathRectangle.Gr.actionOfAnimationEnd += (a) =>
@ -129,7 +134,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
}
};
}
else if(monsterName == "Spider")
else if (monsterName == "Spider")
{
AnimationRectangle deathRectangle = new AnimationRectangle(Pos, "DeathFrom" + monsterName);
deathRectangle.Gr.actionOfAnimationEnd += (a) =>
@ -184,41 +189,37 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
bullets--;
if (isRight)
{
StartCicycleAnimation("playerShootRight");
var targets = AppManager.Instance.GameManager.physicsManager.CheckRectangle(new Rectangle((int)Pos.X, (int)(Pos.Y - 10f), shootLength + 24, 10), typeof(Zombie)).OrderBy(x => (x.Pos - Pos).LengthSquared());
if (targets.Count() > 0)
if (!isUping)
{
Zombie targetZombie = (Zombie)targets.First();
targetZombie.TakeDamage();
StartCicycleAnimation("playerShootRight");
Bullet bullet = new Bullet(new Vector2(Pos.X + 16, Pos.Y));
bullet.ShootRight();
SmokeAfterShoot smokeAfterShoot = new SmokeAfterShoot(new Vector2(Pos.X + 12, Pos.Y - 8));
}
targets = AppManager.Instance.GameManager.physicsManager.CheckRectangle(GetShootRectangle(isRight), typeof(SilasHands)).OrderBy(x => (x.Pos - Pos).LengthSquared());
if (targets.Count() > 0)
else
{
SilasHands targetHand = (SilasHands)targets.First();
targetHand.TakeDamage();
StartCicycleAnimation("playerShootBoomUpRight");
Bullet bullet = new Bullet(new Vector2(Pos.X + 16, Pos.Y));
bullet.ShootUpRight();
SmokeAfterShoot smokeAfterShoot = new SmokeAfterShoot(new Vector2(Pos.X + 12, Pos.Y - 8));
}
SmokeAfterShoot smokeAfterShoot = new SmokeAfterShoot(new Vector2(Pos.X + 30, Pos.Y + 7));
}
else
else if (!isRight)
{
StartCicycleAnimation("playerShootLeft");
var targets = AppManager.Instance.GameManager.physicsManager.CheckRectangle(new Rectangle((int)Pos.X - shootLength, (int)(Pos.Y - 10f), shootLength, 10), typeof(Zombie));
if (targets != null)
if (!isUping)
{
foreach (var target in targets)
{
Zombie targetZombie = (Zombie)target;
targetZombie.TakeDamage();
}
StartCicycleAnimation("playerShootBoomUpLeft");
Bullet bullet = new Bullet(new Vector2(Pos.X, Pos.Y));
bullet.ShootLeft();
SmokeAfterShoot smokeAfterShoot = new SmokeAfterShoot(new Vector2(Pos.X - 6, Pos.Y - 7));
}
targets = AppManager.Instance.GameManager.physicsManager.CheckRectangle(GetShootRectangle(isRight), typeof(SilasHands));
if (targets.Count() > 0)
else
{
SilasHands targetHand = (SilasHands)targets.First();
targetHand.TakeDamage();
StartCicycleAnimation("playerShootBoomUpLeft");
Bullet bullet = new Bullet(new Vector2(Pos.X, Pos.Y));
bullet.ShootUpLeft();
SmokeAfterShoot smokeAfterShoot = new SmokeAfterShoot(new Vector2(Pos.X - 6, Pos.Y - 7));
}
SmokeAfterShoot smokeAfterShoot = new SmokeAfterShoot(new Vector2(Pos.X - 12, Pos.Y + 7));
}
}
}
@ -239,7 +240,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
FallingThroughPlatform = false;
}
GraphicsComponent.SetCameraPosition(Pos);
if (!isAttacked || AppManager.Instance.InputManager.InvincibilityCheat)
if (!isAttacked || AppManager.Instance.InputManager.InvincibilityCheat)
{
if (!isShooting)
{
@ -282,14 +283,7 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
}
else if (AppManager.Instance.InputManager.VectorMovementDirection.X == 0)//стоит
{
if(bullets < 5)
{
if (GraphicsComponent.GetCurrentAnimation != "playerReload")
{
GraphicsComponent.StartAnimation("playerReload");
}
}
else if (isRight)
if (isRight)
{
if (isUping)
{
@ -298,6 +292,13 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
GraphicsComponent.StartAnimation("playerShootUpRight");
}
}
else if (bullets < 5)
{
if (GraphicsComponent.GetCurrentAnimation != "playerReload")
{
GraphicsComponent.StartAnimation("playerReload");
}
}
else
{
GraphicsComponent.StartAnimation("playerRightStay");
@ -312,6 +313,13 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
GraphicsComponent.StartAnimation("playerShootUpLeft");
}
}
else if (bullets < 5)
{
if (GraphicsComponent.GetCurrentAnimation != "playerReload")
{
GraphicsComponent.StartAnimation("playerReload");
}
}
else
{
GraphicsComponent.StartAnimation("playerStayLeft");
@ -319,11 +327,6 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
}
}
}
if (AppManager.Instance.multiPlayerStatus != MultiPlayerStatus.SinglePlayer)
{
NetworkTask task = new NetworkTask(id, Pos);
AppManager.Instance.NetworkTasks.Add(task);
}
}
public void MoveDown()
{
@ -331,13 +334,89 @@ namespace DangerousD.GameCore.GameObjects.LivingEntities
isOnGround = false;
}
public class Bullet : GameObjects.LivingEntity
public class Bullet : LivingEntity
{
public Bullet(Vector2 position) : base(position)
{
Height = 5;
Width = 5;
}
protected override GraphicsComponent GraphicsComponent { get; } = new(new List<string> { "playerMoveLeft" }, "playerMoveLeft");
Vector2 direction;
Vector2 maindirection;
public void ShootUpRight()
{
direction = new Vector2(1, -1);
acceleration = Vector2.Zero;
velocity = new Vector2(10, 10) * direction;
maindirection = velocity;
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Client)
{
NetworkTask task = new NetworkTask(typeof(Bullet), Pos, id, velocity);
AppManager.Instance.NetworkTasks.Add(task);
AppManager.Instance.GameManager.Remove(this);
}
}
public void ShootRight()
{
direction = new Vector2(1, 0);
acceleration = Vector2.Zero;
velocity = new Vector2(10, 10) * direction;
maindirection = velocity;
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Client)
{
NetworkTask task = new NetworkTask(typeof(Bullet), Pos, id, velocity);
AppManager.Instance.NetworkTasks.Add(task);
AppManager.Instance.GameManager.Remove(this);
}
}
public void ShootLeft()
{
direction = new Vector2(-1, 0);
acceleration = Vector2.Zero;
velocity = new Vector2(10, 10) * direction;
maindirection = velocity;
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Client)
{
NetworkTask task = new NetworkTask(typeof(Bullet), Pos, id, velocity);
AppManager.Instance.NetworkTasks.Add(task);
AppManager.Instance.GameManager.Remove(this);
}
}
public void ShootUpLeft()
{
direction = new Vector2(-1, -1);
acceleration = Vector2.Zero;
velocity = new Vector2(10, 10) * direction;
maindirection = velocity;
if (AppManager.Instance.multiPlayerStatus == MultiPlayerStatus.Client)
{
NetworkTask task = new NetworkTask(typeof(Bullet), Pos, id, velocity);
AppManager.Instance.NetworkTasks.Add(task);
AppManager.Instance.GameManager.Remove(this);
}
}
public override void OnCollision(GameObject gameObject)
{
if (gameObject is not Player)
{
if (gameObject is CoreEnemy)
{
CoreEnemy enemy = (CoreEnemy)gameObject;
enemy.TakeDamage(1);
AppManager.Instance.GameManager.Remove(this);
}
base.OnCollision(gameObject);
}
}
public override void Update(GameTime gameTime)
{
if (maindirection != velocity)
{
AppManager.Instance.GameManager.Remove(this);
}
base.Update(gameTime);
}
protected override GraphicsComponent GraphicsComponent { get; } = new("ZombieMoveLeft");
}
}
}
}

View file

@ -236,6 +236,12 @@ namespace DangerousD.GameCore
SoundManager.StartSound(networkTask.name, networkTask.position, GameManager.GetPlayer1.Pos);
break;
case NetworkTaskOperationEnum.CreateEntity:
if (networkTask.type == typeof(Player.Bullet))
{
Player.Bullet bullet = new Player.Bullet(networkTask.position);
bullet.id = networkTask.objId;
bullet.velocity = networkTask.velocity;
}
break;
case NetworkTaskOperationEnum.SendPosition:
if (networkTask.objId != GameManager.GetPlayer1.id )

View file

@ -38,11 +38,12 @@ namespace DangerousD.GameCore.Network
/// <param name="EntityType"></param>
/// <param name="EntityPosition"></param>
/// <param name="ParentId"></param>
public NetworkTask(Type EntityType, Vector2 EntityPosition, int ParentId)
public NetworkTask(Type EntityType, Vector2 EntityPosition, int ParentId, Vector2 velocity)
{
operation = NetworkTaskOperationEnum.CreateEntity;
type = EntityType;
position = EntityPosition;
this.velocity = velocity;
objId = ParentId;
}