refactored cell world building

#8 included resources into cell data
This commit is contained in:
2024-02-06 07:38:23 -06:00
parent d8184ab48b
commit 7c81a2229e
10 changed files with 164 additions and 38 deletions

View File

@@ -1,23 +1,21 @@
extends Node2D
class_name Base
@export var world_grid: TileMap
var grid_loc
var i = 1
@export var world: World
var grid_loc: Vector2i
const curruption_directions := [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN]
var corruption_tiles: Array[Vector2i] = []
func _ready():
grid_loc = world_grid.local_to_map(position)
func _ready() -> void:
grid_loc = world.grid_location(position)
world.change_location_data(grid_loc, Constants.TilemapLayers.BUILDINGS, Vector2i(17,7))
corruption_tiles.append(grid_loc)
world_grid.set_cell(Constants.TilemapLayers.BUILDINGS, grid_loc, 0, Vector2i(17,7))
world.corrupt_location(grid_loc)
func _on_expand_timer_timeout():
var corrupt_tile = corruption_tiles.pick_random() + self.curruption_directions.pick_random()
func _on_expand_timer_timeout() -> void:
var corrupt_tile: Vector2i = corruption_tiles.pick_random() + self.curruption_directions.pick_random()
while corruption_tiles.has(corrupt_tile):
corrupt_tile = corruption_tiles.pick_random() + self.curruption_directions.pick_random()
corruption_tiles.append(corrupt_tile)
world_grid.set_cells_terrain_connect(Constants.TilemapLayers.CORRUPTION, corruption_tiles, 0, 0)
i += 1
world.corrupt_location(corrupt_tile)

View File

@@ -1,12 +1,12 @@
extends Node2D
@onready var world_grid = $"../world_grid"
@onready var world: World = $".."
var base = preload("res://scene/base.tscn")
func _unhandled_input(event):
if event is InputEventMouseButton and event.is_pressed() and event.button_index == MOUSE_BUTTON_LEFT:
var build = base.instantiate()
build.position = get_global_mouse_position()
build.world_grid = world_grid
build.world = world
add_child(build)
#world_grid.set_cell(2, world_grid.local_to_map(get_global_mouse_position()), 0, Vector2i(16,2))

16
scripts/cell_data.gd Normal file
View File

@@ -0,0 +1,16 @@
class_name CellData
extends Node
@export var layer_info: Dictionary = {
Constants.TilemapLayers.CORRUPTION: false
}
var _pos: Vector2i
func _init(pos: Vector2i) -> void:
_pos = pos
func change_layer(layer: int, data: Variant) -> void:
layer_info[layer] = data
func is_corrupted() -> bool:
return layer_info[Constants.TilemapLayers.CORRUPTION]

View File

@@ -1,12 +1,10 @@
extends Node2D
class_name World extends Node2D
@export var noise_texture: NoiseTexture2D
var noise_values
@onready var world_grid = $world_grid
var resource_texture: Noise = FastNoiseLite.new()
@export var resoure_quantity = 0.4
var terrain_noise := FastNoiseLite.new()
var resource_noise: Noise = FastNoiseLite.new()
var temp_size = 150
var temp_noise_val = []
var temp_noise_reg = []
@@ -14,11 +12,15 @@ var temp_noise_reg = []
var noise_regions = [-999,10,150,200,999]
var atlas_regions = [Vector2i(0,1),Vector2i(0,0),Vector2i(2,0),Vector2i(0,2)]
var world_data: Dictionary = {}
@onready var world_grid: TileMap = $world_grid
func get_noise_value(x: int, y: int):
return noise_values.get_noise_2d(x,y) * 500
return terrain_noise.get_noise_2d(x,y) * 500
func is_resource_tile(x: int, y: int) -> bool:
return resource_texture.get_noise_2d(x, y) > resoure_quantity
return resource_noise.get_noise_2d(x, y) > resoure_quantity
func get_noise_region(x,y):
var noise_val = get_noise_value(x,y)
@@ -27,22 +29,35 @@ func get_noise_region(x,y):
return i
func _ready():
resource_texture.seed = randi()
#await resource_texture.changed
noise_values = noise_texture.noise
terrain_noise.seed = randi()
resource_noise.seed = randi()
for x in range(-temp_size, temp_size):
for y in range(-temp_size, temp_size):
world_grid.set_cell(Constants.TilemapLayers.GROUND, Vector2i(x,y),0,atlas_regions[get_noise_region(x,y)])
change_location_data(Vector2i(x,y), Constants.TilemapLayers.GROUND, atlas_regions[get_noise_region(x,y)])
if is_resource_tile(x,y):
world_grid.set_cell(Constants.TilemapLayers.ENVIRONMENT, Vector2i(x,y), 0, Vector2i(5,6))
change_location_data(Vector2i(x,y), Constants.TilemapLayers.ENVIRONMENT, Vector2i(5,6))
temp_noise_val.append(get_noise_value(x,y))
temp_noise_reg.append(get_noise_region(x,y))
prints(temp_noise_val.min(), temp_noise_val.max())
for region in noise_regions.size():
prints(region, noise_regions[region], temp_noise_reg.count(region))
#get_tree().quit()
func _process(delta):
var mouse_pos = world_grid.local_to_map(get_local_mouse_position())
world_grid.clear_layer(Constants.TilemapLayers.CURSOR)
world_grid.set_cell(Constants.TilemapLayers.CURSOR, mouse_pos,0,Vector2i(0,7))
func corrupt_location(loc: Vector2i):
world_data[loc].change_layer(Constants.TilemapLayers.CORRUPTION, true)
var corrupted_cells := world_data.values().filter(func(cell: CellData): return cell.is_corrupted()).map(func(cell: CellData) -> Vector2i: return cell._pos)
world_grid.set_cells_terrain_connect(Constants.TilemapLayers.CORRUPTION, corrupted_cells, 0, 0)
func grid_location(global_pos: Vector2) -> Vector2i:
return world_grid.local_to_map(global_pos)
func change_location_data(pos: Vector2i, layer: int, data: Vector2i) -> void:
if not world_data.has(pos):
world_data[pos] = CellData.new(pos)
world_data[pos].change_layer(layer, data)
world_grid.set_cell(layer, pos, 0, data)