Changed resource spawning

added interact functionality
temp added zoom with scroll wheel
This commit is contained in:
2024-02-27 10:21:28 -06:00
parent 5467e044a7
commit f87b1cfa9a
17 changed files with 163 additions and 41 deletions

View File

@@ -4,7 +4,7 @@ class_name World extends Node2D
const PLAYER = preload("res://scene/player.tscn")
var terrain_noise := FastNoiseLite.new()
var resource_noise: Noise = FastNoiseLite.new()
#var resource_noise: Noise = FastNoiseLite.new()
var temp_size = 150
var temp_noise_val = []
@@ -14,16 +14,18 @@ var noise_regions := [-999,0,999]
var atlas_regions := [Vector2i(0,1),Vector2i(0,0)]
@onready var world_grid: TileMap = $world_grid
@onready var resource_manager: Node2D = $ResourceManager
func get_noise_value(x: int, y: int):
return terrain_noise.get_noise_2d(x,y) * 500
func is_resource_tile(x: int, y: int) -> GameResource:
for res: GameResource in game_resources:
var noise = resource_noise.get_noise_2d(x, y)
if res.spawn_region_min < noise and noise < res.spawn_region_max:
return res
return null
#func is_resource_tile(x: int, y: int) -> GameResource:
#var dist = sqrt(x ^ 2 + y ^ 2)
#for res: GameResource in game_resources:
#var noise = resource_noise.get_noise_2d(x, y)
#if res.distance_probability(dist) > (noise + 1.0) / 2.0:
#return res
#return null
func get_noise_region(x,y):
var noise_val = get_noise_value(x,y)
@@ -34,14 +36,16 @@ func get_noise_region(x,y):
func _ready():
Grid.init(world_grid)
terrain_noise.seed = randi()
resource_noise.seed = randi()
#resource_noise.seed = randi()
for x in range(-temp_size, temp_size):
for y in range(-temp_size, temp_size):
Grid.change_location_data(Vector2i(x,y), Constants.TilemapLayers.GROUND, atlas_regions[get_noise_region(x,y)])
var tile_res := is_resource_tile(x,y)
if tile_res:
change_location_resource(Vector2i(x,y), tile_res)
#var tile_res := is_resource_tile(x,y)
#if tile_res:
#change_location_resource(Vector2i(x,y), tile_res)
#if not tile_res.gained_resource.is_connected(resource_manager._on_gained_resource):
#tile_res.gained_resource.connect(resource_manager._on_gained_resource)
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())
@@ -49,15 +53,16 @@ func _ready():
prints(region, noise_regions[region], temp_noise_reg.count(region))
var player = PLAYER.instantiate()
add_child(player)
add_resources_to_map()
func corrupt_location(loc: Vector2i):
if Grid.corrupt_location(loc):
var corrupted_cells := Grid.get_corrupted_cells()
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_resource(pos: Vector2i, data: GameResource) -> void:
Grid.change_location_resource(pos, data)
world_grid.set_cell(Constants.TilemapLayers.ENVIRONMENT, pos, 0, data.atlas_location)
func add_resources_to_map() -> void:
for res: GameResource in game_resources:
res.gained_resource.connect(resource_manager._on_gained_resource)
var spawns = res.get_spawn_locations()
for spawn in spawns:
Grid.change_location_resource(spawn, res)