add gamestate, doc and fix
This commit is contained in:
@@ -9,9 +9,9 @@ class_name AIBase
|
||||
toggle_enable_state_machine()
|
||||
|
||||
|
||||
@onready var patrol_radius_shape: CollisionShape3D = $PatrolRadiusShape
|
||||
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
|
||||
@onready var state_machine: StateMachine = $StateMachine
|
||||
@onready var patrol_radius_shape: CollisionShape3D = $%PatrolRadiusShape
|
||||
@onready var nav_agent: NavigationAgent3D = $%NavigationAgent3D
|
||||
@onready var state_machine: StateMachine = $%StateMachine
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -19,7 +19,7 @@ func _ready() -> void:
|
||||
toggle_enable_state_machine()
|
||||
|
||||
func toggle_enable_state_machine() -> void:
|
||||
$StateMachine.enable = enable_state_machine
|
||||
state_machine.enable = enable_state_machine
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if !enable_state_machine:
|
||||
|
||||
@@ -22,8 +22,10 @@ mesh = SubResource("CapsuleMesh_mh3lg")
|
||||
shape = SubResource("CapsuleShape3D_mh3lg")
|
||||
|
||||
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="." unique_id=1370024449]
|
||||
unique_name_in_owner = true
|
||||
|
||||
[node name="StateMachine" type="Node" parent="." unique_id=1286404264 node_paths=PackedStringArray("initial_state")]
|
||||
unique_name_in_owner = true
|
||||
script = ExtResource("2_q1hg3")
|
||||
initial_state = NodePath("IdleState")
|
||||
|
||||
@@ -36,6 +38,7 @@ script = ExtResource("4_lim44")
|
||||
state_id = "patrol"
|
||||
|
||||
[node name="PatrolRadiusShape" type="CollisionShape3D" parent="." unique_id=1379515938]
|
||||
unique_name_in_owner = true
|
||||
shape = SubResource("SphereShape3D_lim44")
|
||||
disabled = true
|
||||
debug_color = Color(1, 1, 0, 1)
|
||||
|
||||
@@ -44,5 +44,3 @@ func _on_state_transition(state: State, new_state_id: String) -> void:
|
||||
|
||||
current_state = new_state
|
||||
current_state.enter()
|
||||
|
||||
print("Transizione a: ", current_state.state_id)
|
||||
|
||||
41
core/game_state/game_state.gd
Normal file
41
core/game_state/game_state.gd
Normal file
@@ -0,0 +1,41 @@
|
||||
extends Node
|
||||
|
||||
const SAVE_PATH: String = "user://savegame.json"
|
||||
|
||||
var save_data: Dictionary = {}
|
||||
var is_loaded: bool = false
|
||||
|
||||
func _ready() -> void:
|
||||
load_game()
|
||||
|
||||
func save_game() -> void:
|
||||
var save_file = FileAccess.open(SAVE_PATH, FileAccess.WRITE)
|
||||
var save_nodes = get_tree().get_nodes_in_group("Saveable")
|
||||
var new_save_data: Dictionary = {}
|
||||
|
||||
for node in save_nodes:
|
||||
if not node.has_method("get_save_data"):
|
||||
continue
|
||||
|
||||
var node_data = node.get_save_data()
|
||||
var node_path = str(node.get_path())
|
||||
new_save_data[node_path] = node_data
|
||||
|
||||
var json_string = JSON.stringify(new_save_data)
|
||||
save_file.store_line(json_string)
|
||||
|
||||
func load_game() -> void:
|
||||
if not FileAccess.file_exists(SAVE_PATH):
|
||||
is_loaded = true
|
||||
return
|
||||
|
||||
var file = FileAccess.open(SAVE_PATH, FileAccess.READ)
|
||||
var json_string = file.get_as_text()
|
||||
|
||||
var json = JSON.new()
|
||||
var parse_result = json.parse(json_string)
|
||||
|
||||
if parse_result == OK:
|
||||
save_data = json.data
|
||||
|
||||
is_loaded = true
|
||||
1
core/game_state/game_state.gd.uid
Normal file
1
core/game_state/game_state.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b17g2w2g101o6
|
||||
@@ -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:
|
||||
@@ -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
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
11
core/photo_mode/photo.gd
Normal 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
|
||||
1
core/photo_mode/photo.gd.uid
Normal file
1
core/photo_mode/photo.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dsey5dvc11vpq
|
||||
38
core/photo_mode/photo.tscn
Normal file
38
core/photo_mode/photo.tscn
Normal 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
|
||||
27
core/photo_mode/photo_gallery.gd
Normal file
27
core/photo_mode/photo_gallery.gd
Normal 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)
|
||||
1
core/photo_mode/photo_gallery.gd.uid
Normal file
1
core/photo_mode/photo_gallery.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://k4iqrlkbjppl
|
||||
48
core/photo_mode/photo_gallery.tscn
Normal file
48
core/photo_mode/photo_gallery.tscn
Normal 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
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user