Refactor: Added explicit typing and void return types

- Introduced explicit typing to variables and functions across multiple scripts for better code clarity.
- Specified 'void' as the return type for functions that do not return a value.
- Removed redundant code in some scripts.
This commit is contained in:
Dan 2024-05-15 10:42:16 +01:00
parent a62cd6018e
commit 2a9e78b52e
37 changed files with 216 additions and 248 deletions

View file

@ -1,10 +1,10 @@
extends State
class_name BeeDeath
@onready var bee = get_parent().get_parent() as Bee
@onready var bee : Bee = get_parent().get_parent() as Bee
func enter(_msg := {}):
func enter(_msg : Dictionary = {}) -> void:
GameState.bee_died()
bee.bee_position_animation.play("Death")
bee.bee_wing_animation.stop()

View file

@ -2,13 +2,13 @@ extends State
class_name BeeGathering
@export var animator : AnimationPlayer
@onready var bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works
@onready var bee : Bee= get_parent().get_parent() as Bee # I think this is bad but I dont care it works
var time_at_patch : float = 0.0
var target : Vector2 = Vector2.ZERO
func enter(_msg := {}):
func enter(_msg : Dictionary = {}) -> void:
bee.just_gathering = true
Log.pr("Gathering now...")
@ -16,7 +16,7 @@ func enter(_msg := {}):
randomize()
target = bee.get_global_position() + Vector2(randi_range(-100, 100), randi_range(-100, 100))
func update(_delta : float):
func update(_delta : float) -> void:
if bee.in_range_of_flowers:
#animator.play("Gathering")

View file

@ -1,7 +1,7 @@
extends State
class_name BeeIdle
@onready var bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works
@onready var bee : Bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works
var idle_time : float = 0.0
@ -10,14 +10,14 @@ var target : Vector2 = Vector2.ZERO
var acquire_new_target : bool = false
func enter(_msg := {}):
func enter(_msg : Dictionary = {}) -> void:
if acquire_new_target:
target = bee.get_global_position() + Vector2(randi_range(-60, 60), randi_range(-60, 60))
func exit():
func exit() -> void:
acquire_new_target = true
func update(delta : float):
func update(delta : float) -> void:
if target == Vector2.ZERO:
randomize()
@ -39,7 +39,7 @@ func physics_update(delta : float) -> void:
bee.move_and_collide(bee.velocity)
bee.bee_body.look_at(target)
func find_something_to_do():
func find_something_to_do() -> void:
if bee.nectar > 0:
Log.pr("I have pollen, time to move..")
## Bee has pollen - head home

View file

@ -1,10 +1,10 @@
extends State
class_name BeeReturning
@onready var bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works
@onready var target = get_tree().get_first_node_in_group("beehive")
@onready var bee : Bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works
@onready var target : Beehive = get_tree().get_first_node_in_group("beehive")
func enter(_msg := {}) -> void:
func enter(_msg : Dictionary = {}) -> void:
Log.pr("Returning to the hive")
bee.bee_position_animation.play("Flying")

View file

@ -5,10 +5,10 @@ class_name BeeSleeping
var time_at_patch : float = 0.0
func enter(_msg := {}) -> void:
func enter(_msg : Dictionary = {}) -> void:
pass
func update(_delta) -> void:
func update(_delta : float) -> void:
pass
func physics_update(_delta : float) -> void:

View file

@ -3,12 +3,12 @@ class_name BeeTravelling
@export var target : Drone = null
@onready var bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works
@onready var bee : Bee = get_parent().get_parent() as Bee # I think this is bad but I dont care it works
var return_to_hive : bool = false
var moving_to : Vector2 = Vector2(0,0)
func enter(_msg := {}):
func enter(_msg : Dictionary = {}) -> void:
return_to_hive = false
## Get the next target location from the bee
if bee.just_gathering:

View file

@ -1,11 +1,11 @@
extends Node2D
class_name Bee
@onready var fsm = $StateMachine as FiniteStateMachine
@onready var drone_manager = get_tree().get_first_node_in_group("dronemanager") as DroneManager
@onready var bee_position_animation = $BeePositionAnimation as AnimationPlayer
@onready var bee_wing_animation = $WingAnimation as AnimationPlayer
@onready var bee_body = $BeeBody as Sprite2D
@onready var fsm : FiniteStateMachine = $StateMachine as FiniteStateMachine
@onready var drone_manager : DroneManager = get_tree().get_first_node_in_group("dronemanager") as DroneManager
@onready var bee_position_animation : AnimationPlayer = $BeePositionAnimation as AnimationPlayer
@onready var bee_wing_animation : AnimationPlayer = $WingAnimation as AnimationPlayer
@onready var bee_body : Sprite2D = $BeeBody as Sprite2D
@onready var impact_cloud : CPUParticles2D = $ImpactCloud
@export var nectar : int = 0
@ -17,13 +17,13 @@ var latest_target_director : int = 0
var in_range_of_flowers : bool = false
var just_gathering : bool = false # Used to check if the bee has just been gathering to return to their previous director
func _ready():
func _ready() -> void:
modulate = Color(1,1,1,1)
impact_cloud.visible = false
speed = randi_range(35,55) # Randomise the bee speed a bit
bee_wing_animation.play("Fly")
func get_current_director():
func get_current_director() -> DirectorDrone:
return drone_manager.get_director(latest_target_director)
## Get the next target to move to
@ -31,23 +31,23 @@ func get_current_director():
## If we have nectar, we need to go down the director list
## If we are at the lower director, we need to go the hive
## If we are at the highest director, we need to go to a flower
func get_next_target():
func get_next_target() -> Drone:
if nectar == 0:
Log.pr("No nectar!")
## If there is a next directory drone, lets go to it
var next_drone = drone_manager.get_next_director(latest_target_director)
var next_drone : DirectorDrone = drone_manager.get_next_director(latest_target_director)
if next_drone:
latest_target_director = next_drone.visit_order
return next_drone
## If there is no next drone, check for a collector drone
var collector_drone = drone_manager.get_collector()
var collector_drone : CollectorDrone = drone_manager.get_collector()
if collector_drone:
return collector_drone
else:
## Let's go home, we need the previous director drones location
var previous_drone = drone_manager.get_previous_director(latest_target_director)
var previous_drone : DirectorDrone = drone_manager.get_previous_director(latest_target_director)
if previous_drone:
Log.pr("Previous drone", previous_drone)
latest_target_director = previous_drone.visit_order
@ -56,6 +56,8 @@ func get_next_target():
Log.pr("No previous drone")
return null
return null
func deposit_nectar() -> void:
if nectar > 0:
GameState.add_nectar(nectar)

View file

@ -1,17 +1,13 @@
extends Area2D
@onready var bee = get_parent() as Bee
@onready var bee : Bee = get_parent() as Bee
func _ready():
pass
func _on_area_entered(area:Area2D):
func _on_area_entered(area : Area2D) -> void:
## Check if the area entered is a flower patch
if area.is_in_group("flowers"):
bee.in_range_of_flowers = true
func _on_area_exited(area:Area2D):
func _on_area_exited(area : Area2D) -> void:
## Check if the area exited is a flower patch
if area.is_in_group("flowers"):
bee.in_range_of_flowers = false

View file

@ -3,18 +3,18 @@ class_name Beehive
var dancer_in_range : bool = false
@onready var outline = $AreaHighlight
@onready var outline : Sprite2D = $AreaHighlight
func show_outline():
func show_outline() -> void:
outline.visible = true
func hide_outline():
func hide_outline() -> void:
outline.visible = false
func _on_area_2d_area_entered(area:Area2D):
func _on_area_2d_area_entered(area : Area2D) -> void:
if area.is_in_group("dancer"):
dancer_in_range = true
func _on_area_2d_area_exited(area:Area2D):
func _on_area_2d_area_exited(area : Area2D) -> void:
if area.is_in_group("dancer"):
dancer_in_range = false

View file

@ -1,11 +1 @@
class_name CollectorDrone extends Drone
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
class_name CollectorDrone extends Drone

View file

@ -1,11 +1 @@
class_name DancerDrone extends Drone
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
class_name DancerDrone extends Drone

View file

@ -1,7 +1,7 @@
class_name DirectorDrone extends Drone
@onready var edit_cursor = preload("res://resources/cursors/message_dots_round.png")
@onready var label = get_node("Label")
@onready var edit_cursor : Resource = preload("res://resources/cursors/message_dots_round.png")
@onready var label : Label = get_node("Label")
@export var visit_order : int = 0 :
get:
@ -10,22 +10,16 @@ class_name DirectorDrone extends Drone
visit_order = value
update_label_value()
func _ready():
pass
func _process(_delta):
pass
func update_label_value():
func update_label_value() -> void:
$Label.text = str(visit_order)
func _on_click_detection_mouse_entered():
func _on_click_detection_mouse_entered() -> void:
if GameState.placing_drone == false:
Log.pr("Mouse entered the director drone!")
label.visible = true
Input.set_custom_mouse_cursor(edit_cursor, Input.CURSOR_ARROW, Vector2(32, 32))
func _on_click_detection_mouse_exited():
func _on_click_detection_mouse_exited() -> void:
if GameState.placing_drone == false:
Log.pr("Mouse exited the director drone!")
label.visible = false
@ -33,7 +27,7 @@ func _on_click_detection_mouse_exited():
GameState.reset_cursor()
func _on_click_detection_input_event(_viewport:Node, event:InputEvent, _shape_idx:int):
func _on_click_detection_input_event(_viewport:Node, event:InputEvent, _shape_idx:int) -> void:
if GameState.placing_drone == false:
if (event is InputEventMouseButton && event.button_index == MOUSE_BUTTON_RIGHT && event.pressed):
#Input.set_custom_mouse_cursor(null)

View file

@ -1,12 +1 @@
class_name DistractorDrone extends Drone
#var dog : Dog = null
func _ready():
# Get the dog in the scene (there will only ever be one...)
#dog = get_tree().get_first_node_in_group("dog") as Dog
#Log.pr(dog)
pass
func _process(_delta):
pass
class_name DistractorDrone extends Drone

View file

@ -9,8 +9,8 @@ var current_state : State
#NOTE This is a generic finite_state_machine, it handles all states, changes to this code will affect
# everything that uses a state machine!
func _ready():
for child in get_children():
func _ready() -> void:
for child : Node in get_children():
if child is State:
states[child.name.to_lower()] = child
child.state_transition.connect(change_state)
@ -20,18 +20,18 @@ func _ready():
current_state = initial_state
# Call the current states update function
func _process(delta):
func _process(delta : float) -> void:
if current_state:
current_state.update(delta)
func _physics_process(delta):
func _physics_process(delta : float) -> void:
if current_state:
current_state.physics_update(delta)
# Use force_change_state cautiously, it immediately switches to a state regardless of any transitions.
# This is used to force us into a 'death state' when killed
func force_change_state(new_state : String):
var newState = states.get(new_state.to_lower())
func force_change_state(new_state : String) -> void:
var newState : State = states.get(new_state.to_lower())
if !newState:
print(new_state + " does not exist in the dictionary of states")
@ -44,20 +44,20 @@ func force_change_state(new_state : String):
# NOTE Calling exit like so: (current_state.Exit()) may cause warnings when flushing queries, like when the enemy is being removed after death.
# call_deferred is safe and prevents this from occuring. We get the Exit function from the state as a callable and then call it in a thread-safe manner
if current_state:
var exit_callable = Callable(current_state, "exit")
var exit_callable : Callable = Callable(current_state, "exit")
exit_callable.call_deferred()
newState.enter()
current_state = newState
func change_state(source_state : State, new_state_name : String):
func change_state(source_state : State, new_state_name : String) -> void:
if source_state != current_state:
#print("Invalid change_state trying from: " + source_state.name + " but currently in: " + current_state.name)
#This typically only happens when trying to switch from death state following a force_change
return
var new_state = states.get(new_state_name.to_lower())
var new_state : State = states.get(new_state_name.to_lower())
if !new_state:
print("New state is empty")
return

View file

@ -1,16 +1,16 @@
extends Node2D
class_name Flowers
@onready var outline = $AreaHighlight
@onready var outline : Sprite2D = $AreaHighlight
@export var health : int = 100
var spawn_snails : bool = false
@onready var spawn_area = $FlowerCollectionArea/CollisionShape2D
@onready var snail = $Snail
@onready var spawn_area : CollisionShape2D = $FlowerCollectionArea/CollisionShape2D
@onready var snail : Snail = $Snail
func _ready():
func _ready() -> void:
hide_outline()
## Check if this level is spawning snails or not
@ -22,21 +22,21 @@ func _ready():
snail.global_position = get_random_snail_spawn()
func show_outline():
func show_outline() -> void:
outline.visible = true
func hide_outline():
func hide_outline() -> void:
outline.visible = false
func get_random_snail_spawn():
var circle_radius = spawn_area.shape.radius
var random_angle = randf_range(0, TAU)
func get_random_snail_spawn() -> Vector2:
var circle_radius : float = spawn_area.shape.radius
var random_angle : float = randf_range(0, TAU)
var x = circle_radius * cos(random_angle)
var y = circle_radius * sin(random_angle)
var x : float = circle_radius * cos(random_angle)
var y : float = circle_radius * sin(random_angle)
var circle_center = spawn_area.global_position
var random_point = circle_center + Vector2(x, y)
var circle_center : Vector2 = spawn_area.global_position
var random_point : Vector2 = circle_center + Vector2(x, y)
return random_point

View file

@ -1,4 +1,6 @@
extends Sprite2D
class_name Snail
var eating : bool = false
var enabled : bool = false
var eating : bool = false

View file

@ -4,12 +4,12 @@ class_name State
signal state_transition
func enter(_msg := {}):
func enter(_msg : Dictionary = {}) -> void:
pass
func exit():
func exit() -> void :
pass
func update(_delta:float):
func update(_delta : float) -> void:
pass

View file

@ -2,11 +2,11 @@
extends Node2D
class_name VegetablePatch
@onready var death_box = $DeathBox
@onready var death_box : Area2D = $DeathBox
func _ready():
func _ready() -> void:
death_box.connect("body_entered", Callable(self, "_on_body_entered"))
func _on_body_entered(area):
func _on_body_entered(area : Bee) -> void:
if area.is_in_group("bee"):
area.die()

View file

@ -1,16 +1,16 @@
extends State
class_name SnailEating
@onready var snail = get_parent().get_parent() as Snail # I think this is bad but I dont care it works
@onready var snail : Snail = get_parent().get_parent() as Snail # I think this is bad but I dont care it works
func enter(_msg := {}):
func enter(_msg : Dictionary = {}) -> void:
Log.pr("I am a snail...")
snail.eating = true
func exit():
func exit() -> void:
snail.eating = false
func update(_delta : float):
func update(_delta : float) -> void:
pass
func physics_update(_delta : float) -> void:

View file

@ -1,15 +1,15 @@
extends State
class_name SnailSleeping
@onready var snail = get_parent().get_parent() as Snail # I think this is bad but I dont care it works
@onready var snail : Snail = get_parent().get_parent() as Snail # I think this is bad but I dont care it works
func enter(_msg := {}):
func enter(_msg : Dictionary = {}) -> void:
Log.pr("I am a snail asleep...")
func exit():
func exit() -> void:
pass
func update(_delta : float):
func update(_delta : float) -> void:
pass
func physics_update(_delta : float) -> void: