player's health can decrease

This commit is contained in:
SergoDobro 2024-08-20 11:34:11 +03:00
parent d819caeee2
commit b246532a6e
17 changed files with 127 additions and 111 deletions

View file

@ -233,6 +233,49 @@ namespace ZoFo.GameCore
DeleteObject(ent); DeleteObject(ent);
} }
else if (update is UpdatePlayerParametrs)
{
UpdatePlayerHealth(update as UpdatePlayerParametrs);
}
}
public void UpdatePlayerHealth(UpdatePlayerParametrs update)
{
//check on player hp lowered
if (myPlayer != null)
{
float hpMyPlayerHp = myPlayer.health;
var entity = FindEntityById(update.IdEntity);
if (entity != null)
{
(entity as Player).health = (update as UpdatePlayerParametrs).health;
(entity as Player).rad = (update as UpdatePlayerParametrs).radiatoin;
}
if (entity.Equals(myPlayer))
{
if (hpMyPlayerHp > myPlayer.health)
{
AppManager.Instance.client.AddShaking((hpMyPlayerHp - myPlayer.health));
}
}
return;
}
var ent = FindEntityById(update.IdEntity);
if (ent != null)
{
(ent as Player).health = (update as UpdatePlayerParametrs).health;
(ent as Player).rad = (update as UpdatePlayerParametrs).radiatoin;
}
} }

View file

@ -68,7 +68,12 @@ public class HUD : AbstractGUI
overlayGUI?.Update(gameTime); overlayGUI?.Update(gameTime);
//hpBar.Update(gameTime, AppManager.Instance.client.myPlayer.health / 100f); //hpBar.Update(gameTime, AppManager.Instance.client.myPlayer.health / 100f);
//radBar.Update(gameTime, AppManager.Instance.client.myPlayer.rad / 100f); //radBar.Update(gameTime, AppManager.Instance.client.myPlayer.rad / 100f);
radBar.Update(gameTime, gameTime.TotalGameTime.Seconds / 100f); if (AppManager.Instance.client.myPlayer != null)
{
radBar.Update(gameTime, AppManager.Instance.client.myPlayer.rad / AppManager.Instance.client.myPlayer.MaxRad);
hpBar.Update(gameTime, AppManager.Instance.client.myPlayer.health / AppManager.Instance.client.myPlayer.MaxHealth);
}
base.Update(gameTime); base.Update(gameTime);
} }

View file

@ -12,5 +12,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
public class UpdatePlayerParametrs : UpdateData public class UpdatePlayerParametrs : UpdateData
{ {
public UpdatePlayerParametrs() { UpdateType = "UpdatePlayerParametrs"; } public UpdatePlayerParametrs() { UpdateType = "UpdatePlayerParametrs"; }
public float radiatoin;
public float health;
} }
} }

View file

@ -16,31 +16,7 @@ namespace ZoFo.GameCore.GameObjects
class Ammo : Collectable class Ammo : Collectable
{ {
public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "Ammo"); public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "Ammo");
public Ammo(Vector2 position) : base(position) public Ammo(Vector2 position) : base(position) { }
{
graphicsComponent.ObjectDrawRectangle.Width = 20;
graphicsComponent.ObjectDrawRectangle.Height = 20;
collisionComponent.triggerRectangle = new Rectangle(0, 0, 20, 20);
}
public override void OnInteraction(GameObject sender)
{
DebugHUD.DebugLog("collected");
if (AppManager.Instance.client.myPlayer.lootData.loots.Keys.Contains("Ammo"))
{
AppManager.Instance.client.myPlayer.lootData.loots["Ammo"] += 1;
}
else
{
AppManager.Instance.client.myPlayer.lootData.loots.Add("Ammo", 1);
}
AppManager.Instance.server.AddData(new UpdateLoot("Ammo"));
AppManager.Instance.server.DeleteObject(this);
}
public override void Draw(SpriteBatch spriteBatch)
{
DrawDebugRectangle(spriteBatch, collisionComponent.triggerRectangle.SetOrigin(position), Color.Blue);
base.Draw(spriteBatch);
}
} }
} }

View file

@ -11,17 +11,9 @@ using ZoFo.GameCore.Graphics;
namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
class Antiradine:Collectable class Antiradine : Collectable
{ {
public override StaticGraphicsComponent graphicsComponent { get; } = new("Antiradine"); public override StaticGraphicsComponent graphicsComponent { get; } = new("Antiradine");
public Antiradine(Vector2 position) : base(position) public Antiradine(Vector2 position) : base(position) { }
{
}
public override void OnInteraction(GameObject sender)
{
AppManager.Instance.server.AddData(new UpdateLoot("Antiradine"));
AppManager.Instance.server.DeleteObject(this);
}
} }
} }

View file

@ -15,15 +15,7 @@ namespace ZoFo.GameCore.GameObjects
public class BottleOfWater : Collectable public class BottleOfWater : Collectable
{ {
public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "BottleOfWater"); public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "BottleOfWater");
public BottleOfWater(Vector2 position) : base(position) public BottleOfWater(Vector2 position) : base(position) { }
{
graphicsComponent.ObjectDrawRectangle.Width = 20;
graphicsComponent.ObjectDrawRectangle.Height = 20;
}
public override void OnInteraction(GameObject sender)
{
AppManager.Instance.server.AddData(new UpdateLoot("BottleOfWater"));
AppManager.Instance.server.DeleteObject(this);
}
} }
} }

View file

@ -1,21 +1,44 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System; using System;
using ZoFo.GameCore.GameManagers; using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.CollisionManager; using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.GUI;
namespace ZoFo.GameCore.GameObjects; namespace ZoFo.GameCore.GameObjects;
public class Collectable : Interactable public class Collectable : Interactable
{ {
protected static readonly string _path = "Textures/icons/Collectables/"; protected static readonly string _path = "Textures/icons/Collectables/";
public Collectable(Vector2 position) : base(position) public Collectable(Vector2 position) : base(position) {
{
graphicsComponent.ObjectDrawRectangle.Width = 20;
graphicsComponent.ObjectDrawRectangle.Height = 20;
collisionComponent.triggerRectangle.Width = 20;
collisionComponent.triggerRectangle.Height = 20;
int size = 10;
collisionComponent.triggerRectangle.X -= size;
collisionComponent.triggerRectangle.Y -= size;
collisionComponent.triggerRectangle.Width += 2*size;
collisionComponent.triggerRectangle.Height += 2*size;
} }
public override void OnInteraction(GameObject sender) public override void OnInteraction(GameObject sender)
{ {
// DebugHUD.DebugLog("collected");
AppManager.Instance.server.AddData(new UpdateLoot()); string lootname = this.GetType().ToString().ToLower();
(sender as Player).lootData.AddLoot(lootname, 1);
AppManager.Instance.server.AddData(new UpdateLoot(lootname));
AppManager.Instance.server.DeleteObject(this); AppManager.Instance.server.DeleteObject(this);
base.OnInteraction(sender);
} }
public override void Draw(SpriteBatch spriteBatch)
{
DrawDebugRectangle(spriteBatch, collisionComponent.triggerRectangle.SetOrigin(position), Color.Blue);
base.Draw(spriteBatch);
}
} }

View file

@ -16,15 +16,6 @@ namespace ZoFo.GameCore.GameObjects
{ {
public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "Peeble"); public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "Peeble");
public Peeble(Vector2 position) : base(position) public Peeble(Vector2 position) : base(position) { }
{
graphicsComponent.ObjectDrawRectangle.Width = 20;
graphicsComponent.ObjectDrawRectangle.Height = 20;
}
public override void OnInteraction(GameObject sender)
{
AppManager.Instance.server.AddData(new UpdateLoot("Peeble"));
AppManager.Instance.server.DeleteObject(this);
}
} }
} }

View file

@ -15,15 +15,6 @@ namespace ZoFo.GameCore.GameObjects
{ {
public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "PureBottleOfWater"); public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "PureBottleOfWater");
public PureBottleOfWater(Vector2 position) : base(position) public PureBottleOfWater(Vector2 position) : base(position) { }
{
graphicsComponent.ObjectDrawRectangle.Width = 20;
graphicsComponent.ObjectDrawRectangle.Height = 20;
}
public override void OnInteraction(GameObject sender)
{
AppManager.Instance.server.AddData(new UpdateLoot("PureBottleOfWater"));
AppManager.Instance.server.DeleteObject(this);
}
} }
} }

View file

@ -14,15 +14,6 @@ namespace ZoFo.GameCore.GameObjects
class RottenFlesh:Collectable class RottenFlesh:Collectable
{ {
public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "RottenFlesh"); public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "RottenFlesh");
public RottenFlesh(Vector2 position) : base(position) public RottenFlesh(Vector2 position) : base(position) { }
{
graphicsComponent.ObjectDrawRectangle.Width = 20;
graphicsComponent.ObjectDrawRectangle.Height = 20;
}
public override void OnInteraction(GameObject sender)
{
AppManager.Instance.server.AddData(new UpdateLoot("RottenFlesh"));
AppManager.Instance.server.DeleteObject(this);
}
} }
} }

View file

@ -15,15 +15,7 @@ namespace ZoFo.GameCore.GameObjects
{ {
public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "Steel"); public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "Steel");
public Steel(Vector2 position) : base(position) public Steel(Vector2 position) : base(position) { }
{
graphicsComponent.ObjectDrawRectangle.Width = 20;
graphicsComponent.ObjectDrawRectangle.Height = 20;
}
public override void OnInteraction(GameObject sender)
{
AppManager.Instance.server.AddData(new UpdateLoot("Steel"));
AppManager.Instance.server.DeleteObject(this);
}
} }
} }

View file

@ -11,14 +11,5 @@ public class Wood : Collectable
{ {
public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "Wood"); public override StaticGraphicsComponent graphicsComponent { get; } = new(_path + "Wood");
public Wood(Vector2 position) : base(position) public Wood(Vector2 position) : base(position) { }
{
graphicsComponent.ObjectDrawRectangle.Width = 20;
graphicsComponent.ObjectDrawRectangle.Height = 20;
}
public override void OnInteraction(GameObject sender)
{
AppManager.Instance.server.AddData(new UpdateLoot("Wood"));
AppManager.Instance.server.DeleteObject(this);
}
} }

View file

@ -4,6 +4,7 @@ using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
using ZoFo.GameCore.Graphics; using ZoFo.GameCore.Graphics;
using ZoFo.GameCore.GUI;
namespace ZoFo.GameCore.GameObjects; namespace ZoFo.GameCore.GameObjects;

View file

@ -29,7 +29,10 @@ namespace ZoFo.GameCore.GameObjects
StartAnimation("zombie_walk"); StartAnimation("zombie_walk");
collisionComponent.isTrigger = true; collisionComponent.isTrigger = true;
collisionComponent.hasCollision = true; collisionComponent.hasCollision = true;
(graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += EndAttack; (graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += (animationIdEnded)=>{
if (animationIdEnded == "zombie_attack")
EndAttack(animationIdEnded);
};
collisionComponent.OnTriggerZone += OnPlayerClose; collisionComponent.OnTriggerZone += OnPlayerClose;
collisionComponent.triggerRectangle = new Rectangle(-5, -5, 40, 40); collisionComponent.triggerRectangle = new Rectangle(-5, -5, 40, 40);
(graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += (str) => (graphicsComponent as AnimatedGraphicsComponent).actionOfAnimationEnd += (str) =>
@ -70,7 +73,8 @@ namespace ZoFo.GameCore.GameObjects
var damagedPlayers=AppManager.Instance.server.collisionManager.GetPlayersInZone(collisionComponent.triggerRectangle.SetOrigin(position)); var damagedPlayers=AppManager.Instance.server.collisionManager.GetPlayersInZone(collisionComponent.triggerRectangle.SetOrigin(position));
//TODO ДАМАЖИТЬ ИГРОКОВ В ЗОНЕ //TODO ДАМАЖИТЬ ИГРОКОВ В ЗОНЕ
if (damagedPlayers.Length>0) { DebugHUD.DebugLog("End of" + a); if (damagedPlayers.Length>0) { DebugHUD.DebugLog("End of" + a);
AppManager.Instance.client.AddShaking(2); foreach (var item in damagedPlayers)
item.TakeDamage(5);
} }
isAttacking = false; isAttacking = false;
} }

View file

@ -14,11 +14,9 @@ public class LivingEntity : Entity
/// </summary> /// </summary>
public Vector2 velocity; public Vector2 velocity;
private InputManager inputManager;
public LivingEntity(Vector2 position) : base(position) public LivingEntity(Vector2 position) : base(position)
{ {
inputManager = new InputManager();
collisionComponent.hasCollision = true; collisionComponent.hasCollision = true;
} }

View file

@ -12,6 +12,9 @@ namespace ZoFo.GameCore.GameObjects
public void AddLoot(string lootName, int quantity) public void AddLoot(string lootName, int quantity)
{ {
if (loots.ContainsKey(lootName))
loots[lootName] +=quantity;
else
loots.Add(lootName, quantity); loots.Add(lootName, quantity);
} }
} }

View file

@ -21,8 +21,11 @@ public class Player : LivingEntity
private float speed; private float speed;
public int reloading; public int reloading;
public int health = 100; public float health= 100;
public int rad = 0; public float MaxHealth = 100;
public float rad = 0;
public float MaxRad = 100;
public LootData lootData; public LootData lootData;
@ -99,6 +102,24 @@ public class Player : LivingEntity
{ {
IsTryingToInteract = true; IsTryingToInteract = true;
} }
#region MyRegion
public bool isDying;
public virtual void TakeDamage(float damage)
{
if (isDying) return;
health -= damage;
AppManager.Instance.server.AddData(new UpdatePlayerParametrs() { health = health, radiatoin = rad, IdEntity = Id });
if (health < 0)
Die();
}
public override void Die()
{
base.Die();
}
#endregion
public void HandleShoot(UpdateInputShoot updateInputShoot) public void HandleShoot(UpdateInputShoot updateInputShoot)
{ {
if (reloading > 0) if (reloading > 0)