mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-08 00:25:53 +00:00
Moved player detection to external script
This commit is contained in:
parent
6968bbe72b
commit
d8e1459194
8 changed files with 237 additions and 69 deletions
17
Scripts/Components/PlayerDetection.cs
Normal file
17
Scripts/Components/PlayerDetection.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Components;
|
||||
|
||||
public partial class PlayerDetection : Area2D
|
||||
{
|
||||
public InteractionController CachedPlayer => _cachedPlayer;
|
||||
|
||||
protected InteractionController _cachedPlayer;
|
||||
public virtual bool IsPlayerInRange { get; set; }
|
||||
|
||||
public virtual bool IsPlayerInSight(uint collisionMask)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
45
Scripts/Components/ProximityPlayerDetection.cs
Normal file
45
Scripts/Components/ProximityPlayerDetection.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using System.Diagnostics;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Components;
|
||||
|
||||
public partial class ProximityPlayerDetection : PlayerDetection
|
||||
{
|
||||
public override bool IsPlayerInRange { get; set; }
|
||||
|
||||
public override bool IsPlayerInSight(uint collisionMask)
|
||||
{
|
||||
if (_cachedPlayer == null) return false;
|
||||
|
||||
var spaceState = GetWorld2D().DirectSpaceState;
|
||||
|
||||
// I have no idea why I need to use the parent's collision mask instead of the local one but it doesn't detect the player otherwise
|
||||
|
||||
var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _cachedPlayer.GlobalPosition, collisionMask, new Array<Rid> { GetRid() });
|
||||
|
||||
// var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _cachedPlayer.GlobalPosition, CollisionMask, new Array<Rid> { GetRid() });
|
||||
var result = spaceState.IntersectRay(query);
|
||||
|
||||
// If count is 0 then the player is in sight, otherwise there is level geometry in the way
|
||||
return result.Count == 0;
|
||||
}
|
||||
|
||||
private void _on_area_entered(Area2D area)
|
||||
{
|
||||
// Assume area is player for now
|
||||
if (area is not InteractionController player) return;
|
||||
|
||||
Debug.WriteLine("Enemy detection area Entered by interaction controller");
|
||||
|
||||
_cachedPlayer = player;
|
||||
|
||||
IsPlayerInRange = true;
|
||||
}
|
||||
|
||||
private void _on_area_exited(Area2D area)
|
||||
{
|
||||
if (area is not InteractionController player) return;
|
||||
IsPlayerInRange = false;
|
||||
}
|
||||
}
|
||||
139
Scripts/Enemy.cs
139
Scripts/Enemy.cs
|
|
@ -2,6 +2,8 @@ using Cirno.Scripts;
|
|||
using Godot;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Cirno.Scripts.Components;
|
||||
using Godot.Collections;
|
||||
|
||||
public partial class Enemy : CharacterBody2D
|
||||
{
|
||||
|
|
@ -21,10 +23,13 @@ public partial class Enemy : CharacterBody2D
|
|||
private bool _isDestroyed = false;
|
||||
|
||||
private NavigationAgent2D _navigationAgent;
|
||||
|
||||
private bool _isPlayerInRange = false;
|
||||
|
||||
|
||||
private bool IsPlayerInRange => _playerDetection is { IsPlayerInRange: true };
|
||||
|
||||
private Vector2? _lastPlayerPosition = null;
|
||||
|
||||
[Export]
|
||||
private PlayerDetection _playerDetection;
|
||||
|
||||
[Export] public bool NavigationEnabled { get; set; } = false;
|
||||
|
||||
|
|
@ -35,6 +40,10 @@ public partial class Enemy : CharacterBody2D
|
|||
|
||||
_navigationAgent = GetNode<NavigationAgent2D>("NavigationAgent2D");
|
||||
|
||||
//var asdf = GetNode<ProximityPlayerDetection>("PlayerDetection");
|
||||
|
||||
//_playerDetection = GetNode<PlayerDetection>("PlayerDetection");
|
||||
|
||||
//CallDeferred("Setup");
|
||||
}
|
||||
|
||||
|
|
@ -67,15 +76,19 @@ public partial class Enemy : CharacterBody2D
|
|||
switch (_currentState)
|
||||
{
|
||||
case EnemyState.Idle:
|
||||
HandlePlayerDetection();
|
||||
if (_playerDetection != null && _playerDetection.IsPlayerInSight(CollisionMask))
|
||||
{
|
||||
_currentState = EnemyState.Alert;
|
||||
}
|
||||
//HandlePlayerDetection();
|
||||
|
||||
break;
|
||||
case EnemyState.Alert:
|
||||
|
||||
// Update last known player position if it's in range
|
||||
if (_isPlayerInRange)
|
||||
if (IsPlayerInRange)
|
||||
{
|
||||
_lastPlayerPosition = _cachedPlayer.GlobalPosition;
|
||||
_lastPlayerPosition = _playerDetection.CachedPlayer.GlobalPosition;
|
||||
}
|
||||
|
||||
if (_lastPlayerPosition.HasValue)
|
||||
|
|
@ -127,25 +140,25 @@ public partial class Enemy : CharacterBody2D
|
|||
|
||||
}
|
||||
|
||||
private void HandlePlayerDetection()
|
||||
{
|
||||
if (_cachedPlayer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsPlayerInSight())
|
||||
{
|
||||
// Update player position only if player is in sight
|
||||
// if (NavigationEnabled)
|
||||
// {
|
||||
// _navigationAgent.SetTargetPosition(_cachedPlayer.GlobalPosition);
|
||||
// }
|
||||
//Shoot();
|
||||
|
||||
_currentState = EnemyState.Alert;
|
||||
}
|
||||
}
|
||||
// private void HandlePlayerDetection()
|
||||
// {
|
||||
// if (_cachedPlayer == null)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// if (IsPlayerInSight())
|
||||
// {
|
||||
// // Update player position only if player is in sight
|
||||
// // if (NavigationEnabled)
|
||||
// // {
|
||||
// // _navigationAgent.SetTargetPosition(_cachedPlayer.GlobalPosition);
|
||||
// // }
|
||||
// //Shoot();
|
||||
//
|
||||
// _currentState = EnemyState.Alert;
|
||||
// }
|
||||
// }
|
||||
|
||||
private void Shoot()
|
||||
{
|
||||
|
|
@ -178,45 +191,45 @@ public partial class Enemy : CharacterBody2D
|
|||
// }
|
||||
}
|
||||
|
||||
private bool IsPlayerInSight()
|
||||
{
|
||||
var spaceState = GetWorld2D().DirectSpaceState;
|
||||
var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _cachedPlayer.GlobalPosition, CollisionMask, new Godot.Collections.Array<Rid> { GetRid() });
|
||||
var result = spaceState.IntersectRay(query);
|
||||
// private bool IsPlayerInSight()
|
||||
// {
|
||||
// var spaceState = GetWorld2D().DirectSpaceState;
|
||||
// var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _cachedPlayer.GlobalPosition, CollisionMask, new Godot.Collections.Array<Rid> { GetRid() });
|
||||
// var result = spaceState.IntersectRay(query);
|
||||
//
|
||||
// // If count is 0 then the player is in sight, otherwise there is level geometry in the way
|
||||
// return result.Count == 0;
|
||||
//
|
||||
// // if (result.Count > 0)
|
||||
// // GD.Print("Hit at point: ", result["position"]);
|
||||
// }
|
||||
|
||||
// If count is 0 then the player is in sight, otherwise there is level geometry in the way
|
||||
return result.Count == 0;
|
||||
|
||||
// if (result.Count > 0)
|
||||
// GD.Print("Hit at point: ", result["position"]);
|
||||
}
|
||||
|
||||
private void _on_player_detection_area_entered(Area2D area)
|
||||
{
|
||||
// Assume area is player for now
|
||||
if (area is InteractionController player)
|
||||
{
|
||||
Debug.WriteLine("Enemy detection area Entered by interaction controller");
|
||||
|
||||
_cachedPlayer = player;
|
||||
|
||||
_isPlayerInRange = true;
|
||||
// if (_currentState is EnemyState.Idle)
|
||||
// {
|
||||
// _currentState = EnemyState.Primed;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
private void _on_player_detection_area_exited(Area2D area)
|
||||
{
|
||||
_isPlayerInRange = false;
|
||||
|
||||
// if (_currentState is EnemyState.Primed)
|
||||
// {
|
||||
// _currentState = EnemyState.Idle;
|
||||
// }
|
||||
}
|
||||
// private void _on_player_detection_area_entered(Area2D area)
|
||||
// {
|
||||
// // Assume area is player for now
|
||||
// if (area is InteractionController player)
|
||||
// {
|
||||
// Debug.WriteLine("Enemy detection area Entered by interaction controller");
|
||||
//
|
||||
// _cachedPlayer = player;
|
||||
//
|
||||
// _isPlayerInRange = true;
|
||||
// // if (_currentState is EnemyState.Idle)
|
||||
// // {
|
||||
// // _currentState = EnemyState.Primed;
|
||||
// // }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private void _on_player_detection_area_exited(Area2D area)
|
||||
// {
|
||||
// _isPlayerInRange = false;
|
||||
//
|
||||
// // if (_currentState is EnemyState.Primed)
|
||||
// // {
|
||||
// // _currentState = EnemyState.Idle;
|
||||
// // }
|
||||
// }
|
||||
|
||||
private void _on_damage_hitbox_area_entered(Area2D area)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue