cirnogodot/Scripts/PlayerMovement.cs

235 lines
4.6 KiB
C#
Raw Normal View History

2024-02-26 08:33:37 +01:00
using Godot;
using System;
2024-02-27 17:16:55 +01:00
using System.Diagnostics;
2024-06-09 18:19:57 +02:00
using Cirno.Scripts;
2024-02-26 08:33:37 +01:00
2024-08-18 17:38:32 +02:00
public partial class PlayerMovement : CharacterBody2D, IDestructible
2024-02-26 08:33:37 +01:00
{
[Export]
public int Speed { get; set; } = 400;
2024-02-26 23:45:20 +01:00
2024-05-01 16:39:14 +02:00
[Export]
public float CrosshairDistance { get; set; } = 10f;
2024-06-09 18:19:57 +02:00
[Export]
public PackedScene SelectorScene { get; set; }
2024-08-24 15:25:30 +02:00
[Export]
2024-08-25 17:06:59 +02:00
public string GameOverScene { get; set; }
2024-08-24 15:25:30 +02:00
2024-06-09 18:19:57 +02:00
private Node2D _selector;
private Interactable _lastInteractable;
2024-02-27 17:16:55 +01:00
[Export]
2024-05-01 11:48:04 +02:00
public Marker2D Muzzle { get; set; }
2024-02-27 17:16:55 +01:00
2024-02-26 08:33:37 +01:00
private AnimatedSprite2D _animatedSprite;
2024-02-26 23:45:20 +01:00
2024-05-01 11:48:04 +02:00
private Vector2 _movementDirection { get; set; }
2024-08-24 15:25:30 +02:00
2024-05-01 11:48:04 +02:00
private Vector2 _facingDirection { get; set; }
2024-05-01 16:39:14 +02:00
private Sprite2D _crosshair;
2024-08-18 17:38:32 +02:00
[Export] public float Health = 4f;
2024-11-15 16:32:26 +01:00
[Export] public Weapon EquippedWeapon;
2025-01-21 15:17:26 +01:00
2024-08-18 17:38:32 +02:00
private float _currentHealth = 0f;
private bool _isDestroyed = false;
2025-01-20 21:58:59 +01:00
//private InventoryManager _inventoryManager;
2024-02-26 08:33:37 +01:00
public override void _Ready()
{
2024-08-18 17:38:32 +02:00
_currentHealth = Health;
2024-05-01 12:49:46 +02:00
_animatedSprite = GetNode<AnimatedSprite2D>("./Smoothing2D/AnimatedSprite2D");
2024-05-02 12:50:08 +02:00
_crosshair = GetNode<Sprite2D>("./Smoothing2D/Crosshair");
2025-01-20 21:58:59 +01:00
//_inventoryManager = GetNode<InventoryManager>("/root/GameScene/InventoryManager");
2024-05-01 16:39:14 +02:00
2024-05-01 11:48:04 +02:00
_movementDirection = Vector2.Zero;
_facingDirection = Vector2.Zero;
2024-06-09 18:19:57 +02:00
if (SelectorScene != null)
{
_selector = this.CreateChild<Node2D>(SelectorScene);
_selector.Visible = false;
}
2024-02-26 08:33:37 +01:00
}
/*public override _Process(float _delta)
{
if (Input.IsActionPressed("ui_right"))
{
_animatedSprite.Play("run");
}
else
{
_animatedSprite.Stop();
}
}*/
2024-02-26 23:45:20 +01:00
public override void _Process(double delta)
{
2024-02-27 17:16:55 +01:00
HandleShoot();
2024-02-26 23:45:20 +01:00
SetAnimation();
2024-08-24 15:25:30 +02:00
2024-06-09 18:19:57 +02:00
FindInteractable();
}
private void FindInteractable()
{
if (Input.IsActionJustPressed("Use") && _lastInteractable != null)
{
_lastInteractable.Activate();
}
2024-08-24 15:25:30 +02:00
2024-06-09 18:19:57 +02:00
//var spaceState = GetWorld2D().DirectSpaceState;
2024-08-24 15:25:30 +02:00
2024-06-09 18:19:57 +02:00
//var query = PhysicsRayQueryParameters2D.Create(Vector2.Zero, )
2024-02-26 23:45:20 +01:00
}
2024-02-27 17:16:55 +01:00
private void HandleShoot()
{
2024-11-15 16:32:26 +01:00
if (EquippedWeapon == null) return;
if (!Input.IsActionPressed("shoot")) return;
EquippedWeapon.ShootDirection = this._facingDirection;
EquippedWeapon.Shoot();
// //Debug.WriteLine("Shoot");
// var bullet = BulletScene.Instantiate<Bullet>();
// Owner.AddChild(bullet);
// bullet.Transform = Muzzle.GlobalTransform;
// bullet.Position = this.Position;
// bullet.SetDirection(this._facingDirection);
2024-02-27 17:16:55 +01:00
}
2024-02-26 23:45:20 +01:00
private void SetAnimation()
{
if (Velocity.X == 0 && Velocity.Y == 0)
{
_animatedSprite.SpeedScale = 0;
}
else
{
_animatedSprite.SpeedScale = 1;
}
if (Velocity.X > 0)
{
_animatedSprite.Play("walk_right");
}
else if (Velocity.X < 0)
{
_animatedSprite.Play("walk_left");
}
else if (Velocity.Y > 0)
{
_animatedSprite.Play("walk_down");
}
else if (Velocity.Y < 0)
{
_animatedSprite.Play("walk_up");
}
}
2024-02-26 17:35:40 +01:00
public Vector2 GetInput()
2024-02-26 08:33:37 +01:00
{
2024-02-26 17:35:40 +01:00
return Input.GetVector("left", "right", "up", "down");
2024-02-26 08:33:37 +01:00
}
2024-05-01 16:39:14 +02:00
private Vector2 CalculateCrosshairPosition()
{
return _facingDirection * CrosshairDistance;// + this.Position;
//var angle = Mathf.Atan2(this.Position.X, this.Position.Y);
//var cPos = new Vector2(this.Position.X + CrosshairDistance * Godot.Mathf.Cos(angle), this.Position.Y + CrosshairDistance * Godot.Mathf.Sin(angle));
}
2024-02-26 08:33:37 +01:00
public override void _PhysicsProcess(double delta)
2024-08-24 15:25:30 +02:00
{
2024-05-01 11:48:04 +02:00
_movementDirection = GetInput();
2024-08-24 15:25:30 +02:00
if (_movementDirection != Vector2.Zero)
{
2024-05-01 11:48:04 +02:00
_facingDirection = _movementDirection;
}
Velocity = _movementDirection * (float)(Speed * delta);
2024-02-26 17:35:40 +01:00
2024-02-26 08:33:37 +01:00
MoveAndSlide();
2024-05-01 16:39:14 +02:00
_crosshair.Position = CalculateCrosshairPosition();
2024-06-09 18:19:57 +02:00
//FindInteractable();
2024-08-24 15:25:30 +02:00
2024-06-09 18:19:57 +02:00
}
2024-08-24 15:25:30 +02:00
2024-06-09 18:19:57 +02:00
private void _on_interaction_controller_area_entered(Area2D area)
{
// Replace with function body.
if (area.IsInGroup("Interactable") && area is Interactable interactable)
{
Debug.WriteLine("Interactable Entered");
if (_selector != null)
{
_selector.Position = interactable.Position;
_selector.Visible = true;
_lastInteractable = interactable;
}
}
2024-02-26 08:33:37 +01:00
}
2024-08-18 17:38:32 +02:00
private void Explode()
{
Debug.WriteLine("Ded");
//CreateParticles();
//CreateDebris();
2024-08-24 15:25:30 +02:00
if (GameOverScene != null)
{
2024-08-25 17:06:59 +02:00
GetTree().ChangeSceneToFile(GameOverScene);
2024-08-24 15:25:30 +02:00
}
else
{
QueueFree();
}
2024-08-18 17:38:32 +02:00
}
public void Hit(float damage)
{
GD.Print($"Player damaged for {damage}");
if (_isDestroyed) return;
2024-08-24 15:25:30 +02:00
2024-08-18 17:38:32 +02:00
_currentHealth -= damage;
if (!(_currentHealth <= 0)) return;
_isDestroyed = true;
Explode();
}
public bool IsDestroyed()
{
return _isDestroyed;
}
2024-08-24 15:25:30 +02:00
private void _on_damage_hit_box_area_entered(Area2D area)
{
if (area is Bullet bullet)
{
2024-08-18 17:38:32 +02:00
GD.Print("Received damage manually");
this.Hit(bullet.Damage);
bullet.QueueFree();
}
}
2024-02-26 08:33:37 +01:00
}