51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
#nullable enable
|
|
|
|
using Godot;
|
|
using SideScrollerGame.Hero.Rules;
|
|
|
|
namespace SideScrollerGame.Hero;
|
|
|
|
public partial class HeroActor : Node2D
|
|
{
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
if (m_Runtime is not null && m_Runtime.State.LifeState != HeroLifeState.Alive)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Vector2 direction = Input.GetVector("move_left", "move_right", "move_up", "move_down");
|
|
GlobalPosition = ClampToBounds(GlobalPosition + (direction * MoveSpeed * (float)delta));
|
|
}
|
|
|
|
public void SetRuntime(HeroRuntimeService runtime)
|
|
{
|
|
m_Runtime = runtime;
|
|
}
|
|
|
|
public void SetPlayBounds(Rect2 bounds)
|
|
{
|
|
PlayBounds = bounds;
|
|
GlobalPosition = ClampToBounds(GlobalPosition);
|
|
}
|
|
|
|
[Export]
|
|
public float MoveSpeed { get; set; } = 320.0f;
|
|
|
|
[Export]
|
|
public Rect2 PlayBounds { get; set; } = new(new Vector2(260.0f, 96.0f), new Vector2(640.0f, 420.0f));
|
|
|
|
private Vector2 ClampToBounds(Vector2 position)
|
|
{
|
|
if (PlayBounds.Size == Vector2.Zero)
|
|
{
|
|
return position;
|
|
}
|
|
|
|
float x = Mathf.Clamp(position.X, PlayBounds.Position.X, PlayBounds.Position.X + PlayBounds.Size.X);
|
|
float y = Mathf.Clamp(position.Y, PlayBounds.Position.Y, PlayBounds.Position.Y + PlayBounds.Size.Y);
|
|
return new Vector2(x, y);
|
|
}
|
|
|
|
private HeroRuntimeService? m_Runtime;
|
|
} |