cirnogodot/Scripts/PlayerMovement.cs

128 lines
2.5 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-02-26 08:33:37 +01:00
public partial class PlayerMovement : CharacterBody2D
{
[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-02-27 17:16:55 +01:00
[Export]
public PackedScene BulletScene { get; set; }
[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; }
private Vector2 _facingDirection { get; set; }
2024-05-01 16:39:14 +02:00
private Sprite2D _crosshair;
2024-02-26 08:33:37 +01:00
public override void _Ready()
{
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");
2024-05-01 16:39:14 +02:00
2024-05-01 11:48:04 +02:00
_movementDirection = Vector2.Zero;
_facingDirection = Vector2.Zero;
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-02-27 17:16:55 +01:00
private void HandleShoot()
{
2024-06-09 11:45:22 +02:00
if (!Input.IsActionJustPressed("shoot")) return;
//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-05-01 11:48:04 +02:00
{
_movementDirection = GetInput();
if (_movementDirection != Vector2.Zero) {
_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-02-26 08:33:37 +01:00
}
}