#2 Collects resources

#8 Generates resources based on resource data
#10 Shows total resources and adjusts for new resources
This commit is contained in:
2024-02-07 09:22:14 -06:00
parent 7c81a2229e
commit 84de7fa6f1
10 changed files with 157 additions and 39 deletions

View File

@@ -0,0 +1,11 @@
[gd_resource type="Resource" script_class="GameResource" load_steps=2 format=3 uid="uid://bpjj0x7jr1k6u"]
[ext_resource type="Script" path="res://scripts/game_resource.gd" id="1_4maxk"]
[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"

View File

@@ -0,0 +1,11 @@
[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

@@ -19,6 +19,10 @@ config/icon="res://icon.svg"
gdscript/warnings/untyped_declaration=1
[display]
window/size/always_on_top=true
[gui]
theme/custom="res://data/world_theme.tres"

View File

@@ -1,21 +1,17 @@
[gd_scene load_steps=8 format=3 uid="uid://m0us5xqa3gnk"]
[gd_scene load_steps=9 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="TileSet" uid="uid://d1sh6dy2w10b0" path="res://data/scifi_tileset.tres" id="2_sa7dm"]
[ext_resource type="Script" path="res://scripts/window.gd" id="3_bfjot"]
[ext_resource type="Resource" uid="uid://bpjj0x7jr1k6u" path="res://data/game_resources/carbon_resource.tres" id="3_pq6ic"]
[ext_resource type="Script" path="res://scripts/building_manager.gd" id="4_61js3"]
[ext_resource type="Texture2D" uid="uid://n806c03hgaq1" path="res://assets/scifi_tilesheet@2.png" id="5_qhth7"]
[sub_resource type="AtlasTexture" id="AtlasTexture_m3qlj"]
atlas = ExtResource("5_qhth7")
region = Rect2(640, 768, 128, 128)
[sub_resource type="AtlasTexture" id="AtlasTexture_oqxqp"]
atlas = ExtResource("5_qhth7")
region = Rect2(640, 384, 128, 128)
[ext_resource type="Script" path="res://scripts/resource_manager.gd" id="5_k5643"]
[ext_resource type="Script" path="res://scripts/gui.gd" id="6_yuatk"]
[node name="world" type="Node2D"]
script = ExtResource("1_yf6q1")
game_resources = Array[Resource("res://scripts/game_resource.gd")]([ExtResource("2_rn4a2"), ExtResource("3_pq6ic")])
[node name="world_grid" type="TileMap" parent="."]
tile_set = ExtResource("2_sa7dm")
@@ -39,6 +35,13 @@ script = ExtResource("3_bfjot")
[node name="BuildingManager" type="Node2D" parent="."]
script = ExtResource("4_61js3")
[node name="ResourceManager" type="Node2D" parent="."]
script = ExtResource("5_k5643")
[node name="Timer" type="Timer" parent="ResourceManager"]
wait_time = 5.0
autostart = true
[node name="CanvasLayer" type="CanvasLayer" parent="."]
[node name="GUI" type="Control" parent="CanvasLayer"]
@@ -49,6 +52,7 @@ anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 1
script = ExtResource("6_yuatk")
[node name="ResourceContainer" type="PanelContainer" parent="CanvasLayer/GUI"]
layout_mode = 1
@@ -59,24 +63,18 @@ offset_left = -128.0
offset_bottom = 128.0
grow_horizontal = 0
[node name="GridContainer" type="GridContainer" parent="CanvasLayer/GUI/ResourceContainer"]
[node name="VBoxContainer" type="VBoxContainer" parent="CanvasLayer/GUI/ResourceContainer"]
layout_mode = 2
[node name="ResourcesLabel" type="Label" parent="CanvasLayer/GUI/ResourceContainer/VBoxContainer"]
layout_mode = 2
size_flags_stretch_ratio = 2.0
text = "Resources:"
horizontal_alignment = 1
[node name="GridContainer" type="GridContainer" parent="CanvasLayer/GUI/ResourceContainer/VBoxContainer"]
layout_mode = 2
columns = 2
[node name="GemTexture" type="TextureRect" parent="CanvasLayer/GUI/ResourceContainer/GridContainer"]
layout_mode = 2
texture = SubResource("AtlasTexture_m3qlj")
expand_mode = 3
[node name="GemLabel" type="Label" parent="CanvasLayer/GUI/ResourceContainer/GridContainer"]
layout_mode = 2
text = "100000"
[node name="CarbonTexture" type="TextureRect" parent="CanvasLayer/GUI/ResourceContainer/GridContainer"]
layout_mode = 2
texture = SubResource("AtlasTexture_oqxqp")
expand_mode = 3
[node name="CarbonLabel" type="Label" parent="CanvasLayer/GUI/ResourceContainer/GridContainer"]
layout_mode = 2
text = "100"
[connection signal="changed_resource" from="ResourceManager" to="CanvasLayer/GUI" method="_on_resource_manager_changed_resource"]
[connection signal="timeout" from="ResourceManager/Timer" to="ResourceManager" method="_on_timer_timeout"]

View File

@@ -9,8 +9,20 @@ var _pos: Vector2i
func _init(pos: Vector2i) -> void:
_pos = pos
func change_layer(layer: int, data: Variant) -> void:
func change_layer(layer: int, data: Variant) -> bool:
if layer_info.has(layer) and layer_info[layer] == data:
return false
layer_info[layer] = data
return true
func change_resource(data: GameResource) -> void:
layer_info[Constants.TilemapLayers.ENVIRONMENT] = data
func get_resource() -> GameResource:
return layer_info[Constants.TilemapLayers.ENVIRONMENT] as GameResource
func is_corrupted() -> bool:
return layer_info[Constants.TilemapLayers.CORRUPTION]
func has_layer(layer: int) -> bool:
return layer_info.has(layer)

8
scripts/game_resource.gd Normal file
View File

@@ -0,0 +1,8 @@
class_name GameResource
extends Resource
@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

37
scripts/gui.gd Normal file
View File

@@ -0,0 +1,37 @@
extends Control
@onready var resource_container: GridContainer = $ResourceContainer/VBoxContainer/GridContainer
const SCIFI_TILESHEET = preload("res://assets/scifi_tilesheet@2.png")
var _resource_displays := {}
func _on_resource_manager_changed_resource(changed: ResourceChangedSignal) -> void:
for resource: GameResource in changed.changed_resources.keys():
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)
var img := TextureRect.new()
img.texture = atlas_tex
img.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL
resource_container.add_child(img)
var label = Label.new()
label.text = str(changed.changed_resources[resource])
resource_container.add_child(label)
var data = ResourceData.new()
data.image = img
data.label = label
data.value = changed.changed_resources[resource]
_resource_displays[resource] = data
else:
var data: ResourceData = _resource_displays[resource]
data.value += changed.changed_resources[resource]
data.label.text = str(data.value)
class ResourceData:
var image: TextureRect
var label: Label
var value: int

View File

@@ -0,0 +1,19 @@
extends Node2D
signal changed_resource
@onready var world: World = $".."
@export var resources := {}
func _on_timer_timeout() -> void:
var corrupted_resources := world.get_corrupted_resources()
if corrupted_resources.size() > 0:
var changed_resources := {}
for corrupted_resource: CellData in corrupted_resources:
var res: GameResource = corrupted_resource.get_resource()
if not resources.has(res):
resources[res] = 0
resources[res] += res.pickup_value
changed_resources[res] = resources[res]
changed_resource.emit(ResourceChangedSignal.new(changed_resources))

View File

@@ -0,0 +1,6 @@
class_name ResourceChangedSignal extends Node
var changed_resources := {}
func _init(changed: Dictionary) -> void:
changed_resources = changed

View File

@@ -1,6 +1,6 @@
class_name World extends Node2D
@export var resoure_quantity = 0.4
@export var game_resources: Array[GameResource]
var terrain_noise := FastNoiseLite.new()
var resource_noise: Noise = FastNoiseLite.new()
@@ -9,8 +9,8 @@ var temp_size = 150
var temp_noise_val = []
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 noise_regions := [-999,0,999]
var atlas_regions := [Vector2i(0,1),Vector2i(0,0)]
var world_data: Dictionary = {}
@@ -19,8 +19,12 @@ var world_data: Dictionary = {}
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) -> bool:
return resource_noise.get_noise_2d(x, y) > resoure_quantity
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 get_noise_region(x,y):
var noise_val = get_noise_value(x,y)
@@ -35,8 +39,9 @@ func _ready():
for x in range(-temp_size, temp_size):
for y in range(-temp_size, temp_size):
change_location_data(Vector2i(x,y), Constants.TilemapLayers.GROUND, atlas_regions[get_noise_region(x,y)])
if is_resource_tile(x,y):
change_location_data(Vector2i(x,y), Constants.TilemapLayers.ENVIRONMENT, Vector2i(5,6))
var tile_res := is_resource_tile(x,y)
if tile_res:
change_location_resource(Vector2i(x,y), tile_res)
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,9 +54,9 @@ func _process(delta):
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)
if 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)
@@ -61,3 +66,10 @@ func change_location_data(pos: Vector2i, layer: int, data: Vector2i) -> void:
world_data[pos] = CellData.new(pos)
world_data[pos].change_layer(layer, data)
world_grid.set_cell(layer, pos, 0, data)
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)
func get_corrupted_resources() -> Array:
return world_data.values().filter(func(cell: CellData): return cell.is_corrupted() and cell.has_layer(Constants.TilemapLayers.ENVIRONMENT))