forked from interstellar_development/freettrpg
Commented your code
This commit is contained in:
parent
8bb83e987b
commit
b803229d32
9 changed files with 232 additions and 117 deletions
|
@ -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
|
||||||
|
|
|
@ -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():
|
||||||
data = Load.loadJSON("res://content/stats.json")
|
# Load JSON data from stats.json
|
||||||
for i in range(int(data.get("amount"))):
|
data = Load.loadJSON("res://content/stats.json")
|
||||||
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
|
|
||||||
|
|
||||||
|
# Instantiate input fields based on 'amount' from data
|
||||||
|
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
|
||||||
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 saveData = {}
|
var savePath = "user://player_data" + $"ScrollContainer/VBoxContainer".get_child(0).text + ".json"
|
||||||
for i in range(int(data.get("amount"))):
|
|
||||||
if i > 0:
|
|
||||||
saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text
|
|
||||||
Save.saveJSON(savePath, saveData)
|
|
||||||
|
|
||||||
|
# Prepare data to save
|
||||||
|
var saveData = {}
|
||||||
|
for i in range(int(data.get("amount"))):
|
||||||
|
if i > 0:
|
||||||
|
saveData[data.get(str(i))] = $"ScrollContainer/VBoxContainer".get_child(i).text
|
||||||
|
|
||||||
|
# Save data to JSON file
|
||||||
|
Save.saveJSON(savePath, saveData)
|
||||||
|
|
||||||
|
# Process function called every frame
|
||||||
func _process(delta):
|
func _process(delta):
|
||||||
if Input.is_action_pressed("escape"):
|
# Check if the Escape key is pressed to change scene to main.tscn
|
||||||
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
if Input.is_action_pressed("escape"):
|
||||||
|
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
||||||
|
|
|
@ -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():
|
||||||
data = Content.data
|
# Get data from Content singleton
|
||||||
for i in range(int(data.get("amount"))):
|
data = Content.data
|
||||||
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")
|
|
||||||
|
|
||||||
|
# Instantiate input fields based on 'amount' from data
|
||||||
|
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
|
||||||
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):
|
||||||
if Input.is_action_pressed("escape"):
|
# Check if the Escape key is pressed to change scene to main.tscn
|
||||||
get_tree().change_scene_to_file("res://scenes/menu/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():
|
func hideGet():
|
||||||
$button.visible = false
|
$button.visible = false
|
||||||
$button.disabled = true
|
$button.disabled = true
|
||||||
|
|
|
@ -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
|
||||||
if not FileAccess.file_exists(savePath):
|
|
||||||
return 1
|
|
||||||
var fileAccess = FileAccess.open(savePath, FileAccess.READ)
|
|
||||||
var jsonString = fileAccess.get_line()
|
|
||||||
fileAccess.close()
|
|
||||||
|
|
||||||
var json = JSON.new()
|
# Check if the file exists
|
||||||
var error = json.parse(jsonString)
|
if not FileAccess.file_exists(savePath):
|
||||||
if error:
|
return 1 # Return error code 1 if file does not exist
|
||||||
return 1
|
|
||||||
|
|
||||||
data = json.data
|
# Open the file for reading
|
||||||
return data
|
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
|
||||||
|
|
||||||
|
data = json.data # Extract data from the parsed JSON
|
||||||
|
|
||||||
|
return data # Return the loaded data
|
||||||
|
|
|
@ -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():
|
||||||
$"../characterViewer".hideGet()
|
# Hide character viewer GUI initially
|
||||||
next()
|
$"../characterViewer".hideGet()
|
||||||
|
# Start cycling through players
|
||||||
|
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)
|
|
||||||
$"../characterViewer/ScrollContainer/VBoxContainer".get_child(0).editable = false
|
|
||||||
$"../characterViewer"._on_button_pressed()
|
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# Simulate button press in character viewer (assuming this triggers further actions)
|
||||||
|
$"../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
|
||||||
|
|
|
@ -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")
|
||||||
|
|
|
@ -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():
|
||||||
if Input.is_action_pressed("escape"):
|
# Change scene if Escape key is pressed
|
||||||
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
if Input.is_action_pressed("escape"):
|
||||||
var input_direction = Input.get_vector("left", "right", "up", "down")
|
get_tree().change_scene_to_file("res://scenes/menu/main.tscn")
|
||||||
velocity = input_direction * speed
|
|
||||||
|
|
||||||
|
# Get directional input and set velocity accordingly
|
||||||
|
var input_direction = Input.get_vector("left", "right", "up", "down")
|
||||||
|
velocity = input_direction * speed
|
||||||
|
|
||||||
|
# Physics process function called every frame
|
||||||
func _physics_process(delta):
|
func _physics_process(delta):
|
||||||
var toCalculate = position
|
# Store current position for distance calculation
|
||||||
if active:
|
var toCalculate = position
|
||||||
get_input()
|
|
||||||
move_and_slide()
|
|
||||||
distanceTo += position.distance_to(toCalculate)
|
|
||||||
print(distanceTo)
|
|
||||||
if distanceTo > 500 and active:
|
|
||||||
stop()
|
|
||||||
distanceTo = 0
|
|
||||||
get_parent().next()
|
|
||||||
|
|
||||||
|
# Process input if the character is active
|
||||||
|
if active:
|
||||||
|
get_input()
|
||||||
|
move_and_slide(velocity) # Move the character based on current velocity
|
||||||
|
|
||||||
|
# Accumulate distance traveled
|
||||||
|
distanceTo += position.distance_to(toCalculate)
|
||||||
|
print(distanceTo) # Print distance traveled (for debugging)
|
||||||
|
|
||||||
|
# Check if distance threshold is exceeded and the character is active
|
||||||
|
if distanceTo > 500 and active:
|
||||||
|
stop() # Stop character movement and reset distance traveled
|
||||||
|
distanceTo = 0
|
||||||
|
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
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -1,31 +1,41 @@
|
||||||
[gd_scene load_steps=4 format=3 uid="uid://dv67vdgb4h44q"]
|
[gd_scene load_steps=4 format=3 uid="uid://dv67vdgb4h44q"]
|
||||||
|
|
||||||
|
# External resource for the player texture
|
||||||
[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"]
|
||||||
|
|
||||||
|
# External resource for the player script
|
||||||
[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 for the collision shape of the player
|
||||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kf6qt"]
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kf6qt"]
|
||||||
size = Vector2(20, 18)
|
size = Vector2(20, 18) # Setting the size of the RectangleShape2D
|
||||||
|
|
||||||
|
# Root node of the scene, a CharacterBody2D named "player"
|
||||||
[node name="player" type="CharacterBody2D"]
|
[node name="player" type="CharacterBody2D"]
|
||||||
script = ExtResource("1_oik63")
|
script = ExtResource("1_oik63") # Assigning the external script to the player node
|
||||||
|
|
||||||
|
# Child node of the player, a Sprite2D named "sprite"
|
||||||
[node name="sprite" type="Sprite2D" parent="."]
|
[node name="sprite" type="Sprite2D" parent="."]
|
||||||
texture = ExtResource("1_cujcj")
|
texture = ExtResource("1_cujcj") # Assigning the external texture to the sprite node
|
||||||
|
|
||||||
|
# Child node of the player, a CollisionShape2D named "CollisionShape2D"
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
shape = SubResource("RectangleShape2D_kf6qt")
|
shape = SubResource("RectangleShape2D_kf6qt") # Assigning the rectangle shape as the collision shape
|
||||||
|
|
||||||
|
# Child node of the player, a Camera2D named "camera"
|
||||||
[node name="camera" type="Camera2D" parent="."]
|
[node name="camera" type="Camera2D" parent="."]
|
||||||
enabled = false
|
enabled = false # Camera is initially disabled
|
||||||
|
|
||||||
|
# Child node of the player, a Button named "stats"
|
||||||
[node name="stats" type="Button" parent="."]
|
[node name="stats" type="Button" parent="."]
|
||||||
visible = false
|
visible = false # Button is initially invisible
|
||||||
offset_left = 112.0
|
offset_left = 112.0 # Left offset of the button
|
||||||
offset_top = 152.0
|
offset_top = 152.0 # Top offset of the button
|
||||||
offset_right = 238.0
|
offset_right = 238.0 # Right offset of the button
|
||||||
offset_bottom = 187.0
|
offset_bottom = 187.0 # Bottom offset of the button
|
||||||
scale = Vector2(2, 2)
|
scale = Vector2(2, 2) # Scaling the button by a factor of 2
|
||||||
disabled = true
|
disabled = true # Button is initially disabled
|
||||||
text = "Stats"
|
text = "Stats" # Text displayed on the button
|
||||||
|
|
||||||
|
# Connecting the "pressed" signal of the "stats" button to the "_on_stats_pressed" method in the current node
|
||||||
[connection signal="pressed" from="stats" to="." method="_on_stats_pressed"]
|
[connection signal="pressed" from="stats" to="." method="_on_stats_pressed"]
|
||||||
|
|
Loading…
Reference in a new issue