Compare commits

...

2 Commits

Author SHA1 Message Date
84b908ef51 Rotate building tiles 2025-08-27 17:37:31 +08:00
c078204e60 Player movement with sprints and optimzation 2025-08-27 17:37:04 +08:00
3 changed files with 81 additions and 6 deletions

View File

@@ -5,7 +5,11 @@ namespace AceFieldNewHorizon.Scripts.Entities;
public partial class Player : CharacterBody2D public partial class Player : CharacterBody2D
{ {
[Export] public float Speed = 400.0f; [Export] public float MaxSpeed = 400.0f;
[Export] public float SprintMultiplier = 1.8f; // 80% faster when sprinting
[Export] public float Acceleration = 1500.0f;
[Export] public float SprintAcceleration = 1800.0f; // Slightly faster acceleration when sprinting
[Export] public float Deceleration = 1200.0f;
[Export] public float RotationSpeed = 3.0f; [Export] public float RotationSpeed = 3.0f;
public override void _Process(double delta) public override void _Process(double delta)
@@ -20,10 +24,43 @@ public partial class Player : CharacterBody2D
{ {
// Get movement input // Get movement input
var moveForward = Input.GetActionStrength("move_up") - Input.GetActionStrength("move_down"); var moveForward = Input.GetActionStrength("move_up") - Input.GetActionStrength("move_down");
var moveHorizontal = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left");
var isSprinting = Input.IsActionPressed("move_sprint");
// Calculate movement direction based on rotation // Calculate movement parameters based on sprint state
Velocity = Vector2.Right.Rotated(Rotation) * moveForward * Speed; var currentMaxSpeed = isSprinting ? MaxSpeed * SprintMultiplier : MaxSpeed;
var currentAcceleration = isSprinting ? SprintAcceleration : Acceleration;
// Calculate desired movement direction
var forwardVector = Vector2.Right.Rotated(Rotation);
var rightVector = new Vector2(-forwardVector.Y, forwardVector.X);
// Calculate target velocity based on input
var targetVelocity = (forwardVector * moveForward + rightVector * moveHorizontal).Normalized() * currentMaxSpeed;
// Apply acceleration or deceleration
var currentSpeed = Velocity.Length();
var isAccelerating = targetVelocity != Vector2.Zero;
if (isAccelerating)
{
// Accelerate towards target velocity
Velocity = Velocity.MoveToward(targetVelocity, (float)(currentAcceleration * delta));
}
else
{
// Apply deceleration when no input
if (currentSpeed > 0)
{
var decelAmount = (float)(Deceleration * delta);
if (currentSpeed <= decelAmount)
Velocity = Vector2.Zero;
else
Velocity = Velocity.Normalized() * (currentSpeed - decelAmount);
}
}
// Apply the movement
MoveAndSlide(); MoveAndSlide();
} }
} }

View File

@@ -12,7 +12,7 @@ public partial class PlacementManager : Node2D
private Vector2I _hoveredCell; private Vector2I _hoveredCell;
private BaseTile _ghostBuilding; private BaseTile _ghostBuilding;
public void SetCurrentBuilding(string buildingId) public void SetCurrentBuilding(string buildingId)
{ {
_currentBuildingId = buildingId; _currentBuildingId = buildingId;
@@ -23,19 +23,39 @@ public partial class PlacementManager : Node2D
_ghostBuilding = null; _ghostBuilding = null;
} }
// Add this field to PlacementManager
private float _currentRotation = 0f;
// Add this method to handle rotation
private void RotateGhost(bool reverse = false)
{
if (_ghostBuilding == null) return;
if (reverse)
_currentRotation = (_currentRotation - 90f) % 360f;
else
_currentRotation = (_currentRotation + 90f) % 360f;
_ghostBuilding.RotationDegrees = _currentRotation;
}
public override void _Process(double delta) public override void _Process(double delta)
{ {
// Snap mouse to grid // Snap mouse to grid
var mousePos = GetGlobalMousePosition(); var mousePos = GetGlobalMousePosition();
_hoveredCell = GridUtils.WorldToGrid(mousePos); _hoveredCell = GridUtils.WorldToGrid(mousePos);
if (Input.IsActionJustPressed("rotate_tile_reverse"))
RotateGhost(reverse: true);
else if (Input.IsActionJustPressed("rotate_tile"))
RotateGhost();
if (_ghostBuilding == null) if (_ghostBuilding == null)
{ {
var scene = Registry.GetBuilding(_currentBuildingId)?.Scene; var scene = Registry.GetBuilding(_currentBuildingId)?.Scene;
if (scene == null) return; if (scene == null) return;
_ghostBuilding = (BaseTile)scene.Instantiate(); _ghostBuilding = (BaseTile)scene.Instantiate();
_ghostBuilding.SetGhostMode(true); _ghostBuilding.SetGhostMode(true);
_ghostBuilding.RotationDegrees = _currentRotation;
AddChild(_ghostBuilding); AddChild(_ghostBuilding);
} }
@@ -58,13 +78,15 @@ public partial class PlacementManager : Node2D
_ghostBuilding.Position = placementPos; _ghostBuilding.Position = placementPos;
_ghostBuilding.SetGhostMode(canPlace); _ghostBuilding.SetGhostMode(canPlace);
// Left click to place // Left click to place
if (Input.IsActionPressed("build_tile") && canPlace) if (Input.IsActionPressed("build_tile") && canPlace)
{ {
var scene = Registry.GetBuilding(_currentBuildingId)?.Scene; var scene = Registry.GetBuilding(_currentBuildingId)?.Scene;
if (scene == null) return; if (scene == null) return;
_ghostBuilding.FinalizePlacement(); _ghostBuilding.FinalizePlacement();
_ghostBuilding.RotationDegrees = _currentRotation;
var buildingData = Registry.GetBuilding(_currentBuildingId); var buildingData = Registry.GetBuilding(_currentBuildingId);
Grid.OccupyCell(_hoveredCell, _ghostBuilding); Grid.OccupyCell(_hoveredCell, _ghostBuilding);
@@ -74,6 +96,7 @@ public partial class PlacementManager : Node2D
_ghostBuilding = (BaseTile)scene.Instantiate(); _ghostBuilding = (BaseTile)scene.Instantiate();
_ghostBuilding.SetGhostMode(true); _ghostBuilding.SetGhostMode(true);
_ghostBuilding.RotationDegrees = _currentRotation;
AddChild(_ghostBuilding); AddChild(_ghostBuilding);
} }

View File

@@ -55,6 +55,21 @@ destroy_tile={
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(481, 42),"global_position":Vector2(500, 138),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null) "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(481, 42),"global_position":Vector2(500, 138),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null)
] ]
} }
rotate_tile={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":79,"key_label":0,"unicode":111,"location":0,"echo":false,"script":null)
]
}
rotate_tile_reverse={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":79,"key_label":0,"unicode":79,"location":0,"echo":false,"script":null)
]
}
move_sprint={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
[rendering] [rendering]