Skip to content

Commit 1b592a7

Browse files
Merge pull request #4 from LeonardoCruzx/feat/adicionar-tile-data-e-tile-manager
Feat/adicionar tile data e tile manager
2 parents d4ff361 + 3c8bfe0 commit 1b592a7

File tree

11 files changed

+105
-3
lines changed

11 files changed

+105
-3
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Runtime.CompilerServices;
4+
using DungeonRoyale.Modules.Tiles.Scripts;
5+
using DungeonRoyale.Shared.Scripts.Constants;
6+
7+
namespace DungeonRoyale.Modules.GameManagers.Scripts;
8+
9+
public partial class TilesManager : Node2D
10+
{
11+
public static TilesManager? Instance { get; private set; }
12+
13+
public DRTileData[,] Tiles { get; private set; } = new DRTileData[0, 0];
14+
15+
private int _width;
16+
private int _height;
17+
18+
public override void _Ready()
19+
{
20+
if (Instance is null)
21+
{
22+
Instance = this;
23+
}
24+
else
25+
{
26+
GD.PrintErr("There is already an instance of TilesManager in the scene.");
27+
}
28+
}
29+
30+
public void SetUpTiles(int width, int height)
31+
{
32+
if (width <= 0 || height <= 0)
33+
{
34+
throw new ArgumentException("Width and height must be greater than 0.");
35+
}
36+
37+
_width = width;
38+
_height = height;
39+
40+
Tiles = new DRTileData[_width, _height];
41+
}
42+
43+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
44+
private bool IsOutOfMapBounds(int x, int y) =>
45+
x < 0 || x >= _width || y < 0 || y >= _height;
46+
47+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
48+
public DRTileData? GetTileAt(int x, int y) =>
49+
IsOutOfMapBounds(x, y) ? null : Tiles[x, y];
50+
51+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52+
public bool TryGetTileAt(int x, int y, [NotNullWhen(returnValue: true)] out DRTileData? tileData) =>
53+
(tileData = GetTileAt(x, y)) is not null;
54+
55+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
56+
public bool TryGetTileAt((int x, int y) position, [NotNullWhen(returnValue: true)] out DRTileData? tileData) =>
57+
TryGetTileAt(position.x, position.y, out tileData);
58+
59+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60+
public bool TryGetTileAt(Vector2I position, [NotNullWhen(returnValue: true)] out DRTileData? tileData) =>
61+
TryGetTileAt(position.X, position.Y, out tileData);
62+
63+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
64+
public bool TryGetTileAtGlobalCoords(Vector2 position, [NotNullWhen(returnValue: true)] out DRTileData? tileData) =>
65+
(tileData = GetTileAt((int) position.X / TileConstants.TILE_SIZE, (int) position.Y / TileConstants.TILE_SIZE)) is not null;
66+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://bmr66sr26pdab

Modules/Player/Scenes/player.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
[ext_resource type="Script" uid="uid://de61d5m60vonw" path="res://Modules/Player/Scripts/Player.cs" id="1_d6hpj"]
44
[ext_resource type="Texture2D" uid="uid://brmg31ce6noqj" path="res://Modules/Player/Textures/d_idle.png" id="2_4itp8"]
5-
[ext_resource type="Texture2D" uid="uid://du7cgbk1wqk0g" path="res://Modules/Player/Textures/Idle.png" id="2_eeunm"]
5+
[ext_resource type="Texture2D" uid="uid://jf1knh77qpy2" path="res://Modules/Player/Textures/u_idle.png" id="2_eeunm"]
66
[ext_resource type="Texture2D" uid="uid://bu3wecbmouixt" path="res://Modules/Player/Textures/d_walk.png" id="2_i873j"]
77
[ext_resource type="Texture2D" uid="uid://c31cs04ddln8k" path="res://Modules/Player/Textures/s_walk.png" id="3_bxhrv"]
8-
[ext_resource type="Texture2D" uid="uid://dpu3tffj8in05" path="res://Modules/Player/Textures/walk.png" id="3_umw3d"]
8+
[ext_resource type="Texture2D" uid="uid://bu8810dpn686q" path="res://Modules/Player/Textures/u_walk.png" id="3_umw3d"]
99
[ext_resource type="Texture2D" uid="uid://dyoql8jmirecv" path="res://Modules/Player/Textures/s_idle.png" id="4_5c0ey"]
1010

1111
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_d6hpj"]

Modules/Player/Scripts/Player.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using DungeonRoyale.Shared.Scripts.Constants;
2+
13
namespace DungeonRoyale.Modules.Player.Scripts;
24

35
public partial class Player : CharacterBody2D
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace DungeonRoyale.Modules.Tiles.Scripts;
2+
3+
public partial class DRTileData
4+
{
5+
public int X { get; private set; }
6+
public int Y { get; private set; }
7+
8+
public bool IsWalkable { get; private set; }
9+
10+
public bool IsSpawnPoint { get; private set; }
11+
12+
public DRTileData()
13+
{
14+
}
15+
16+
public DRTileData(int x, int y)
17+
{
18+
X = x;
19+
Y = y;
20+
}
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://d0s8c7afp4vgy

Shared/Scripts/MappedInputs.cs renamed to Shared/Scripts/Constants/MappedInputs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace DungeonRoyale.Shared.Scripts;
1+
namespace DungeonRoyale.Shared.Scripts.Constants;
22

33
public static class MappedInputs
44
{
File renamed without changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace DungeonRoyale.Shared.Scripts.Constants;
2+
3+
public static class TileConstants
4+
{
5+
public const int TILE_SIZE = 16;
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://1v5312025i62

0 commit comments

Comments
 (0)