feat: add random auto teleport state

This commit is contained in:
Bragin Stepan
2026-03-03 19:28:16 +05:00
parent c04b0a259a
commit 11e28b1e09
20 changed files with 350 additions and 28 deletions

View File

@@ -32,6 +32,7 @@ namespace Assets._Project.Develop.Runtime.Infrastructure.EntryPoint
container.RegisterAsSingle(CreateSceneSwitcherService);
container.RegisterAsSingle(CreateInputFactory);
container.RegisterAsSingle(CreateInputStateMachine);
container.RegisterAsSingle<IPlayerInput>(CreatePlayerInput);
container.RegisterAsSingle<IUIInput>(CreateUIInput);
@@ -65,6 +66,7 @@ namespace Assets._Project.Develop.Runtime.Infrastructure.EntryPoint
private static ResourcesAssetsLoader CreateResourcesAssetsLoader(DIContainer c) => new();
private static InputFactory CreateInputFactory(DIContainer c) => new();
private static InputStateMachine CreateInputStateMachine(DIContainer c) => new(c);
private static TimerServiceFactory CreateTimerServiceFactory(DIContainer c) => new (c);

View File

@@ -210,7 +210,7 @@ namespace _Project.Develop.Runtime.Entities
.AddMaxHealth(new ReactiveVariable<float>(150))
.AddCurrentHealth(new ReactiveVariable<float>(150))
.AddTeleportTarget(entity.Transform)
.AddTeleportSource(entity.Transform)
.AddTeleportToPoint(entity.Transform)
.AddStartTeleportEvent()
.AddStartTeleportRequest()
@@ -226,6 +226,10 @@ namespace _Project.Develop.Runtime.Entities
.AddTeleportEnergyCost(new ReactiveVariable<int>(20))
.AddTeleportSearchRadius(new ReactiveVariable<float>(6))
.AddTeleportCooldownInitialTime(new ReactiveVariable<float>(3))
.AddTeleportCooldownCurrentTime()
.AddInTeleportCooldown()
.AddCurrentEnergy(new ReactiveVariable<int>(60))
.AddMaxEnergy(new ReactiveVariable<int>(60))
.AddUseEnergyEvent()
@@ -253,6 +257,7 @@ namespace _Project.Develop.Runtime.Entities
ICompositeCondition canStartTeleport = new CompositeCondition()
.Add(new FuncCondition(() => entity.IsDead.Value == false))
.Add(new FuncCondition(() => entity.InTeleportCooldown.Value == false))
.Add(new FuncCondition(() => entity.CurrentEnergy.Value >= entity.TeleportEnergyCost.Value));
ICompositeCondition mustDie = new CompositeCondition()
@@ -286,6 +291,7 @@ namespace _Project.Develop.Runtime.Entities
.AddSystem(new FindRandomPointForTeleportSystem())
.AddSystem(new EndTeleportSystem())
.AddSystem(new InstantTeleportSystem())
.AddSystem(new TeleportCooldownTimerSystem())
.AddSystem(new DealDamageAfterTeleportSystem(_collidersRegistryService))
.AddSystem(new BodyContactsDetectingSystem())

View File

@@ -78,13 +78,13 @@ namespace _Project.Develop.Runtime.Entities
return AddComponent(new Assets._Project.Develop.Runtime.Gameplay.Common.NavMeshAgentComponent() {Value = value});
}
public _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportTarget TeleportTargetC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportTarget>();
public _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportSource TeleportSourceC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportSource>();
public UnityEngine.Transform TeleportTarget => TeleportTargetC.Value;
public UnityEngine.Transform TeleportSource => TeleportSourceC.Value;
public bool TryGetTeleportTarget(out UnityEngine.Transform value)
public bool TryGetTeleportSource(out UnityEngine.Transform value)
{
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportTarget component);
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportSource component);
if(result)
value = component.Value;
else
@@ -92,9 +92,9 @@ namespace _Project.Develop.Runtime.Entities
return result;
}
public _Project.Develop.Runtime.Entities.Entity AddTeleportTarget(UnityEngine.Transform value)
public _Project.Develop.Runtime.Entities.Entity AddTeleportSource(UnityEngine.Transform value)
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportTarget() {Value = value});
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportSource() {Value = value});
}
public _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportToPoint TeleportToPointC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportToPoint>();
@@ -327,6 +327,78 @@ namespace _Project.Develop.Runtime.Entities
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportEnergyCost() {Value = value});
}
public _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownInitialTime TeleportCooldownInitialTimeC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownInitialTime>();
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> TeleportCooldownInitialTime => TeleportCooldownInitialTimeC.Value;
public bool TryGetTeleportCooldownInitialTime(out _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
{
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownInitialTime 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 AddTeleportCooldownInitialTime()
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownInitialTime() { Value = new _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>() });
}
public _Project.Develop.Runtime.Entities.Entity AddTeleportCooldownInitialTime(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownInitialTime() {Value = value});
}
public _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownCurrentTime TeleportCooldownCurrentTimeC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownCurrentTime>();
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> TeleportCooldownCurrentTime => TeleportCooldownCurrentTimeC.Value;
public bool TryGetTeleportCooldownCurrentTime(out _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
{
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownCurrentTime 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 AddTeleportCooldownCurrentTime()
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownCurrentTime() { Value = new _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>() });
}
public _Project.Develop.Runtime.Entities.Entity AddTeleportCooldownCurrentTime(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportCooldownCurrentTime() {Value = value});
}
public _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.InTeleportCooldown InTeleportCooldownC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.InTeleportCooldown>();
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Boolean> InTeleportCooldown => InTeleportCooldownC.Value;
public bool TryGetInTeleportCooldown(out _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Boolean> value)
{
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.InTeleportCooldown component);
if(result)
value = component.Value;
else
value = default(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Boolean>);
return result;
}
public _Project.Develop.Runtime.Entities.Entity AddInTeleportCooldown()
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.InTeleportCooldown() { Value = new _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Boolean>() });
}
public _Project.Develop.Runtime.Entities.Entity AddInTeleportCooldown(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Boolean> value)
{
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.InTeleportCooldown() {Value = value});
}
public _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportDamage TeleportDamageC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.TeleportDamage>();
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> TeleportDamage => TeleportDamageC.Value;

View File

@@ -13,8 +13,6 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.AI
{
public class BrainsFactory
{
private readonly DIContainer _container;
private readonly EntitiesLifeContext _entitiesLifeContext;
private readonly AIBrainsContext _aiBrainsContext;
private readonly TimerServiceFactory _timerServiceFactory;
@@ -22,12 +20,10 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.AI
public BrainsFactory(DIContainer container)
{
_container = container;
_playerInput = _container.Resolve<IPlayerInput>();
_aiBrainsContext = _container.Resolve<AIBrainsContext>();
_timerServiceFactory = _container.Resolve<TimerServiceFactory>();
_entitiesLifeContext = _container.Resolve<EntitiesLifeContext>();
_playerInput = container.Resolve<IPlayerInput>();
_aiBrainsContext = container.Resolve<AIBrainsContext>();
_timerServiceFactory = container.Resolve<TimerServiceFactory>();
_entitiesLifeContext = container.Resolve<EntitiesLifeContext>();
}
public StateMachineBrain CreateGhostBrain(Entity entity)
@@ -40,6 +36,21 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.AI
return brain;
}
public StateMachineBrain CreateWizardBrain(Entity entity)
{
AIStateMachine stateMachine = CreateRandomTeleportStateMachine(entity);
StateMachineBrain brain = new (stateMachine);
_aiBrainsContext.SetFor(entity, brain);
return brain;
}
public StateMachineBrain CreateDangerWizardBrain(Entity entity)
{
return null;
}
public StateMachineBrain CreateMainHeroBrain(Entity entity, ITargetSelector targetSelector)
{
AIStateMachine combatState = CreateAutoAttackStateMachine(entity);
@@ -146,5 +157,34 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.AI
return stateMachine;
}
private AIStateMachine CreateRandomTeleportStateMachine(Entity entity)
{
TeleportTriggerState teleportTriggerState = new (entity);
EmptyState emptyState = new ();
ICompositeCondition fromIdleToTeleportCondition = new CompositeCondition()
.Add(entity.CanStartTeleport);
ICompositeCondition fromTeleportToIdleCondition = new CompositeCondition()
.Add(new FuncCondition(() => entity.InTeleportProcess.Value == false));
AIStateMachine stateMachine = new ();
stateMachine.AddState(teleportTriggerState);
stateMachine.AddState(emptyState);
stateMachine.AddTransition(emptyState, teleportTriggerState, fromIdleToTeleportCondition);
stateMachine.AddTransition(teleportTriggerState, emptyState, fromTeleportToIdleCondition);
return stateMachine;
}
private AIStateMachine CreateToTargetTeleportStateMachine(Entity entity)
{
TeleportTriggerState teleportTriggerState = new (entity);
return null;
}
}
}

View File

@@ -6,17 +6,17 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.AI.States
{
public class AttackTriggerState : State, IUpdatableState
{
private ReactiveEvent _attackRequest;
private ReactiveEvent _request;
public AttackTriggerState(Entity entity)
{
_attackRequest = entity.StartAttackRequest;
_request = entity.StartAttackRequest;
}
public override void Enter()
{
base.Enter();
_attackRequest.Invoke();
_request.Invoke();
}
public void Update(float deltaTime)

View File

@@ -0,0 +1,25 @@
using _Project.Develop.Runtime.Entities;
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
using Assets._Project.Develop.Runtime.Utilities.StateMachineCore;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.AI.States
{
public class TeleportTriggerState : State, IUpdatableState
{
private ReactiveEvent _request;
public TeleportTriggerState(Entity entity)
{
_request = entity.StartTeleportRequest;
}
public override void Enter()
{
base.Enter();
_request.Invoke();
}
public void Update(float deltaTime)
{ }
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 399ae3c830b94229a8b94350eca901d2
timeCreated: 1772543966

View File

@@ -9,7 +9,7 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.Systems
{
public class FindRandomPointForTeleportSystem : IInitializableSystem, IDisposableSystem
{
private Transform _target;
private Transform _source;
private Transform _toPoint;
private ReactiveEvent _findPointRequest;
@@ -21,7 +21,7 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.Systems
public void OnInit(Entity entity)
{
_target = entity.TeleportTarget;
_source = entity.TeleportSource;
_toPoint = entity.TeleportToPoint;
_radius = entity.TeleportSearchRadius;
_findPointRequest = entity.FindTeleportPointRequest;
@@ -37,7 +37,7 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.Systems
private void OnFindPointRequest()
{
_toPoint.position = GetRandomPointInRadius(_target.position, _radius.Value);
_toPoint.position = GetRandomPointInRadius(_source.position, _radius.Value);
_findPointEvent.Invoke();
}

View File

@@ -16,7 +16,7 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.Systems
public void OnInit(Entity entity)
{
_target = entity.TeleportTarget;
_target = entity.TeleportSource;
_toPoint = entity.TeleportToPoint;
_endTeleportEvent = entity.EndTeleportEvent;

View File

@@ -0,0 +1,52 @@
using System;
using _Project.Develop.Runtime.Entities;
using _Project.Develop.Runtime.Utils.ReactiveManagement;
using _Project.Develop.Runtime.Utils.ReactiveManagement.Event;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport.Systems
{
public class TeleportCooldownTimerSystem : IInitializableSystem, IUpdatableSystem, IDisposableSystem
{
private ReactiveVariable<float> _currentTime;
private ReactiveVariable<float> _initialTime;
private ReactiveVariable<bool> _inTeleportCooldown;
private ReactiveEvent _endTeleportEvent;
private IDisposable _endTeleportEventDisposable;
public void OnInit(Entity entity)
{
_currentTime = entity.TeleportCooldownCurrentTime;
_initialTime = entity.TeleportCooldownInitialTime;
_inTeleportCooldown = entity.InTeleportCooldown;
_endTeleportEvent = entity.EndTeleportEvent;
_endTeleportEventDisposable = _endTeleportEvent.Subscribe(OnEndTeleport);
}
private void OnEndTeleport()
{
_currentTime.Value = _initialTime.Value;
_inTeleportCooldown.Value = true;
}
public void OnUpdate(float deltaTime)
{
if (_inTeleportCooldown.Value == false)
return;
_currentTime.Value -= deltaTime;
if (CooldownIsOver())
_inTeleportCooldown.Value = false;
}
private bool CooldownIsOver() => _currentTime.Value <= 0;
public void OnDispose()
{
_endTeleportEventDisposable.Dispose();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 203bcf3a28764fb7bfac1482c48bad2f
timeCreated: 1772546509

View File

@@ -6,7 +6,7 @@ using UnityEngine;
namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport
{
public class TeleportTarget : IEntityComponent { public Transform Value; }
public class TeleportSource : IEntityComponent { public Transform Value; }
public class TeleportToPoint : IEntityComponent { public Transform Value; }
public class TeleportSearchRadius : IEntityComponent { public ReactiveVariable<float> Value; }
@@ -23,6 +23,10 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features.Teleport
public class TeleportEnergyCost : IEntityComponent { public ReactiveVariable<int> Value; }
public class TeleportCooldownInitialTime : IEntityComponent { public ReactiveVariable<float> Value; }
public class TeleportCooldownCurrentTime : IEntityComponent { public ReactiveVariable<float> Value; }
public class InTeleportCooldown : IEntityComponent { public ReactiveVariable<bool> Value; }
public class TeleportDamage : IEntityComponent { public ReactiveVariable<float> Value; }
public class TeleportDamageRadius : IEntityComponent { public ReactiveVariable<float> Value; }
public class TeleportDamageMask : IEntityComponent { public LayerMask Value; }

View File

@@ -15,7 +15,7 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features
private BrainsFactory _brainsFactory;
private Entity _hero;
private Entity _ghost;
private Entity _enemy;
private bool _isRunning;
@@ -34,8 +34,8 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features
_hero.AddCurrentTarget();
_brainsFactory.CreateMainHeroBrain(_hero, new NearestDamageableTargetSelector(_hero));
_ghost = _entitiesFactory.CreateGhost(Vector3.zero + Vector3.forward * 5);
_brainsFactory.CreateGhostBrain(_ghost);
_enemy = _entitiesFactory.CreateTeleportWizard(Vector3.zero + Vector3.forward * 5);
_brainsFactory.CreateWizardBrain(_enemy);
_isRunning = true;
}
@@ -48,11 +48,11 @@ namespace _Project.Develop.Runtime.Logic.Gameplay.Features
private void OnGUI()
{
if (_hero == null)
if (_hero == null || _enemy == null)
return;
GUI.Label(new Rect(10, 20, 200, 50), $"Health: {_hero.CurrentHealth.Value}/{_hero.MaxHealth.Value}");
// GUI.Label(new Rect(10, 40, 200, 50), $"Energy: {_hero.CurrentEnergy.Value}/{_hero.MaxEnergy.Value}");
GUI.Label(new Rect(10, 40, 200, 50), $"Energy: {_enemy.CurrentEnergy.Value}/{_enemy.MaxEnergy.Value}");
}
}
}

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using _Project.Develop.Runtime.Utilities.Conditions;
using _Project.Develop.Runtime.Utils.InputManagement.States;
using Assets._Project.Develop.Runtime.Infrastructure.DI;
using Assets._Project.Develop.Runtime.Utilities.StateMachineCore;
namespace _Project.Develop.Runtime.Utils.InputManagement
{
public class InputStateMachine : StateMachine<IState>, IInitializable, IDisposable
{
private readonly IPlayerInput _playerInput;
private readonly IUIInput _uiInput;
public InputStateMachine(List<IDisposable> disposables) : base(disposables)
{ }
public InputStateMachine(DIContainer container) : base(new List<IDisposable>())
{
_playerInput = container.Resolve<IPlayerInput>();
_uiInput = container.Resolve<IUIInput>();
}
public void Initialize()
{
PlayerInputState playerInputState = new(_playerInput);
UIInputState uiInputState = new(_uiInput);
ICompositeCondition playerToUiStateCondition = new CompositeCondition(LogicOperationsUtils.Or)
.Add(new FuncCondition(() => _uiInput.IsEnabled));
// .Add(new FuncCondition(() => _playerHorseInput.IsEnabled));
ICompositeCondition uiToPlayerStateCondition = new CompositeCondition(LogicOperationsUtils.Or)
.Add(new FuncCondition(() => _playerInput.IsEnabled));
// .Add(new FuncCondition(() => _playerHorseInput.IsEnabled));
AddState(playerInputState);
AddState(uiInputState);
AddTransition(playerInputState, uiInputState, playerToUiStateCondition);
AddTransition(uiInputState, playerInputState, uiToPlayerStateCondition);
Enter();
}
public void Dispose()
{
Exit();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b1a3543621b74566aa3f737018932731
timeCreated: 1772547242

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 60ce6781167b4204a72055d939441a5b
timeCreated: 1772547589

View File

@@ -0,0 +1,26 @@
using Assets._Project.Develop.Runtime.Utilities.StateMachineCore;
namespace _Project.Develop.Runtime.Utils.InputManagement.States
{
public class PlayerInputState : State
{
private readonly IPlayerInput _playerInput;
public PlayerInputState(IPlayerInput playerInput)
{
_playerInput = playerInput;
}
public override void Enter()
{
base.Enter();
_playerInput.Enable();
}
public override void Exit()
{
_playerInput.Disable();
base.Exit();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0efa0cca886c45e295d4adcb82d59f8b
timeCreated: 1770556305

View File

@@ -0,0 +1,26 @@
using Assets._Project.Develop.Runtime.Utilities.StateMachineCore;
namespace _Project.Develop.Runtime.Utils.InputManagement.States
{
public class UIInputState : State
{
private readonly IUIInput _uiInput;
public UIInputState(IUIInput uiInput)
{
_uiInput = uiInput;
}
public override void Enter()
{
base.Enter();
_uiInput.Enable();
}
public override void Exit()
{
_uiInput.Disable();
base.Exit();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2cac35b3aedd43b1959f82e36c30ac47
timeCreated: 1770556297