add gamestate, doc and fix

This commit is contained in:
2026-04-16 11:01:04 +02:00
parent dc0bf0ba37
commit e02bd1819f
29 changed files with 466 additions and 71 deletions

View File

@@ -1,13 +1,19 @@
extends Control
@onready var grid_container: GridContainer = $PanelContainer/MarginContainer/ScrollContainer/GridContainer
@onready var grid_container: GridContainer = $%CollectibleGrid
@onready var collectible_ui_scene = preload("res://core/photo_mode/collectible_ui.tscn")
func _ready() -> void:
CollectionManager.on_collectible_unlocked.connect(_unlock_collectible)
setup()
func setup() -> void:
var unlocked_collectible_ids = CollectionManager.get_unlocked_collectible_ids()
var collectibles = CollectionManager.get_all_collectibles()
for child in grid_container.get_children():
child.queue_free()
for collectible in collectibles:
_add_collectible(collectible)
if unlocked_collectible_ids.has(collectible.id):
@@ -21,7 +27,6 @@ func _add_collectible(collectible: CollectibleResource) -> void:
collectible_ui.setup(collectible)
grid_container.add_child(collectible_ui)
func _unlock_collectible(collectible_id: String) -> void:
for collectible_ui in grid_container.get_children():
if collectible_ui.collectible_resource.id == collectible_id:

View File

@@ -1,8 +1,8 @@
[gd_scene format=3 uid="uid://bvw086glfpcba"]
[ext_resource type="Script" uid="uid://dq3qtcrdnikl7" path="res://core/photo_mode/collection_compendium.gd" id="1_67tug"]
[ext_resource type="Script" uid="uid://dq3qtcrdnikl7" path="res://core/photo_mode/collectible_gallery.gd" id="1_67tug"]
[node name="CollectionCompendium" type="Control" unique_id=354419843]
[node name="CollectibleGallery" type="Control" unique_id=354419843]
layout_mode = 3
anchors_preset = 8
anchor_left = 0.5
@@ -20,10 +20,10 @@ anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -400.0
offset_top = -400.0
offset_right = 400.0
offset_bottom = 400.0
offset_left = -350.0
offset_top = -350.0
offset_right = 350.0
offset_bottom = 350.0
grow_horizontal = 2
grow_vertical = 2
@@ -38,7 +38,8 @@ theme_override_constants/margin_bottom = 12
layout_mode = 2
draw_focus_border = true
[node name="GridContainer" type="GridContainer" parent="PanelContainer/MarginContainer/ScrollContainer" unique_id=1187865988]
[node name="CollectibleGrid" type="GridContainer" parent="PanelContainer/MarginContainer/ScrollContainer" unique_id=1187865988]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 6
size_flags_vertical = 2

View File

@@ -2,15 +2,21 @@ extends Control
class_name CollectibleUI
@onready var label: Label = $Panel/MarginContainer/VBoxContainer/Label
@onready var texture_rect: TextureRect = $Panel/MarginContainer/VBoxContainer/TextureRect
@onready var label: Label = $%CollectibleName
@onready var texture_rect: TextureRect = $%CollectibleTexture
var collectible_resource: CollectibleResource
func setup(collectible: CollectibleResource) -> void:
collectible_resource = collectible
$Panel/MarginContainer/VBoxContainer/Label.text = collectible_resource.title
if !label:
label = $%CollectibleName
label.text = collectible_resource.title
if collectible.image:
$Panel/MarginContainer/VBoxContainer/TextureRect.texture = collectible_resource.image
if !texture_rect:
texture_rect = $%CollectibleTexture
texture_rect.texture = collectible_resource.image
func unlock() -> void:
$Panel/MarginContainer/VBoxContainer/TextureRect.set_modulate(Color(1,1,1,1))
if !texture_rect:
texture_rect = $%CollectibleTexture
texture_rect.set_modulate(Color(1,1,1,1))

View File

@@ -30,7 +30,8 @@ theme_override_constants/margin_bottom = 12
layout_mode = 2
alignment = 1
[node name="Label" type="Label" parent="Panel/MarginContainer/VBoxContainer" unique_id=117774359]
[node name="CollectibleName" type="Label" parent="Panel/MarginContainer/VBoxContainer" unique_id=117774359]
unique_name_in_owner = true
layout_mode = 2
size_flags_vertical = 0
text = "Collectible"
@@ -39,7 +40,8 @@ horizontal_alignment = 1
[node name="HSeparator" type="HSeparator" parent="Panel/MarginContainer/VBoxContainer" unique_id=1345822244]
layout_mode = 2
[node name="TextureRect" type="TextureRect" parent="Panel/MarginContainer/VBoxContainer" unique_id=506844785]
[node name="CollectibleTexture" type="TextureRect" parent="Panel/MarginContainer/VBoxContainer" unique_id=506844785]
unique_name_in_owner = true
modulate = Color(0.16206557, 0.1620656, 0.16206557, 1)
layout_mode = 2
size_flags_vertical = 3

View File

@@ -1,26 +1,70 @@
extends Node
signal on_collectible_unlocked(collectible_id: String)
signal on_photo_saved(filePath: String)
#TODO -> assign library resource to game_state
var library: CollectibleLibrary = preload("res://core/photo_mode/collectible_library.tres")
var unlocked_ids: Array[String] = ['cane']
var collectibles_library: CollectibleLibrary = preload("res://core/photo_mode/collectible_library.tres")
func unlock_photo(collectible_id: String) -> void:
if not collectible_id in unlocked_ids:
unlocked_ids.append(collectible_id)
var unlocked_collectibles_ids: Array[String] = []
var saved_photos: Array[String] = []
func _ready() -> void:
add_to_group("Saveable")
load_save_data()
func unlock_collectible(collectible_id: String) -> void:
if not collectible_id in unlocked_collectibles_ids:
var new_collectible = get_collectible_by_id(collectible_id)
if new_collectible:
unlocked_collectibles_ids.append(collectible_id)
on_collectible_unlocked.emit(collectible_id)
func get_collectible_by_id(id: String) -> CollectibleResource:
for collectible in library.collectibles:
for collectible in collectibles_library.collectibles:
if collectible.id == id:
return collectible
return null
func get_all_collectibles() -> Array[CollectibleResource]:
return library.collectibles
return collectibles_library.collectibles
func get_unlocked_collectible_ids() -> Array[String]:
return unlocked_ids
return unlocked_collectibles_ids
func save_photo_to_disk_async() -> void:
await RenderingServer.frame_post_draw
var image = get_viewport().get_texture().get_image()
if not DirAccess.dir_exists_absolute("user://photos"):
DirAccess.make_dir_absolute("user://photos")
var timestamp = str(Time.get_unix_time_from_system())
var file_path = "user://photos/photo_" + timestamp + ".png"
image.save_png(file_path)
saved_photos.append(file_path)
on_photo_saved.emit(file_path)
func get_save_data() -> Dictionary:
return {
"unlocked_collectibles_ids": unlocked_collectibles_ids,
"saved_photos": saved_photos
}
func load_save_data() -> void:
var my_path = str(get_path())
if GameState.save_data.has(my_path):
var data = GameState.save_data[my_path]
if data.has("unlocked_collectibles_ids"):
var loaded_ids = data["unlocked_collectibles_ids"]
unlocked_collectibles_ids.clear()
for id in loaded_ids:
unlocked_collectibles_ids.append(str(id))
if data.has("saved_photos"):
var loaded_photos = data["saved_photos"]
saved_photos.clear()
for photo in loaded_photos:
saved_photos.append(str(photo))
else:
unlocked_collectibles_ids.append("cane")

11
core/photo_mode/photo.gd Normal file
View File

@@ -0,0 +1,11 @@
extends Control
class_name Photo
@onready var texture_rect: TextureRect = $%PhotoTexture
func setup(texture: Texture) -> void:
if texture:
if !texture_rect:
texture_rect = $%PhotoTexture
texture_rect.texture = texture

View File

@@ -0,0 +1 @@
uid://dsey5dvc11vpq

View File

@@ -0,0 +1,38 @@
[gd_scene format=3 uid="uid://cvus62qkop3qi"]
[ext_resource type="Script" uid="uid://dsey5dvc11vpq" path="res://core/photo_mode/photo.gd" id="1_u0arp"]
[node name="Photo" type="Control" unique_id=201865221]
custom_minimum_size = Vector2(200, 200)
layout_mode = 3
anchors_preset = 0
offset_right = 200.0
offset_bottom = 200.0
script = ExtResource("1_u0arp")
[node name="ColorRect" type="ColorRect" parent="." unique_id=1208008621]
layout_mode = 0
offset_right = 200.0
offset_bottom = 200.0
[node name="MarginContainer" type="MarginContainer" parent="ColorRect" unique_id=1141643525]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme_override_constants/margin_left = 12
theme_override_constants/margin_top = 12
theme_override_constants/margin_right = 12
theme_override_constants/margin_bottom = 12
[node name="Panel" type="PanelContainer" parent="ColorRect/MarginContainer" unique_id=1372516531]
layout_mode = 2
[node name="PhotoTexture" type="TextureRect" parent="ColorRect/MarginContainer/Panel" unique_id=506844785]
unique_name_in_owner = true
layout_mode = 2
size_flags_vertical = 3
expand_mode = 1
stretch_mode = 6

View File

@@ -0,0 +1,27 @@
extends Control
@onready var grid_container: GridContainer = $%PhotoGrid
@onready var photo_scene = preload("res://core/photo_mode/photo.tscn")
func _ready() -> void:
CollectionManager.on_photo_saved.connect(_create_photo_thumbnail)
load_gallery()
func load_gallery() -> void:
for child in grid_container.get_children():
child.queue_free()
var saved_photos = CollectionManager.saved_photos
for file_path in saved_photos:
if FileAccess.file_exists(file_path):
_create_photo_thumbnail(file_path)
func _create_photo_thumbnail(file_path: String) -> void:
var image = Image.load_from_file(file_path)
if image:
var photo: Photo = photo_scene.instantiate()
var texture = ImageTexture.create_from_image(image)
photo.setup(texture)
grid_container.add_child(photo)

View File

@@ -0,0 +1 @@
uid://k4iqrlkbjppl

View File

@@ -0,0 +1,48 @@
[gd_scene format=3 uid="uid://b6r787sik5yil"]
[ext_resource type="Script" uid="uid://k4iqrlkbjppl" path="res://core/photo_mode/photo_gallery.gd" id="1_bp5uf"]
[node name="PhotoGallery" type="Control" unique_id=354419843]
layout_mode = 3
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_bp5uf")
[node name="PanelContainer" type="PanelContainer" parent="." unique_id=640179265]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -350.0
offset_top = -350.0
offset_right = 350.0
offset_bottom = 350.0
grow_horizontal = 2
grow_vertical = 2
[node name="MarginContainer" type="MarginContainer" parent="PanelContainer" unique_id=125147979]
layout_mode = 2
theme_override_constants/margin_left = 12
theme_override_constants/margin_top = 12
theme_override_constants/margin_right = 12
theme_override_constants/margin_bottom = 12
[node name="ScrollContainer" type="ScrollContainer" parent="PanelContainer/MarginContainer" unique_id=348576028]
layout_mode = 2
draw_focus_border = true
[node name="PhotoGrid" type="GridContainer" parent="PanelContainer/MarginContainer/ScrollContainer" unique_id=1187865988]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 6
size_flags_vertical = 2
theme_override_constants/h_separation = 12
theme_override_constants/v_separation = 12
columns = 3

View File

@@ -10,7 +10,7 @@ extends Node3D
@export_group("Ref")
@export var rotation_target: Node3D
@onready var camera: Camera3D = $Camera3D
@onready var camera: Camera3D = $%Camera3D
var total_yaw: float = 0.0
var total_pitch: float = 0.0
@@ -29,6 +29,8 @@ func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("toggle_photo_mode"):
is_active = !is_active
get_tree().paused = is_active
if !is_active:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
else:
@@ -38,7 +40,7 @@ func _unhandled_input(event: InputEvent) -> void:
total_yaw -= event.relative.x * rotation_speed
if event.is_action_pressed("take_photo"):
take_photo()
take_photo_async()
func _process(delta: float) -> void:
if not rotation_target:
@@ -66,13 +68,15 @@ func _process(delta: float) -> void:
global_position = orbit_pos + final_pan_offset
global_basis = rot_basis
func take_photo() -> void:
func take_photo_async() -> void:
if !is_active:
return
var targets = get_tree().get_nodes_in_group("collectible")
var space_state = get_world_3d().direct_space_state
await CollectionManager.save_photo_to_disk_async()
for target in targets:
if not target.collectible_data:
continue
@@ -85,5 +89,6 @@ func take_photo() -> void:
var result = space_state.intersect_ray(query)
if result and result.collider == target:
CollectionManager.unlock_photo(target.collectible_data.id)
print("Foto scattata a: ", target.collectible_data.id)
CollectionManager.unlock_collectible(target.collectible_data.id)
GameState.save_game()

View File

@@ -3,6 +3,8 @@
[ext_resource type="Script" uid="uid://d7ln6iru6mq6" path="res://core/photo_mode/photo_mode_controller.gd" id="1_uhpya"]
[node name="PhotoModeController" type="Node3D" unique_id=695158870]
process_mode = 3
script = ExtResource("1_uhpya")
[node name="Camera3D" type="Camera3D" parent="." unique_id=1369039645]
unique_name_in_owner = true