Code Refactoring: Part 2
Hopefully final refactoring, offloaded ui scripts to its own global .gd to prevent repetition.
This commit is contained in:
parent
1f13b9d7b2
commit
6b2159b840
10 changed files with 182 additions and 187 deletions
|
@ -20,6 +20,7 @@ config/icon="res://icon.svg"
|
||||||
Load="*res://scripts/load.gd"
|
Load="*res://scripts/load.gd"
|
||||||
Save="*res://scripts/save.gd"
|
Save="*res://scripts/save.gd"
|
||||||
Content="*res://scripts/content.gd"
|
Content="*res://scripts/content.gd"
|
||||||
|
Ui="*res://scripts/ui.gd"
|
||||||
|
|
||||||
[display]
|
[display]
|
||||||
|
|
||||||
|
|
|
@ -37,12 +37,20 @@ text = "Creator"
|
||||||
|
|
||||||
[node name="Label" type="Label" parent="."]
|
[node name="Label" type="Label" parent="."]
|
||||||
layout_mode = 0
|
layout_mode = 0
|
||||||
offset_left = 488.0
|
offset_left = 456.0
|
||||||
offset_top = 432.0
|
offset_top = 432.0
|
||||||
offset_right = 592.0
|
offset_right = 571.0
|
||||||
offset_bottom = 455.0
|
offset_bottom = 455.0
|
||||||
scale = Vector2(3, 3)
|
scale = Vector2(3, 3)
|
||||||
text = "Version: 0.0.4"
|
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"
|
||||||
|
|
||||||
[connection signal="pressed" from="Button" to="." method="_on_button_pressed"]
|
[connection signal="pressed" from="Button" to="." method="_on_button_pressed"]
|
||||||
[connection signal="pressed" from="Button2" to="." method="_on_button_2_pressed"]
|
[connection signal="pressed" from="Button2" to="." method="_on_button_2_pressed"]
|
||||||
|
|
|
@ -6,7 +6,7 @@ var loadPath
|
||||||
# Variable to store the loaded JSON data
|
# Variable to store the loaded JSON data
|
||||||
var data
|
var data
|
||||||
|
|
||||||
# Called when the node is added to the scene
|
# Currently placeholder function, to be deprecated once custom content loading is enabled.
|
||||||
func _ready():
|
func _ready():
|
||||||
loadPath = "res://content/stats.json" # Set the path to load JSON data from
|
loadPath = "res://content/stats.json"
|
||||||
data = Load.loadJSON(loadPath) # Load JSON data from the specified path
|
data = Load.loadJSON(loadPath)
|
||||||
|
|
|
@ -1,42 +1,31 @@
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
# Reference to the inputOutput.tscn scene
|
|
||||||
var field = preload("res://scenes/menu/inputOutput.tscn")
|
|
||||||
|
|
||||||
# Variable to store loaded JSON data
|
# Variable to store loaded JSON data
|
||||||
var data
|
var data
|
||||||
|
var amount
|
||||||
|
|
||||||
# Called when the node is added to the scene
|
# Get all needed things for Ui.placeFields and save amount from it.
|
||||||
func _ready():
|
func _ready():
|
||||||
# Load JSON data from stats.json
|
data = Content.data
|
||||||
data = Load.loadJSON("res://content/stats.json")
|
var container = $"ScrollContainer/VBoxContainer"
|
||||||
|
Ui.placeFields(data, container, true)
|
||||||
# Instantiate input fields based on 'amount' from data
|
amount = data.get("amount")
|
||||||
for i in range(int(data.get("amount"))):
|
|
||||||
var fieldInstance = field.instance()
|
|
||||||
$"ScrollContainer/VBoxContainer".add_child(fieldInstance)
|
|
||||||
|
|
||||||
# Set position, placeholder text, and minimum size for each field
|
|
||||||
$"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
|
|
||||||
|
|
||||||
# Handler for when a button is pressed
|
# Upon pressing the "Send" button
|
||||||
func _on_button_pressed():
|
func _on_button_pressed():
|
||||||
# Construct save path based on user input
|
# Create the save path based on which ID was entered
|
||||||
var savePath = "user://player_data" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json"
|
var savePath = "user://player_" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json"
|
||||||
|
|
||||||
# Prepare data to save
|
# Get data from the fields to save, except the first one, which is the ID and should be used in the filename.
|
||||||
var saveData = {}
|
var saveData = {}
|
||||||
for i in range(int(data.get("amount"))):
|
for i in range(int(data.get("amount"))):
|
||||||
if i > 0:
|
if i > 0:
|
||||||
saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text
|
saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text
|
||||||
|
|
||||||
# Save data to JSON file
|
# Call the global save function
|
||||||
Save.saveJSON(savePath, saveData)
|
Save.saveJSON(savePath, saveData)
|
||||||
|
|
||||||
# Process function called every frame
|
# Only for the Main Menu "shortcut"
|
||||||
func _process(delta):
|
func _process(delta):
|
||||||
# Check if the Escape key is pressed to change scene to main.tscn
|
if Input.is_action_pressed("escape"):
|
||||||
if Input.is_action_pressed("escape"):
|
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
||||||
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
|
||||||
|
|
|
@ -1,53 +1,37 @@
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
# Reference to the inputOutput.tscn scene
|
# Variable to store loaded JSON data
|
||||||
var field = preload("res://scenes/menu/inputOutput.tscn")
|
|
||||||
|
|
||||||
# Variables to store data and amount
|
|
||||||
var data
|
var data
|
||||||
var amount
|
var amount
|
||||||
|
|
||||||
# Called when the node is added to the scene
|
# Get all needed things for Ui.placeFields and save amount from it.
|
||||||
func _ready():
|
func _ready():
|
||||||
# Get data from Content singleton
|
data = Content.data
|
||||||
data = Content.data
|
var container = $"ScrollContainer/VBoxContainer"
|
||||||
|
Ui.placeFields(data, container, false)
|
||||||
# Instantiate input fields based on 'amount' from data
|
amount = data.get("amount")
|
||||||
for i in range(int(data.get("amount"))):
|
|
||||||
var fieldInstance = field.instance()
|
|
||||||
$"ScrollContainer/VBoxContainer".add_child(fieldInstance)
|
|
||||||
|
|
||||||
# Set placeholder text and minimum size for each field
|
|
||||||
$"ScrollContainer/VBoxContainer".get_child(i).placeholder_text = data.get(str(i))
|
|
||||||
$"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40
|
|
||||||
|
|
||||||
# Make fields editable except the first one
|
|
||||||
if i > 0:
|
|
||||||
$"ScrollContainer/VBoxContainer".get_child(i).editable = false
|
|
||||||
|
|
||||||
amount = data.get("amount") # Store the amount of fields
|
|
||||||
|
|
||||||
# Handler for when a button is pressed
|
# Upon pressing the "Get" button, also called in game
|
||||||
func _on_button_pressed():
|
func _on_button_pressed():
|
||||||
# Construct save path based on user input
|
# Construct save path based on user input
|
||||||
var savePath = "user://player_data" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json"
|
var savePath = "user://player_" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json"
|
||||||
|
|
||||||
# Load JSON data from the constructed save path
|
# Load JSON data from the constructed save path
|
||||||
data = Load.loadJSON(savePath)
|
data = Load.loadJSON(savePath)
|
||||||
|
|
||||||
# Check if data is successfully loaded (type 27 corresponds to Dictionary in Godot)
|
# Check if data is successfully loaded (type 27 corresponds to Dictionary in Godot, although the wiki says it's 18...)
|
||||||
if typeof(data) == 27:
|
if typeof(data) == 27:
|
||||||
# Populate fields with loaded data
|
# Populate fields with loaded data
|
||||||
for i in range(int(amount) - 1):
|
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))
|
$"ScrollContainer/VBoxContainer".get_child(i + 1).text = data.get(str($"ScrollContainer/VBoxContainer".get_child(i + 1).placeholder_text))
|
||||||
|
|
||||||
# Process function called every frame
|
# Only for the Main Menu "shortcut"
|
||||||
func _process(_delta):
|
func _process(_delta):
|
||||||
# Check if the Escape key is pressed to change scene to main.tscn
|
# Check if the Escape key is pressed to change scene to main.tscn
|
||||||
if Input.is_action_pressed("escape"):
|
if Input.is_action_pressed("escape"):
|
||||||
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
||||||
|
|
||||||
# Function to hide the 'get' button
|
# Function to hide the 'get' button
|
||||||
func hideGet():
|
func hideGet():
|
||||||
$button.visible = false
|
$button.visible = false
|
||||||
$button.disabled = true
|
$button.disabled = true
|
||||||
|
|
|
@ -1,30 +1,26 @@
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
# Function to load JSON data from a file
|
# Function to load JSON data from a file
|
||||||
func loadJSON(savePath):
|
func loadJSON(savePath):
|
||||||
var data # Variable to store loaded data
|
# Check if the file exists
|
||||||
|
if not FileAccess.file_exists(savePath):
|
||||||
# Check if the file exists
|
return 1 # Return error code 1 if file does not exist
|
||||||
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)
|
||||||
# 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()
|
||||||
# Read the entire JSON string from the file
|
|
||||||
var jsonString = fileAccess.get_line()
|
fileAccess.close() # Close the file
|
||||||
|
|
||||||
fileAccess.close() # Close the file
|
# Create a new JSON instance
|
||||||
|
var json = JSON.new()
|
||||||
# Create a new JSON instance
|
|
||||||
var json = JSON.new()
|
# Parse the JSON string into a JSON object
|
||||||
|
var error = json.parse(jsonString)
|
||||||
# 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
|
||||||
if error:
|
|
||||||
return 1 # Return error code 1 if there was an error parsing JSON
|
return json.data # Return the loaded data
|
||||||
|
|
||||||
data = json.data # Extract data from the parsed JSON
|
|
||||||
|
|
||||||
return data # Return the loaded data
|
|
||||||
|
|
|
@ -7,50 +7,52 @@ var playerIndex = 0
|
||||||
# Variable to hold the current player's object name
|
# Variable to hold the current player's object name
|
||||||
var objectName
|
var objectName
|
||||||
|
|
||||||
# Called when the node is added to the scene
|
# Kick the game off
|
||||||
func _ready():
|
func _ready():
|
||||||
# Hide character viewer GUI initially
|
# Hide character viewer GUI initially
|
||||||
$"../characterViewer".hideGet()
|
$"../characterViewer".hideGet()
|
||||||
# Start cycling through players
|
# Start cycling through players
|
||||||
next()
|
next()
|
||||||
|
|
||||||
# Function to switch to the next player
|
# Function to switch to the next player
|
||||||
func next():
|
func next():
|
||||||
if playerIndex >= playerAmount:
|
if playerIndex >= playerAmount:
|
||||||
playerIndex = 0
|
playerIndex = 0
|
||||||
|
|
||||||
# Construct object name based on player index
|
# Construct object name based on player index
|
||||||
objectName = "player%d" % playerIndex
|
objectName = "player%d" % playerIndex
|
||||||
print(objectName) # Print current player's object name (for debugging)
|
print(objectName) # Print current player's object name (for debugging)
|
||||||
|
|
||||||
# Start the current player's activity
|
# Start the current player's activity
|
||||||
get_node(objectName).start()
|
get_node(objectName).start()
|
||||||
|
|
||||||
playerIndex += 1 # Move to the next player index
|
playerIndex += 1 # Move to the next player index
|
||||||
|
|
||||||
# Function to display statistics
|
# Function to display statistics, lots of relative links, may change eventually
|
||||||
func stats():
|
func stats():
|
||||||
visible = false # Hide current node (assuming this node should hide itself)
|
visible = false # Hide game area
|
||||||
$"../characterViewer".visible = true # Show character viewer GUI
|
$"../characterViewer".visible = true
|
||||||
get_node(objectName).stop() # Stop current player's activity
|
get_node(objectName).stop()
|
||||||
$"../back".disabled = false # Enable back button
|
# Activate back button
|
||||||
$"../back".visible = true # Make back button visible
|
$"../back".disabled = false
|
||||||
|
$"../back".visible = true
|
||||||
# Update player index display in character viewer
|
|
||||||
$"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).text = str(playerIndex - 1)
|
# Update player index display in character viewer
|
||||||
$"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).editable = false
|
$"../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()
|
# Simulate button press in character viewer (assuming this triggers further actions)
|
||||||
|
$"../characterViewer"._on_button_pressed()
|
||||||
|
|
||||||
# Function to restart the node's state
|
# Function to restart the node's state
|
||||||
func restart():
|
func restart():
|
||||||
visible = true # Show current node
|
visible = true # Show game area
|
||||||
$"../characterViewer".visible = false # Hide character viewer GUI
|
$"../characterViewer".visible = false
|
||||||
get_node(objectName).start() # Start current player's activity
|
get_node(objectName).start()
|
||||||
$"../back".disabled = true # Disable back button
|
# Activate back button
|
||||||
$"../back".visible = false # Hide back button
|
$"../back".disabled = true
|
||||||
|
$"../back".visible = false
|
||||||
|
|
||||||
# Handler for when the back button is pressed
|
# Handler for when the back button is pressed
|
||||||
func _on_back_pressed():
|
func _on_back_pressed():
|
||||||
restart() # Restart the node's state
|
restart() # Gives control back to the current player character
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
# Function called when button 1 is pressed
|
# Function called when "Play" is pressed
|
||||||
func _on_button_pressed():
|
func _on_button_pressed():
|
||||||
get_tree().change_scene_to_file("res://scenes/map/map.tscn")
|
get_tree().change_scene_to_file("res://scenes/map/map.tscn")
|
||||||
# Change the scene to the map scene ("map.tscn")
|
|
||||||
|
|
||||||
# Function called when button 2 is pressed
|
# Function called when "View" is pressed
|
||||||
func _on_button_2_pressed():
|
func _on_button_2_pressed():
|
||||||
get_tree().change_scene_to_file("res://scenes/menu/view.tscn")
|
get_tree().change_scene_to_file("res://scenes/menu/view.tscn")
|
||||||
# Change the scene to the view menu scene ("view.tscn")
|
|
||||||
|
|
||||||
# Function called when button 3 is pressed
|
# Function called when "Create" is pressed
|
||||||
func _on_button_3_pressed():
|
func _on_button_3_pressed():
|
||||||
get_tree().change_scene_to_file("res://scenes/menu/create.tscn")
|
get_tree().change_scene_to_file("res://scenes/menu/create.tscn")
|
||||||
# Change the scene to the create menu scene ("create.tscn")
|
|
||||||
|
|
|
@ -1,58 +1,55 @@
|
||||||
extends CharacterBody2D
|
extends CharacterBody2D
|
||||||
|
|
||||||
# Exported variable for movement speed
|
|
||||||
@export var speed = 200
|
@export var speed = 200
|
||||||
|
|
||||||
# Distance traveled accumulator
|
# Distance traveled accumulator
|
||||||
var distanceTo = 0
|
var distanceTo = 0
|
||||||
|
|
||||||
# Flag to indicate if the character is active
|
# Flag to indicate if the character is currently the active player.
|
||||||
var active = false
|
var active = false
|
||||||
|
|
||||||
# Function to handle user input
|
# Function to handle user input
|
||||||
func get_input():
|
func get_input():
|
||||||
# Change scene if Escape key is pressed
|
# Main Menu "shortcut"
|
||||||
if Input.is_action_pressed("escape"):
|
if Input.is_action_pressed("escape"):
|
||||||
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
||||||
|
|
||||||
# Get directional input and set velocity accordingly
|
# Get directional input and set velocity accordingly
|
||||||
var input_direction = Input.get_vector("left", "right", "up", "down")
|
var input_direction = Input.get_vector("left", "right", "up", "down")
|
||||||
velocity = input_direction * speed
|
velocity = input_direction * speed
|
||||||
|
|
||||||
# Physics process function called every frame
|
|
||||||
func _physics_process(delta):
|
func _physics_process(delta):
|
||||||
# Store current position for distance calculation
|
# Store current position for distance calculation
|
||||||
var toCalculate = position
|
var toCalculate = position
|
||||||
|
|
||||||
# Process input if the character is active
|
# Process input if the character is active
|
||||||
if active:
|
if active:
|
||||||
get_input()
|
get_input()
|
||||||
move_and_slide(velocity) # Move the character based on current velocity
|
move_and_slide()
|
||||||
|
|
||||||
# Accumulate distance traveled
|
# Accumulate distance traveled
|
||||||
distanceTo += position.distance_to(toCalculate)
|
distanceTo += position.distance_to(toCalculate)
|
||||||
print(distanceTo) # Print distance traveled (for debugging)
|
|
||||||
|
|
||||||
# Check if distance threshold is exceeded and the character is active
|
# Check if distance threshold is exceeded and the character is active
|
||||||
if distanceTo > 500 and active:
|
if distanceTo > 500 and active:
|
||||||
stop() # Stop character movement and reset distance traveled
|
stop()
|
||||||
distanceTo = 0
|
distanceTo = 0
|
||||||
get_parent().next() # Trigger next action in parent node
|
get_parent().next() # Go to the next player
|
||||||
|
|
||||||
# Function to start the character's activity
|
# Set main character as active
|
||||||
func start():
|
func start():
|
||||||
$camera.enabled = true # Enable camera
|
$camera.enabled = true
|
||||||
active = true # Set character to active state
|
active = true
|
||||||
$stats.disabled = false # Enable stats UI node
|
$stats.disabled = false
|
||||||
$stats.visible = true # Make stats UI node visible
|
$stats.visible = true
|
||||||
|
|
||||||
# Function to stop the character's activity
|
# Set main character as inactive
|
||||||
func stop():
|
func stop():
|
||||||
$camera.enabled = false # Disable camera
|
$camera.enabled = false
|
||||||
active = false # Set character to inactive state
|
active = false
|
||||||
$stats.disabled = true # Disable stats UI node
|
$stats.disabled = true
|
||||||
$stats.visible = false # Hide stats UI node
|
$stats.visible = false
|
||||||
|
|
||||||
# Handler for when the stats button is pressed
|
# Handler for when the stats button is pressed
|
||||||
func _on_stats_pressed():
|
func _on_stats_pressed():
|
||||||
get_parent().stats() # Call stats function in the parent node
|
get_parent().stats() # Open up the stats menu for the current player
|
||||||
|
|
21
scripts/ui.gd
Normal file
21
scripts/ui.gd
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
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
|
Loading…
Reference in a new issue