82 lines
2.7 KiB
GDScript
82 lines
2.7 KiB
GDScript
extends Node
|
|
|
|
const TERRAIN_LAYER_ID: int = 0
|
|
const OBSTACLE_LAYER_ID: int = 1
|
|
const TERRAIN_SOURCE_ID: int = 1
|
|
const OBSTACLE_SOURCE_ID: int = 2
|
|
const CHUNK_SIZE: int = 15
|
|
const CLEAN_UP_EVERY: int = 3
|
|
const OBSTACLE_PROBABILITY: float = 0.01
|
|
|
|
var _source: TileSetAtlasSource
|
|
var _obstacle_source: TileSetAtlasSource
|
|
var _tile_probabilities: Dictionary
|
|
var _obstacle_probabilities: Dictionary
|
|
var _clean_up_counter: int
|
|
|
|
const CHUNK_DOUBLE: int = CHUNK_SIZE*2;
|
|
|
|
const GREEN_TILE: Vector2i = Vector2i(1, 1)
|
|
|
|
var tilemap: TileMap;
|
|
|
|
@onready var Player = $Player/AnimatedSprite2D
|
|
@onready var GroundLayer: TileMapLayer = $Ground
|
|
|
|
func _get_player_position() -> Vector2i:
|
|
var playerPosition: Vector2i = GroundLayer.local_to_map(Player.global_position)
|
|
print("Playerposition is " + str(playerPosition))
|
|
return playerPosition
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
_source = $Ground.tile_set.get_source(TERRAIN_SOURCE_ID) as TileSetAtlasSource
|
|
var grid_size: Vector2i = _source.get_atlas_grid_size()
|
|
for i in range(grid_size.x):
|
|
for j in range(grid_size.y):
|
|
var atlas_coords: Vector2i = Vector2i(i, j)
|
|
var has_tile: bool = _source.has_tile(atlas_coords)
|
|
_tile_probabilities[atlas_coords] = (_source.get_tile_data(atlas_coords, 0).probability if has_tile else 0.0)
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
pass
|
|
|
|
func _on_exited_chunk():
|
|
print("Chunk exited...")
|
|
$VisibleOnScreenNotifier2D.global_position = 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(coords: Vector2i, source_id: int, atlas_coords: Vector2i) -> void:
|
|
var alternativeTilesCount: int = GroundLayer.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)
|
|
GroundLayer.set_cell.call_deferred(coords, source_id, atlas_coords, alternativeTileId)
|
|
|
|
|
|
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 GroundLayer.get_cell_source_id(pos) == -1 else false;
|