cirnogodot/Scripts/PlayerMovement.cs

98 lines
1.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-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-02-27 17:16:55 +01:00
[Export]
public PackedScene BulletScene { get; set; }
[Export]
public Marker2D Muzzle {get;set;}
2024-02-26 08:33:37 +01:00
private AnimatedSprite2D _animatedSprite;
2024-02-26 23:45:20 +01:00
2024-02-26 08:33:37 +01:00
public override void _Ready()
{
_animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
}
/*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()
{
if (Input.IsActionJustPressed("shoot"))
{
2024-02-27 22:54:42 +01:00
Debug.WriteLine("Shoot");
2024-02-27 17:16:55 +01:00
Bullet bullet = BulletScene.Instantiate<Bullet>();
Owner.AddChild(bullet);
bullet.Transform = Muzzle.GlobalTransform;
bullet.Position = this.Position;
}
}
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
}
public override void _PhysicsProcess(double delta)
{
2024-02-26 17:35:40 +01:00
var inputDirection = GetInput();
Velocity = inputDirection * (float)(Speed * delta);
2024-02-26 08:33:37 +01:00
MoveAndSlide();
}
}