Map generation.

This commit is contained in:
gdz
2025-12-06 13:39:23 +01:00
parent aa1497ee0c
commit 7335047f55
2 changed files with 33 additions and 7 deletions

View File

@@ -3,13 +3,18 @@ extends Node
const CHUNK_SIZE: int = 12;
const CHUNK_DOUBLE: int = CHUNK_SIZE*2;
func _get_player_position() -> Vector2i:
return Vector2i(1, 1)
const GREEN_TILE = Vector2i(1, 1)
var tilemap: TileMap;
func _get_player_position() -> Vector2i:
var playerPosition: Vector2i = $Ground.local_to_map($Player/AnimatedSprite2D.global_position)
print("Playerposition is " + str(playerPosition))
return playerPosition
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
_populate_terrain()
# Called every frame. 'delta' is the elapsed time since the previous frame.
@@ -30,11 +35,31 @@ func _populate_terrain():
for j in range(-CHUNK_DOUBLE, CHUNK_DOUBLE):
var pos: Vector2i = player_position + Vector2i(i, j)
if _is_empty(pos):
_populate_cell(pos, _pick_random_tile())
_populate_cell(pos, 1, _pick_random_tile())
#func _populate_cell(pos: Vector2i, tile: Vector2i) -> void:
#$Ground.set_cell.call_deferet(pos, 1, tile, 0)
func _populate_cell(coords: Vector2i, source_id: int, atlas_coords: Vector2i) -> void:
var alternativeTilesCount: int = $Ground.tile_set.get_source(source_id).get_alternative_tiles_count(atlas_coords)
var alternativeTileId: int = 0
if alternativeTilesCount > 0:
alternativeTileId = randi_range(0, alternativeTilesCount-1)
# set_cell(coords: Vector2i, source_id: int = -1, atlas_coords: Vector2i = Vector2i(-1, -1), alternative_tile: int = 0)
$Ground.set_cell.call_deferred(coords, source_id, atlas_coords, alternativeTileId)
func _populate_cell_ds(layer_id: int, coords: Vector2i, source_id: int, atlas_coords: Vector2i) -> void:
var alternative_tiles_count = (
tilemap.tile_set.get_source(source_id).get_alternative_tiles_count(atlas_coords)
)
var alternative_tile_id = 0
if alternative_tiles_count > 0:
alternative_tile_id = randi_range(0, alternative_tiles_count - 1)
tilemap.set_cell.call_deferred(layer_id, coords, source_id, atlas_coords, alternative_tile_id)
func _populate_cell(pos: Vector2i, tile: Vector2i) -> void:
$Ground.set_cell.call_deferet(pos, 1, tile, 0)
func _pick_random_tile() -> Vector2i:
# Make all Tiles green
@@ -42,4 +67,4 @@ func _pick_random_tile() -> Vector2i:
func _is_empty(pos: Vector2i) -> bool:
# Check if the cell is empty (source_id is -1)
return true if $Ground.get_cell_source_id(pos) == -1 else false;
return true if $Ground.get_cell_source_id(pos) == -1 else false;

View File

@@ -37,6 +37,7 @@ position = Vector2(576, 324)
rect = Rect2(0, 0, 32, 32)
[node name="Player" type="Area2D" parent="." groups=["Player"]]
position = Vector2(100, 100)
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="Player"]
scale = Vector2(0.165, 0.165)