Skip to content

Commit

Permalink
refactor: static typing
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Feb 27, 2024
1 parent 5bc3f52 commit f0ddf04
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ nodes/OpenHand/node = SubResource("4")
nodes/OpenHand/position = Vector2(-600, 100)
nodes/Trigger/node = SubResource("5")
nodes/Trigger/position = Vector2(-360, 20)
node_connections = [&"output", 0, &"Grip", &"Grip", 0, &"Trigger", &"Grip", 1, &"ClosedHand2", &"Trigger", 0, &"OpenHand", &"Trigger", 1, &"ClosedHand1"]
node_connections = [&"Grip", 0, &"Trigger", &"Grip", 1, &"ClosedHand2", &"Trigger", 0, &"OpenHand", &"Trigger", 1, &"ClosedHand1", &"output", 0, &"Grip"]
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ nodes/OpenHand/node = SubResource("3")
nodes/OpenHand/position = Vector2(-600, 100)
nodes/Trigger/node = SubResource("5")
nodes/Trigger/position = Vector2(-360, 40)
node_connections = [&"output", 0, &"Grip", &"Grip", 0, &"Trigger", &"Grip", 1, &"ClosedHand2", &"Trigger", 0, &"OpenHand", &"Trigger", 1, &"ClosedHand1"]
node_connections = [&"Grip", 0, &"Trigger", &"Grip", 1, &"ClosedHand2", &"Trigger", 0, &"OpenHand", &"Trigger", 1, &"ClosedHand1", &"output", 0, &"Grip"]
69 changes: 27 additions & 42 deletions components/helpers/scatter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,47 @@ extends Node3D
## random locations.

@export var extend: Vector3 = Vector3(1.0, 0.0, 1.0):
set = set_extend
set(new_value):
extend = new_value
_set_dirty()

@export var instance_count: int = 10:
set = set_instance_count
set(new_value):
instance_count = new_value
_set_dirty()

@export var min_scale: float = 1.0:
set = set_min_scale
set(new_value):
min_scale = new_value
_set_dirty()
@export var max_scale: float = 1.0:
set = set_max_scale
set(new_value):
max_scale = new_value
_set_dirty()
@export var mesh: Mesh:
set = set_mesh
set(new_mesh):
mesh = new_mesh
if multi_mesh:
multi_mesh.mesh = mesh

@export var material_override: Material:
set = set_material_override
set(new_material):
material_override = new_material
if multi_mesh_instance:
multi_mesh_instance.material_override = material_override

var dirty: bool = true
var multi_mesh: MultiMesh
var multi_mesh_instance: MultiMeshInstance3D


func set_extend(new_value):
extend = new_value
_set_dirty()


func set_instance_count(new_value):
instance_count = new_value
_set_dirty()


func set_min_scale(new_value):
min_scale = new_value
_set_dirty()


func set_max_scale(new_value):
max_scale = new_value
_set_dirty()


func set_mesh(new_mesh):
mesh = new_mesh
if multi_mesh:
multi_mesh.mesh = mesh


func set_material_override(new_material):
material_override = new_material
if multi_mesh_instance:
multi_mesh_instance.material_override = material_override


func _set_dirty():
func _set_dirty() -> void:
if !dirty:
dirty = true
call_deferred("_update_multimesh")
_update_multimesh.call_deferred()


func _update_multimesh():
func _update_multimesh() -> void:
if !dirty:
return

Expand All @@ -83,7 +68,7 @@ func _update_multimesh():


# Called when the node enters the scene tree for the first time.
func _ready():
func _ready() -> void:
multi_mesh = MultiMesh.new()
multi_mesh.transform_format = MultiMesh.TRANSFORM_3D
multi_mesh.mesh = mesh
Expand Down
8 changes: 5 additions & 3 deletions components/helpers/zone_save_point.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ extends Node3D
@export var saved_material: Material

# We only allow saving once after our scene loads
var is_saved = false
var is_saved := false

@onready var button: MeshInstance3D = $Pole/SaveButton/Button

func _on_save_button_button_pressed(_button):

func _on_save_button_button_pressed(_button: XRToolsInteractableAreaButton) -> void:
# Skip if already saved
if is_saved:
return
Expand All @@ -16,5 +18,5 @@ func _on_save_button_button_pressed(_button):
return

# Change button to saved
$Pole/SaveButton/Button.set_surface_override_material(0, saved_material)
button.set_surface_override_material(0, saved_material)
is_saved = true
6 changes: 3 additions & 3 deletions components/helpers/zone_switch_area.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ extends Area3D


# Called when the node enters the scene tree for the first time.
func _ready():
connect("body_entered", Callable(self, "_on_body_entered"))
func _ready() -> void:
var _connection_err := body_entered.connect(_on_body_entered)


# Called when a body enters this area
func _on_body_entered(body: Node3D):
func _on_body_entered(body: Node3D) -> void:
# Ignore if not enabled
if not enabled:
return
Expand Down
43 changes: 22 additions & 21 deletions components/persistent/persistent_item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func is_xr_class(p_name: String) -> bool:


# Called when the node enters the scene tree for the first time.
func _ready():
func _ready() -> void:
super()

# Subscribe to picked_up and dropped signals
picked_up.connect(_on_picked_up)
dropped.connect(_on_dropped)
var _connection_err := picked_up.connect(_on_picked_up)
var _connection_err2 := dropped.connect(_on_dropped)


# Get configuration warnings
Expand All @@ -70,15 +70,15 @@ func _get_configuration_warnings() -> PackedStringArray:

# Verify item ID is set
if not item_id:
warnings.append("PersistentItem ID not zet")
var _did_append := warnings.append("PersistentItem ID not zet")

# Verify the item type is set
if not item_type:
warnings.append("PersistentItem Type not set")
var _did_append := warnings.append("PersistentItem Type not set")

# Verify item is in persistent group
if not is_in_group("persistent"):
warnings.append("PersistentItem not in 'persistent' group")
var _did_append := warnings.append("PersistentItem not in 'persistent' group")

# Return warnings
return warnings
Expand All @@ -102,7 +102,7 @@ func _notification(what: int) -> void:


## This method is called when the [PersistentItem] is dropped and freed
func drop_and_free():
func drop_and_free() -> void:
super()

# Propagate destruction to this node and all children
Expand All @@ -114,17 +114,17 @@ func drop_and_free():
# and all children for destruction, otherwise it restores the items state.
func _load_state() -> void:
# Restore the item state
var state = PersistentWorld.instance.get_value(item_id)
if not state is Dictionary:
return
var state: Variant = PersistentWorld.instance.get_value(item_id)
if state is Dictionary:
var dict_state: Dictionary = state

# If the item is recorded as having been destroyed then destroy it
if state.get("destroyed", false):
propagate_notification(Persistent.NOTIFICATION_DESTROY)
return
# If the item is recorded as having been destroyed then destroy it
if dict_state.get("destroyed", false):
propagate_notification(Persistent.NOTIFICATION_DESTROY)
return

# Restore the item state
_load_world_state(state)
# Restore the item state
_load_world_state(dict_state)


# This method saves the state of the item to [PersistentWorld]. If the item
Expand Down Expand Up @@ -166,9 +166,10 @@ func _destroy() -> void:
## state information from the dictionary.
func _load_world_state(state: Dictionary) -> void:
# Restore the location
var location = state.get("location")
var location: Variant = state.get("location")
if location is Transform3D:
global_transform = location
var loc: Transform3D = location
global_transform = loc


## This method saves item state to the [param state] world data. The base
Expand All @@ -188,15 +189,15 @@ func _start_auto_return_timer() -> void:
if not _auto_return_timer:
_auto_return_timer = Timer.new()
_auto_return_timer.one_shot = true
_auto_return_timer.timeout.connect(_on_auto_return)
var _connection_err := _auto_return_timer.timeout.connect(_on_auto_return)
add_child(_auto_return_timer)

# Start the auto-return timer
_auto_return_timer.start(auto_return_timeout)


# Called when this object is picked up
func _on_picked_up(_pickable) -> void:
func _on_picked_up(_pickable: XRToolsPickable) -> void:
# Save the last pocket
if get_picked_up_by() is PersistentPocket:
_last_pocket = get_picked_up_by()
Expand All @@ -207,7 +208,7 @@ func _on_picked_up(_pickable) -> void:


# Called when this object is dropped
func _on_dropped(_pickable) -> void:
func _on_dropped(_pickable: XRToolsPickable) -> void:
# Start the auto-return timer if possible
if auto_return and _last_pocket:
_start_auto_return_timer()
Expand Down
17 changes: 7 additions & 10 deletions components/persistent/persistent_item_database.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ extends Resource

## This property is the array of supported persistent item types
@export var items: Array[PersistentItemType]:
set = _set_items
# Handle setting the items
set(p_items):
# Save the new items
items = p_items

# Invalidate the cache
_cache_valid = false

# Items cache
var _cache := {}
Expand All @@ -28,15 +34,6 @@ func get_type(type_id: String) -> PersistentItemType:
return _cache.get(type_id)


# Handle setting the items
func _set_items(p_items: Array[PersistentItemType]) -> void:
# Save the new items
items = p_items

# Invalidate the cache
_cache_valid = false


# Populate the type cache
func _populate_cache() -> void:
# Populate the cache
Expand Down
37 changes: 18 additions & 19 deletions components/persistent/persistent_pocket.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ enum HeldBehavior {

## Pocket behavior when held
@export var held_behavior := HeldBehavior.ENABLE:
set = _set_held_behavior
# Called when the held_behavior property has been modified
set(p_held_behavior):
held_behavior = p_held_behavior
if is_inside_tree() and _parent_body:
_update_held_behavior()

# Parent pickable body
var _parent_body: XRToolsPickable
Expand All @@ -42,7 +46,7 @@ func is_xr_class(p_name: String) -> bool:


# Called when the node enters the scene tree for the first time.
func _ready():
func _ready() -> void:
super()

# Skip initialization if in editor
Expand All @@ -52,8 +56,8 @@ func _ready():
# Search for an ancestor XRToolsPickable
_parent_body = XRTools.find_xr_ancestor(self, "*", "XRToolsPickable")
if _parent_body:
_parent_body.picked_up.connect(_on_picked_up)
_parent_body.dropped.connect(_on_dropped)
var _connection_err := _parent_body.picked_up.connect(_on_picked_up)
var _connection_err2 := _parent_body.dropped.connect(_on_dropped)

# Update the held behavior
_update_held_behavior()
Expand All @@ -65,11 +69,11 @@ func _get_configuration_warnings() -> PackedStringArray:

# Verify pocket ID is set
if not pocket_id:
warnings.append("Pocket ID not zet")
var _did_append := warnings.append("Pocket ID not zet")

# Verify pocket is in persistent group
if not is_in_group("persistent"):
warnings.append("Pocket not in 'persistent' group")
var _did_append := warnings.append("Pocket not in 'persistent' group")

# Return warnings
return warnings
Expand Down Expand Up @@ -108,9 +112,10 @@ func _save_state() -> void:
# Save that the pocket is empty
PersistentWorld.instance.clear_value(pocket_id)
return
var persistent_picked_up_object: PersistentItem = picked_up_object

# Get the item_id of the PersistentItem in the pocket
var item_id: String = picked_up_object.item_id
var item_id := persistent_picked_up_object.item_id

# Save that the pocket holds the item
PersistentWorld.instance.set_value(pocket_id, item_id)
Expand All @@ -128,12 +133,13 @@ func _destroy() -> void:
# Populate the contents of a pocket
func _populate_pocket() -> void:
# Get the ID of the item in the pocket
var item_id = PersistentWorld.instance.get_value(pocket_id)
if not item_id is String:
var _item_id: Variant = PersistentWorld.instance.get_value(pocket_id)
if not _item_id is String:
return
var item_id: String = _item_id

# Construct the item for the pocket
var zone = PersistentZone.find_instance(self)
var zone := PersistentZone.find_instance(self)
var item := zone.create_item_instance(item_id)
if not item:
return
Expand All @@ -144,22 +150,15 @@ func _populate_pocket() -> void:


# Called when the parent pickable body is picked up
func _on_picked_up(_pickable) -> void:
func _on_picked_up(_pickable: XRToolsPickable) -> void:
_update_held_behavior()


# Called when the parent pickable body is dropped
func _on_dropped(_pickable) -> void:
func _on_dropped(_pickable: XRToolsPickable) -> void:
_update_held_behavior()


# Called when the held_behavior property has been modified
func _set_held_behavior(p_held_behavior: HeldBehavior) -> void:
held_behavior = p_held_behavior
if is_inside_tree() and _parent_body:
_update_held_behavior()


# Update the pocket enable
func _update_held_behavior() -> void:
# Skip if no valid parent body
Expand Down
2 changes: 1 addition & 1 deletion components/persistent/persistent_staging.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func is_xr_class(p_name: String) -> bool:


# Called when the node enters the scene tree for the first time.
func _ready():
func _ready() -> void:
super()

# Register ourselves as the persistent stage instances
Expand Down

0 comments on commit f0ddf04

Please sign in to comment.