🎉 Initial Commit
✨ Grid system, player movement etc
			
			
This commit is contained in:
		
							
								
								
									
										26
									
								
								Scripts/Entities/Player.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								Scripts/Entities/Player.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| using System; | ||||
| using Godot; | ||||
|  | ||||
| namespace AceFieldNewHorizon.Scripts.Entities; | ||||
|  | ||||
| public partial class Player : CharacterBody2D | ||||
| { | ||||
|     [Export] public float Speed = 400.0f; | ||||
|  | ||||
|     public override void _Process(double delta) | ||||
|     { | ||||
|         // Get direction to mouse and calculate angle | ||||
|         var mousePos = GetGlobalMousePosition(); | ||||
|         var direction = GlobalPosition.DirectionTo(mousePos); | ||||
|         Rotation = direction.Angle() + (float)Math.PI / 2; | ||||
|     } | ||||
|  | ||||
|     public override void _PhysicsProcess(double delta) | ||||
|     { | ||||
|         // Get movement input | ||||
|         var inputDirection = Input.GetVector("move_left", "move_right", "move_up", "move_down"); | ||||
|         Velocity = inputDirection * Speed; | ||||
|  | ||||
|         MoveAndSlide(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										1
									
								
								Scripts/Entities/Player.cs.uid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Scripts/Entities/Player.cs.uid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| uid://dgm1o21sv2o1x | ||||
							
								
								
									
										45
									
								
								Scripts/System/GridManager.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								Scripts/System/GridManager.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | ||||
| using System.Collections.Generic; | ||||
| using Godot; | ||||
|  | ||||
| namespace AceFieldNewHorizon.Scripts.System; | ||||
|  | ||||
| public partial class GridManager : Node | ||||
| { | ||||
|     private Dictionary<Vector2I, Node2D> _grid = new(); | ||||
|  | ||||
|     public bool IsCellFree(Vector2I cell) | ||||
|     { | ||||
|         return !_grid.ContainsKey(cell); | ||||
|     } | ||||
|  | ||||
|     public void OccupyCell(Vector2I cell, Node2D building) | ||||
|     { | ||||
|         _grid[cell] = building; | ||||
|     } | ||||
|  | ||||
|     public void FreeCell(Vector2I cell) | ||||
|     { | ||||
|         _grid.Remove(cell); | ||||
|     } | ||||
| } | ||||
|  | ||||
| public static class GridUtils | ||||
| { | ||||
|     public const int TileSize = 54; | ||||
|  | ||||
|     public static Vector2I WorldToGrid(Vector2 pos) | ||||
|     { | ||||
|         return new Vector2I( | ||||
|             Mathf.FloorToInt(pos.X / TileSize), | ||||
|             Mathf.FloorToInt(pos.Y / TileSize) | ||||
|         ); | ||||
|     } | ||||
|  | ||||
|     public static Vector2 GridToWorld(Vector2I cell) | ||||
|     { | ||||
|         return new Vector2( | ||||
|             cell.X * TileSize, | ||||
|             cell.Y * TileSize | ||||
|         ); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										1
									
								
								Scripts/System/GridManager.cs.uid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Scripts/System/GridManager.cs.uid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| uid://cudpc3w17mbsw | ||||
							
								
								
									
										40
									
								
								Scripts/System/PlacementManager.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								Scripts/System/PlacementManager.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| using AceFieldNewHorizon.Scripts.Tiles; | ||||
| using Godot; | ||||
|  | ||||
| namespace AceFieldNewHorizon.Scripts.System; | ||||
|  | ||||
| public partial class PlacementManager : Node2D | ||||
| { | ||||
|     [Export] public PackedScene BuildingScene { get; set; } | ||||
|     [Export] public GridManager Grid { get; set; } | ||||
|  | ||||
|     private Vector2I _hoveredCell; | ||||
|     private BaseTile _ghostBuilding; | ||||
|  | ||||
|     public override void _Process(double delta) | ||||
|     { | ||||
|         // Snap mouse to grid | ||||
|         var mousePos = GetGlobalMousePosition(); | ||||
|         _hoveredCell = GridUtils.WorldToGrid(mousePos); | ||||
|  | ||||
|         if (_ghostBuilding == null) | ||||
|         { | ||||
|             _ghostBuilding = (BaseTile)BuildingScene.Instantiate(); | ||||
|             _ghostBuilding.SetGhostMode(true); | ||||
|             AddChild(_ghostBuilding); | ||||
|         } | ||||
|  | ||||
|         _ghostBuilding.Position = GridUtils.GridToWorld(_hoveredCell); | ||||
|         _ghostBuilding.SetGhostMode(true); | ||||
|  | ||||
|         // Left click to place | ||||
|         if (!Input.IsActionPressed("build_tile") || !Grid.IsCellFree(_hoveredCell)) return; | ||||
|  | ||||
|         _ghostBuilding.SetGhostMode(false); | ||||
|         Grid.OccupyCell(_hoveredCell, _ghostBuilding); | ||||
|  | ||||
|         _ghostBuilding = (BaseTile)BuildingScene.Instantiate(); | ||||
|         _ghostBuilding.SetGhostMode(true); | ||||
|         AddChild(_ghostBuilding); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										1
									
								
								Scripts/System/PlacementManager.cs.uid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Scripts/System/PlacementManager.cs.uid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| uid://bx1wj7gn6vrqe | ||||
							
								
								
									
										30
									
								
								Scripts/Tiles/BaseTile.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								Scripts/Tiles/BaseTile.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| using Godot; | ||||
|  | ||||
| namespace AceFieldNewHorizon.Scripts.Tiles; | ||||
|  | ||||
| public partial class BaseTile : Node2D | ||||
| { | ||||
|     protected CollisionShape2D CollisionShape; | ||||
|     protected Sprite2D Sprite; | ||||
|  | ||||
|     public override void _Ready() | ||||
|     { | ||||
|         // Get references (optional: you can also Export and assign in editor) | ||||
|         CollisionShape = GetNode<CollisionShape2D>("CollisionShape2D"); | ||||
|         Sprite = GetNode<Sprite2D>("Sprite2D"); | ||||
|     } | ||||
|  | ||||
|     public void SetGhostMode(bool ghost) | ||||
|     { | ||||
|         if (CollisionShape != null) | ||||
|             CollisionShape.Disabled = ghost; | ||||
|  | ||||
|         if (Sprite != null) | ||||
|         { | ||||
|             if (ghost) | ||||
|                 Sprite.Modulate = new Color(1, 1, 1, 0.5f); // semi-transparent | ||||
|             else | ||||
|                 Sprite.Modulate = Colors.White; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										1
									
								
								Scripts/Tiles/BaseTile.cs.uid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Scripts/Tiles/BaseTile.cs.uid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| uid://clw4adumlgw43 | ||||
							
								
								
									
										8
									
								
								Scripts/Tiles/WallTile.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Scripts/Tiles/WallTile.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| using Godot; | ||||
|  | ||||
| namespace AceFieldNewHorizon.Scripts.Tiles; | ||||
|   | ||||
| public partial class WallTile : BaseTile | ||||
| { | ||||
|      | ||||
| } | ||||
							
								
								
									
										1
									
								
								Scripts/Tiles/WallTile.cs.uid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Scripts/Tiles/WallTile.cs.uid
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| uid://cywgb6hvlw8lr | ||||
		Reference in New Issue
	
	Block a user