98 lines
2.3 KiB
GDScript
98 lines
2.3 KiB
GDScript
class_name Chunk
|
|
|
|
var tiles : Array[int]
|
|
var position : Vector2i
|
|
|
|
var spawner_tiles : Array[Vector2i]
|
|
|
|
var items : Dictionary[Vector2i, Item]
|
|
var arrows : Dictionary[Vector2i, Arrow]
|
|
|
|
var processed = false
|
|
|
|
var incoming_items : Dictionary[Vector2i, Item]
|
|
|
|
var tickable = false
|
|
|
|
func _init(position : Vector2i) -> void:
|
|
|
|
self.position = position
|
|
|
|
var noise : FastNoiseLite = FastNoiseLite.new()
|
|
noise.noise_type = FastNoiseLite.TYPE_PERLIN
|
|
noise.fractal_type = FastNoiseLite.FRACTAL_FBM
|
|
|
|
noise.seed = Globals.seed
|
|
noise.frequency = 0.025
|
|
|
|
noise.fractal_octaves = 5
|
|
noise.fractal_lacunarity = 2
|
|
noise.fractal_gain = 0.5
|
|
|
|
var x : int = 0
|
|
var y : int = 0
|
|
|
|
var offset : Vector2i = position * TileGlobals.chunk_size
|
|
|
|
for i in range(TileGlobals.chunk_size ** 2):
|
|
if x >= TileGlobals.chunk_size:
|
|
x = 0
|
|
y += 1
|
|
|
|
var point_value : float = remap(noise.get_noise_2d(x+offset.x,y+offset.y), -1, 1, 0, 1)
|
|
var tile_id : int = TileGlobals.chunk_gradient.sample(point_value).r8
|
|
|
|
tiles.append(tile_id)
|
|
|
|
x += 1
|
|
|
|
if randi_range(0,10) == 0:
|
|
var tile_idx = randi_range(0, TileGlobals.chunk_size ** 2)
|
|
if tiles[tile_idx] == TileGlobals.GRASS:
|
|
tiles[tile_idx] = TileGlobals.TREE
|
|
|
|
var tile_position = Vector2i(0, 0)
|
|
for tile : int in tiles:
|
|
if tile_position.x >= TileGlobals.chunk_size:
|
|
tile_position.y += 1
|
|
tile_position.x = 0
|
|
|
|
if tile in TileGlobals.resource_tiles.keys():
|
|
spawner_tiles.append(tile_position)
|
|
|
|
tile_position.x += 1
|
|
|
|
## Updates chunk and returns whether it changed
|
|
func update() -> bool:
|
|
if not tickable:
|
|
return false
|
|
|
|
var new_items : Dictionary[Vector2i, Item]
|
|
|
|
for item_position : Vector2i in items.keys():
|
|
var item = items[item_position]
|
|
|
|
var new_position = item_position + item.direction
|
|
|
|
new_items[new_position] = item
|
|
|
|
items = new_items
|
|
|
|
for tile_pos in spawner_tiles:
|
|
if arrows.has(tile_pos) and not items.has(tile_pos):
|
|
items[tile_pos] = Item.new(TileGlobals.resource_tiles[get_tile(tile_pos)], arrows[tile_pos].direction)
|
|
|
|
return true
|
|
|
|
func recalculate():
|
|
if len(items) > 0 or len(arrows) > 0:
|
|
tickable = true
|
|
else:
|
|
tickable = false
|
|
|
|
func get_tile(pos : Vector2i):
|
|
return tiles[pos.y * TileGlobals.chunk_size + pos.x]
|
|
|
|
func set_tile(pos : Vector2i, tile_id : int):
|
|
tiles[pos.y * TileGlobals.chunk_size + pos.x] = tile_id
|