From f87b1cfa9a77d9d26c9f2567ab0517f72c5c694a Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 27 Feb 2024 10:21:28 -0600 Subject: [PATCH] Changed resource spawning added interact functionality temp added zoom with scroll wheel --- data/buildings/corruptor.tres | 4 +- data/buildings/hub.tres | 2 +- .../{ => carbon}/carbon_resource.tres | 6 +-- .../carbon/spawn_pattern_close.tres | 11 ++++++ data/game_resources/gem/gem_resource.tres | 11 ++++++ .../gem/spawn_pattern_close.tres | 11 ++++++ data/game_resources/gem_resource.tres | 11 ------ scene/player.tscn | 4 ++ scene/world.tscn | 5 ++- scripts/autoloads/grid.gd | 4 ++ scripts/cell_data.gd | 26 ++++++++++++- scripts/game_resource.gd | 11 +++++- scripts/gui.gd | 2 +- scripts/player.gd | 16 +++++++- scripts/resource_manager.gd | 7 ++++ scripts/spawn_pattern.gd | 34 ++++++++++++++++ scripts/world.gd | 39 +++++++++++-------- 17 files changed, 163 insertions(+), 41 deletions(-) rename data/game_resources/{ => carbon}/carbon_resource.tres (53%) create mode 100644 data/game_resources/carbon/spawn_pattern_close.tres create mode 100644 data/game_resources/gem/gem_resource.tres create mode 100644 data/game_resources/gem/spawn_pattern_close.tres delete mode 100644 data/game_resources/gem_resource.tres create mode 100644 scripts/spawn_pattern.gd diff --git a/data/buildings/corruptor.tres b/data/buildings/corruptor.tres index 7eaacd7..06304a5 100644 --- a/data/buildings/corruptor.tres +++ b/data/buildings/corruptor.tres @@ -1,8 +1,8 @@ [gd_resource type="Resource" script_class="Building" load_steps=4 format=3 uid="uid://bgwoy0uh2cpvu"] -[ext_resource type="Resource" uid="uid://bpjj0x7jr1k6u" path="res://data/game_resources/carbon_resource.tres" id="1_5s38g"] +[ext_resource type="Resource" uid="uid://bpjj0x7jr1k6u" path="res://data/game_resources/carbon/carbon_resource.tres" id="1_5s38g"] [ext_resource type="Script" path="res://scripts/building.gd" id="1_ssts7"] -[ext_resource type="Resource" uid="uid://dr00rd4f42jqe" path="res://data/game_resources/gem_resource.tres" id="2_u1tpw"] +[ext_resource type="Resource" uid="uid://dr00rd4f42jqe" path="res://data/game_resources/gem/gem_resource.tres" id="2_u1tpw"] [resource] script = ExtResource("1_ssts7") diff --git a/data/buildings/hub.tres b/data/buildings/hub.tres index eb1907a..f6cd42c 100644 --- a/data/buildings/hub.tres +++ b/data/buildings/hub.tres @@ -2,7 +2,7 @@ [ext_resource type="Script" path="res://scripts/expansion_patterns/expansion_square_pattern.gd" id="1_attvt"] [ext_resource type="Script" path="res://scripts/building.gd" id="1_ssd30"] -[ext_resource type="Resource" uid="uid://bpjj0x7jr1k6u" path="res://data/game_resources/carbon_resource.tres" id="2_d8jrj"] +[ext_resource type="Resource" uid="uid://bpjj0x7jr1k6u" path="res://data/game_resources/carbon/carbon_resource.tres" id="2_d8jrj"] [sub_resource type="Resource" id="Resource_nqpn0"] script = ExtResource("1_attvt") diff --git a/data/game_resources/carbon_resource.tres b/data/game_resources/carbon/carbon_resource.tres similarity index 53% rename from data/game_resources/carbon_resource.tres rename to data/game_resources/carbon/carbon_resource.tres index ec041b2..8037464 100644 --- a/data/game_resources/carbon_resource.tres +++ b/data/game_resources/carbon/carbon_resource.tres @@ -1,11 +1,11 @@ -[gd_resource type="Resource" script_class="GameResource" load_steps=2 format=3 uid="uid://bpjj0x7jr1k6u"] +[gd_resource type="Resource" script_class="GameResource" load_steps=3 format=3 uid="uid://bpjj0x7jr1k6u"] [ext_resource type="Script" path="res://scripts/game_resource.gd" id="1_4maxk"] +[ext_resource type="Resource" uid="uid://bsrqp68lxf2f" path="res://data/game_resources/carbon/spawn_pattern_close.tres" id="2_eyt7m"] [resource] script = ExtResource("1_4maxk") atlas_location = Vector2i(5, 3) -spawn_region_min = -0.4 -spawn_region_max = -0.3 pickup_value = 20 name = "Carbon" +spawn_patterns = [ExtResource("2_eyt7m")] diff --git a/data/game_resources/carbon/spawn_pattern_close.tres b/data/game_resources/carbon/spawn_pattern_close.tres new file mode 100644 index 0000000..110213e --- /dev/null +++ b/data/game_resources/carbon/spawn_pattern_close.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" script_class="SpawnPattern" load_steps=2 format=3 uid="uid://bsrqp68lxf2f"] + +[ext_resource type="Script" path="res://scripts/spawn_pattern.gd" id="1_pksks"] + +[resource] +script = ExtResource("1_pksks") +min_distance = 8 +max_distance = 20 +min_spread = 15 +quantity = 4 +cluster_size = 12 diff --git a/data/game_resources/gem/gem_resource.tres b/data/game_resources/gem/gem_resource.tres new file mode 100644 index 0000000..c7f0b25 --- /dev/null +++ b/data/game_resources/gem/gem_resource.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" script_class="GameResource" load_steps=3 format=3 uid="uid://dr00rd4f42jqe"] + +[ext_resource type="Script" path="res://scripts/game_resource.gd" id="1_tfoa4"] +[ext_resource type="Resource" uid="uid://0crnt5nbhhlv" path="res://data/game_resources/gem/spawn_pattern_close.tres" id="2_n3fuo"] + +[resource] +script = ExtResource("1_tfoa4") +atlas_location = Vector2i(5, 6) +pickup_value = 10 +name = "Gem" +spawn_patterns = Array[Resource("res://scripts/spawn_pattern.gd")]([ExtResource("2_n3fuo")]) diff --git a/data/game_resources/gem/spawn_pattern_close.tres b/data/game_resources/gem/spawn_pattern_close.tres new file mode 100644 index 0000000..d625b8b --- /dev/null +++ b/data/game_resources/gem/spawn_pattern_close.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" script_class="SpawnPattern" load_steps=2 format=3 uid="uid://0crnt5nbhhlv"] + +[ext_resource type="Script" path="res://scripts/spawn_pattern.gd" id="1_bi4rm"] + +[resource] +script = ExtResource("1_bi4rm") +min_distance = 16 +max_distance = 40 +min_spread = 30 +quantity = 4 +cluster_size = 6 diff --git a/data/game_resources/gem_resource.tres b/data/game_resources/gem_resource.tres deleted file mode 100644 index f132873..0000000 --- a/data/game_resources/gem_resource.tres +++ /dev/null @@ -1,11 +0,0 @@ -[gd_resource type="Resource" script_class="GameResource" load_steps=2 format=3 uid="uid://dr00rd4f42jqe"] - -[ext_resource type="Script" path="res://scripts/game_resource.gd" id="1_tfoa4"] - -[resource] -script = ExtResource("1_tfoa4") -atlas_location = Vector2i(5, 6) -spawn_region_min = 0.4 -spawn_region_max = 0.8 -pickup_value = 10 -name = "Gem" diff --git a/scene/player.tscn b/scene/player.tscn index c5fa2ef..0baa3a1 100644 --- a/scene/player.tscn +++ b/scene/player.tscn @@ -47,3 +47,7 @@ libraries = { } [node name="Camera2D" type="Camera2D" parent="."] + +[node name="InteractionTimer" type="Timer" parent="."] +process_callback = 0 +one_shot = true diff --git a/scene/world.tscn b/scene/world.tscn index f6422a3..6514d1e 100644 --- a/scene/world.tscn +++ b/scene/world.tscn @@ -1,9 +1,9 @@ [gd_scene load_steps=12 format=3 uid="uid://m0us5xqa3gnk"] [ext_resource type="Script" path="res://scripts/world.gd" id="1_yf6q1"] -[ext_resource type="Resource" uid="uid://dr00rd4f42jqe" path="res://data/game_resources/gem_resource.tres" id="2_rn4a2"] +[ext_resource type="Resource" uid="uid://dr00rd4f42jqe" path="res://data/game_resources/gem/gem_resource.tres" id="2_rn4a2"] [ext_resource type="TileSet" uid="uid://d1sh6dy2w10b0" path="res://data/scifi_tileset.tres" id="2_sa7dm"] -[ext_resource type="Resource" uid="uid://bpjj0x7jr1k6u" path="res://data/game_resources/carbon_resource.tres" id="3_pq6ic"] +[ext_resource type="Resource" uid="uid://bpjj0x7jr1k6u" path="res://data/game_resources/carbon/carbon_resource.tres" id="3_pq6ic"] [ext_resource type="Script" path="res://scripts/building_manager.gd" id="4_61js3"] [ext_resource type="Script" path="res://scripts/resource_manager.gd" id="5_k5643"] [ext_resource type="Texture2D" uid="uid://n806c03hgaq1" path="res://assets/scifi_tilesheet@2.png" id="5_qhth7"] @@ -90,6 +90,7 @@ layout_mode = 2 columns = 2 [node name="BottomBar" type="PanelContainer" parent="CanvasLayer/GUI"] +visible = false layout_mode = 1 anchors_preset = 12 anchor_top = 1.0 diff --git a/scripts/autoloads/grid.gd b/scripts/autoloads/grid.gd index 86f1740..8d63c7a 100644 --- a/scripts/autoloads/grid.gd +++ b/scripts/autoloads/grid.gd @@ -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) diff --git a/scripts/cell_data.gd b/scripts/cell_data.gd index 7bafc96..00dfb89 100644 --- a/scripts/cell_data.gd +++ b/scripts/cell_data.gd @@ -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()) diff --git a/scripts/game_resource.gd b/scripts/game_resource.gd index 5cb0bea..e4654fd 100644 --- a/scripts/game_resource.gd +++ b/scripts/game_resource.gd @@ -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 diff --git a/scripts/gui.gd b/scripts/gui.gd index 9d996f2..2a27c60 100644 --- a/scripts/gui.gd +++ b/scripts/gui.gd @@ -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 diff --git a/scripts/player.gd b/scripts/player.gd index 89d28a6..cdd75dd 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -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) diff --git a/scripts/resource_manager.gd b/scripts/resource_manager.gd index 1b7d968..6b50340 100644 --- a/scripts/resource_manager.gd +++ b/scripts/resource_manager.gd @@ -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: diff --git a/scripts/spawn_pattern.gd b/scripts/spawn_pattern.gd new file mode 100644 index 0000000..0c3d587 --- /dev/null +++ b/scripts/spawn_pattern.gd @@ -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 diff --git a/scripts/world.gd b/scripts/world.gd index b44ece5..4f7dc22 100644 --- a/scripts/world.gd +++ b/scripts/world.gd @@ -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)