Merge pull request #71 from progtime-net/TriggerZoneFeature
Physics: added triggers
This commit is contained in:
commit
d638fa585a
18 changed files with 112 additions and 41 deletions
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
{
|
{
|
||||||
|
if (component.hasCollision)
|
||||||
ObjectsWithCollisions.Add(component);
|
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
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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());
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ public class Player : LivingEntity
|
||||||
float t;
|
float t;
|
||||||
public void MovementLogic()
|
public void MovementLogic()
|
||||||
{
|
{
|
||||||
|
IsTryingToShoot = true;
|
||||||
StartAnimation("player_look_down");//gslkjfsnblkjsdfnnlkjbn;zkcjnb;kkjnzx;cjkb;kzjxb;kSErgo
|
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++;
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -58,9 +58,9 @@ 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;
|
||||||
|
|
|
@ -156,7 +156,7 @@ namespace ZoFo.GameCore
|
||||||
{
|
{
|
||||||
go.UpdateLogic();
|
go.UpdateLogic();
|
||||||
}
|
}
|
||||||
collisionManager.UpdatePositions();
|
collisionManager.ResolvePhysics();
|
||||||
ticks = 0;
|
ticks = 0;
|
||||||
networkManager.SendData();
|
networkManager.SendData();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue