-
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 Handle player attacks - make enemy attacks track p…
…layer
- Loading branch information
John Ludlow
committed
Oct 11, 2024
1 parent
b5d5cad
commit 8c7dfcc
Showing
7 changed files
with
122 additions
and
46 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
using Godot; | ||
|
||
namespace Udemy25dRpg.Scenes.Characters.Enemy; | ||
|
||
public partial class Enemy : CharacterBase | ||
|
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,27 +1,70 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
using Godot; | ||
|
||
namespace Udemy25dRpg.Scenes.Characters.Enemy; | ||
|
||
public partial class EnemyAttackState : EnemyState | ||
{ | ||
protected override void ExitState() | ||
{ | ||
base.ExitState(); | ||
|
||
CharacterNode.AnimatedSprite3DNode.AnimationFinished -= HandleAnimationFinished; | ||
} | ||
private Vector3 _targetPosition; | ||
|
||
protected override void EnterState() | ||
{ | ||
base.EnterState(); | ||
protected override void ExitState() | ||
{ | ||
base.ExitState(); | ||
|
||
CharacterNode.AnimatedSprite3DNode.AnimationFinished += HandleAnimationFinished; | ||
CharacterNode.AnimatedSprite3DNode.Play(nameof(Enemy.EnemyAnimations.Attack)); | ||
CharacterNode.AnimatedSprite3DNode.AnimationFinished -= HandleAnimationFinished; | ||
CharacterNode.AnimatedSprite3DNode.FrameChanged -= HandleAnimationFrameChanged; | ||
|
||
} | ||
|
||
private void HandleAnimationFrameChanged() | ||
{ | ||
switch (CharacterNode.AnimatedSprite3DNode.Animation) | ||
{ | ||
case "Attack" when CharacterNode.AnimatedSprite3DNode.Frame == 3: | ||
PerformHit(); | ||
break; | ||
} | ||
} | ||
|
||
private void HandleAnimationFinished() | ||
protected override void EnterState() | ||
{ | ||
base.EnterState(); | ||
|
||
_targetPosition = CharacterNode | ||
.AttackAreaNode | ||
.GetOverlappingBodies() | ||
.First() | ||
.GlobalPosition; | ||
|
||
CharacterNode.AnimatedSprite3DNode.AnimationFinished += HandleAnimationFinished; | ||
CharacterNode.AnimatedSprite3DNode.FrameChanged += HandleAnimationFrameChanged; | ||
|
||
CharacterNode.HitboxShapeNode.Disabled = true; | ||
|
||
CharacterNode.AnimatedSprite3DNode.Play(nameof(Enemy.EnemyAnimations.Attack)); | ||
} | ||
|
||
private void HandleAnimationFinished() | ||
{ | ||
if (CharacterNode | ||
.AttackAreaNode | ||
.GetOverlappingBodies() | ||
.OfType<Player.Player>() | ||
.Any()) | ||
{ | ||
CharacterNode.StateMachineNode.SwitchState<EnemyChaseState>(); | ||
EnterState(); | ||
} | ||
else | ||
{ | ||
CharacterNode.StateMachineNode.SwitchState<EnemyChaseState>(); | ||
} | ||
} | ||
|
||
private void PerformHit() | ||
{ | ||
CharacterNode.HitboxAreaNode.GlobalPosition = _targetPosition; | ||
CharacterNode.HitboxShapeNode.Disabled = false; | ||
} | ||
} |
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