45 lines
1.3 KiB
GDScript
45 lines
1.3 KiB
GDScript
extends Node
|
|
|
|
const CHUNK_SIZE: int = 12;
|
|
const CHUNK_DOUBLE: int = CHUNK_SIZE*2;
|
|
|
|
func _get_player_position() -> Vector2i:
|
|
return Vector2i(1, 1)
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
pass # Replace with function body.
|
|
|
|
|
|
# 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, _pick_random_tile())
|
|
|
|
|
|
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
|
|
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; |