71 lines
2.4 KiB
GDScript
71 lines
2.4 KiB
GDScript
extends Node
|
|
|
|
const CHUNK_SIZE: int = 12;
|
|
const CHUNK_DOUBLE: int = CHUNK_SIZE*2;
|
|
|
|
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:
|
|
_populate_terrain()
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
pass
|
|
|
|
func _on_exited_chunk():
|
|
$VisibleOnScreenNotifier2D.global_position = (get_tree().get_first_node_in_group("Player").global_position)
|
|
|
|
var thread: Thread = Thread.new()
|
|
thread.start(func(): _populate_terrain())
|
|
thread.wait_to_finish()
|
|
|
|
func _populate_terrain():
|
|
var player_position: Vector2i = _get_player_position()
|
|
|
|
for i in range(-CHUNK_DOUBLE, CHUNK_DOUBLE):
|
|
for j in range(-CHUNK_DOUBLE, CHUNK_DOUBLE):
|
|
var pos: Vector2i = player_position + Vector2i(i, j)
|
|
if _is_empty(pos):
|
|
_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 _pick_random_tile() -> Vector2i:
|
|
# Make all Tiles green
|
|
return Vector2i(1, 1)
|
|
|
|
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;
|