Merge pull request #71 from progtime-net/TriggerZoneFeature

Physics: added triggers
This commit is contained in:
SergoDobro 2024-08-18 20:02:27 +03:00 committed by GitHub
commit d638fa585a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 112 additions and 41 deletions

View file

@ -51,7 +51,7 @@ namespace ZoFo.GameCore
{ {
InputMovementDirection = AppManager.Instance.InputManager.InputMovementDirection, InputMovementDirection = AppManager.Instance.InputManager.InputMovementDirection,
InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection
}); });
}; };
} }
@ -166,7 +166,7 @@ namespace ZoFo.GameCore
else if (update is UpdatePosition) else if (update is UpdatePosition)
{ {
var ent = FindEntityById(update.IdEntity); var ent = FindEntityById(update.IdEntity);
ent.position = (update as UpdatePosition).NewPosition; ent.position = (update as UpdatePosition).NewPosition;
} }
else if (update is UpdateAnimation) else if (update is UpdateAnimation)
@ -174,7 +174,7 @@ namespace ZoFo.GameCore
var ent = FindEntityById(update.IdEntity); var ent = FindEntityById(update.IdEntity);
((ent as Entity).graphicsComponent as AnimatedGraphicsComponent).StartAnimation((update as UpdateAnimation).animationId); ((ent as Entity).graphicsComponent as AnimatedGraphicsComponent).StartAnimation((update as UpdateAnimation).animationId);
DebugHUD.Instance.Log("new Animation " + ent.position); DebugHUD.Instance.Log("new Animation " + ent.position);
} }
} }

View file

@ -62,6 +62,10 @@ public class DebugHUD
{ {
_text[key] = value; _text[key] = value;
} }
public static void DebugSet(string key, string value)
{
Instance._text[key] = value;
}
public void Log(string value) public void Log(string value)
{ {
@ -71,4 +75,12 @@ public class DebugHUD
_log.RemoveAt(0); _log.RemoveAt(0);
} }
} }
public static void DebugLog(string value)
{
Instance._log.Add(value);
if (Instance._log.Count > 30)
{
Instance._log.RemoveAt(0);
}
}
} }

View file

@ -43,12 +43,11 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
public GameObject gameObject { get; set; } public GameObject gameObject { get; set; }
bool doesStop; public bool hasCollision;
bool hasCollision;
public Rectangle stopRectangle; public Rectangle stopRectangle;
// triggers for rectangle // triggers for rectangle
bool isTrigger; public bool isTrigger;
public Rectangle triggerRectangle; public Rectangle triggerRectangle;
//delegate //delegate
@ -58,13 +57,21 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
//events DoorInteraction //events DoorInteraction
public event EventHandler<CollisionComponent> OnTriggerEnter; public delegate void CollisionAction(Player player);
public event EventHandler<CollisionComponent> OnTriggerZone; public event CollisionAction? OnTriggerEnter;
public event EventHandler<CollisionComponent> OnTriggerExit; public event CollisionAction? OnTriggerZone;
public event CollisionAction? OnTriggerExit;
public event EventHandler<CollisionComponent> OnCollision;
public delegate void CoollisionEvent(GameObject gameObject);
public event CoollisionEvent? OnCollision;
public void PlayerInZone(Player player) => OnTriggerZone?.Invoke(player);
public void PlayerEnter(Player player) => OnTriggerEnter?.Invoke(player);
public void PlayerExit(Player player) => OnTriggerExit?.Invoke(player);
public void OnCollisionWithObject(GameObject gameObject) => OnCollision?.Invoke(gameObject);
} }
} }

View file

@ -12,6 +12,7 @@ using ZoFo.GameCore.GameObjects.Entities;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities; using ZoFo.GameCore.GameObjects.Entities.LivingEntities;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.Graphics; using ZoFo.GameCore.Graphics;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
namespace ZoFo.GameCore.GameManagers.CollisionManager namespace ZoFo.GameCore.GameManagers.CollisionManager
{ {
@ -53,8 +54,9 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
{ {
collidedX = true;// меняем значение соприкосновения на true collidedX = true;// меняем значение соприкосновения на true
entity.OnCollision(item);//подписываем entity на ивент коллизии //entity.OnCollision(item);//подписываем entity на ивент коллизии
item.OnCollisionWithObject(entity);
entity.collisionComponent.OnCollisionWithObject(item.gameObject);
break;// выход break;// выход
} }
} }
@ -109,35 +111,72 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager
entity.velocity = Vector2.Zero; entity.velocity = Vector2.Zero;
} }
public void UpdateTriggerZones(Player player)
{
var entity = player as LivingEntity;
var currentRect = entity.collisionComponent.stopRectangle;//задаём РЕК
currentRect.X += (int)entity.position.X;
currentRect.Y += (int)entity.position.Y;
foreach (var item in ObjectsWithTriggers)//фильтрация
{
if (item.triggerRectangle.SetOrigin(item.gameObject.position).Intersects(currentRect))
{
item.PlayerInZone(player);
}
}
}
//обновление позиции объекта //обновление позиции объекта
public void UpdatePositions() public void ResolvePhysics()
{ {
foreach (var item in EntitiesWithMovements) foreach (var item in EntitiesWithMovements)
{ {
CheckComponentCollision(item); CheckComponentCollision(item);
} }
foreach (var item in AppManager.Instance.server.players)
{
UpdateTriggerZones(item);
}
} }
public CollisionManager() public CollisionManager()
{ {
//graphicsComponent
//.ObjectDrawRectangle = new Rectangle(0, 0, 16 * 12, 16 * 16);
EntitiesWithMovements = new List<CollisionComponent>(); EntitiesWithMovements = new List<CollisionComponent>();
ObjectsWithCollisions = new List<CollisionComponent>(); ObjectsWithCollisions = new List<CollisionComponent>();
ObjectsWithTriggers = new List<CollisionComponent>();
} }
//регистрация компонента(его коллизии) //регистрация компонента(его коллизии)
public void Register(CollisionComponent component) public void Register(CollisionComponent component)
{ {
ObjectsWithCollisions.Add(component); if (component.hasCollision)
ObjectsWithCollisions.Add(component);
if (component.isTrigger)
ObjectsWithTriggers.Add(component);
if (component.gameObject is LivingEntity) if (component.gameObject is LivingEntity)
{ {
EntitiesWithMovements.Add(component); EntitiesWithMovements.Add(component);
} }
} }
public Player[] GetPlayersInZone(Rectangle rectangle)
{
List<Player> players = new List<Player>();
foreach (var item in AppManager.Instance.server.players)//фильтрация
{
if (item.collisionComponent.stopRectangle.SetOrigin(item.position).Intersects(rectangle))
{
players.Add(item);
}
}
return players.ToArray();
}
} }
public static class ExtentionClass public static class ExtentionClass

View file

@ -8,6 +8,8 @@ using ZoFo.GameCore.GameManagers.CollisionManager;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.GameManagers; using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.Graphics; using ZoFo.GameCore.Graphics;
using Microsoft.Xna.Framework.Graphics;
using ZoFo.GameCore.GUI;
namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
@ -18,11 +20,19 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
graphicsComponent.ObjectDrawRectangle.Width = 20; graphicsComponent.ObjectDrawRectangle.Width = 20;
graphicsComponent.ObjectDrawRectangle.Height = 20; graphicsComponent.ObjectDrawRectangle.Height = 20;
collisionComponent.triggerRectangle = new Rectangle(0, 0, 20, 20);
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
DebugHUD.DebugLog("collected");
AppManager.Instance.server.AddData(new UpdateLoot("Ammo")); AppManager.Instance.server.AddData(new UpdateLoot("Ammo"));
AppManager.Instance.server.DeleteObject(this); AppManager.Instance.server.DeleteObject(this);
} }
public override void Draw(SpriteBatch spriteBatch)
{
DrawDebugRectangle(spriteBatch, collisionComponent.triggerRectangle.SetOrigin(position), Color.Blue);
base.Draw(spriteBatch);
}
} }
} }

View file

@ -18,7 +18,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
AppManager.Instance.server.AddData(new UpdateLoot("Antiradine")); AppManager.Instance.server.AddData(new UpdateLoot("Antiradine"));
AppManager.Instance.server.DeleteObject(this); AppManager.Instance.server.DeleteObject(this);

View file

@ -19,7 +19,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
AppManager.Instance.server.AddData(new UpdateLoot("BottleOfWater")); AppManager.Instance.server.AddData(new UpdateLoot("BottleOfWater"));
AppManager.Instance.server.DeleteObject(this); AppManager.Instance.server.DeleteObject(this);

View file

@ -11,7 +11,7 @@ public class Collectable : Interactable
{ {
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
// //
AppManager.Instance.server.AddData(new UpdateLoot()); AppManager.Instance.server.AddData(new UpdateLoot());

View file

@ -20,7 +20,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
AppManager.Instance.server.AddData(new UpdateLoot("Peeble")); AppManager.Instance.server.AddData(new UpdateLoot("Peeble"));
AppManager.Instance.server.DeleteObject(this); AppManager.Instance.server.DeleteObject(this);

View file

@ -18,7 +18,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
public PureBottleOfWater(Vector2 position) : base(position) public PureBottleOfWater(Vector2 position) : base(position)
{ {
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
AppManager.Instance.server.AddData(new UpdateLoot("PureBottleOfWater")); AppManager.Instance.server.AddData(new UpdateLoot("PureBottleOfWater"));
AppManager.Instance.server.DeleteObject(this); AppManager.Instance.server.DeleteObject(this);

View file

@ -18,7 +18,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
{ {
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
AppManager.Instance.server.AddData(new UpdateLoot("RottenFlesh")); AppManager.Instance.server.AddData(new UpdateLoot("RottenFlesh"));
AppManager.Instance.server.DeleteObject(this); AppManager.Instance.server.DeleteObject(this);

View file

@ -18,7 +18,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables
public Steel(Vector2 position) : base(position) public Steel(Vector2 position) : base(position)
{ {
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
AppManager.Instance.server.AddData(new UpdateLoot("Steel")); AppManager.Instance.server.AddData(new UpdateLoot("Steel"));
AppManager.Instance.server.DeleteObject(this); AppManager.Instance.server.DeleteObject(this);

View file

@ -15,7 +15,7 @@ public class Wood : Collectable
{ {
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
AppManager.Instance.server.AddData(new UpdateLoot("Wood")); AppManager.Instance.server.AddData(new UpdateLoot("Wood"));
AppManager.Instance.server.DeleteObject(this); AppManager.Instance.server.DeleteObject(this);

View file

@ -16,7 +16,7 @@ public class Door : Interactable
//graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - SD //graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; };//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> - SD
} }
public override void OnInteraction(object sender, CollisionComponent e) public override void OnInteraction(GameObject sender)
{ {
//graphicsComponent.AnimationSelect("DoorInteraction", isOpened); //graphicsComponent.AnimationSelect("DoorInteraction", isOpened);
//graphicsComponent.AnimationStep(); //graphicsComponent.AnimationStep();

View file

@ -13,17 +13,19 @@ public class Interactable : Entity
public Interactable(Vector2 position) : base(position) public Interactable(Vector2 position) : base(position)
{ {
collisionComponent.OnTriggerEnter += (sender, e) => ChangeInteraction(sender, e, true); collisionComponent.isTrigger = true;
collisionComponent.OnTriggerExit += (sender, e) => ChangeInteraction(sender, e, false); collisionComponent.hasCollision = false;
collisionComponent.OnTriggerEnter += (sender) => ChangeInteraction(sender, true);
collisionComponent.OnTriggerExit += (sender) => ChangeInteraction(sender, false);
collisionComponent.OnTriggerZone += OnInteraction; collisionComponent.OnTriggerZone += OnInteraction;
} }
private void ChangeInteraction(object sender, CollisionComponent e, bool isReady) private void ChangeInteraction(GameObject sender, bool isReady)
{ {
AppManager.Instance.server.AddData(new UpdateInteractionReady((sender as Player).Id, isReady)); AppManager.Instance.server.AddData(new UpdateInteractionReady((sender as Player).Id, isReady));
} }
public virtual void OnInteraction(object sender, CollisionComponent e) public virtual void OnInteraction(GameObject sender)
{ {
} }

View file

@ -39,8 +39,9 @@ public class Player : LivingEntity
} }
float t; float t;
public void MovementLogic() public void MovementLogic()
{ {
StartAnimation("player_look_down");//gslkjfsnblkjsdfnnlkjbn;zkcjnb;kkjnzx;cjkb;kzjxb;kSErgo IsTryingToShoot = true;
StartAnimation("player_look_down");//gslkjfsnblkjsdfnnlkjbn;zkcjnb;kkjnzx;cjkb;kzjxb;kSErgo
//velocity.X = 3+(float)Math.Sin(t); //velocity.X = 3+(float)Math.Sin(t);
t++; t++;
if (InputPlayerRotation.X > 0.9) if (InputPlayerRotation.X > 0.9)

View file

@ -47,7 +47,7 @@ namespace ZoFo.GameCore.Graphics
public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch)
{ {
DebugHUD.Instance.Log("draw "); //DebugHUD.Instance.Log("draw ");
destinationRectangle.X -= CameraPosition.X; destinationRectangle.X -= CameraPosition.X;
destinationRectangle.Y -= CameraPosition.Y; destinationRectangle.Y -= CameraPosition.Y;
@ -57,10 +57,10 @@ namespace ZoFo.GameCore.Graphics
} }
public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle)
{ {
//DebugHUD.Instance.Log("draw ");
// Uncomment to go brrrr // Uncomment to go brrrr
//Rotation = new Random().Next(1, 365); //Rotation = new Random().Next(1, 365);
DebugHUD.Instance.Log("draw ");
destinationRectangle.X -= CameraPosition.X; destinationRectangle.X -= CameraPosition.X;
destinationRectangle.Y -= CameraPosition.Y; destinationRectangle.Y -= CameraPosition.Y;

View file

@ -156,7 +156,7 @@ namespace ZoFo.GameCore
{ {
go.UpdateLogic(); go.UpdateLogic();
} }
collisionManager.UpdatePositions(); collisionManager.ResolvePhysics();
ticks = 0; ticks = 0;
networkManager.SendData(); networkManager.SendData();
} }