Alarm box

This commit is contained in:
Marco 2025-02-04 13:22:05 +01:00
commit 60dad2d002
15 changed files with 581 additions and 401 deletions

View file

@ -1,16 +1,34 @@
using Godot;
using System;
using Cirno.Scripts;
public partial class Camera : Node2D
public partial class Camera : Enemy
{
private AnimatedSprite2D _sprite;
private GameManager _gameManager;
///private AlarmManager _alarmManager;
public override void _Ready()
{
base._Ready();
_gameManager = GetNode<GameManager>("/root/GameScene");
//_alarmManager = _gameManager.AlarmManager;
_sprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
_sprite.Play("full_scan");
}
protected override void Shoot()
{
_gameManager.AlarmManager.SoundAlarm(this.GlobalPosition);
}
}

32
Scripts/AlarmManager.cs Normal file
View file

@ -0,0 +1,32 @@
using Godot;
namespace Cirno.Scripts;
public partial class AlarmManager : Node2D
{
public bool IsAlarmOn { get; private set; } = false;
public Vector2 LastAlarmPosition { get; private set; } = new Vector2();
[Signal]
public delegate void AlarmEnabledEventHandler(Vector2 location);
[Signal]
public delegate void AlarmDisabledEventHandler();
public void SoundAlarm(Vector2 location)
{
if (IsAlarmOn) return;
IsAlarmOn = true;
LastAlarmPosition = location;
EmitSignal(nameof(AlarmEnabled), location);
GD.Print($"Alarm sounded at {location}");
}
public void DisableAlarm()
{
IsAlarmOn = false;
EmitSignal(nameof(AlarmDisabled));
}
}

View file

@ -26,31 +26,29 @@ public partial class Enemy : CharacterBody2D
private bool IsPlayerInRange => _playerDetection is { IsPlayerInRange: true };
private bool IsPlayerInSight => _playerDetection is not null && _playerDetection.IsPlayerInSight(CollisionMask);
private Vector2? _lastPlayerPosition = null;
[Export]
private PlayerDetection _playerDetection;
[Export]
private bool _navigationEnabled = false;
[Export] public bool NavigationEnabled { get; set; } = false;
public bool NavigationEnabled
{
get => _navigationEnabled && _navigationAgent != null;
set => _navigationEnabled = value;
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_currentHealth = Health;
_navigationAgent = GetNode<NavigationAgent2D>("NavigationAgent2D");
//var asdf = GetNode<ProximityPlayerDetection>("PlayerDetection");
//_playerDetection = GetNode<PlayerDetection>("PlayerDetection");
//CallDeferred("Setup");
_navigationAgent = GetNodeOrNull<NavigationAgent2D>("NavigationAgent2D");
}
// private void Setup()
// {
//
// }
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
@ -90,41 +88,54 @@ public partial class Enemy : CharacterBody2D
{
_lastPlayerPosition = _playerDetection.CachedPlayer.GlobalPosition;
}
if (NavigationEnabled)
{
if (_lastPlayerPosition.HasValue)
{
_navigationAgent.SetTargetPosition(_lastPlayerPosition.Value);
}
var currentAgentPosition = GlobalPosition;
var nextPathPosition = _navigationAgent.GetNextPathPosition();
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * (float)(WalkSpeed * delta);
// Navigation is over, can do other things like shooting
if (_navigationAgent.IsNavigationFinished())
{
// Shoot player
if (IsPlayerInSight)
{
Shoot();
}
// TODO: If player totally left the max range it should stop shooting and go back to idle
return;
}
if (_navigationAgent.AvoidanceEnabled)
{
_navigationAgent.SetVelocity(newVelocity);
}
else
{
_on_navigation_agent_2d_velocity_computed(newVelocity);
}
if (_lastPlayerPosition.HasValue)
{
_navigationAgent.SetTargetPosition(_lastPlayerPosition.Value);
}
var currentAgentPosition = GlobalPosition;
var nextPathPosition = _navigationAgent.GetNextPathPosition();
var newVelocity = currentAgentPosition.DirectionTo(nextPathPosition) * (float)(WalkSpeed * delta);
// Navigation is over, can do other things like shooting
if (_navigationAgent.IsNavigationFinished())
{
// Shoot player
Shoot();
// TODO: If player totally left the max range it should stop shooting and go back to idle
return;
}
if (_navigationAgent.AvoidanceEnabled)
{
_navigationAgent.SetVelocity(newVelocity);
MoveAndSlide();
}
else
{
_on_navigation_agent_2d_velocity_computed(newVelocity);
if (IsPlayerInSight)
{
Shoot();
}
}
MoveAndSlide();
break;
case EnemyState.Patrolling:
@ -140,96 +151,15 @@ 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 Shoot()
protected virtual void Shoot()
{
if (EquippedWeapon == null || !_lastPlayerPosition.HasValue) return;
// Shoot at the player's last known position
EquippedWeapon.ShootDirection = (_lastPlayerPosition.Value - this.GlobalPosition).Normalized();
EquippedWeapon.Shoot();
// // SHOOT
// var bullet = this.CreateChild<Bullet>(BulletScene);
// // var bullet = BulletScene.Instantiate<Bullet>();
// // Owner.AddChild(bullet);
// // bullet.Transform = this.GlobalTransform;
// // bullet.Position = this.Position;
// bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized());
// bullet.Speed = BulletSpeed;
//
// _ammo -= 1;
//
// if (_ammo <= 0)
// {
// _ammo = BulletCount;
// _cooldownTimer.Start(ReloadTime);
// }
// else
// {
// _cooldownTimer.Start(RateOfFire);
// }
}
// 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"]);
// }
// 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)
{

View file

@ -17,8 +17,12 @@ public partial class GameManager : Node2D
public Marker2D PlayerSpawnMarker { get; set; }
private InventoryManager _inventoryManager { get; set; }
private AlarmManager _alarmManager { get; set; }
public InventoryManager Inventory => _inventoryManager;
public AlarmManager AlarmManager => _alarmManager;
private Node2D _bulletsContainer;
public Node2D BulletsContainer => _bulletsContainer;
@ -31,6 +35,8 @@ public partial class GameManager : Node2D
_inventoryManager = GetNode<InventoryManager>("InventoryManager");
_alarmManager = GetNode<AlarmManager>("AlarmManager");
SpawnBulletsContainer();
if (PlayerSpawnMarker != null)

View file

@ -0,0 +1,49 @@
using Godot;
namespace Cirno.Scripts.Interactables;
public partial class AlarmBox : Interactable
{
private GameManager _gameManager;
private AnimatedSprite2D _sprite;
public override void _Ready()
{
base._Ready();
_gameManager = GetNode<GameManager>("/root/GameScene");
_sprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
_gameManager.AlarmManager.AlarmEnabled += AlarmManagerOnAlarmEnabled;
_gameManager.AlarmManager.AlarmDisabled += AlarmManagerOnAlarmDisabled;
}
private void AlarmManagerOnAlarmDisabled()
{
GD.Print("Playing disabled alarm");
_sprite.Play("default");
}
private void AlarmManagerOnAlarmEnabled(Vector2 location)
{
GD.Print("Playing enabled alarm");
_sprite.Play("alarmed");
}
public override bool Activate()
{
if (MeetsRequirements() && _gameManager.AlarmManager.IsAlarmOn)
{
_gameManager.AlarmManager.DisableAlarm();
// disable alarm
GD.Print("Alarm disabled");
return true;
}
return false;
}
}