Restructuring of Nodes and Scenes. Added WorldCamera. Added a Main Scene.
This commit is contained in:
14
Scenes/Main/main.gd
Normal file
14
Scenes/Main/main.gd
Normal file
@@ -0,0 +1,14 @@
|
||||
extends Node
|
||||
|
||||
@onready var _Map = $Map
|
||||
@onready var _Player = $Player
|
||||
@onready var _Camera = $WorldCamera
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
_Map.populate_terrain(_Camera.global_position)
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
1
Scenes/Main/main.gd.uid
Normal file
1
Scenes/Main/main.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://btdvxp8ckmeb3
|
||||
16
Scenes/Main/main.tscn
Normal file
16
Scenes/Main/main.tscn
Normal file
@@ -0,0 +1,16 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://d05j5yuhlsxp0"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://cywuuce71rmgb" path="res://Scenes/Map/map.tscn" id="1_1r6ip"]
|
||||
[ext_resource type="Script" uid="uid://btdvxp8ckmeb3" path="res://Scenes/Main/main.gd" id="1_qw60k"]
|
||||
[ext_resource type="PackedScene" uid="uid://dh8pjs2s1kud3" path="res://Scenes/Player/player.tscn" id="2_lixft"]
|
||||
|
||||
[node name="Main" type="Node"]
|
||||
script = ExtResource("1_qw60k")
|
||||
|
||||
[node name="Map" parent="." instance=ExtResource("1_1r6ip")]
|
||||
|
||||
[node name="Player" parent="." instance=ExtResource("2_lixft")]
|
||||
|
||||
[node name="WorldCamera" type="Camera2D" parent="."]
|
||||
|
||||
[editable path="Player"]
|
||||
11
Scenes/Main/world_camera.gd
Normal file
11
Scenes/Main/world_camera.gd
Normal file
@@ -0,0 +1,11 @@
|
||||
extends Camera2D
|
||||
|
||||
|
||||
# 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
|
||||
1
Scenes/Main/world_camera.gd.uid
Normal file
1
Scenes/Main/world_camera.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cpp4vte6rs37q
|
||||
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
|
||||
24
Scenes/Player/player.gd
Normal file
24
Scenes/Player/player.gd
Normal file
@@ -0,0 +1,24 @@
|
||||
extends Area2D
|
||||
class_name Player
|
||||
|
||||
const GROUP = "player"
|
||||
|
||||
const _movementSpeed = 2
|
||||
|
||||
# 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:
|
||||
if Input.is_action_pressed("MoveUp"):
|
||||
$AnimatedSprite2D.global_position += Vector2.UP * 2
|
||||
if Input.is_action_pressed("MoveDown"):
|
||||
$AnimatedSprite2D.global_position += Vector2.DOWN * 2
|
||||
if Input.is_action_pressed("MoveLeft"):
|
||||
$AnimatedSprite2D.global_position += Vector2.LEFT * 2
|
||||
if Input.is_action_pressed("MoveRight"):
|
||||
$AnimatedSprite2D.global_position += Vector2.RIGHT * 2
|
||||
|
||||
$Camera2D.global_position = $AnimatedSprite2D.global_position
|
||||
1
Scenes/Player/player.gd.uid
Normal file
1
Scenes/Player/player.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://q6cxagn6igja
|
||||
34
Scenes/Player/player.tscn
Normal file
34
Scenes/Player/player.tscn
Normal file
@@ -0,0 +1,34 @@
|
||||
[gd_scene load_steps=5 format=3 uid="uid://dh8pjs2s1kud3"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://q6cxagn6igja" path="res://Scenes/Player/player.gd" id="1_a3d8c"]
|
||||
[ext_resource type="Texture2D" uid="uid://04ecsvg7ausn" path="res://icon.svg" id="2_xpcdj"]
|
||||
|
||||
[sub_resource type="SpriteFrames" id="SpriteFrames_c3an2"]
|
||||
animations = [{
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": ExtResource("2_xpcdj")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"default",
|
||||
"speed": 5.0
|
||||
}]
|
||||
|
||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_vjcy3"]
|
||||
size = Vector2(32, 32)
|
||||
|
||||
[node name="Player" type="Area2D" groups=["player"]]
|
||||
script = ExtResource("1_a3d8c")
|
||||
metadata/_edit_group_ = true
|
||||
metadata/_edit_lock_ = true
|
||||
|
||||
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
|
||||
position = Vector2(4.76837e-07, -4.76837e-07)
|
||||
scale = Vector2(0.25, 0.25)
|
||||
sprite_frames = SubResource("SpriteFrames_c3an2")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
shape = SubResource("RectangleShape2D_vjcy3")
|
||||
|
||||
[node name="PlayerCamera" type="Camera2D" parent="."]
|
||||
zoom = Vector2(4, 4)
|
||||
Reference in New Issue
Block a user