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

@@ -1,8 +1,8 @@
[gd_resource type="Resource" script_class="Building" load_steps=4 format=3 uid="uid://bgwoy0uh2cpvu"] [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="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] [resource]
script = ExtResource("1_ssts7") script = ExtResource("1_ssts7")

View File

@@ -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/expansion_patterns/expansion_square_pattern.gd" id="1_attvt"]
[ext_resource type="Script" path="res://scripts/building.gd" id="1_ssd30"] [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"] [sub_resource type="Resource" id="Resource_nqpn0"]
script = ExtResource("1_attvt") script = ExtResource("1_attvt")

View File

@@ -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="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] [resource]
script = ExtResource("1_4maxk") script = ExtResource("1_4maxk")
atlas_location = Vector2i(5, 3) atlas_location = Vector2i(5, 3)
spawn_region_min = -0.4
spawn_region_max = -0.3
pickup_value = 20 pickup_value = 20
name = "Carbon" name = "Carbon"
spawn_patterns = [ExtResource("2_eyt7m")]

View File

@@ -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

View File

@@ -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")])

View File

@@ -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

View File

@@ -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"

View File

@@ -47,3 +47,7 @@ libraries = {
} }
[node name="Camera2D" type="Camera2D" parent="."] [node name="Camera2D" type="Camera2D" parent="."]
[node name="InteractionTimer" type="Timer" parent="."]
process_callback = 0
one_shot = true

View File

@@ -1,9 +1,9 @@
[gd_scene load_steps=12 format=3 uid="uid://m0us5xqa3gnk"] [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="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="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/building_manager.gd" id="4_61js3"]
[ext_resource type="Script" path="res://scripts/resource_manager.gd" id="5_k5643"] [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"] [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 columns = 2
[node name="BottomBar" type="PanelContainer" parent="CanvasLayer/GUI"] [node name="BottomBar" type="PanelContainer" parent="CanvasLayer/GUI"]
visible = false
layout_mode = 1 layout_mode = 1
anchors_preset = 12 anchors_preset = 12
anchor_top = 1.0 anchor_top = 1.0

View File

@@ -10,6 +10,9 @@ func init(grid: TileMap) -> void:
func world_to_grid(world_loc: Vector2) -> Vector2i: func world_to_grid(world_loc: Vector2) -> Vector2i:
return Vector2i(floor(world_loc.x / GRID_SIZE), floor(world_loc.y / GRID_SIZE)) 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: func set_selected_tile(tile: Vector2i) -> void:
world_grid.clear_layer(Constants.TilemapLayers.CURSOR) world_grid.clear_layer(Constants.TilemapLayers.CURSOR)
world_grid.set_cell(Constants.TilemapLayers.CURSOR, tile,0,Vector2i(0,7)) 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: func change_location_resource(pos: Vector2i, data: GameResource) -> void:
world_data[pos].change_resource(data) 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 class_name CellData
extends Node extends Resource
@export var layer_info: Dictionary = { @export var layer_info: Dictionary = {
Constants.TilemapLayers.CORRUPTION: false Constants.TilemapLayers.CORRUPTION: false
} }
var _pos: Vector2i var _pos: Vector2i
var interaction_display: ProgressBar
func _to_string() -> String: func _to_string() -> String:
return "{%s %s}" % [_pos, layer_info] return "{%s %s}" % [_pos, layer_info]
@@ -24,8 +25,31 @@ func change_resource(data: GameResource) -> void:
func get_resource() -> GameResource: func get_resource() -> GameResource:
return layer_info[Constants.TilemapLayers.ENVIRONMENT] as GameResource return layer_info[Constants.TilemapLayers.ENVIRONMENT] as GameResource
func has_resource() -> bool:
return has_layer(Constants.TilemapLayers.ENVIRONMENT)
func is_corrupted() -> bool: func is_corrupted() -> bool:
return layer_info[Constants.TilemapLayers.CORRUPTION] return layer_info[Constants.TilemapLayers.CORRUPTION]
func has_layer(layer: int) -> bool: func has_layer(layer: int) -> bool:
return layer_info.has(layer) 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 class_name GameResource
extends Resource extends Resource
signal gained_resource(res: GameResource)
@export var atlas_location: Vector2i @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 pickup_value: int
@export var name: String @export var name: String
@export var spawn_patterns: Array[SpawnPattern]
func _to_string() -> String: func _to_string() -> String:
return name 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): if not _resource_displays.has(resource):
var atlas_tex := AtlasTexture.new() var atlas_tex := AtlasTexture.new()
atlas_tex.atlas = SCIFI_TILESHEET 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() var img := TextureRect.new()
img.texture = atlas_tex img.texture = atlas_tex
img.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL img.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL

View File

@@ -7,6 +7,7 @@ var last_direction = 0
var interaction_location: Vector2i var interaction_location: Vector2i
@onready var animation_player: AnimationPlayer = $AnimationPlayer @onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var interaction_timer: Timer = $InteractionTimer
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
var input_direction := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") 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() move_and_slide()
if input_direction: if input_direction:
animation_player.current_animation = "walk"
animation_player.play() animation_player.play()
last_direction = input_direction.angle() + 3 * PI / 2 last_direction = input_direction.angle() + 3 * PI / 2
rotation = last_direction rotation = last_direction
@@ -24,4 +26,16 @@ func _physics_process(delta: float) -> void:
func _input(event: InputEvent) -> void: func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_accept"): 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 := {} @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: #func _on_timer_timeout() -> void:
#var corrupted_resources := Grid.get_corrupted_resources() #var corrupted_resources := Grid.get_corrupted_resources()
#if corrupted_resources.size() > 0: #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") const PLAYER = preload("res://scene/player.tscn")
var terrain_noise := FastNoiseLite.new() var terrain_noise := FastNoiseLite.new()
var resource_noise: Noise = FastNoiseLite.new() #var resource_noise: Noise = FastNoiseLite.new()
var temp_size = 150 var temp_size = 150
var temp_noise_val = [] var temp_noise_val = []
@@ -14,16 +14,18 @@ var noise_regions := [-999,0,999]
var atlas_regions := [Vector2i(0,1),Vector2i(0,0)] var atlas_regions := [Vector2i(0,1),Vector2i(0,0)]
@onready var world_grid: TileMap = $world_grid @onready var world_grid: TileMap = $world_grid
@onready var resource_manager: Node2D = $ResourceManager
func get_noise_value(x: int, y: int): func get_noise_value(x: int, y: int):
return terrain_noise.get_noise_2d(x,y) * 500 return terrain_noise.get_noise_2d(x,y) * 500
func is_resource_tile(x: int, y: int) -> GameResource: #func is_resource_tile(x: int, y: int) -> GameResource:
for res: GameResource in game_resources: #var dist = sqrt(x ^ 2 + y ^ 2)
var noise = resource_noise.get_noise_2d(x, y) #for res: GameResource in game_resources:
if res.spawn_region_min < noise and noise < res.spawn_region_max: #var noise = resource_noise.get_noise_2d(x, y)
return res #if res.distance_probability(dist) > (noise + 1.0) / 2.0:
return null #return res
#return null
func get_noise_region(x,y): func get_noise_region(x,y):
var noise_val = get_noise_value(x,y) var noise_val = get_noise_value(x,y)
@@ -34,14 +36,16 @@ func get_noise_region(x,y):
func _ready(): func _ready():
Grid.init(world_grid) Grid.init(world_grid)
terrain_noise.seed = randi() terrain_noise.seed = randi()
resource_noise.seed = randi() #resource_noise.seed = randi()
for x in range(-temp_size, temp_size): for x in range(-temp_size, temp_size):
for y 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)]) 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) #var tile_res := is_resource_tile(x,y)
if tile_res: #if tile_res:
change_location_resource(Vector2i(x,y), 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_val.append(get_noise_value(x,y))
temp_noise_reg.append(get_noise_region(x,y)) temp_noise_reg.append(get_noise_region(x,y))
prints(temp_noise_val.min(), temp_noise_val.max()) 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)) prints(region, noise_regions[region], temp_noise_reg.count(region))
var player = PLAYER.instantiate() var player = PLAYER.instantiate()
add_child(player) add_child(player)
add_resources_to_map()
func corrupt_location(loc: Vector2i): func corrupt_location(loc: Vector2i):
if Grid.corrupt_location(loc): if Grid.corrupt_location(loc):
var corrupted_cells := Grid.get_corrupted_cells() var corrupted_cells := Grid.get_corrupted_cells()
world_grid.set_cells_terrain_connect(Constants.TilemapLayers.CORRUPTION, corrupted_cells, 0, 0) world_grid.set_cells_terrain_connect(Constants.TilemapLayers.CORRUPTION, corrupted_cells, 0, 0)
func grid_location(global_pos: Vector2) -> Vector2i: func add_resources_to_map() -> void:
return world_grid.local_to_map(global_pos) for res: GameResource in game_resources:
res.gained_resource.connect(resource_manager._on_gained_resource)
func change_location_resource(pos: Vector2i, data: GameResource) -> void: var spawns = res.get_spawn_locations()
Grid.change_location_resource(pos, data) for spawn in spawns:
world_grid.set_cell(Constants.TilemapLayers.ENVIRONMENT, pos, 0, data.atlas_location) Grid.change_location_resource(spawn, res)