feat: add hero and body deal damage

This commit is contained in:
Bragin Stepan
2026-02-21 18:23:43 +05:00
parent 6e04b47ac2
commit dfd7b5ccf3
26 changed files with 977 additions and 225 deletions

View File

@@ -27,16 +27,13 @@ namespace _Project.Develop.Runtime.Entities
_playerInput = container.Resolve<IPlayerInput>();
}
public Entity CreateGhostEntity(Vector3 position)
public Entity CreateHeroEntity(Vector3 position)
{
Entity entity = CreateEmpty();
_monoEntitiesFactory.Create(entity, position, PathToResources.Entity.Ghost);
_monoEntitiesFactory.Create(entity, position, PathToResources.Entity.Hero);
entity
.AddContactsDetectingMask(Layers.CharactersMask)
.AddContactCollidersBuffer(new Buffer<Collider>(64))
.AddContactEntitiesBuffer(new Buffer<Entity>(64))
.AddMoveDirection()
.AddRotateDirection()
.AddMoveSpeed(new ReactiveVariable<float>(10))
@@ -75,15 +72,79 @@ namespace _Project.Develop.Runtime.Entities
.AddMustSelfRelease(mustSelfRelease);
entity
.AddSystem(new MoveDirectionByInputSystem(_playerInput))
.AddSystem(new RotateDirectionByMoveInputSystem(_playerInput))
.AddSystem(new RigidbodyMovementSystem())
.AddSystem(new RigidbodyRotationSystem())
.AddSystem(new ApplyDamageSystem())
.AddSystem(new DeathSwitcherSystem())
.AddSystem(new DeathProcessTimerSystem())
.AddSystem(new DisableCollidersOnDeathSystem())
.AddSystem(new SelfReleaseSystem(_entitiesLifeContext));
_entitiesLifeContext.Add(entity);
return entity;
}
public Entity CreateGhostEntity(Vector3 position)
{
Entity entity = CreateEmpty();
_monoEntitiesFactory.Create(entity, position, PathToResources.Entity.Ghost);
entity
.AddContactsDetectingMask(Layers.CharactersMask)
.AddContactCollidersBuffer(new Buffer<Collider>(64))
.AddContactEntitiesBuffer(new Buffer<Entity>(64))
.AddMoveDirection()
.AddRotateDirection()
.AddMoveSpeed(new ReactiveVariable<float>(10))
.AddRotationSpeed(new ReactiveVariable<float>(800))
.AddMaxHealth(new ReactiveVariable<float>(150))
.AddCurrentHealth(new ReactiveVariable<float>(150))
.AddBodyContactDamage(new ReactiveVariable<float>(50))
.AddTakeDamageRequest()
.AddTakeDamageEvent()
.AddIsDead()
.AddIsMoving()
.AddInDeathProcess()
.AddDeathProcessInitialTime(new ReactiveVariable<float>(2))
.AddDeathProcessCurrentTime();
ICompositeCondition canMove = new CompositeCondition()
.Add(new FuncCondition(() => entity.IsDead.Value == false));
ICompositeCondition canRotate = new CompositeCondition()
.Add(new FuncCondition(() => entity.IsDead.Value == false));
ICompositeCondition mustDie = new CompositeCondition()
.Add(new FuncCondition(() => entity.CurrentHealth.Value <= 0));
ICompositeCondition mustSelfRelease = new CompositeCondition()
.Add(new FuncCondition(() => entity.IsDead.Value))
.Add(new FuncCondition(() => entity.InDeathProcess.Value == false));
ICompositeCondition canApplyDamage = new CompositeCondition()
.Add(new FuncCondition(() => entity.IsDead.Value == false));
entity
.AddCanMove(canMove)
.AddCanRotate(canRotate)
.AddCanApplyDamage(canApplyDamage)
.AddMustDie(mustDie)
.AddMustSelfRelease(mustSelfRelease);
entity
.AddSystem(new BodyContactsDetectingSystem())
.AddSystem(new BodyContactsEntitiesFilterSystem(_collidersRegistryService))
.AddSystem(new MoveDirectionByInputSystem(_playerInput))
.AddSystem(new RotateDirectionByInputSystem(_playerInput))
.AddSystem(new DealDamageOnContactSystem())
.AddSystem(new ApplyDamageSystem())
.AddSystem(new RigidbodyMovementSystem())
.AddSystem(new RigidbodyRotationSystem())
.AddSystem(new DeathSwitcherSystem())
.AddSystem(new DeathProcessTimerSystem())
.AddSystem(new DisableCollidersOnDeathSystem())
.AddSystem(new SelfReleaseSystem(_entitiesLifeContext));
_entitiesLifeContext.Add(entity);

View File

@@ -403,6 +403,30 @@ namespace _Project.Develop.Runtime.Entities
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime.InDeathProcess() {Value = value});
}
public _Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime.DisableCollidersOnDeath DisableCollidersOnDeathC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime.DisableCollidersOnDeath>();
public System.Collections.Generic.List<UnityEngine.Collider> DisableCollidersOnDeath => DisableCollidersOnDeathC.Value;
public bool TryGetDisableCollidersOnDeath(out System.Collections.Generic.List<UnityEngine.Collider> value)
{
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime.DisableCollidersOnDeath component);
if(result)
value = component.Value;
else
value = default(System.Collections.Generic.List<UnityEngine.Collider>);
return result;
}
public _Project.Develop.Runtime.Entities.Entity AddDisableCollidersOnDeath()
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime.DisableCollidersOnDeath() { Value = new System.Collections.Generic.List<UnityEngine.Collider>() });
}
public _Project.Develop.Runtime.Entities.Entity AddDisableCollidersOnDeath(System.Collections.Generic.List<UnityEngine.Collider> value)
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime.DisableCollidersOnDeath() {Value = value});
}
public _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveDirection MoveDirectionC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveDirection>();
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3> MoveDirection => MoveDirectionC.Value;
@@ -671,5 +695,29 @@ namespace _Project.Develop.Runtime.Entities
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Damage.CanApplyDamage() {Value = value});
}
public _Project.Develop.Runtime.Logic.Gameplay.Features.Damage.BodyContactDamage BodyContactDamageC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Damage.BodyContactDamage>();
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> BodyContactDamage => BodyContactDamageC.Value;
public bool TryGetBodyContactDamage(out _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
{
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Damage.BodyContactDamage component);
if(result)
value = component.Value;
else
value = default(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>);
return result;
}
public _Project.Develop.Runtime.Entities.Entity AddBodyContactDamage()
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Damage.BodyContactDamage() { Value = new _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>() });
}
public _Project.Develop.Runtime.Entities.Entity AddBodyContactDamage(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Damage.BodyContactDamage() {Value = value});
}
}
}

View File

@@ -1,5 +1,6 @@
using _Project.Develop.Runtime.Entities;
using _Project.Develop.Runtime.Utilities.Conditions;
using _Project.Develop.Runtime.Utils.ReactiveManagement;
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Damage
@@ -9,4 +10,6 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Damage
public class TakeDamageEvent : IEntityComponent { public ReactiveEvent<float> Value; }
public class CanApplyDamage : IEntityComponent { public ICompositeCondition Value; }
public class BodyContactDamage : IEntityComponent { public ReactiveVariable<float> Value; }
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 184b10356b354fbb9d4ec3e182cbe8e5
timeCreated: 1771676819

View File

@@ -3,6 +3,7 @@ using _Project.Develop.Runtime.Entities;
using _Project.Develop.Runtime.Utilities.Conditions;
using _Project.Develop.Runtime.Utils.ReactiveManagement;
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
using UnityEngine;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Damage
{

View File

@@ -0,0 +1,56 @@
using System.Collections.Generic;
using _Project.Develop.Runtime.Entities;
using _Project.Develop.Runtime.Utilities;
using _Project.Develop.Runtime.Utils.ReactiveManagement;
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
using UnityEngine;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Damage
{
public class DealDamageOnContactSystem: IInitializableSystem, IUpdatableSystem
{
private Entity _entity;
private Buffer<Entity> _contacts;
private ReactiveVariable<float> _damage;
private List<Entity> _processedEntities;
public void OnInit(Entity entity)
{
_entity = entity;
_contacts = entity.ContactEntitiesBuffer;
_damage = entity.BodyContactDamage;
_processedEntities = new List<Entity>(_contacts.Items.Length);
}
public void OnUpdate(float deltaTime)
{
for (int i = 0; i < _contacts.Count; i++)
{
Entity contactEntity = _contacts.Items[i];
if(_processedEntities.Contains(contactEntity) == false)
{
_processedEntities.Add(contactEntity);
if (contactEntity.TryGetTakeDamageRequest(out ReactiveEvent<float> takeDamageRequest))
takeDamageRequest.Invoke(_damage.Value);
}
}
for (int i = _processedEntities.Count - 1; i >= 0; i--)
if (ContainInContacts(_processedEntities[i]) == false)
_processedEntities.RemoveAt(i);
}
private bool ContainInContacts(Entity entity)
{
for (int i = 0; i < _contacts.Count; i++)
if (_contacts.Items[i] == entity)
return true;
return false;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b87d3d31c8d940df81767830621e290f
timeCreated: 1771676878

View File

@@ -5,13 +5,13 @@ using UnityEngine;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Movement
{
public class RotateDirectionByInputSystem : IInitializableSystem, IUpdatableSystem
public class RotateDirectionByMoveInputSystem : IInitializableSystem, IUpdatableSystem
{
private readonly IPlayerInput _playerInput;
private ReactiveVariable<Vector3> _rotateDirection;
public RotateDirectionByInputSystem(IPlayerInput playerInput)
public RotateDirectionByMoveInputSystem(IPlayerInput playerInput)
{
_playerInput = playerInput;
}

View File

@@ -0,0 +1,16 @@
using System.Collections.Generic;
using _Project.Develop.Runtime.Entities;
using UnityEngine;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime
{
public class DisableCollidersOnDeathRegistrator : MonoEntityRegistrator
{
[SerializeField] private List<Collider> _colliders;
public override void Register(Entity entity)
{
entity.AddDisableCollidersOnDeath(_colliders);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 801746c26fe0478e80182979f64c22f2
timeCreated: 1771677582

View File

@@ -1,6 +1,8 @@
using _Project.Develop.Runtime.Entities;
using System.Collections.Generic;
using _Project.Develop.Runtime.Entities;
using _Project.Develop.Runtime.Utilities.Conditions;
using _Project.Develop.Runtime.Utils.ReactiveManagement;
using UnityEngine;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime
{
@@ -14,4 +16,6 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime
public class DeathProcessInitialTime : IEntityComponent { public ReactiveVariable<float> Value; }
public class DeathProcessCurrentTime : IEntityComponent { public ReactiveVariable<float> Value; }
public class InDeathProcess : IEntityComponent { public ReactiveVariable<bool> Value; }
public class DisableCollidersOnDeath : IEntityComponent { public List<Collider> Value; }
}

View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using _Project.Develop.Runtime.Entities;
using _Project.Develop.Runtime.Utils.ReactiveManagement;
using UnityEngine;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Lifetime.Systems
{
public class DisableCollidersOnDeathSystem : IInitializableSystem, IDisposableSystem
{
private List<Collider> _colliders;
private ReactiveVariable<bool> _isDead;
private IDisposable _isDeadChangedDisposable;
public void OnInit(Entity entity)
{
_colliders = entity.DisableCollidersOnDeath;
_isDead = entity.IsDead;
_isDeadChangedDisposable = _isDead.Subscribe(OnIsDeadChanged);
}
private void OnIsDeadChanged(bool arg1, bool isDead)
{
if (isDead)
foreach (Collider collider in _colliders)
collider.enabled = false;
}
public void OnDispose()
{
_isDeadChangedDisposable.Dispose();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 79df8b8c7f584c9aafa1e98fea894da6
timeCreated: 1771677719

View File

@@ -3,7 +3,7 @@ using UnityEngine;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Sensors
{
public class CapsuleColliderRegistratorEntityRegistrator : MonoEntityRegistrator
public class CapsuleColliderRegistrator : MonoEntityRegistrator
{
[SerializeField] private CapsuleCollider _collider;

View File

@@ -10,8 +10,6 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features
private DIContainer _container;
private EntitiesFactory _entitiesFactory;
private Entity _entity;
private bool _isRunning;
public void Initialize(DIContainer container)
@@ -24,7 +22,8 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features
public void Run()
{
_entity = _entitiesFactory.CreateGhostEntity(Vector3.zero);
_entitiesFactory.CreateGhostEntity(Vector3.zero + Vector3.forward * 5);
_entitiesFactory.CreateHeroEntity(Vector3.zero);
_isRunning = true;
}