Godot project with a few initial classes

State
State machine
Log resource
Lesson logger
Game globals
This commit is contained in:
Laurent Tourte
2022-12-14 16:08:24 +01:00
parent 256fd061e1
commit 9ded82ef24
10 changed files with 268 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
extends Node
class_name GameGlobalData
var language: = "French"
var teacher_id: = ""
var user_id: = ""
var minigame_difficulty: = 0
var lesson_difficulty: = 0
+31
View File
@@ -0,0 +1,31 @@
extends Node
class_name Logger
const log_resource_class: = preload("res://sources/utils/minigame_resources/log.gd")
var log_resources: = {}
func save_logs(logs: Dictionary, teacher_id: String, user_id: String, minigame_name: String, lesson: String, time: String) -> void:
var file_path: = "user://" + teacher_id + "/" + user_id + "/" + minigame_name + ".tres"
if not ResourceLoader.exists(file_path):
var log_resource: LogResource = log_resource_class.instance()
ResourceSaver.save(log_resource, file_path, ResourceSaver.FLAG_COMPRESS)
if not file_path in log_resources:
var log_resource: = ResourceLoader.load(file_path)
log_resources[file_path] = log_resource
if not log_resources[file_path].has(lesson):
log_resources[file_path][lesson] = {}
if not log_resources[file_path][lesson].has(time):
log_resources[file_path][lesson][time] = logs
else:
var i: = 0
while log_resources[file_path][lesson].has(time + "_" + str(i)):
i += 1
log_resources[file_path][lesson][time + "_" + str(i)] = logs
ResourceSaver.save(log_resources[file_path], file_path, ResourceSaver.FLAG_COMPRESS)
+4
View File
@@ -0,0 +1,4 @@
extends Resource
class_name LogResource
@export var logs: = {}
+34
View File
@@ -0,0 +1,34 @@
extends Node
class_name State
@export var can_be_previous: = true
var state_machine: StateMachine
func _init() -> void:
add_to_group("state")
func enter_state(_msg: Dictionary = {}) -> void:
pass
func exit_state() -> void:
pass
func state_process(_delta: float) -> void:
pass
func state_physics_process(_delta: float) -> void:
pass
func state_unhandled_input(_event: InputEvent) -> void:
pass
func go_to_state(state_path: String, msg: Dictionary = {}) -> void:
state_machine.go_to_state(state_path, msg)
@@ -0,0 +1,87 @@
extends Node
class_name StateMachine
signal state_changed(state_path, msg)
@export_node_path(Node) var initial_state_path: NodePath
var state: State = null
var current_msg: = {}
var previous_state: State = null
var previous_msg: = {}
var _is_active: = false
var is_active: = false:
get: return _get_is_active()
set(value): _set_is_active(value)
func _ready() -> void:
self.is_active = false
var children: = get_children()
for node in children:
children.append_array(node.get_children())
if node is State:
node.state_machine = self
func start() -> void:
go_to_state(initial_state_path)
self.is_active = true
func _process(delta: float) -> void:
if state:
state.state_process(delta)
func _physics_process(delta: float) -> void:
if state:
state.state_physics_process(delta)
func _unhandled_input(event: InputEvent) -> void:
if state:
state.state_unhandled_input(event)
func go_to_state(state_path: String, msg:= {}):
var new_state: State = null
if state_path == "Previous":
if previous_state == null:
push_error("No previous state at this point")
return
new_state = previous_state
for key in previous_msg.keys():
if not msg.has(key):
msg[key] = previous_msg[key]
else:
if not has_node(state_path):
push_error("State not found :" + state_path)
return
new_state = get_node(state_path)
if state and state.can_be_previous:
previous_msg = current_msg
previous_state = state
if state:
state.exit_state()
self.state = new_state
state.enter_state(msg)
current_msg = msg
emit_signal("state_changed", state_path, msg)
func _set_is_active(value: bool) -> void:
_is_active = value
set_process(value)
set_physics_process(value)
set_process_unhandled_input(value)
func _get_is_active() -> bool:
return _is_active