mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:05:34 +00:00
Camera Player Detection sweep
This commit is contained in:
parent
059d076295
commit
da1fde4062
3 changed files with 139 additions and 6 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
[ext_resource type="Script" path="res://Scripts/Actors/Camera.cs" id="1_2qb38"]
|
||||
[ext_resource type="SpriteFrames" uid="uid://yfxog8pww6ih" path="res://Resources/Sprites/Camera.tres" id="2_omkb8"]
|
||||
[ext_resource type="Script" path="res://Scripts/Components/ProximityPlayerDetection.cs" id="3_1qxk5"]
|
||||
[ext_resource type="Script" path="res://Scripts/Components/CameraPlayerDetection.cs" id="3_ax0x5"]
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_nkpag"]
|
||||
|
||||
|
|
@ -14,7 +14,7 @@ size = Vector2(8, 12)
|
|||
|
||||
[node name="Camera" type="CharacterBody2D" node_paths=PackedStringArray("_playerDetection")]
|
||||
collision_layer = 16
|
||||
collision_mask = 73
|
||||
collision_mask = 75
|
||||
script = ExtResource("1_2qb38")
|
||||
Health = 1.0
|
||||
WalkSpeed = 0.0
|
||||
|
|
@ -29,12 +29,13 @@ frame_progress = 0.779565
|
|||
shape = SubResource("CircleShape2D_nkpag")
|
||||
|
||||
[node name="PlayerDetection" type="Area2D" parent="."]
|
||||
visible = false
|
||||
collision_layer = 16
|
||||
collision_mask = 2
|
||||
script = ExtResource("3_1qxk5")
|
||||
script = ExtResource("3_ax0x5")
|
||||
SweepSpeed = 10.0
|
||||
Debug = true
|
||||
|
||||
[node name="PlayerDetectionArea" type="CollisionShape2D" parent="PlayerDetection"]
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="PlayerDetection"]
|
||||
shape = SubResource("CircleShape2D_gs7jc")
|
||||
|
||||
[node name="DamageHitbox" type="Area2D" parent="."]
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
129
Scripts/Components/CameraPlayerDetection.cs
Normal file
129
Scripts/Components/CameraPlayerDetection.cs
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
using System.Diagnostics;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Components;
|
||||
|
||||
public partial class CameraPlayerDetection : PlayerDetection
|
||||
{
|
||||
|
||||
[Export] public float SweepAngle = 90f; // In degrees
|
||||
[Export] public float SweepSpeed = 1f; // Speed of sweeping
|
||||
[Export] public bool Debug = false; // Enable debug lines
|
||||
|
||||
private float _currentAngle;
|
||||
private float _sweepDirection = 1f;
|
||||
private float _raycastLength;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
base._Ready();
|
||||
|
||||
var collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
|
||||
if (collisionShape.Shape is CircleShape2D circle)
|
||||
{
|
||||
_raycastLength = circle.Radius;
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
base._PhysicsProcess(delta);
|
||||
SweepCamera((float)delta);
|
||||
}
|
||||
|
||||
private void SweepCamera(float delta)
|
||||
{
|
||||
_currentAngle += _sweepDirection * SweepSpeed * delta;
|
||||
|
||||
// Clamp angle within the sweep range
|
||||
float halfAngle = SweepAngle / 2f;
|
||||
if (_currentAngle > halfAngle || _currentAngle < -halfAngle)
|
||||
{
|
||||
_sweepDirection *= -1f;
|
||||
_currentAngle = Mathf.Clamp(_currentAngle, -halfAngle, halfAngle);
|
||||
}
|
||||
|
||||
// if (result.Count > 0 && result["collider"] is InteractionController interactionController && interactionController == _cachedPlayer)
|
||||
// {
|
||||
// GD.Print("Player detected!");
|
||||
// // Handle detection logic here
|
||||
// }
|
||||
}
|
||||
|
||||
private void DrawDebugLine(Vector2 endPoint)
|
||||
{
|
||||
// Request the node to redraw
|
||||
QueueRedraw();
|
||||
_debugLineEndPoint = endPoint;
|
||||
}
|
||||
|
||||
private Vector2 _debugLineEndPoint;
|
||||
|
||||
public override void _Draw()
|
||||
{
|
||||
if (Debug)
|
||||
{
|
||||
DrawLine(Vector2.Zero, ToLocal(_debugLineEndPoint), Colors.Red, 2);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool IsPlayerInSight(uint collisionMask)
|
||||
{
|
||||
if (_cachedPlayer == null) return false;
|
||||
|
||||
Vector2 direction = new Vector2(0, 1).Rotated(Mathf.DegToRad(_currentAngle));
|
||||
Vector2 rayEnd = GlobalPosition + direction * _raycastLength;
|
||||
|
||||
// Perform raycast
|
||||
var spaceState = GetWorld2D().DirectSpaceState;
|
||||
var query = PhysicsRayQueryParameters2D.Create(GlobalPosition, rayEnd);
|
||||
query.Exclude = new Godot.Collections.Array<Rid> { GetRid() };
|
||||
query.CollideWithAreas = true;
|
||||
query.CollideWithBodies = true;
|
||||
query.CollisionMask = collisionMask;
|
||||
|
||||
var result = spaceState.IntersectRay(query);
|
||||
|
||||
bool detected = false;
|
||||
|
||||
Vector2 debugLineEnd = rayEnd;
|
||||
if (result.Count > 0)
|
||||
{
|
||||
Node colliderNode = result["collider"].As<Node>();
|
||||
if (colliderNode is InteractionController interactionController && interactionController == _cachedPlayer)
|
||||
{
|
||||
GD.Print("Player detected!");
|
||||
detected = true;
|
||||
// Handle detection logic here
|
||||
}
|
||||
// Adjust debug line to collision point
|
||||
debugLineEnd = (Vector2)result["position"];
|
||||
}
|
||||
|
||||
if (Debug)
|
||||
{
|
||||
DrawDebugLine(debugLineEnd);
|
||||
}
|
||||
|
||||
return detected;
|
||||
}
|
||||
|
||||
private void _on_area_entered(Area2D area)
|
||||
{
|
||||
// Assume area is player for now
|
||||
if (area is not InteractionController player) return;
|
||||
|
||||
GD.Print("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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue