Added job component and queue system

- Introduced a new JobComponent and JobQueue to manage jobs in the game.
- Created two new jobs: DigJob and InfectJob with their respective scripts.
- Updated CrystalGlowComponent, FreeCameraComponent, MushroomGlowComponent, WaterEffectComponent to improve logging messages.
- Adjusted camera movement limits in FreeCameraGameCameraComponent for better control.
- Added FiniteStateMachine class for managing states of entities.
- Implemented GlowingIdle state as an example of using the state machine.
- Included a utility function to fetch file paths by extension from a directory.
This commit is contained in:
Dan 2024-06-06 15:49:05 +01:00
parent fc896925d6
commit bb5724429a
23 changed files with 311 additions and 29 deletions

View file

@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=3 uid="uid://buadlj7sbutdh"]
[ext_resource type="Script" path="res://components/scripts/job_component.gd" id="1_lsesq"]
[ext_resource type="Script" path="res://components/scripts/job_queue.gd" id="2_tal7p"]
[node name="JobComponent" type="Node"]
script = ExtResource("1_lsesq")
[node name="JobQueue" type="Node" parent="."]
script = ExtResource("2_tal7p")

6
components/jobs/Dig.tscn Normal file
View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://bypebl5w1aivf"]
[ext_resource type="Script" path="res://components/jobs/scripts/dig.gd" id="1_my7sj"]
[node name="Dig" type="Node"]
script = ExtResource("1_my7sj")

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://pbnva2nkgral"]
[ext_resource type="Script" path="res://components/jobs/scripts/infect.gd" id="1_xpq3i"]
[node name="Infect" type="Node"]
script = ExtResource("1_xpq3i")

View file

@ -0,0 +1,10 @@
extends Job
class_name DigJob
func _init() -> void:
job_name = "Dig"
job_description = "Dig some stuff!"
super._init()
func _ready() -> void:
Log.pr("Dig job added to the scene...")

View file

@ -0,0 +1,10 @@
extends Job
class_name InfectJob
func _init() -> void:
job_name = "Infect"
job_description = "Infect some roots"
super._init()
func _ready() -> void:
Log.pr("Infect job added to the scene...")

View file

@ -0,0 +1,11 @@
extends Node
class_name Job
signal job_completed
signal job_failed
var job_name : String
var job_description : String
func _init() -> void:
pass

View file

@ -16,13 +16,13 @@ func _ready() -> void:
if !tile_map:
Log.err("CrystalGlowComponent: TileMap not set")
Log.pr("CrystalGlowComponent ready")
tile_size = tile_map.get_tileset().tile_size
tilemap_size = tile_map.get_used_rect().end - tile_map.get_used_rect().position
crystal_layer = tile_map.get_layer_id_by_name('Crystals')
Log.pr(crystal_layer)
Log.pr("CrystalGlowComponent ready, working on layer: ", crystal_layer)
add_crystal_glow()
## Look for crystals in the scene and add the glow to the approprirate angle and colour
@ -46,5 +46,3 @@ func add_crystal_glow() -> void:
glow.orientation = orientation
crystal_glow_container.add_child(glow)
glowing_crystals.append(coords)
Log.pr("Glowing crystals: ", glowing_crystals)

View file

@ -9,20 +9,12 @@ func _ready() -> void:
if !tile_map:
Log.err("FreeCameraComponent: tile_map is not set")
## Set the limits for the camera based on the tilemap
#camera.set_limits(0, tile_map.)
# Stop the camera scrolling out of bounds of the map
var tile_size : Vector2i = tile_map.get_tileset().tile_size
var tilemap_size : Vector2i = tile_map.get_used_rect().end - tile_map.get_used_rect().position
var map_size_x : int = tile_size.x * tilemap_size.x
var map_size_y : int = tile_size.y * tilemap_size.y
Log.pr("Size Y", map_size_y, get_viewport().size.y)
Log.pr("Size X", map_size_x, get_viewport().size.x)
#Log.pr(map_size - get_viewport().size.x)
camera.set_limit(SIDE_LEFT, 0)
camera.set_limit(SIDE_TOP, 0)
camera.set_limit(SIDE_RIGHT, map_size_x)

View file

@ -64,18 +64,18 @@ func _physics_process(delta : float) -> void:
# If this movement would take us out of clamp we don't want to do it:
var position_change : Vector2 = camera_movement * get_zoom()
if (position.x + position_change.x) <= limit_left or\
(position.x + position_change.x) >= limit_right:
if (position.x + position_change.x) < limit_left or\
(position.x + position_change.x) > limit_right:
camera_movement.x = 0
if (position.y + position_change.y) <= limit_top or\
(position.y + position_change.y) >= limit_bottom:
if (position.y + position_change.y) < limit_top or\
(position.y + position_change.y) > limit_bottom:
camera_movement.y = 0
if camera_movement.y != 0 or camera_movement.x != 0:
# Update position of the camera.
position += camera_movement * get_zoom()
# Set camera movement to zero, update old mouse position.
camera_movement = Vector2(0,0)
_prev_mouse_pos = get_local_mouse_position()

View file

@ -0,0 +1,57 @@
extends Node
class_name JobComponent
## Constants
const JOB_FOLDER = "res://components/jobs/"
## Variables
var job_types : Dictionary = {}
@onready var queue : JobQueue = $JobQueue
func _ready() -> void:
## Get the list of job types from the job folder
load_jobs()
add_jobs_to_queue()
add_jobs_to_queue()
add_jobs_to_queue()
Log.pr("I am the job component, and I am ready.")
# Look in the JOB_FOLDER and preload all scenes into a dictionary
func load_jobs() -> void:
var job_files : Array = FileUtil.get_file_paths_by_extension(JOB_FOLDER, "tscn")
#Log.pr("Job files: ", job_files)
for file : String in job_files:
#Log.pr("Loading job: ", file)
var job_scene : PackedScene = load(file)
var job_instance : Job = job_scene.instantiate()
#Log.pr("⚒️ Job Name:", job_instance.job_name, "-- Description:", job_instance.job_description)
var job_details : Dictionary = {
'job_name': job_instance.job_name,
'job_description': job_instance.job_description,
'job_instance': job_scene
}
job_types[job_instance.job_name] = job_details
#Log.pr(job_details)
Log.pr("Job types: ", job_types)
func add_jobs_to_queue() -> void:
#Log.pr("This is a debug function!")
for job : String in job_types.keys():
#Log.pr("Adding job to queue: ", job)
if job_types[job].job_instance != null:
var job_scene : PackedScene = job_types[job].job_instance
var job_instance : Node = job_scene.instantiate()
queue.add_child(job_instance)

View file

@ -0,0 +1,5 @@
extends Node
class_name JobQueue
func _ready() -> void:
Log.pr("Job Queue ready...")

View file

@ -15,7 +15,7 @@ func _ready() -> void:
if !tile_map:
Log.err("MushroomGlowComponent: TileMap not set")
Log.pr("MushroomGlowComponent ready")
tile_map.tilemap_updated.connect(add_mushroom_glow)
@ -23,7 +23,9 @@ func _ready() -> void:
tilemap_size = tile_map.get_used_rect().end - tile_map.get_used_rect().position
mushroom_layer = tile_map.get_layer_id_by_name('Mushrooms')
Log.pr(mushroom_layer)
Log.pr("MushroomGlowComponent ready, working on layer:", mushroom_layer)
add_mushroom_glow()
## Look for crystals in the scene and add the glow to the approprirate angle and colour

View file

@ -18,13 +18,15 @@ func _ready() -> void:
if !tile_map:
Log.err("WaterEffectComponent: TileMap not set")
Log.pr("WaterEffectComponent ready")
tile_size = tile_map.get_tileset().tile_size
tilemap_size = tile_map.get_used_rect().end - tile_map.get_used_rect().position
water_layer = tile_map.get_layer_id_by_name('Water')
Log.pr(water_layer)
Log.pr("WaterEffectComponent ready, working on layer:", water_layer)
add_water_effects()
## Look for crystals in the scene and add the glow to the approprirate angle and colour