chunk class was missing set_tileset() ...

This commit is contained in:
Antti Hakkarainen 2023-02-12 19:12:27 +02:00
parent abbaa6f450
commit fccef57210
7 changed files with 39 additions and 50 deletions

View file

@ -25,8 +25,8 @@ gdscript/warnings/integer_division=0
[display] [display]
window/size/viewport_width=1920 window/size/viewport_width=2560
window/size/viewport_height=1080 window/size/viewport_height=1440
window/size/resizable=false window/size/resizable=false
[input] [input]

View file

@ -7,18 +7,18 @@ var y:int = -1
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _init(xpos:int, ypos:int): func _init(xpos:int, ypos:int):
self.x = xpos self.x = xpos
self.y = ypos self.y = ypos
self.name = "Chunk [%d,%d]" % [x, y]
func _draw(): self.set_tileset(Globals.TILESET_TERRAIN)
draw_circle(Vector2(x,y), 25.0, Color(0,0,0)) self.position = Vector2i(
x*Globals.CHUNK_SIZE*Globals.TILE_SIZE_X,
y*Globals.CHUNK_SIZE*Globals.TILE_SIZE_Y
)
func generate_chunk() -> void: func generate_chunk() -> void:
for row in Globals.CHUNK_SIZE: for row in Globals.CHUNK_SIZE:
for col in Globals.CHUNK_SIZE: for col in Globals.CHUNK_SIZE:
var tile_data: Array = Globals.map_tile_data[row+y*Globals.CHUNK_SIZE][col+x*Globals.CHUNK_SIZE] var tile_data: Array = Globals.map_tile_data[row+y*Globals.CHUNK_SIZE][col+x*Globals.CHUNK_SIZE]
#var msg = "%s %s %s"
#print(msg % [tile_data, row, col])
# layer | tile coords at tilemap | tilemap id | coords of the tile at tileset | alternative tile # layer | tile coords at tilemap | tilemap id | coords of the tile at tileset | alternative tile
self.set_cell( self.set_cell(
Globals.LAYER_TERRAIN, Globals.LAYER_TERRAIN,

View file

@ -6,4 +6,5 @@
[node name="Chunk" type="TileMap"] [node name="Chunk" type="TileMap"]
tile_set = ExtResource("1_x70ay") tile_set = ExtResource("1_x70ay")
format = 2 format = 2
layer_0/tile_data = PackedInt32Array(0, 0, 65536, -1, 0, 65536)
script = ExtResource("1_lawsj") script = ExtResource("1_lawsj")

View file

@ -1,22 +1,13 @@
[gd_scene load_steps=7 format=3 uid="uid://nfayf78xiuap"] [gd_scene load_steps=5 format=3 uid="uid://nfayf78xiuap"]
[ext_resource type="Script" path="res://scripts/Main.gd" id="1_ysxum"] [ext_resource type="Script" path="res://scripts/Main.gd" id="1_ysxum"]
[ext_resource type="Script" path="res://scripts/ChunkHandler.gd" id="2_2e0t1"]
[ext_resource type="Script" path="res://scripts/Control.gd" id="3_1t1c8"] [ext_resource type="Script" path="res://scripts/Control.gd" id="3_1t1c8"]
[ext_resource type="Texture2D" uid="uid://cxn2fno1j7vf5" path="res://icon.svg" id="3_c64oi"]
[ext_resource type="PackedScene" uid="uid://2we3txfr812u" path="res://scenes/Camera_zoom_2d.tscn" id="4_rx82t"] [ext_resource type="PackedScene" uid="uid://2we3txfr812u" path="res://scenes/Camera_zoom_2d.tscn" id="4_rx82t"]
[ext_resource type="Script" path="res://scripts/EntityPlacer.gd" id="5_8jju5"] [ext_resource type="Script" path="res://scripts/EntityPlacer.gd" id="5_8jju5"]
[node name="Main" type="Node2D"] [node name="Main" type="Node2D"]
script = ExtResource("1_ysxum") script = ExtResource("1_ysxum")
[node name="ChunkHandler" type="Node2D" parent="."]
script = ExtResource("2_2e0t1")
[node name="Sprite2D" type="Sprite2D" parent="ChunkHandler"]
position = Vector2(952, 536)
texture = ExtResource("3_c64oi")
[node name="EntityPlacer" type="Control" parent="."] [node name="EntityPlacer" type="Control" parent="."]
layout_mode = 3 layout_mode = 3
anchors_preset = 0 anchors_preset = 0

View file

@ -9,35 +9,36 @@ extends Node2D
# This is done to speed up game loading and avoiding setting one large tilemap in one go # This is done to speed up game loading and avoiding setting one large tilemap in one go
# which is extremely slow in godot 4.0, 4096x4096 takes minutes to fill with set_cell() commands # which is extremely slow in godot 4.0, 4096x4096 takes minutes to fill with set_cell() commands
var map_tiles:Array[Array] = [[]]
var chunks:Dictionary = {} var chunks:Dictionary = {}
var unused_chunks:Dictionary = {} var unused_chunks:Dictionary = {}
func _init() -> void:
self.name = "ChunkHandler"
func _ready(): func _ready():
#thread = Thread.new() #thread = Thread.new()
pass pass
#var map_tiles:Array[Array] = [[]]
#
#func start_handler() -> void: func start_handler() -> void:
# # Initialize the map tile array with enough chunks to cover the whole map # Initialize the map tile array with enough chunks to cover the whole map
# map_tiles.resize(Globals.map_size/Globals.CHUNK_SIZE) map_tiles.resize(Globals.map_size/Globals.CHUNK_SIZE)
# for y in map_tiles.size(): for y in map_tiles.size():
# map_tiles[y].resize(Globals.map_size/Globals.CHUNK_SIZE) map_tiles[y].resize(Globals.map_size/Globals.CHUNK_SIZE)
# for x in map_tiles.size():
# map_tiles[y][x] = chunk.instantiate()
func add_chunk(x:int, y:int): func add_chunk(x:int, y:int):
var chunk = Chunk.new(x,y) var chunk = Chunk.new(x,y)
var start = Time.get_ticks_usec()
chunk.generate_chunk()
self.add_child(chunk) self.add_child(chunk)
var end = Time.get_ticks_usec()
chunk.generate_chunk() print("generate a chunk ", (end-start)/1000.0, "ms")
chunk.set_position(Vector2(x*Globals.CHUNK_SIZE,y*Globals.CHUNK_SIZE))
chunk.set_visible(true)
#chunk.set_position(Vector2(x*Globals.CHUNK_SIZE,y*Globals.CHUNK_SIZE))
print("nodes in children: ", self.get_children()[0].get_used_cells(Globals.LAYER_TERRAIN).size())
print("chunk visible in tree? : ", self.get_children()[0].is_visible_in_tree())
print("handler visible? ", self.is_visible_in_tree())
#map_tiles[chunk_pos.y][chunk_pos.x].clear() #map_tiles[chunk_pos.y][chunk_pos.x].clear()

View file

@ -4,7 +4,7 @@
extends Node extends Node
# world map chunk size # world map chunk size
const CHUNK_SIZE:int = 128 const CHUNK_SIZE:int = 16
# tilemap tile types # tilemap tile types
enum {TILE_WATER, TILE_TERRAIN, TILE_FOREST, TILE_BOG} enum {TILE_WATER, TILE_TERRAIN, TILE_FOREST, TILE_BOG}
@ -12,6 +12,8 @@ enum {TILE_WATER, TILE_TERRAIN, TILE_FOREST, TILE_BOG}
# tilemap layers # tilemap layers
enum {LAYER_TERRAIN, LAYER_BUILDINGS} enum {LAYER_TERRAIN, LAYER_BUILDINGS}
const TILESET_TERRAIN:TileSet = preload("res://scenes/Chunk.tres")
func are_coords_valid(value:int, bounds:Vector2i, errmsg:String) -> bool: func are_coords_valid(value:int, bounds:Vector2i, errmsg:String) -> bool:
if bounds.x > value or value > bounds.y: if bounds.x > value or value > bounds.y:
errmsg = errmsg % [value, bounds.x, bounds.y] errmsg = errmsg % [value, bounds.x, bounds.y]

View file

@ -40,13 +40,11 @@ func _ready():
var result = _world_generator.generate_world(map_filename) var result = _world_generator.generate_world(map_filename)
if result: if result:
#_chunk_handler.start_handler() _chunk_handler.start_handler()
_chunk_handler.set_visible(true) for y in Globals.map_size/Globals.CHUNK_SIZE:
_chunk_handler.add_chunk(0, 0) for x in Globals.map_size/Globals.CHUNK_SIZE:
#_chunk_handler.show() if (y + x) % 2 == 0:
#_chunk_handler.set_visible(true) _chunk_handler.add_chunk(x, y)
print("main visible? ", is_visible_in_tree())
else: else:
push_error("World generation failed :-(") push_error("World generation failed :-(")
@ -56,10 +54,6 @@ func _ready():
Vector2(Globals.map_size / 2.0 * Globals.TILE_SIZE_X, Vector2(Globals.map_size / 2.0 * Globals.TILE_SIZE_X,
Globals.map_size / 2.0 * Globals.TILE_SIZE_Y) Globals.map_size / 2.0 * Globals.TILE_SIZE_Y)
) )
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
func quit_game(): func quit_game():
get_tree().get_root().propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST) get_tree().get_root().propagate_notification(NOTIFICATION_WM_CLOSE_REQUEST)