Compare commits

...

3 Commits

Author SHA1 Message Date
LittleSheep
e54215423d Grid layer system 2025-08-27 17:52:53 +08:00
LittleSheep
84b908ef51 Rotate building tiles 2025-08-27 17:37:31 +08:00
LittleSheep
c078204e60 Player movement with sprints and optimzation 2025-08-27 17:37:04 +08:00
4 changed files with 167 additions and 26 deletions

View File

@@ -5,7 +5,11 @@ namespace AceFieldNewHorizon.Scripts.Entities;
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;
public override void _Process(double delta)
@@ -20,10 +24,43 @@ public partial class Player : CharacterBody2D
{
// Get movement input
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
Velocity = Vector2.Right.Rotated(Rotation) * moveForward * Speed;
// Calculate movement parameters based on sprint state
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();
}
}

View File

@@ -1,31 +1,68 @@
#nullable enable
using System;
using System.Collections.Generic;
using Godot;
namespace AceFieldNewHorizon.Scripts.System;
public enum GridLayer
{
Ground, // Base layer (e.g., terrain, floors)
Building, // Main building layer
Decoration // Additional layer for decorations, effects, etc.
}
public partial class GridManager : Node
{
private Dictionary<Vector2I, Node2D> _grid = new();
private Dictionary<GridLayer, Dictionary<Vector2I, Node2D>> _layers = new();
public bool IsCellFree(Vector2I cell)
public GridManager()
{
return !_grid.ContainsKey(cell);
// Initialize all layers
foreach (GridLayer layer in Enum.GetValues(typeof(GridLayer)))
{
_layers[layer] = new Dictionary<Vector2I, Node2D>();
}
}
public void OccupyCell(Vector2I cell, Node2D building)
public bool IsCellFree(Vector2I cell, GridLayer layer = GridLayer.Building)
{
_grid[cell] = building;
return !_layers[layer].ContainsKey(cell);
}
public void FreeCell(Vector2I cell)
public void OccupyCell(Vector2I cell, Node2D building, GridLayer layer = GridLayer.Building)
{
_grid.Remove(cell);
_layers[layer][cell] = building;
}
public Node2D? GetBuildingAtCell(Vector2I cell)
public void FreeCell(Vector2I cell, GridLayer layer = GridLayer.Building)
{
return _grid.GetValueOrDefault(cell);
if (_layers[layer].ContainsKey(cell))
_layers[layer].Remove(cell);
}
public Node2D? GetBuildingAtCell(Vector2I cell, GridLayer layer = GridLayer.Building)
{
return _layers[layer].GetValueOrDefault(cell);
}
public bool IsAnyCellOccupied(Vector2I cell, params GridLayer[] layers)
{
if (layers.Length == 0)
{
// Check all layers if none specified
foreach (var layer in _layers.Values)
{
if (layer.ContainsKey(cell)) return true;
}
return false;
}
foreach (var layer in layers)
{
if (_layers[layer].ContainsKey(cell)) return true;
}
return false;
}
}

View File

@@ -1,3 +1,4 @@
using System;
using AceFieldNewHorizon.Scripts.Tiles;
using Godot;
@@ -8,14 +9,16 @@ public partial class PlacementManager : Node2D
[Export] public GridManager Grid { get; set; }
[Export] public BuildingRegistry Registry { get; set; }
private string _currentBuildingId = "miner";
private string _currentBuildingId = "wall";
private GridLayer _currentLayer = GridLayer.Building; // Default layer
private Vector2I _hoveredCell;
private BaseTile _ghostBuilding;
public void SetCurrentBuilding(string buildingId)
private float _currentRotation = 0f;
public void SetCurrentBuilding(string buildingId, GridLayer layer = GridLayer.Building)
{
_currentBuildingId = buildingId;
_currentLayer = layer;
// Replace ghost immediately
if (_ghostBuilding == null) return;
@@ -23,19 +26,48 @@ public partial class PlacementManager : Node2D
_ghostBuilding = null;
}
public void SetCurrentLayer(GridLayer layer)
{
_currentLayer = layer;
// Update ghost building if needed
if (_ghostBuilding != null)
{
_ghostBuilding.QueueFree();
_ghostBuilding = null;
}
}
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)
{
// Snap mouse to grid
var mousePos = GetGlobalMousePosition();
_hoveredCell = GridUtils.WorldToGrid(mousePos);
if (Input.IsActionJustPressed("rotate_tile_reverse"))
RotateGhost(reverse: true);
else if (Input.IsActionJustPressed("rotate_tile"))
RotateGhost();
if (_ghostBuilding == null)
{
var scene = Registry.GetBuilding(_currentBuildingId)?.Scene;
if (scene == null) return;
_ghostBuilding = (BaseTile)scene.Instantiate();
_ghostBuilding.SetGhostMode(true);
_ghostBuilding.RotationDegrees = _currentRotation;
_ghostBuilding.ZAsRelative = false;
_ghostBuilding.ZIndex = (int)_currentLayer; // Use layer as Z-index
AddChild(_ghostBuilding);
}
@@ -48,12 +80,14 @@ public partial class PlacementManager : Node2D
Position = centerPos,
CollideWithBodies = true,
CollideWithAreas = true,
CollisionMask = uint.MaxValue // check against all layers/masks
// Exclude = new Godot.Collections.Array<Rid>() // optional, see below
CollisionMask = uint.MaxValue
};
var collision = spaceState.IntersectPoint(query, 8);
var canPlace = Grid.IsCellFree(_hoveredCell) && collision.Count == 0;
// Check if the current cell is free in the current layer
var canPlace = Grid.IsCellFree(_hoveredCell, _currentLayer) &&
!Grid.IsAnyCellOccupied(_hoveredCell, GetBlockingLayers());
_ghostBuilding.Position = placementPos;
_ghostBuilding.SetGhostMode(canPlace);
@@ -63,27 +97,45 @@ public partial class PlacementManager : Node2D
{
var scene = Registry.GetBuilding(_currentBuildingId)?.Scene;
if (scene == null) return;
_ghostBuilding.FinalizePlacement();
_ghostBuilding.RotationDegrees = _currentRotation;
_ghostBuilding.ZIndex = (int)_currentLayer;
var buildingData = Registry.GetBuilding(_currentBuildingId);
Grid.OccupyCell(_hoveredCell, _ghostBuilding);
Grid.OccupyCell(_hoveredCell, _ghostBuilding, _currentLayer);
if (buildingData is { BuildTime: > 0f })
_ghostBuilding.StartConstruction(buildingData.BuildTime);
// Create new ghost for next placement
_ghostBuilding = (BaseTile)scene.Instantiate();
_ghostBuilding.SetGhostMode(true);
_ghostBuilding.RotationDegrees = _currentRotation;
_ghostBuilding.ZAsRelative = false;
_ghostBuilding.ZIndex = (int)_currentLayer;
AddChild(_ghostBuilding);
}
if (Input.IsActionPressed("destroy_tile") && !Grid.IsCellFree(_hoveredCell))
if (Input.IsActionPressed("destroy_tile") && !Grid.IsCellFree(_hoveredCell, _currentLayer))
{
// Right click to destroy
var building = Grid.GetBuildingAtCell(_hoveredCell);
// Right click to destroy from current layer
var building = Grid.GetBuildingAtCell(_hoveredCell, _currentLayer);
if (building == null) return;
building.QueueFree();
Grid.FreeCell(_hoveredCell);
Grid.FreeCell(_hoveredCell, _currentLayer);
}
}
// Define which layers should block placement
private GridLayer[] GetBlockingLayers()
{
return _currentLayer switch
{
GridLayer.Ground => new[] { GridLayer.Ground },
GridLayer.Building => new[] { GridLayer.Ground, GridLayer.Building },
GridLayer.Decoration => new[] { GridLayer.Ground, GridLayer.Building, GridLayer.Decoration },
_ => []
};
}
}

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)
]
}
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]