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

@@ -10,6 +10,9 @@ func init(grid: TileMap) -> void:
func world_to_grid(world_loc: Vector2) -> Vector2i:
return Vector2i(floor(world_loc.x / GRID_SIZE), floor(world_loc.y / GRID_SIZE))
func grid_to_world_center(grid_loc: Vector2i) -> Vector2:
return Vector2((grid_loc.x + 0.5) * GRID_SIZE, (grid_loc.y + 0.5) * GRID_SIZE)
func set_selected_tile(tile: Vector2i) -> void:
world_grid.clear_layer(Constants.TilemapLayers.CURSOR)
world_grid.set_cell(Constants.TilemapLayers.CURSOR, tile,0,Vector2i(0,7))
@@ -33,3 +36,4 @@ func get_location_data(pos: Vector2i) -> CellData:
func change_location_resource(pos: Vector2i, data: GameResource) -> void:
world_data[pos].change_resource(data)
world_grid.set_cell(Constants.TilemapLayers.ENVIRONMENT, pos, 0, data.atlas_location)

View File

@@ -1,10 +1,11 @@
class_name CellData
extends Node
extends Resource
@export var layer_info: Dictionary = {
Constants.TilemapLayers.CORRUPTION: false
}
var _pos: Vector2i
var interaction_display: ProgressBar
func _to_string() -> String:
return "{%s %s}" % [_pos, layer_info]
@@ -24,8 +25,31 @@ func change_resource(data: GameResource) -> void:
func get_resource() -> GameResource:
return layer_info[Constants.TilemapLayers.ENVIRONMENT] as GameResource
func has_resource() -> bool:
return has_layer(Constants.TilemapLayers.ENVIRONMENT)
func is_corrupted() -> bool:
return layer_info[Constants.TilemapLayers.CORRUPTION]
func has_layer(layer: int) -> bool:
return layer_info.has(layer)
func interact(timer: Timer) -> void:
var is_interactable := has_resource() or has_layer(Constants.TilemapLayers.BUILDINGS)
if is_interactable and not interaction_display:
timer.timeout.connect(_on_interaction_finished.bind(timer))
timer.start(3)
interaction_display = ProgressBar.new()
interaction_display.position = Grid.grid_to_world_center(_pos) - Vector2(30, 10)
interaction_display.size = Vector2(60, 20)
interaction_display.show_percentage = false
var tween = timer.get_tree().create_tween()
tween.tween_property(interaction_display, "value", 100, 3)
timer.get_parent().add_sibling(interaction_display)
func _on_interaction_finished(timer: Timer) -> void:
timer.timeout.disconnect(_on_interaction_finished)
if has_resource():
interaction_display.queue_free()
get_resource().gained_resource.emit(get_resource())

View File

@@ -1,11 +1,18 @@
class_name GameResource
extends Resource
signal gained_resource(res: GameResource)
@export var atlas_location: Vector2i
@export_range(-1, 1, 0.05) var spawn_region_min: float
@export_range(-1, 1, 0.05) var spawn_region_max: float
@export var pickup_value: int
@export var name: String
@export var spawn_patterns: Array[SpawnPattern]
func _to_string() -> String:
return name
func get_spawn_locations() -> Array[Vector2i]:
var spawns: Array[Vector2i] = []
for spawn in spawn_patterns:
spawns.append_array(spawn.get_spawn_locations())
return spawns

View File

@@ -11,7 +11,7 @@ func _on_resource_manager_changed_resource(changed: ResourceChangedSignal) -> vo
if not _resource_displays.has(resource):
var atlas_tex := AtlasTexture.new()
atlas_tex.atlas = SCIFI_TILESHEET
atlas_tex.region = Rect2(128 * resource.atlas_location.x, 128 * resource.atlas_location.y, 128, 128)
atlas_tex.region = Rect2(Grid.GRID_SIZE * resource.atlas_location.x, Grid.GRID_SIZE * resource.atlas_location.y, Grid.GRID_SIZE, Grid.GRID_SIZE)
var img := TextureRect.new()
img.texture = atlas_tex
img.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL

View File

@@ -7,6 +7,7 @@ var last_direction = 0
var interaction_location: Vector2i
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var interaction_timer: Timer = $InteractionTimer
func _physics_process(delta: float) -> void:
var input_direction := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
@@ -14,6 +15,7 @@ func _physics_process(delta: float) -> void:
move_and_slide()
if input_direction:
animation_player.current_animation = "walk"
animation_player.play()
last_direction = input_direction.angle() + 3 * PI / 2
rotation = last_direction
@@ -24,4 +26,16 @@ func _physics_process(delta: float) -> void:
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_accept"):
print("Interacted with: %s" % Grid.get_location_data(interaction_location))
Grid.get_location_data(interaction_location).interact(interaction_timer)
print("interaction started")
func _finished_interaction(results: Dictionary) -> void:
print("interaction finished: %s" % results)
func _unhandled_input(event):
if event is InputEventMouseButton:
match event.button_index:
MOUSE_BUTTON_WHEEL_DOWN:
$Camera2D.zoom -= Vector2(0.01, 0.01)
MOUSE_BUTTON_WHEEL_UP:
$Camera2D.zoom += Vector2(0.01, 0.01)

View File

@@ -6,6 +6,13 @@ signal changed_resource
@export var resources := {}
func _on_gained_resource(res: GameResource) -> void:
print("Gained Resource: %s" % res)
var changed_resources := {
res: res.pickup_value
}
changed_resource.emit(ResourceChangedSignal.new(changed_resources))
#func _on_timer_timeout() -> void:
#var corrupted_resources := Grid.get_corrupted_resources()
#if corrupted_resources.size() > 0:

34
scripts/spawn_pattern.gd Normal file
View File

@@ -0,0 +1,34 @@
extends Resource
class_name SpawnPattern
@export var min_distance: int
@export var max_distance: int
@export var min_spread: int
@export var quantity: int
@export var cluster_size: int
func get_spawn_locations() -> Array[Vector2i]:
var spawns: Array[Vector2i] = []
var clusters: Array[Vector2] = []
for cluster_i in range(quantity):
var cluster
var is_valid_cluster := false
while not is_valid_cluster:
is_valid_cluster = true
var dist = randf_range(min_distance, max_distance)
var angle = randf_range(0, 2*PI)
cluster = Vector2.RIGHT.rotated(angle)
cluster *= dist
for old_cluster: Vector2 in clusters:
is_valid_cluster = is_valid_cluster and old_cluster.distance_to(cluster) > min_spread
clusters.append(cluster)
var local_spawns = [Vector2i(cluster)]
for point in range(cluster_size - 1):
var next = local_spawns.pick_random()
while local_spawns.has(next):
var next_change = [Vector2i.LEFT, Vector2i.RIGHT, Vector2i.UP, Vector2i.DOWN].pick_random()
next = next + next_change
local_spawns.append(next)
spawns.append_array(local_spawns)
return spawns

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)