diff --git a/scenes/menu/create.tscn b/scenes/menu/create.tscn index 45ea7f2..26d6d07 100644 --- a/scenes/menu/create.tscn +++ b/scenes/menu/create.tscn @@ -11,37 +11,6 @@ grow_horizontal = 2 grow_vertical = 2 script = ExtResource("1_xsj3b") -[node name="input_name" type="TextEdit" parent="."] -offset_left = 16.0 -offset_top = 72.0 -offset_right = 272.0 -offset_bottom = 112.0 -placeholder_text = "Name" - -[node name="input_id" type="TextEdit" parent="."] -layout_mode = 0 -offset_left = 16.0 -offset_top = 16.0 -offset_right = 272.0 -offset_bottom = 56.0 -placeholder_text = "ID" - -[node name="input_hp" type="TextEdit" parent="."] -layout_mode = 0 -offset_left = 16.0 -offset_top = 128.0 -offset_right = 272.0 -offset_bottom = 168.0 -placeholder_text = "HP" - -[node name="input_level" type="TextEdit" parent="."] -layout_mode = 0 -offset_left = 16.0 -offset_top = 184.0 -offset_right = 272.0 -offset_bottom = 224.0 -placeholder_text = "Level" - [node name="button" type="Button" parent="."] layout_mode = 0 offset_left = 512.0 diff --git a/scenes/menu/input-output.tscn b/scenes/menu/input-output.tscn new file mode 100644 index 0000000..b72c7a6 --- /dev/null +++ b/scenes/menu/input-output.tscn @@ -0,0 +1,6 @@ +[gd_scene format=3 uid="uid://rkl6u7cdusys"] + +[node name="Input-output" type="TextEdit"] +offset_right = 168.0 +offset_bottom = 40.0 +scale = Vector2(2, 2) diff --git a/scenes/menu/main.tscn b/scenes/menu/main.tscn index 0f944aa..42c90dd 100644 --- a/scenes/menu/main.tscn +++ b/scenes/menu/main.tscn @@ -42,7 +42,7 @@ offset_top = 432.0 offset_right = 592.0 offset_bottom = 455.0 scale = Vector2(3, 3) -text = "Version: 0.0.1" +text = "Version: 0.0.2" [connection signal="pressed" from="Button" to="." method="_on_button_pressed"] [connection signal="pressed" from="Button2" to="." method="_on_button_2_pressed"] diff --git a/scenes/menu/view.tscn b/scenes/menu/view.tscn index 002cd6d..6bfc537 100644 --- a/scenes/menu/view.tscn +++ b/scenes/menu/view.tscn @@ -11,41 +11,6 @@ grow_horizontal = 2 grow_vertical = 2 script = ExtResource("1_dcehj") -[node name="input_id" type="TextEdit" parent="."] -layout_mode = 0 -offset_left = 16.0 -offset_top = 16.0 -offset_right = 272.0 -offset_bottom = 56.0 -placeholder_text = "ID" - -[node name="output_name" type="TextEdit" parent="."] -layout_mode = 0 -offset_left = 16.0 -offset_top = 72.0 -offset_right = 272.0 -offset_bottom = 112.0 -placeholder_text = "Name" -editable = false - -[node name="output_hp" type="TextEdit" parent="."] -layout_mode = 0 -offset_left = 16.0 -offset_top = 128.0 -offset_right = 272.0 -offset_bottom = 168.0 -placeholder_text = "HP" -editable = false - -[node name="output_level" type="TextEdit" parent="."] -layout_mode = 0 -offset_left = 16.0 -offset_top = 184.0 -offset_right = 272.0 -offset_bottom = 224.0 -placeholder_text = "Level" -editable = false - [node name="button" type="Button" parent="."] layout_mode = 0 offset_left = 512.0 diff --git a/scripts/createStats.gd b/scripts/createStats.gd index ed650bd..a3d4d49 100644 --- a/scripts/createStats.gd +++ b/scripts/createStats.gd @@ -1,15 +1,42 @@ extends Node -func saveJSON(): - var save_path = str("user://player_data"+$input_id.text+".json") +var field = preload("res://scenes/menu/input-output.tscn") +var data:Dictionary + +func loadJSON(save_path): + if not FileAccess.file_exists(save_path): + return false + var file_access = FileAccess.open(save_path, FileAccess.READ) + var json_string = file_access.get_line() + file_access.close() + + var json = JSON.new() + var error = json.parse(json_string) + if error: + print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) + return false + + data = json.data + return true + +func _ready(): + if loadJSON("res://content/stats.json"): + for i in range(int(data.get("amount"))): + var field_instance = field.instantiate() + add_child(field_instance) + field.resource_name = str("field_"+str(i)) + for i in range(int(data.get("amount"))): + get_child(i+1).position = Vector2(16,(16+i*88)) + get_child(i+1).placeholder_text = data.get(str(i)) + +func saveJSON(save_path): + var save_data = {} - var data := { - "name": $input_name.text, - "hp": $input_hp.text, - "level": $input_level.text - } + for i in range(int(data.get("amount"))): + if i > 0: + save_data[data.get(str(i))] = get_child(i+1).text - var json_string = JSON.stringify(data) + var json_string = JSON.stringify(save_data) var file_access = FileAccess.open(save_path, FileAccess.WRITE) if not file_access: @@ -20,7 +47,8 @@ func saveJSON(): file_access.close() func _on_button_pressed(): - saveJSON() + var save_path = str("user://player_data"+get_child(1).text+".json") + saveJSON(save_path) func _process(delta): if Input.is_action_pressed("escape"): diff --git a/scripts/getStats.gd b/scripts/getStats.gd index 3d4ba5f..06b08a9 100644 --- a/scripts/getStats.gd +++ b/scripts/getStats.gd @@ -1,10 +1,12 @@ extends Node -func loadJSON(): - var save_path = str("user://player_data"+$input_id.text+".json") - print(save_path) +var field = preload("res://scenes/menu/input-output.tscn") +var data:Dictionary +var amount + +func loadJSON(save_path): if not FileAccess.file_exists(save_path): - return + return false var file_access = FileAccess.open(save_path, FileAccess.READ) var json_string = file_access.get_line() file_access.close() @@ -13,16 +15,29 @@ func loadJSON(): var error = json.parse(json_string) if error: print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) - return + return false - var data:Dictionary = json.data - $output_name.text = str("Name: "+data.get("name")) - $output_hp.text = str("HP: "+data.get("hp")) - $output_level.text = str("Level: "+data.get("level")) - + data = json.data + return true + +func _ready(): + if loadJSON("res://content/stats.json"): + for i in range(int(data.get("amount"))): + var field_instance = field.instantiate() + add_child(field_instance) + field.resource_name = str("field_"+str(i)) + for i in range(int(data.get("amount"))): + get_child(i+1).position = Vector2(16,(16+i*88)) + get_child(i+1).placeholder_text = data.get(str(i)) + if i > 0: + get_child(i+1).editable = false + amount = data.get("amount") func _on_button_pressed(): - loadJSON() + var save_path = str("user://player_data"+get_child(1).text+".json") + if loadJSON(save_path): + for i in range(int(amount)-1): + get_child(i+2).text = data.get(str(get_child(i+2).placeholder_text)) func _process(delta): if Input.is_action_pressed("escape"): diff --git a/scripts/player.gd b/scripts/player.gd index 5306326..fb073d5 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -1,7 +1,6 @@ extends CharacterBody2D @export var speed = 200 -var toCalculate = position var distanceTo = 0 var active = false @@ -15,11 +14,9 @@ func _physics_process(delta): if active: get_input() move_and_slide() - distanceTo = position.distance_to(toCalculate) + var toCalculate = position + distanceTo += position.distance_to(toCalculate) if distanceTo > 500 and active: - $Label1.visible = false - $Label2.visible = false - $Label3.visible = false active = false $camera.enabled = false get_parent().next() @@ -28,28 +25,4 @@ func _physics_process(delta): func start(): $camera.enabled = true active = true - toCalculate = position - $Label1.visible = true - $Label2.visible = true - $Label3.visible = true -func loadJSON(id): - var save_path = str("user://player_data"+str(id)+".json") - print(save_path) - if not FileAccess.file_exists(save_path): - return - var file_access = FileAccess.open(save_path, FileAccess.READ) - var json_string = file_access.get_line() - file_access.close() - - var json = JSON.new() - var error = json.parse(json_string) - if error: - print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) - return - - var data:Dictionary = json.data - $Label1.text = str("Name: "+data.get("name")) - $Label2.text = str("HP: "+data.get("hp")) - $Label3.text = str("Level: "+data.get("level")) - diff --git a/testing/test/player.tscn b/testing/test/player.tscn index 532b903..beb3f36 100644 --- a/testing/test/player.tscn +++ b/testing/test/player.tscn @@ -17,30 +17,3 @@ shape = SubResource("RectangleShape2D_kf6qt") [node name="camera" type="Camera2D" parent="."] enabled = false - -[node name="Label1" type="Label" parent="."] -visible = false -offset_left = -384.0 -offset_top = 56.0 -offset_right = -288.0 -offset_bottom = 88.0 -scale = Vector2(2, 2) -text = "1231" - -[node name="Label2" type="Label" parent="."] -visible = false -offset_left = -384.0 -offset_top = 120.0 -offset_right = -288.0 -offset_bottom = 152.0 -scale = Vector2(2, 2) -text = "1231" - -[node name="Label3" type="Label" parent="."] -visible = false -offset_left = -384.0 -offset_top = 184.0 -offset_right = -288.0 -offset_bottom = 216.0 -scale = Vector2(2, 2) -text = "1231"