-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#16 Udemy 2.5D RPG - Enemies chasing player and attacking
- Loading branch information
1 parent
6c26b21
commit ae4d2c8
Showing
16 changed files
with
294 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
|
||
namespace Udemy25dRpg.Scenes.Characters.Enemy; | ||
|
||
public partial class EnemyAttackState : EnemyState | ||
{ | ||
protected override void ExitState() | ||
{ | ||
base.ExitState(); | ||
|
||
_characterNode.AnimatedSprite3DNode.AnimationFinished -= HandleAnimationFinished; | ||
} | ||
|
||
protected override void EnterState() | ||
{ | ||
base.EnterState(); | ||
|
||
_characterNode.AnimatedSprite3DNode.AnimationFinished += HandleAnimationFinished; | ||
_characterNode.AnimatedSprite3DNode.Play(nameof(Enemy.EnemyAnimations.Attack)); | ||
} | ||
|
||
private void HandleAnimationFinished() | ||
{ | ||
_characterNode.StateMachineNode.SwitchState<EnemyChaseState>(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Godot; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Udemy25dRpg.Scenes.Characters.Enemy; | ||
|
||
public partial class EnemyChaseState : EnemyState | ||
{ | ||
private CharacterBody3D _target; | ||
|
||
[Export] public Timer TimerNode { get; set; } | ||
|
||
protected override void ExitState() | ||
{ | ||
base.ExitState(); | ||
|
||
_characterNode.AttackAreaNode.BodyEntered -= HandleAttackAreaBodyEntered; | ||
_characterNode.ChaseAreaNode.BodyExited -= HandleChaseAreaBodyExited; | ||
|
||
TimerNode.Timeout -= HandleTimerTimeout; | ||
TimerNode.Stop(); | ||
} | ||
|
||
protected override void EnterState() | ||
{ | ||
base.EnterState(); | ||
|
||
_characterNode.AnimatedSprite3DNode.Play(nameof(Enemy.EnemyAnimations.Move)); | ||
|
||
_characterNode.AttackAreaNode.BodyEntered += HandleAttackAreaBodyEntered; | ||
_characterNode.ChaseAreaNode.BodyExited += HandleChaseAreaBodyExited; | ||
|
||
_target = _characterNode.ChaseAreaNode.GetOverlappingBodies().OfType<CharacterBody3D>().First(); | ||
|
||
TimerNode.Timeout += HandleTimerTimeout; | ||
TimerNode.Start(); | ||
} | ||
|
||
private void HandleChaseAreaBodyExited(Node3D body) => _characterNode.StateMachineNode.SwitchState<EnemyReturnState>(); | ||
|
||
private void HandleAttackAreaBodyEntered(Node3D body) => _characterNode.StateMachineNode.SwitchState<EnemyAttackState>(); | ||
|
||
|
||
private void HandleTimerTimeout() | ||
{ | ||
_destination = _target.GlobalPosition; | ||
_characterNode.NavigationAgentNode.TargetPosition = _destination; | ||
} | ||
|
||
|
||
public override void _PhysicsProcess(double delta) | ||
{ | ||
base._PhysicsProcess(delta); | ||
|
||
Move(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,63 @@ | ||
using Godot; | ||
|
||
namespace Udemy25dRpg.Scenes.Characters.Enemy; | ||
|
||
public partial class EnemyPatrolState : StateMachineStateBase | ||
public partial class EnemyPatrolState : EnemyState | ||
{ | ||
[Export] | ||
public Timer IdleTimerNode { get; set; } | ||
|
||
[Export(PropertyHint.Range, "0, 20, .1")] | ||
public float MaxIdleTime { get; set; } = 4; | ||
|
||
private int _pathNodeIndex = 0; | ||
|
||
protected override void ExitState() | ||
{ | ||
_characterNode.NavigationAgentNode.NavigationFinished -= HandleNavigationFinished; | ||
_characterNode.ChaseAreaNode.BodyEntered -= HandleChaseAreaBodyEntered; | ||
|
||
IdleTimerNode.Timeout -= HandleTimerTimeout; | ||
IdleTimerNode.Stop(); | ||
} | ||
|
||
protected override void EnterState() | ||
{ | ||
_characterNode.AnimatedSprite3DNode.Play(nameof(Enemy.EnemyAnimations.Move)); | ||
|
||
_pathNodeIndex = 1; | ||
_destination = GetPointGlobalPosition(_pathNodeIndex); | ||
_characterNode.NavigationAgentNode.TargetPosition = _destination; | ||
|
||
_characterNode.NavigationAgentNode.NavigationFinished += HandleNavigationFinished; | ||
_characterNode.ChaseAreaNode.BodyEntered += HandleChaseAreaBodyEntered; | ||
|
||
IdleTimerNode.Timeout += HandleTimerTimeout; | ||
IdleTimerNode.Start(); | ||
} | ||
|
||
private void HandleTimerTimeout() | ||
{ | ||
_characterNode.AnimatedSprite3DNode.Play(nameof(Enemy.EnemyAnimations.Move)); | ||
|
||
_pathNodeIndex = Mathf.Wrap(_pathNodeIndex + 1, 0, _characterNode.PathNode.Curve.PointCount); | ||
_destination = GetPointGlobalPosition(_pathNodeIndex); | ||
_characterNode.NavigationAgentNode.TargetPosition = _destination; | ||
} | ||
|
||
private void HandleNavigationFinished() | ||
{ | ||
_characterNode.AnimatedSprite3DNode.Play(nameof(Enemy.EnemyAnimations.Idle)); | ||
|
||
IdleTimerNode.WaitTime = new RandomNumberGenerator().RandfRange(0, MaxIdleTime); | ||
IdleTimerNode.Start(); | ||
} | ||
|
||
public override void _PhysicsProcess(double delta) | ||
{ | ||
base._PhysicsProcess(delta); | ||
|
||
if (_characterNode.NavigationAgentNode.IsNodeReady() && _characterNode.NavigationAgentNode.IsNavigationFinished()) | ||
{ | ||
if (_pathNodeIndex >= _characterNode.PathNode.Curve.PointCount) | ||
{ | ||
_pathNodeIndex = 0; | ||
} | ||
|
||
_characterNode.NavigationAgentNode.TargetPosition = | ||
_characterNode.PathNode.Curve.GetPointPosition(_pathNodeIndex++) + | ||
_characterNode.PathNode.GlobalPosition; | ||
if (IdleTimerNode.IsStopped()) | ||
{ | ||
Move(); | ||
} | ||
|
||
_characterNode.Velocity = _characterNode.GlobalPosition.DirectionTo(_characterNode.NavigationAgentNode.TargetPosition); | ||
_characterNode.MoveAndSlide(); | ||
} | ||
} |
Oops, something went wrong.