load and remove chunks around camera pos working

This commit is contained in:
Antti Hakkarainen 2023-02-12 22:17:21 +02:00
parent fccef57210
commit 0109934933
8 changed files with 137 additions and 73 deletions

View file

@ -3,22 +3,39 @@ extends TileMap
var x:int = -1
var y:int = -1
var should_remove:bool = true
# Called when the node enters the scene tree for the first time.
func _init(xpos:int, ypos:int):
self.x = xpos
self.y = ypos
self.name = "Chunk [%d,%d]" % [x, y]
self.set_tileset(Globals.TILESET_TERRAIN)
self.position = Vector2i(
x*Globals.CHUNK_SIZE*Globals.TILE_SIZE_X,
y*Globals.CHUNK_SIZE*Globals.TILE_SIZE_Y
x*Globals.CHUNK_SIZE.x*Globals.TILE_SIZE_X,
y*Globals.CHUNK_SIZE.y*Globals.TILE_SIZE_Y
)
func _ready():
generate_chunk()
func _draw():
self.draw_rect(
Rect2(
Vector2(0,0),
Vector2(
Globals.CHUNK_SIZE.x*Globals.TILE_SIZE_X,
Globals.CHUNK_SIZE.y*Globals.TILE_SIZE_Y)
),
Color(0,0,0,0.5),
false
)
func generate_chunk() -> void:
for row 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]
for row in Globals.CHUNK_SIZE.y:
for col in Globals.CHUNK_SIZE.x:
var tile_data: Array = Globals.map_tile_data[row+y*Globals.CHUNK_SIZE.y][col+x*Globals.CHUNK_SIZE.x]
# layer | tile coords at tilemap | tilemap id | coords of the tile at tileset | alternative tile
self.set_cell(
Globals.LAYER_TERRAIN,

View file

@ -1,10 +0,0 @@
[gd_scene load_steps=3 format=3 uid="uid://54iw2fjk2oth"]
[ext_resource type="Script" path="res://scenes/Chunk.gd" id="1_lawsj"]
[ext_resource type="TileSet" uid="uid://cxiva1a6jdae2" path="res://scenes/Chunk.tres" id="1_x70ay"]
[node name="Chunk" type="TileMap"]
tile_set = ExtResource("1_x70ay")
format = 2
layer_0/tile_data = PackedInt32Array(0, 0, 65536, -1, 0, 65536)
script = ExtResource("1_lawsj")