diff --git a/project.godot b/project.godot index f159b28..2734c58 100644 --- a/project.godot +++ b/project.godot @@ -15,13 +15,6 @@ run/main_scene="res://scenes/menu/main.tscn" config/features=PackedStringArray("4.2", "GL Compatibility") config/icon="res://icon.svg" -[autoload] - -Load="*res://scripts/load.gd" -Save="*res://scripts/save.gd" -Content="*res://scripts/content.gd" -Ui="*res://scripts/ui.gd" - [display] window/size/viewport_width=800 diff --git a/scenes/menu/main.tscn b/scenes/menu/main.tscn index 7fe18d8..b853ba5 100644 --- a/scenes/menu/main.tscn +++ b/scenes/menu/main.tscn @@ -37,20 +37,12 @@ text = "Creator" [node name="Label" type="Label" parent="."] layout_mode = 0 -offset_left = 456.0 +offset_left = 488.0 offset_top = 432.0 -offset_right = 571.0 +offset_right = 592.0 offset_bottom = 455.0 scale = Vector2(3, 3) -text = "Milestone 1 b5" - -[node name="Label2" type="Label" parent="."] -layout_mode = 0 -offset_left = 544.0 -offset_right = 629.0 -offset_bottom = 23.0 -scale = Vector2(3, 3) -text = "FreeTTRPG" +text = "Version: 0.0.4" [connection signal="pressed" from="Button" to="." method="_on_button_pressed"] [connection signal="pressed" from="Button2" to="." method="_on_button_2_pressed"] diff --git a/scripts/content.gd b/scripts/content.gd deleted file mode 100644 index 6208c99..0000000 --- a/scripts/content.gd +++ /dev/null @@ -1,12 +0,0 @@ -extends Node - -# Variable to store the path from which JSON data is loaded -var loadPath - -# Variable to store the loaded JSON data -var data - -# Currently placeholder function, to be deprecated once custom content loading is enabled. -func _ready(): - loadPath = "res://content/stats.json" - data = Load.loadJSON(loadPath) diff --git a/scripts/createStats.gd b/scripts/createStats.gd index f15f719..4d09ec6 100644 --- a/scripts/createStats.gd +++ b/scripts/createStats.gd @@ -1,31 +1,54 @@ extends Node -# Variable to store loaded JSON data -var data -var amount +var field = preload("res://scenes/menu/inputOutput.tscn") +var data:Dictionary + +func loadJSON(savePath): + if not FileAccess.file_exists(savePath): + return false + var fileAccess = FileAccess.open(savePath, FileAccess.READ) + var jsonString = fileAccess.get_line() + fileAccess.close() + + var json = JSON.new() + var error = json.parse(jsonString) + if error: + print("JSON Parse Error: ", json.get_error_message(), " in ", jsonString, " at line ", json.get_error_line()) + return false + + data = json.data + return true -# Get all needed things for Ui.placeFields and save amount from it. func _ready(): - data = Content.data - var container = $"ScrollContainer/VBoxContainer" - Ui.placeFields(data, container, true) - amount = data.get("amount") + if loadJSON("res://content/stats.json"): + for i in range(int(data.get("amount"))): + var fieldInstance = field.instantiate() + $"ScrollContainer/VBoxContainer".add_child(fieldInstance) + $"ScrollContainer/VBoxContainer".get_child(i).position = Vector2(16,(16+i*88)) + $"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i)) + $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 -# Upon pressing the "Send" button -func _on_button_pressed(): - # Create the save path based on which ID was entered - var savePath = "user://player_" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json" - - # Get data from the fields to save, except the first one, which is the ID and should be used in the filename. +func saveJSON(savePath): var saveData = {} + for i in range(int(data.get("amount"))): if i > 0: saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text - # Call the global save function - Save.saveJSON(savePath, saveData) + var jsonString = JSON.stringify(saveData) + + var fileAccess = FileAccess.open(savePath, FileAccess.WRITE) + if not fileAccess: + print("An error happened while saving data: ", FileAccess.get_open_error()) + return + + fileAccess.store_line(jsonString) + fileAccess.close() + +func _on_button_pressed(): + var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") + saveJSON(savePath) -# Only for the Main Menu "shortcut" func _process(delta): if Input.is_action_pressed("escape"): get_tree().change_scene_to_file("res://scenes/menu/main.tscn") diff --git a/scripts/getStats.gd b/scripts/getStats.gd index 750d561..a856898 100644 --- a/scripts/getStats.gd +++ b/scripts/getStats.gd @@ -1,37 +1,46 @@ extends Node -# Variable to store loaded JSON data -var data +var field = preload("res://scenes/menu/inputOutput.tscn") +var data:Dictionary var amount -# Get all needed things for Ui.placeFields and save amount from it. +func loadJSON(savePath): + if not FileAccess.file_exists(savePath): + return false + var fileAccess = FileAccess.open(savePath, FileAccess.READ) + var json_string = fileAccess.get_line() + fileAccess.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(): - data = Content.data - var container = $"ScrollContainer/VBoxContainer" - Ui.placeFields(data, container, false) - amount = data.get("amount") + if loadJSON("res://content/stats.json"): + for i in range(int(data.get("amount"))): + var fieldInstance = field.instantiate() + $"ScrollContainer/VBoxContainer".add_child(fieldInstance) + $"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i)) + $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 + if i > 0: + $"ScrollContainer/VBoxContainer".get_child(i).editable = false + amount = data.get("amount") -# Upon pressing the "Get" button, also called in game func _on_button_pressed(): - # Construct save path based on user input - var savePath = "user://player_" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json" - - # Load JSON data from the constructed save path - data = Load.loadJSON(savePath) - - # Check if data is successfully loaded (type 27 corresponds to Dictionary in Godot, although the wiki says it's 18...) - if typeof(data) == 27: - # Populate fields with loaded data - for i in range(int(amount) - 1): - $"ScrollContainer/VBoxContainer".get_child(i + 1).text = data.get(str($"ScrollContainer/VBoxContainer".get_child(i + 1).placeholder_text)) + var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") + if loadJSON(savePath): + for i in range(int(amount)-1): + $"ScrollContainer/VBoxContainer".get_child(i+1).text = data.get(str($"ScrollContainer/VBoxContainer".get_child(i+1).placeholder_text)) -# Only for the Main Menu "shortcut" func _process(_delta): - # Check if the Escape key is pressed to change scene to main.tscn if Input.is_action_pressed("escape"): get_tree().change_scene_to_file("res://scenes/menu/main.tscn") -# Function to hide the 'get' button func hideGet(): $button.visible = false $button.disabled = true diff --git a/scripts/load.gd b/scripts/load.gd deleted file mode 100644 index 28e9dc1..0000000 --- a/scripts/load.gd +++ /dev/null @@ -1,26 +0,0 @@ -extends Node - -# Function to load JSON data from a file -func loadJSON(savePath): - # Check if the file exists - if not FileAccess.file_exists(savePath): - return 1 # Return error code 1 if file does not exist - - # Open the file for reading - var fileAccess = FileAccess.open(savePath, FileAccess.READ) - - # Read the entire JSON string from the file - var jsonString = fileAccess.get_line() - - fileAccess.close() # Close the file - - # Create a new JSON instance - var json = JSON.new() - - # Parse the JSON string into a JSON object - var error = json.parse(jsonString) - - if error: - return 1 # Return error code 1 if there was an error parsing JSON - - return json.data # Return the loaded data diff --git a/scripts/mapscript.gd b/scripts/mapscript.gd index 16a71bb..5bc567e 100644 --- a/scripts/mapscript.gd +++ b/scripts/mapscript.gd @@ -1,57 +1,37 @@ extends Node2D -# Number of players and current player index -var playerAmount = 2 -var playerIndex = 0 - -# Variable to hold the current player's object name +var playerAmount=2 +var playerIndex=0 var objectName -# Kick the game off func _ready(): - # Hide character viewer GUI initially $"../characterViewer".hideGet() - # Start cycling through players next() -# Function to switch to the next player func next(): if playerIndex >= playerAmount: playerIndex = 0 - - # Construct object name based on player index - objectName = "player%d" % playerIndex - - # Start the current player's activity + objectName= "player%d" % playerIndex + print(objectName) get_node(objectName).start() - - playerIndex += 1 # Move to the next player index + playerIndex += 1 -# Function to display statistics, lots of relative links, may change eventually func stats(): - visible = false # Hide game area + visible = false $"../characterViewer".visible = true get_node(objectName).stop() - # Activate back button $"../back".disabled = false $"../back".visible = true - - # Update player index display in character viewer - $"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).text = str(playerIndex - 1) + $"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).text = str(playerIndex-1) $"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).editable = false - - # Simulate button press in character viewer (assuming this triggers further actions) $"../characterViewer"._on_button_pressed() -# Function to restart the node's state func restart(): - visible = true # Show game area + visible = true $"../characterViewer".visible = false get_node(objectName).start() - # Activate back button $"../back".disabled = true $"../back".visible = false -# Handler for when the back button is pressed func _on_back_pressed(): - restart() # Gives control back to the current player character + restart() diff --git a/scripts/menu.gd b/scripts/menu.gd index 276be54..8c35282 100644 --- a/scripts/menu.gd +++ b/scripts/menu.gd @@ -1,13 +1,14 @@ extends Node -# Function called when "Play" is pressed + + func _on_button_pressed(): get_tree().change_scene_to_file("res://scenes/map/map.tscn") -# Function called when "View" is pressed + func _on_button_2_pressed(): get_tree().change_scene_to_file("res://scenes/menu/view.tscn") -# Function called when "Create" is pressed + func _on_button_3_pressed(): get_tree().change_scene_to_file("res://scenes/menu/create.tscn") diff --git a/scripts/player.gd b/scripts/player.gd index 0b614f1..b1c9c40 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -1,55 +1,39 @@ extends CharacterBody2D @export var speed = 200 - -# Distance traveled accumulator var distanceTo = 0 - -# Flag to indicate if the character is currently the active player. var active = false -# Function to handle user input func get_input(): - # Main Menu "shortcut" if Input.is_action_pressed("escape"): get_tree().change_scene_to_file("res://scenes/menu/main.tscn") - - # Get directional input and set velocity accordingly var input_direction = Input.get_vector("left", "right", "up", "down") velocity = input_direction * speed func _physics_process(delta): - # Store current position for distance calculation var toCalculate = position - - # Process input if the character is active if active: get_input() move_and_slide() - - # Accumulate distance traveled distanceTo += position.distance_to(toCalculate) - - # Check if distance threshold is exceeded and the character is active + print(distanceTo) if distanceTo > 500 and active: stop() distanceTo = 0 - get_parent().next() # Go to the next player + get_parent().next() + -# Set main character as active func start(): $camera.enabled = true active = true $stats.disabled = false $stats.visible = true -# Set main character as inactive func stop(): $camera.enabled = false active = false $stats.disabled = true $stats.visible = false -# Handler for when the stats button is pressed func _on_stats_pressed(): - get_parent().stats() # Open up the stats menu for the current player + get_parent().stats() diff --git a/scripts/save.gd b/scripts/save.gd deleted file mode 100644 index f23022c..0000000 --- a/scripts/save.gd +++ /dev/null @@ -1,21 +0,0 @@ -extends Node - -# Function to save data in JSON format to a specified path -func saveJSON(savePath, saveData): - # Convert the saveData dictionary to a JSON string - var jsonString = JSON.stringify(saveData) - - # Attempt to open a file for writing at the specified savePath - var fileAccess = FileAccess.open(savePath, FileAccess.WRITE) - - # Check if the file was successfully opened - if not fileAccess: - # If not, print an error message with the reason for the failure and return an error code - print("An error happened while saving data: ", FileAccess.get_open_error()) - return 1 - - # Write the JSON string to the file - fileAccess.store_line(jsonString) - - # Close the file to ensure all data is properly saved and resources are freed - fileAccess.close() diff --git a/scripts/ui.gd b/scripts/ui.gd deleted file mode 100644 index bb5ca8f..0000000 --- a/scripts/ui.gd +++ /dev/null @@ -1,21 +0,0 @@ -extends Node - -# Loading the stat field -var field = preload("res://scenes/menu/inputOutput.tscn") - -# Creating all necessary stat fields -func placeFields(data, container, isWriteable): - # Load which stats should be displayed - - # Create the fields one-by-one - for i in range(int(data.get("amount"))): - var fieldInstance = field.instantiate() - container.add_child(fieldInstance) - - # Changes their placeholder based on what should be entered or seen in that field - container.get_child(i).placeholder_text = data.get(str(i)) - container.get_child(i).custom_minimum_size.y = 40 - - # Every field except ID (if not isWriteable) - if i > 0 and !isWriteable: - container.get_child(i).editable = false diff --git a/testing/test/player.tscn b/testing/test/player.tscn index 8a64c0f..943ffa7 100644 --- a/testing/test/player.tscn +++ b/testing/test/player.tscn @@ -1,32 +1,31 @@ [gd_scene load_steps=4 format=3 uid="uid://dv67vdgb4h44q"] [ext_resource type="Texture2D" uid="uid://dheqglouhkis6" path="res://testing/testAssets/player.png" id="1_cujcj"] - [ext_resource type="Script" path="res://scripts/player.gd" id="1_oik63"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_kf6qt"] size = Vector2(20, 18) [node name="player" type="CharacterBody2D"] -script = ExtResource("1_oik63") +script = ExtResource("1_oik63") [node name="sprite" type="Sprite2D" parent="."] texture = ExtResource("1_cujcj") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource("RectangleShape2D_kf6qt") +shape = SubResource("RectangleShape2D_kf6qt") [node name="camera" type="Camera2D" parent="."] -enabled = false +enabled = false [node name="stats" type="Button" parent="."] -visible = false -offset_left = 112.0 -offset_top = 152.0 -offset_right = 238.0 -offset_bottom = 187.0 +visible = false +offset_left = 112.0 +offset_top = 152.0 +offset_right = 238.0 +offset_bottom = 187.0 scale = Vector2(2, 2) -disabled = true -text = "Stats" +disabled = true +text = "Stats" [connection signal="pressed" from="stats" to="." method="_on_stats_pressed"] diff --git a/testing/testAssets/sand.png b/testing/testAssets/sand.png new file mode 100644 index 0000000..5647d26 Binary files /dev/null and b/testing/testAssets/sand.png differ