Restructuring of Nodes and Scenes. Added WorldCamera. Added a Main Scene.
This commit is contained in:
88
Scenes/Map/Map.gd
Normal file
88
Scenes/Map/Map.gd
Normal file
@@ -0,0 +1,88 @@
|
||||
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(startPos: Vector2i):
|
||||
for i in range(-CHUNK_DOUBLE, CHUNK_DOUBLE):
|
||||
for j in range(-CHUNK_DOUBLE, CHUNK_DOUBLE):
|
||||
var pos: Vector2i = startPos + Vector2i(i, j)
|
||||
if _is_empty(pos):
|
||||
_populate_cell(pos, 1, _pick_random_tile())
|
||||
|
||||
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;
|
||||
1
Scenes/Map/Map.gd.uid
Normal file
1
Scenes/Map/Map.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dneqmqsd8yj4p
|
||||
39
Scenes/Map/map.tscn
Normal file
39
Scenes/Map/map.tscn
Normal file
@@ -0,0 +1,39 @@
|
||||
[gd_scene load_steps=5 format=3 uid="uid://cywuuce71rmgb"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dneqmqsd8yj4p" path="res://Scenes/Map/Map.gd" id="1_anho1"]
|
||||
[ext_resource type="TileSet" uid="uid://duodt2t14xjc8" path="res://Resource/UrbanKitTileMap.tres" id="2_fyo8k"]
|
||||
[ext_resource type="Script" uid="uid://bapvlrx6dm7gu" path="res://Scenes/Map/visible_on_screen_notifier_2d.gd" id="3_eu35m"]
|
||||
|
||||
[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_vjcy3"]
|
||||
|
||||
[node name="Map" type="Node"]
|
||||
script = ExtResource("1_anho1")
|
||||
|
||||
[node name="Ground" type="TileMapLayer" parent="."]
|
||||
tile_set = ExtResource("2_fyo8k")
|
||||
metadata/_edit_lock_ = true
|
||||
|
||||
[node name="Roads" type="TileMapLayer" parent="."]
|
||||
tile_set = ExtResource("2_fyo8k")
|
||||
metadata/_edit_lock_ = true
|
||||
|
||||
[node name="Buildings" type="TileMapLayer" parent="."]
|
||||
tile_set = ExtResource("2_fyo8k")
|
||||
metadata/_edit_lock_ = true
|
||||
|
||||
[node name="Details" type="TileMapLayer" parent="."]
|
||||
tile_set = ExtResource("2_fyo8k")
|
||||
metadata/_edit_lock_ = true
|
||||
|
||||
[node name="VisibleOnScreenNotifier2D" type="VisibleOnScreenNotifier2D" parent="."]
|
||||
rect = Rect2(0, 0, 100, 100)
|
||||
script = ExtResource("3_eu35m")
|
||||
metadata/_edit_lock_ = true
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="VisibleOnScreenNotifier2D"]
|
||||
position = Vector2(50, 50)
|
||||
scale = Vector2(10, 10)
|
||||
texture = SubResource("PlaceholderTexture2D_vjcy3")
|
||||
|
||||
[connection signal="screen_exited" from="VisibleOnScreenNotifier2D" to="." method="_on_exited_chunk"]
|
||||
15
Scenes/Map/visible_on_screen_notifier_2d.gd
Normal file
15
Scenes/Map/visible_on_screen_notifier_2d.gd
Normal file
@@ -0,0 +1,15 @@
|
||||
extends VisibleOnScreenNotifier2D
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
_calculateRect()
|
||||
get_tree().root.size_changed.connect(_calculateRect)
|
||||
|
||||
|
||||
func _calculateRect():
|
||||
var multiplier: float = get_viewport_rect().size.y / get_viewport_rect().size.x
|
||||
var size: float = 640 / multiplier
|
||||
|
||||
rect.position.y = -size/2
|
||||
rect.size.y = size
|
||||
1
Scenes/Map/visible_on_screen_notifier_2d.gd.uid
Normal file
1
Scenes/Map/visible_on_screen_notifier_2d.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bapvlrx6dm7gu
|
||||
Reference in New Issue
Block a user