load/save maps

This commit is contained in:
James 2023-12-05 15:48:07 +00:00
parent b81a581fd2
commit 13c1bf8350
14 changed files with 134 additions and 198 deletions

View File

@ -1,4 +1,4 @@
extends Label extends PanelContainer
var elapsed_time = 0.0 # Initialize a variable to keep track of elapsed time var elapsed_time = 0.0 # Initialize a variable to keep track of elapsed time
@ -13,4 +13,10 @@ func _process(delta):
var seconds = int(elapsed_time) % 60 var seconds = int(elapsed_time) % 60
var time_string = "%02d:%02d:%02d" % [hours, minutes, seconds] # Format time as H:M:S var time_string = "%02d:%02d:%02d" % [hours, minutes, seconds] # Format time as H:M:S
var fps = Engine.get_frames_per_second() # Get the current FPS var fps = Engine.get_frames_per_second() # Get the current FPS
text = time_string+" "+str(fps) + " fps" # Combine FPS and runtime into one string $HBoxContainer/Debug.text = time_string+" "+str(fps) + " fps" # Combine FPS and runtime into one string
func _on_save_pressed():
$"../SaveDialog".popup_centered()
func _on_load_pressed():
$"../LoadDialog".popup_centered()

36
LoadDialog.gd Normal file
View File

@ -0,0 +1,36 @@
extends ConfirmationDialog
func _on_about_to_popup():
$ItemList.clear()
#var dir = DirAccess.open("user://saves/")
var dir = DirAccess.open("user://saves/")
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if dir.current_is_dir():
print("Found directory: " + file_name)
else:
print("Found file: " + file_name)
$ItemList.add_item(file_name)
file_name = dir.get_next()
else:
print("An error occurred when trying to access the path.")
func _on_confirmed():
# Get the Map node
var sim = get_node("../../../Sim")
var old_map = get_node("../../Map")
# Load and instance the selected item
for item in $ItemList.get_selected_items():
print("load:", $ItemList.get_item_text(item))
var res = ResourceLoader.load("user://saves/" + $ItemList.get_item_text(item))
if res and res is PackedScene:
var instance = res.instantiate()
sim.remove_child(old_map)
sim.add_child(instance)
else:
print("Error loading")

View File

@ -6,20 +6,10 @@ var spawn_position=Vector2(100,100);
func _ready(): func _ready():
pass pass
func _input(event): func _unhandled_input(event):
if event.is_action_pressed("escape"):
get_tree().change_scene_to_file("res://map_select/map_select.tscn")
get_node("/root/Sim").queue_free()
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT: if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
spawn_position=get_global_mouse_position() spawn_position=get_global_mouse_position()
spawn() spawn()
$Timer.stop()
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_RIGHT:
spawn_position=get_global_mouse_position()
$Timer.start()
func _on_timer_timeout():
spawn()
func spawn(): func spawn():
var new_player = load("res://player.tscn").instantiate() var new_player = load("res://player.tscn").instantiate()

21
SaveDialog.gd Normal file
View File

@ -0,0 +1,21 @@
extends ConfirmationDialog
var base_path = "user://saves/"
func _ready():
if not DirAccess.dir_exists_absolute(base_path):
DirAccess.make_dir_recursive_absolute(base_path)
func _on_about_to_popup():
pass # Replace with function body.
func _on_confirmed():
var filename=base_path+$LineEdit.text+".tscn"
print("Save: "+filename)
var packed_scene = PackedScene.new()
if packed_scene.pack($"../../Map") == OK:
var result = ResourceSaver.save(packed_scene,filename)
if result != OK:
print("Failed to save scene. Error #",result)
else:
print("Failed to pack scene.")

View File

@ -1,17 +0,0 @@
extends Button
@export_file var map_path
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func _on_pressed():
if map_path==null:
return
var sim = load("res://sim.tscn").instantiate()
var map = load(map_path).new()
map.create_map(sim)
get_tree().root.add_child(sim)
get_node("/root/MapSelect").queue_free()

View File

@ -1,12 +0,0 @@
extends Node2D
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
if Input.is_action_just_pressed("escape"):
get_tree().quit() # default behavior

View File

@ -1,59 +0,0 @@
[gd_scene load_steps=4 format=3 uid="uid://iskh4m3ltqul"]
[ext_resource type="Script" path="res://map_select/map_select.gd" id="1_w7uu4"]
[ext_resource type="Script" path="res://map_select/Button.gd" id="2_ykkpr"]
[sub_resource type="LabelSettings" id="LabelSettings_kd1xo"]
font_size = 32
[node name="MapSelect" type="Node2D"]
script = ExtResource("1_w7uu4")
[node name="Title" type="Label" parent="."]
offset_left = 29.0
offset_top = 17.0
offset_right = 198.0
offset_bottom = 65.0
text = "Map Select"
label_settings = SubResource("LabelSettings_kd1xo")
[node name="Button" type="Button" parent="."]
offset_left = 26.0
offset_top = 165.0
offset_right = 201.0
offset_bottom = 228.0
text = "Rare"
script = ExtResource("2_ykkpr")
map_path = "res://maps/rare.gd"
[node name="Button2" type="Button" parent="."]
offset_left = 211.0
offset_top = 80.0
offset_right = 386.0
offset_bottom = 143.0
text = "Large"
script = ExtResource("2_ykkpr")
map_path = "res://maps/large.gd"
[node name="Button7" type="Button" parent="."]
offset_left = 26.0
offset_top = 250.0
offset_right = 201.0
offset_bottom = 313.0
text = "Sections"
script = ExtResource("2_ykkpr")
map_path = "res://maps/sections.gd"
[node name="Button8" type="Button" parent="."]
offset_left = 24.0
offset_top = 80.0
offset_right = 199.0
offset_bottom = 143.0
text = "Small"
script = ExtResource("2_ykkpr")
map_path = "res://maps/small.gd"
[connection signal="pressed" from="Button" to="Button" method="_on_pressed"]
[connection signal="pressed" from="Button2" to="Button2" method="_on_pressed"]
[connection signal="pressed" from="Button7" to="Button7" method="_on_pressed"]
[connection signal="pressed" from="Button8" to="Button8" method="_on_pressed"]

View File

@ -1,17 +0,0 @@
var root
func create_map(_root):
root=_root
poolcircle(Vector2(0,0),2000,100,1000,1000,1000)
root.get_node("Camera2D").zoom=Vector2(0.2,0.2)
func poolcircle(pos: Vector2, radius, count, r,g,b):
for n in range(1,count+1):
var new_pool: Pool = load("res://pool.tscn").instantiate()
var t=(float(n)/count)*3.141*2
new_pool.position = pos + (Vector2(sin(t),cos(t))*radius)
new_pool.mana_r=r
new_pool.mana_g=g
new_pool.mana_b=b
new_pool.get_node("StaticBody2D/CollisionShape2D2").disabled=true;
root.call_deferred("add_child",new_pool)

View File

@ -1,18 +0,0 @@
var root
func create_map(_root):
self.root=_root
poolcircle(Vector2(0,0),0,1,10,0,0)
poolcircle(Vector2(0,0),2000,100,0,5000,0)
root.get_node("Camera2D").zoom=Vector2(0.3,0.3)
func poolcircle(pos: Vector2, radius, count, r,g,b):
for n in range(1,count+1):
var new_pool: Pool = load("res://pool.tscn").instantiate()
var t=(float(n)/count)*3.141*2
new_pool.position = pos + (Vector2(sin(t),cos(t))*radius)
new_pool.mana_r=r
new_pool.mana_g=g
new_pool.mana_b=b
new_pool.get_node("StaticBody2D/CollisionShape2D2").disabled=true;
root.call_deferred("add_child",new_pool)

View File

@ -1,25 +0,0 @@
var root
func create_map(root):
self.root=root
poolcircle(Vector2(0,0),200,32,100,0,0)
poolcircle(Vector2(600,0),200,32,0,100,0)
poolcircle(Vector2(1200,0),200,32,0,0,100)
poolcircle(Vector2(0,600),200,32,0,50,50)
poolcircle(Vector2(600,600),200,32,50,0,50)
poolcircle(Vector2(1200,600),200,32,50,50,0)
poolcircle(Vector2(600,200),2000,100,1000,1000,1000)
func poolcircle(pos: Vector2, radius, count, r,g,b):
for n in range(1,count+1):
var new_pool: Pool = load("res://pool.tscn").instantiate()
var t=(float(n)/count)*3.141*2
new_pool.position = pos + (Vector2(sin(t),cos(t))*radius)
new_pool.mana_r=r
new_pool.mana_g=g
new_pool.mana_b=b
new_pool.get_node("StaticBody2D/CollisionShape2D2").disabled=true;
root.call_deferred("add_child",new_pool)

View File

@ -1,16 +0,0 @@
var root
func create_map(_root):
root=_root
poolcircle(Vector2(0,0),200,16,100,100,100)
func poolcircle(pos: Vector2, radius, count, r,g,b):
for n in range(1,count+1):
var new_pool: Pool = load("res://pool.tscn").instantiate()
var t=(float(n)/count)*3.141*2
new_pool.position = pos + (Vector2(sin(t),cos(t))*radius)
new_pool.mana_r=r
new_pool.mana_g=g
new_pool.mana_b=b
new_pool.get_node("StaticBody2D/CollisionShape2D2").disabled=true;
root.call_deferred("add_child",new_pool)

View File

@ -11,7 +11,7 @@ config_version=5
[application] [application]
config/name="RGB Physics Life" config/name="RGB Physics Life"
run/main_scene="res://map_select/map_select.tscn" run/main_scene="res://sim.tscn"
config/features=PackedStringArray("4.2", "Mobile") config/features=PackedStringArray("4.2", "Mobile")
config/icon="res://images/icon.svg" config/icon="res://images/icon.svg"

View File

@ -1,31 +1,78 @@
[gd_scene load_steps=5 format=3 uid="uid://bpy475nyq5ea1"] [gd_scene load_steps=7 format=3 uid="uid://bpy475nyq5ea1"]
[ext_resource type="Script" path="res://sim.gd" id="1_wbm7g"] [ext_resource type="Script" path="res://Map.gd" id="2_rolej"]
[ext_resource type="Script" path="res://Debug.gd" id="2_iqnjl"] [ext_resource type="Script" path="res://ScreenShots.gd" id="3_7b3mn"]
[ext_resource type="Script" path="res://TimerSS.gd" id="3_7b3mn"]
[ext_resource type="Script" path="res://Camera2D.gd" id="4_g4kw0"] [ext_resource type="Script" path="res://Camera2D.gd" id="4_g4kw0"]
[ext_resource type="Script" path="res://HUD.gd" id="5_pfkj4"]
[ext_resource type="Script" path="res://LoadDialog.gd" id="6_0mayr"]
[ext_resource type="Script" path="res://SaveDialog.gd" id="6_v2wl8"]
[node name="Sim" type="Node2D"] [node name="Sim" type="Node2D"]
script = ExtResource("1_wbm7g")
[node name="Timer" type="Timer" parent="."]
wait_time = 0.3
[node name="TimerSS" type="Timer" parent="."]
wait_time = 60.0
autostart = true
script = ExtResource("3_7b3mn")
[node name="Camera2D" type="Camera2D" parent="."] [node name="Camera2D" type="Camera2D" parent="."]
script = ExtResource("4_g4kw0") script = ExtResource("4_g4kw0")
[node name="CanvasLayer" type="CanvasLayer" parent="."] [node name="Map" type="Node2D" parent="."]
script = ExtResource("2_rolej")
[node name="Debug" type="Label" parent="CanvasLayer"] [node name="ScreenOverlay" type="CanvasLayer" parent="."]
offset_right = 67.0
offset_bottom = 26.0 [node name="HUD" type="PanelContainer" parent="ScreenOverlay"]
anchors_preset = 10
anchor_right = 1.0
grow_horizontal = 2
script = ExtResource("5_pfkj4")
[node name="HBoxContainer" type="HBoxContainer" parent="ScreenOverlay/HUD"]
layout_mode = 2
[node name="Debug" type="Label" parent="ScreenOverlay/HUD/HBoxContainer"]
layout_mode = 2
text = "debug" text = "debug"
script = ExtResource("2_iqnjl")
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"] [node name="Save" type="Button" parent="ScreenOverlay/HUD/HBoxContainer"]
[connection signal="timeout" from="TimerSS" to="TimerSS" method="_on_timeout"] layout_mode = 2
text = "Save..."
[node name="Load" type="Button" parent="ScreenOverlay/HUD/HBoxContainer"]
layout_mode = 2
text = "Load..."
[node name="LoadDialog" type="ConfirmationDialog" parent="ScreenOverlay"]
title = "Load"
position = Vector2i(0, 36)
size = Vector2i(200, 200)
script = ExtResource("6_0mayr")
[node name="ItemList" type="ItemList" parent="ScreenOverlay/LoadDialog"]
offset_left = 8.0
offset_top = 8.0
offset_right = 192.0
offset_bottom = 151.0
[node name="SaveDialog" type="ConfirmationDialog" parent="ScreenOverlay"]
mode = 1
title = "Save"
initial_position = 4
ok_button_text = "Save"
script = ExtResource("6_v2wl8")
[node name="LineEdit" type="LineEdit" parent="ScreenOverlay/SaveDialog"]
offset_left = 8.0
offset_top = 8.0
offset_right = 192.0
offset_bottom = 51.0
placeholder_text = "Filename"
[node name="ScreenShots" type="Timer" parent="."]
wait_time = 60.0
autostart = true
script = ExtResource("3_7b3mn")
[connection signal="pressed" from="ScreenOverlay/HUD/HBoxContainer/Save" to="ScreenOverlay/HUD" method="_on_save_pressed"]
[connection signal="pressed" from="ScreenOverlay/HUD/HBoxContainer/Load" to="ScreenOverlay/HUD" method="_on_load_pressed"]
[connection signal="about_to_popup" from="ScreenOverlay/LoadDialog" to="ScreenOverlay/LoadDialog" method="_on_about_to_popup"]
[connection signal="confirmed" from="ScreenOverlay/LoadDialog" to="ScreenOverlay/LoadDialog" method="_on_confirmed"]
[connection signal="about_to_popup" from="ScreenOverlay/SaveDialog" to="ScreenOverlay/SaveDialog" method="_on_about_to_popup"]
[connection signal="confirmed" from="ScreenOverlay/SaveDialog" to="ScreenOverlay/SaveDialog" method="_on_confirmed"]
[connection signal="timeout" from="ScreenShots" to="ScreenShots" method="_on_timeout"]