Merge pull request 'Commented your code' (#6) from sageTheDm/FreeTTRPG:main into main

Reviewed-on: https://codeberg.org/Patrick_Pluto/FreeTTRPG/pulls/6
This commit is contained in:
Patrick 2024-06-18 20:29:30 +00:00
commit 1f13b9d7b2
9 changed files with 220 additions and 114 deletions

View file

@ -1,8 +1,12 @@
extends Node extends Node
# Variable to store the path from which JSON data is loaded
var loadPath var loadPath
# Variable to store the loaded JSON data
var data var data
# Called when the node is added to the scene
func _ready(): func _ready():
loadPath = "res://content/stats.json" loadPath = "res://content/stats.json" # Set the path to load JSON data from
data = Load.loadJSON(loadPath) data = Load.loadJSON(loadPath) # Load JSON data from the specified path

View file

@ -1,25 +1,42 @@
extends Node extends Node
# Reference to the inputOutput.tscn scene
var field = preload("res://scenes/menu/inputOutput.tscn") var field = preload("res://scenes/menu/inputOutput.tscn")
# Variable to store loaded JSON data
var data var data
# Called when the node is added to the scene
func _ready(): func _ready():
# Load JSON data from stats.json
data = Load.loadJSON("res://content/stats.json") data = Load.loadJSON("res://content/stats.json")
# Instantiate input fields based on 'amount' from data
for i in range(int(data.get("amount"))): for i in range(int(data.get("amount"))):
var fieldInstance = field.instantiate() var fieldInstance = field.instance()
$"ScrollContainer/VBoxContainer".add_child(fieldInstance) $"ScrollContainer/VBoxContainer".add_child(fieldInstance)
$"ScrollContainer/VBoxContainer".get_child(i).position = Vector2(16,(16+i*88))
# 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).placeholder_text = data.get(str(i))
$"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40
# Handler for when a button is pressed
func _on_button_pressed(): func _on_button_pressed():
var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") # Construct save path based on user input
var savePath = "user://player_data" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json"
# Prepare data to save
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
Save.saveJSON(savePath, saveData) Save.saveJSON(savePath, saveData)
# Process function called every frame
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")

View file

@ -1,31 +1,53 @@
extends Node extends Node
# Reference to the inputOutput.tscn scene
var field = preload("res://scenes/menu/inputOutput.tscn") 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
func _ready(): func _ready():
# Get data from Content singleton
data = Content.data data = Content.data
# Instantiate input fields based on 'amount' from data
for i in range(int(data.get("amount"))): for i in range(int(data.get("amount"))):
var fieldInstance = field.instantiate() var fieldInstance = field.instance()
$"ScrollContainer/VBoxContainer".add_child(fieldInstance) $"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).placeholder_text = data.get(str(i))
$"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40 $"ScrollContainer/VBoxContainer".get_child(i).custom_minimum_size.y = 40
# Make fields editable except the first one
if i > 0: if i > 0:
$"ScrollContainer/VBoxContainer".get_child(i).editable = false $"ScrollContainer/VBoxContainer".get_child(i).editable = false
amount = data.get("amount")
amount = data.get("amount") # Store the amount of fields
# Handler for when a button is pressed
func _on_button_pressed(): func _on_button_pressed():
var savePath = str("user://player_data"+$"ScrollContainer/VBoxContainer".get_child(0).text+".json") # Construct save path based on user input
data = Load.loadJSON(savePath) var savePath = "user://player_data" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json"
if typeof(data) == 27:
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))
# 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)
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))
# Process function called every frame
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")
# Function to hide the 'get' button
func hideGet(): func hideGet():
$button.visible = false $button.visible = false
$button.disabled = true $button.disabled = true

View file

@ -1,17 +1,30 @@
extends Node extends Node
# Function to load JSON data from a file
func loadJSON(savePath): func loadJSON(savePath):
var data var data # Variable to store loaded data
# Check if the file exists
if not FileAccess.file_exists(savePath): if not FileAccess.file_exists(savePath):
return 1 return 1 # Return error code 1 if file does not exist
# Open the file for reading
var fileAccess = FileAccess.open(savePath, FileAccess.READ) var fileAccess = FileAccess.open(savePath, FileAccess.READ)
# Read the entire JSON string from the file
var jsonString = fileAccess.get_line() var jsonString = fileAccess.get_line()
fileAccess.close()
fileAccess.close() # Close the file
# Create a new JSON instance
var json = JSON.new() var json = JSON.new()
var error = json.parse(jsonString)
if error:
return 1
data = json.data # Parse the JSON string into a JSON object
return data var error = json.parse(jsonString)
if error:
return 1 # Return error code 1 if there was an error parsing JSON
data = json.data # Extract data from the parsed JSON
return data # Return the loaded data

View file

@ -1,37 +1,56 @@
extends Node2D extends Node2D
var playerAmount=2 # Number of players and current player index
var playerIndex=0 var playerAmount = 2
var playerIndex = 0
# Variable to hold the current player's object name
var objectName var objectName
# Called when the node is added to the scene
func _ready(): func _ready():
# Hide character viewer GUI initially
$"../characterViewer".hideGet() $"../characterViewer".hideGet()
# Start cycling through players
next() next()
# Function to switch to the next player
func next(): func next():
if playerIndex >= playerAmount: if playerIndex >= playerAmount:
playerIndex = 0 playerIndex = 0
objectName= "player%d" % playerIndex
print(objectName)
get_node(objectName).start()
playerIndex += 1
# Construct object name based on player index
objectName = "player%d" % playerIndex
print(objectName) # Print current player's object name (for debugging)
# Start the current player's activity
get_node(objectName).start()
playerIndex += 1 # Move to the next player index
# Function to display statistics
func stats(): func stats():
visible = false visible = false # Hide current node (assuming this node should hide itself)
$"../characterViewer".visible = true $"../characterViewer".visible = true # Show character viewer GUI
get_node(objectName).stop() get_node(objectName).stop() # Stop current player's activity
$"../back".disabled = false $"../back".disabled = false # Enable back button
$"../back".visible = true $"../back".visible = true # Make back button visible
$"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).text = str(playerIndex-1)
# Update player index display in character viewer
$"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).text = str(playerIndex - 1)
$"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).editable = false $"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).editable = false
# Simulate button press in character viewer (assuming this triggers further actions)
$"../characterViewer"._on_button_pressed() $"../characterViewer"._on_button_pressed()
# Function to restart the node's state
func restart(): func restart():
visible = true visible = true # Show current node
$"../characterViewer".visible = false $"../characterViewer".visible = false # Hide character viewer GUI
get_node(objectName).start() get_node(objectName).start() # Start current player's activity
$"../back".disabled = true $"../back".disabled = true # Disable back button
$"../back".visible = false $"../back".visible = false # Hide back button
# Handler for when the back button is pressed
func _on_back_pressed(): func _on_back_pressed():
restart() restart() # Restart the node's state

View file

@ -1,14 +1,16 @@
extends Node extends Node
# Function called when button 1 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
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
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")

View file

@ -1,39 +1,58 @@
extends CharacterBody2D extends CharacterBody2D
# Exported variable for movement speed
@export var speed = 200 @export var speed = 200
# Distance traveled accumulator
var distanceTo = 0 var distanceTo = 0
# Flag to indicate if the character is active
var active = false var active = false
# Function to handle user input
func get_input(): func get_input():
# Change scene if Escape key is pressed
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
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
var toCalculate = position var toCalculate = position
# Process input if the character is active
if active: if active:
get_input() get_input()
move_and_slide() move_and_slide(velocity) # Move the character based on current velocity
# Accumulate distance traveled
distanceTo += position.distance_to(toCalculate) distanceTo += position.distance_to(toCalculate)
print(distanceTo) print(distanceTo) # Print distance traveled (for debugging)
# Check if distance threshold is exceeded and the character is active
if distanceTo > 500 and active: if distanceTo > 500 and active:
stop() stop() # Stop character movement and reset distance traveled
distanceTo = 0 distanceTo = 0
get_parent().next() get_parent().next() # Trigger next action in parent node
# Function to start the character's activity
func start(): func start():
$camera.enabled = true $camera.enabled = true # Enable camera
active = true active = true # Set character to active state
$stats.disabled = false $stats.disabled = false # Enable stats UI node
$stats.visible = true $stats.visible = true # Make stats UI node visible
# Function to stop the character's activity
func stop(): func stop():
$camera.enabled = false $camera.enabled = false # Disable camera
active = false active = false # Set character to inactive state
$stats.disabled = true $stats.disabled = true # Disable stats UI node
$stats.visible = false $stats.visible = false # Hide stats UI node
# Handler for when the stats button is pressed
func _on_stats_pressed(): func _on_stats_pressed():
get_parent().stats() get_parent().stats() # Call stats function in the parent node

View file

@ -1,12 +1,21 @@
extends Node extends Node
# Function to save data in JSON format to a specified path
func saveJSON(savePath, saveData): func saveJSON(savePath, saveData):
# Convert the saveData dictionary to a JSON string
var jsonString = JSON.stringify(saveData) var jsonString = JSON.stringify(saveData)
# Attempt to open a file for writing at the specified savePath
var fileAccess = FileAccess.open(savePath, FileAccess.WRITE) var fileAccess = FileAccess.open(savePath, FileAccess.WRITE)
# Check if the file was successfully opened
if not fileAccess: 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()) print("An error happened while saving data: ", FileAccess.get_open_error())
return 1 return 1
# Write the JSON string to the file
fileAccess.store_line(jsonString) fileAccess.store_line(jsonString)
# Close the file to ensure all data is properly saved and resources are freed
fileAccess.close() fileAccess.close()

View file

@ -1,6 +1,7 @@
[gd_scene load_steps=4 format=3 uid="uid://dv67vdgb4h44q"] [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="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"] [ext_resource type="Script" path="res://scripts/player.gd" id="1_oik63"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kf6qt"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_kf6qt"]