General code improvements, progress step tracker implementation for worldgen, and work towards enemy behavior

This commit is contained in:
2024-01-23 21:45:52 +01:00
parent 3f465a708c
commit 49562ba9ce
11 changed files with 517 additions and 214 deletions

View File

@ -8,43 +8,51 @@ var _subset_count: int = 0
var _tracker: ProgressTracker = null
func _init(tracker: ProgressTracker, step_name: String):
self._tracker = tracker
self._step_name = step_name
# set_substeps: set the number of substeps for this step
## Sets the number of substeps for this step
func set_substeps(substeps: int):
self._subset_count = substeps
# substep: should be called when a substep is started
## Progresses to the next substep
func substep(message: String = ""):
self._subset_index += 1
self._message = message
self._tracker.progress_update.emit()
# complete: should be called when this step is done
## Completes this step
func complete():
self._tracker.step_complete.emit()
# get_step_name: get the name of this step
func get_step_name() -> String:
return self._step_name
# get_message: get the message for this step
## Returns the name of this step
func get_step_name() -> String:
return self._step_name
## Returns the message for this step
func get_message() -> String:
return self._message
# get_substep_index: get the index of the current substep
## Returns the index of the current substep
func get_substep_index() -> int:
return self._subset_index
# get_substep_count: get the number of substeps for this step
## Returns the number of substeps for this step
func get_substep_count() -> int:
return self._subset_count
# get_progress: get the progress of this step
## Returns the progress of this step
func get_progress() -> float:
if self._subset_count == 0:
return 0.0
else:
return float(self._subset_index) / float(self._subset_count)
return float(self._subset_index) / float(self._subset_count)

View File

@ -1,25 +1,25 @@
class_name ProgressTracker
extends Object
signal progress_update()
## Signal emitted when there is a progress update
signal progress_update
## Signal emitted when the current step changes
signal step_change(step: int)
signal complete()
signal step_complete()
## Signal emitted when the all steps are completed
signal complete
## Signal emitted when a step is completed
signal step_complete
var _current_step: ProgressStepTracker = null
var _current_step_index: int = -1
var _steps_count: int
func _init(step_count: int = 0) -> void:
_steps_count = step_count
#self.substep_complete.connect(self, "_on_substep_complete")
func _on_step_complete() -> void:
if _current_step_index == _steps_count:
self.complete.emit()
return
# next_step: should be called before the step is actually started
## Progresses to the next step
func next_step(step_name: String = "") -> ProgressStepTracker:
_current_step_index += 1
if _current_step_index > _steps_count:
@ -30,21 +30,36 @@ func next_step(step_name: String = "") -> ProgressStepTracker:
self.progress_update.emit()
return self._current_step
# step_complete: returns the progress of all steps
## Returns the progress of the current step
func get_step_progress() -> float:
return float(_current_step_index) / float(_steps_count)
# get_total_progress_percentage: returns the progress of all steps and substeps
func get_total_progress_percentage() -> float:
return self.get_step_progress()
# get_progress_data: returns a dictionary with data about the current progress for the UI
## Returns the progress of the current step
func get_total_progress_percentage() -> float:
var progress: float = self.get_step_progress()
if self._current_step:
return progress + (self._current_step.get_progress() / self._steps_count)
return progress
## Returns a dictionary with the following keys:
## - step_name: the name of the current step
## - message: the message of the current step
## - substeps: the number of substeps in the current step
## - current_substep: the index of the current substep
## - total_progress: the progress of all steps and substeps
func get_progress_data() -> Dictionary:
if not _current_step:
return {
"step_name": "", "message": "", "substeps": 0, "current_substep": 0, "total_progress": 0
}
return {
"step_name": self._current_step.get_step_name(),
"message": self._current_step.get_message(),
"substeps": self._current_step.get_substeps(),
"substeps": self._current_step.get_substep_count(),
"current_substep": self._current_step.get_substep_index(),
"total_progress":self.get_total_progress_percentage()
"total_progress": self.get_total_progress_percentage()
}