prefix private variables with _
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
extends ControlledUnit
|
||||
class_name ControlledAntUnit
|
||||
|
||||
@onready var skeleton: Skeleton3D = $AntModel/Armature/Skeleton3D
|
||||
@onready var _skeleton: Skeleton3D = $AntModel/Armature/Skeleton3D
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
assert(skeleton != null, "skeleton missing!")
|
||||
assert(_skeleton != null, "_skeleton missing!")
|
||||
super._ready()
|
||||
|
||||
@@ -4,12 +4,12 @@ class_name ControlledUnit
|
||||
signal moving_started
|
||||
signal moving_ended
|
||||
|
||||
var anthill: Anthill
|
||||
var _anthill: Anthill
|
||||
|
||||
var hovered_rect: bool = false
|
||||
var selected: bool = false
|
||||
var is_relocating: bool = false
|
||||
var ground_plane: Plane = Plane(Vector3.UP, 0)
|
||||
var _hovered_rect: bool = false
|
||||
var _selected: bool = false
|
||||
var _is_relocating: bool = false
|
||||
var _ground_plane: Plane = Plane(Vector3.UP, 0)
|
||||
|
||||
@onready var selection_indicator: VisualInstance3D = $SelectionIndicator
|
||||
|
||||
@@ -19,9 +19,9 @@ static func get_cost() -> int:
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
max_wander_distance = 2
|
||||
min_wander_interval = 0.5
|
||||
max_wander_interval = 10
|
||||
_max_wander_distance = 2
|
||||
_min_wander_interval = 0.5
|
||||
_max_wander_interval = 10
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -32,23 +32,23 @@ func _ready() -> void:
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
super._process(delta)
|
||||
selection_indicator.visible = selected
|
||||
hover_indicator.visible = hovered or hovered_rect
|
||||
selection_indicator.visible = _selected
|
||||
hover_indicator.visible = _hovered or _hovered_rect
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
super._physics_process(delta)
|
||||
if is_relocating and nav_agent.is_navigation_finished():
|
||||
is_relocating = false
|
||||
if _is_relocating and nav_agent.is_navigation_finished():
|
||||
_is_relocating = false
|
||||
moving_ended.emit()
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
super._input(event)
|
||||
if not is_on_screen:
|
||||
if not _is_on_screen:
|
||||
return
|
||||
|
||||
if event is InputEventMouseButton and selected:
|
||||
if event is InputEventMouseButton and _selected:
|
||||
var button_event := event as InputEventMouseButton
|
||||
if (
|
||||
button_event.button_index == MOUSE_BUTTON_RIGHT
|
||||
@@ -62,21 +62,21 @@ func _input(event: InputEvent) -> void:
|
||||
|
||||
|
||||
func initialize(from: Anthill, pos: Vector3) -> ControlledUnit:
|
||||
anthill = from
|
||||
spawn_pos = pos
|
||||
_anthill = from
|
||||
_spawn_pos = pos
|
||||
return self
|
||||
|
||||
|
||||
func set_hovered_rect(on: bool) -> void:
|
||||
hovered_rect = on
|
||||
_hovered_rect = on
|
||||
|
||||
|
||||
func set_selected(on: bool) -> void:
|
||||
selected = on
|
||||
_selected = on
|
||||
|
||||
|
||||
func navigate(to: Vector3, relocating: bool = false) -> void:
|
||||
is_relocating = relocating
|
||||
_is_relocating = relocating
|
||||
nav_agent.set_target_position(to)
|
||||
|
||||
|
||||
@@ -95,8 +95,8 @@ func _set_target_click(mouse_pos: Vector2) -> void:
|
||||
func _click_raycast(mouse_pos: Vector2) -> Vector3:
|
||||
var from := StaticNodesManager.main_camera.global_position
|
||||
var to := StaticNodesManager.main_camera.project_ray_normal(mouse_pos)
|
||||
return ground_plane.intersects_ray(from, to)
|
||||
return _ground_plane.intersects_ray(from, to)
|
||||
|
||||
|
||||
func _on_nav_agent_navigation_finished() -> void:
|
||||
wandering_center = nav_agent.get_final_position()
|
||||
_wandering_center = nav_agent.get_final_position()
|
||||
|
||||
@@ -4,18 +4,18 @@ class_name Unit
|
||||
const MOVE_SPEED: float = 3
|
||||
const TURN_SPEED: float = 10
|
||||
|
||||
var max_wander_distance: float = 5
|
||||
var min_wander_interval: float = 0.25
|
||||
var max_wander_interval: float = 5
|
||||
var _max_wander_distance: float = 5
|
||||
var _min_wander_interval: float = 0.25
|
||||
var _max_wander_interval: float = 5
|
||||
|
||||
var is_on_screen: bool = false
|
||||
var wandering_timer: float = 0
|
||||
var wandering_center: Vector3 = Vector3.ZERO
|
||||
var spawn_pos: Vector3
|
||||
var _is_on_screen: bool = false
|
||||
var _wandering_timer: float = 0
|
||||
var _wandering_center: Vector3 = Vector3.ZERO
|
||||
var _spawn_pos: Vector3
|
||||
|
||||
var locomotion_value: float = 0
|
||||
var showing_info: bool = false
|
||||
var advance_anim_delta_accum: float = 0
|
||||
var _locomotion_value: float = 0
|
||||
var _showing_info: bool = false
|
||||
var _advance_anim_delta_accum: float = 0
|
||||
|
||||
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
|
||||
@onready var ui_origin: Node3D = $UiOrigin
|
||||
@@ -39,10 +39,10 @@ func _ready() -> void:
|
||||
super._ready()
|
||||
|
||||
anim_advance_indicator.visible = false
|
||||
if spawn_pos != null and spawn_pos != Vector3.ZERO:
|
||||
global_position = spawn_pos
|
||||
if _spawn_pos != null and _spawn_pos != Vector3.ZERO:
|
||||
global_position = _spawn_pos
|
||||
|
||||
wandering_center = global_position
|
||||
_wandering_center = global_position
|
||||
nav_agent.max_speed = MOVE_SPEED
|
||||
nav_agent.velocity_computed.connect(_on_nav_agent_velocity_computed)
|
||||
set_max_slides(2)
|
||||
@@ -64,7 +64,7 @@ func _physics_process(_delta: float) -> void:
|
||||
|
||||
|
||||
func toggle_info(on: bool) -> void:
|
||||
showing_info = on
|
||||
_showing_info = on
|
||||
|
||||
|
||||
func _click() -> void:
|
||||
@@ -85,7 +85,7 @@ func _navigate() -> void:
|
||||
|
||||
|
||||
func _animate(delta: float) -> void:
|
||||
if not is_on_screen:
|
||||
if not _is_on_screen:
|
||||
return
|
||||
|
||||
if velocity.length() > 0.01:
|
||||
@@ -98,14 +98,14 @@ func _animate(delta: float) -> void:
|
||||
)
|
||||
# look_at(global_position + velocity, Vector3.UP, true)
|
||||
|
||||
locomotion_value = move_toward(
|
||||
locomotion_value,
|
||||
_locomotion_value = move_toward(
|
||||
_locomotion_value,
|
||||
velocity.length() / MOVE_SPEED,
|
||||
delta * 8
|
||||
)
|
||||
animation_tree.set("parameters/locomotion/blend_position", locomotion_value)
|
||||
animation_tree.set("parameters/locomotion/blend_position", _locomotion_value)
|
||||
|
||||
advance_anim_delta_accum += delta
|
||||
_advance_anim_delta_accum += delta
|
||||
|
||||
var advance_anim_step := maxi(
|
||||
StaticNodesManager.main_camera.advance_anim_step,
|
||||
@@ -115,21 +115,21 @@ func _animate(delta: float) -> void:
|
||||
var advance := (frame + get_instance_id()) % advance_anim_step == 0
|
||||
anim_advance_indicator.visible = advance and DebugManager.enabled
|
||||
if advance:
|
||||
animation_tree.advance(advance_anim_delta_accum)
|
||||
advance_anim_delta_accum = 0
|
||||
animation_tree.advance(_advance_anim_delta_accum)
|
||||
_advance_anim_delta_accum = 0
|
||||
|
||||
|
||||
func _wander(delta: float) -> void:
|
||||
wandering_timer -= delta
|
||||
if wandering_timer <= 0:
|
||||
_wandering_timer -= delta
|
||||
if _wandering_timer <= 0:
|
||||
var new_pos_offset := Vector3(
|
||||
randf_range(-max_wander_distance, max_wander_distance),
|
||||
randf_range(-_max_wander_distance, _max_wander_distance),
|
||||
0,
|
||||
randf_range(-max_wander_distance, max_wander_distance),
|
||||
randf_range(-_max_wander_distance, _max_wander_distance),
|
||||
)
|
||||
var new_pos := wandering_center + new_pos_offset
|
||||
var new_pos := _wandering_center + new_pos_offset
|
||||
nav_agent.set_target_position(new_pos)
|
||||
wandering_timer = randf_range(-min_wander_interval, max_wander_interval)
|
||||
_wandering_timer = randf_range(-_min_wander_interval, _max_wander_interval)
|
||||
|
||||
|
||||
func _on_nav_agent_velocity_computed(safe_velocity: Vector3) -> void:
|
||||
@@ -138,8 +138,8 @@ func _on_nav_agent_velocity_computed(safe_velocity: Vector3) -> void:
|
||||
|
||||
|
||||
func _on_visibility_notifier_screen_entered() -> void:
|
||||
is_on_screen = true
|
||||
_is_on_screen = true
|
||||
|
||||
|
||||
func _on_visibility_notifier_screen_exited() -> void:
|
||||
is_on_screen = false
|
||||
_is_on_screen = false
|
||||
|
||||
@@ -27,14 +27,14 @@ func _ready() -> void:
|
||||
nav_agent.navigation_finished.connect(gathering.on_nav_agent_navigation_finished)
|
||||
var item_bones: Array[int] = []
|
||||
for i in MAX_CARRY:
|
||||
item_bones.append(skeleton.find_bone(ITEM_BONE_NAME + str(i)))
|
||||
gathering.initialize(anthill, skeleton, item_bones, MAX_CARRY, 0.4, 1)
|
||||
item_bones.append(_skeleton.find_bone(ITEM_BONE_NAME + str(i)))
|
||||
gathering.initialize(_anthill, _skeleton, item_bones, MAX_CARRY, 0.4, 1)
|
||||
gathering.navigate_to.connect(_on_gathering_navigate_to)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
super._process(delta)
|
||||
if is_relocating:
|
||||
if _is_relocating:
|
||||
state = State.MOVING
|
||||
|
||||
_handle_wandering(delta)
|
||||
@@ -55,7 +55,7 @@ func _handle_wandering(delta: float) -> void:
|
||||
|
||||
|
||||
func _handle_gathering() -> void:
|
||||
gathering.handle_gathering(showing_info)
|
||||
gathering.handle_gathering(_showing_info)
|
||||
|
||||
|
||||
func _on_moving_ended() -> void:
|
||||
|
||||
@@ -26,14 +26,14 @@ func _ready() -> void:
|
||||
nav_agent.navigation_finished.connect(gathering.on_nav_agent_navigation_finished)
|
||||
var item_bones: Array[int] = []
|
||||
for i in gathering.DEFAULT_MAX_CARRYING:
|
||||
item_bones.append(skeleton.find_bone(ITEM_BONE_NAME + str(i)))
|
||||
gathering.initialize(anthill, skeleton, item_bones)
|
||||
item_bones.append(_skeleton.find_bone(ITEM_BONE_NAME + str(i)))
|
||||
gathering.initialize(_anthill, _skeleton, item_bones)
|
||||
gathering.navigate_to.connect(_on_gathering_navigate_to)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
super._process(delta)
|
||||
if is_relocating:
|
||||
if _is_relocating:
|
||||
state = State.MOVING
|
||||
|
||||
_handle_wandering(delta)
|
||||
@@ -54,7 +54,7 @@ func _handle_wandering(delta: float) -> void:
|
||||
|
||||
|
||||
func _handle_gathering() -> void:
|
||||
gathering.handle_gathering(showing_info)
|
||||
gathering.handle_gathering(_showing_info)
|
||||
|
||||
|
||||
func _on_moving_ended() -> void:
|
||||
|
||||
@@ -18,18 +18,18 @@ enum State {
|
||||
|
||||
var state: State = State.STOP
|
||||
|
||||
var nearby_items: Dictionary = {}
|
||||
var carrying_items: Array[Honeydew] = []
|
||||
var max_carrying: int = DEFAULT_MAX_CARRYING
|
||||
var _nearby_items: Dictionary = {}
|
||||
var _carrying_items: Array[Honeydew] = []
|
||||
var _max_carrying: int = DEFAULT_MAX_CARRYING
|
||||
|
||||
var target: Honeydew
|
||||
var anthill: Anthill
|
||||
var skeleton: Skeleton3D
|
||||
var _target: Honeydew
|
||||
var _anthill: Anthill
|
||||
var _skeleton: Skeleton3D
|
||||
|
||||
var drop_interval: float = DEFAULT_DROP_INTERVAL
|
||||
var pickup_interval: float = DEFAULT_PICKUP_INTERVAL
|
||||
var item_bones: Array[int] = []
|
||||
var showing_after_set: bool = false
|
||||
var _drop_interval: float = DEFAULT_DROP_INTERVAL
|
||||
var _pickup_interval: float = DEFAULT_PICKUP_INTERVAL
|
||||
var _item_bones: Array[int] = []
|
||||
var _showing_after_set: bool = false
|
||||
|
||||
@onready var gathering_center: Vector3 = global_position
|
||||
@onready var collision_shape: CollisionShape3D = $NearbyItemsSearch
|
||||
@@ -50,19 +50,19 @@ func _ready() -> void:
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
for i in range(carrying_items.size()):
|
||||
var item := carrying_items[i]
|
||||
for i in range(_carrying_items.size()):
|
||||
var item := _carrying_items[i]
|
||||
item.global_position = _get_nth_pile_pos(i)
|
||||
|
||||
if target != null:
|
||||
DebugManager.circle(target.global_position)
|
||||
if _target != null:
|
||||
DebugManager.circle(_target.global_position)
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if not visible:
|
||||
return
|
||||
|
||||
if event is InputEventMouseButton and showing_after_set:
|
||||
if event is InputEventMouseButton and _showing_after_set:
|
||||
var button_event := event as InputEventMouseButton
|
||||
if not button_event.pressed:
|
||||
return
|
||||
@@ -70,7 +70,7 @@ func _input(event: InputEvent) -> void:
|
||||
button_event.button_index == MOUSE_BUTTON_LEFT
|
||||
or button_event.button_index == MOUSE_BUTTON_RIGHT
|
||||
):
|
||||
showing_after_set = false
|
||||
_showing_after_set = false
|
||||
|
||||
|
||||
func initialize(
|
||||
@@ -81,12 +81,12 @@ func initialize(
|
||||
drop_interv: float = DEFAULT_DROP_INTERVAL,
|
||||
pickup_interv: float = DEFAULT_PICKUP_INTERVAL,
|
||||
) -> void:
|
||||
anthill = from
|
||||
max_carrying = max_carry
|
||||
drop_interval = drop_interv
|
||||
pickup_interval = pickup_interv
|
||||
skeleton = skeleton_3d
|
||||
item_bones = bones
|
||||
_anthill = from
|
||||
_max_carrying = max_carry
|
||||
_drop_interval = drop_interv
|
||||
_pickup_interval = pickup_interv
|
||||
_skeleton = skeleton_3d
|
||||
_item_bones = bones
|
||||
|
||||
|
||||
func handle_gathering(showing_info: bool) -> void:
|
||||
@@ -95,20 +95,20 @@ func handle_gathering(showing_info: bool) -> void:
|
||||
|
||||
radius_indicator.visible = (
|
||||
(state != State.STOP and showing_info)
|
||||
or showing_after_set
|
||||
or _showing_after_set
|
||||
)
|
||||
|
||||
|
||||
func start_gathering(item: Honeydew) -> void:
|
||||
gathering_center = item.global_position
|
||||
showing_after_set = true
|
||||
_showing_after_set = true
|
||||
state = State.AWAITING
|
||||
_go_pick_up(item)
|
||||
|
||||
|
||||
func stop_gathering() -> void:
|
||||
state = State.STOP
|
||||
target = null
|
||||
_target = null
|
||||
|
||||
|
||||
func on_nav_agent_navigation_finished() -> void:
|
||||
@@ -117,57 +117,57 @@ func on_nav_agent_navigation_finished() -> void:
|
||||
|
||||
if (
|
||||
state == State.DEPOSITING
|
||||
and global_position.distance_to(anthill.global_position) < 1
|
||||
and global_position.distance_to(_anthill.global_position) < 1
|
||||
):
|
||||
_deposit()
|
||||
|
||||
|
||||
func _go_pick_up(item: Honeydew) -> void:
|
||||
state = State.AWAITING
|
||||
if anthill.space_left() <= 0:
|
||||
if _anthill.space_left() <= 0:
|
||||
return
|
||||
if carrying_items.size() >= max_carrying:
|
||||
if _carrying_items.size() >= _max_carrying:
|
||||
_go_deposit()
|
||||
return
|
||||
target = item
|
||||
_target = item
|
||||
state = State.PICKING_UP
|
||||
navigate_to.emit(item.global_position)
|
||||
|
||||
|
||||
func _go_deposit() -> void:
|
||||
state = State.DEPOSITING
|
||||
var dir := anthill.global_position.direction_to(global_position)
|
||||
var dir := _anthill.global_position.direction_to(global_position)
|
||||
navigate_to.emit(
|
||||
anthill.global_position
|
||||
_anthill.global_position
|
||||
+ dir
|
||||
* ANTHILL_DEPOSIT_RADIUS
|
||||
)
|
||||
|
||||
|
||||
func _get_nth_pile_pos(n: int) -> Vector3:
|
||||
return skeleton.to_global(skeleton.get_bone_global_pose(item_bones[n]).origin)
|
||||
return _skeleton.to_global(_skeleton.get_bone_global_pose(_item_bones[n]).origin)
|
||||
|
||||
|
||||
func _pick_up() -> void:
|
||||
var nearest := _find_nearest(nearby_items.values())
|
||||
if target == null or target.carried:
|
||||
var nearest := _find_nearest(_nearby_items.values())
|
||||
if _target == null or _target.carried:
|
||||
state = State.AWAITING
|
||||
if nearest != null:
|
||||
_go_pick_up(nearest)
|
||||
elif carrying_items.size() > 0:
|
||||
elif _carrying_items.size() > 0:
|
||||
_go_deposit()
|
||||
return
|
||||
|
||||
carrying_items.append(target)
|
||||
target.set_carried(true)
|
||||
_carrying_items.append(_target)
|
||||
_target.set_carried(true)
|
||||
audio_player.play_sound(SoundManager.swoosh())
|
||||
await target.start_moving(
|
||||
_get_nth_pile_pos(carrying_items.size() - 1)
|
||||
await _target.start_moving(
|
||||
_get_nth_pile_pos(_carrying_items.size() - 1)
|
||||
).moved
|
||||
audio_player.play_sound(SoundManager.pop())
|
||||
|
||||
await get_tree().create_timer(pickup_interval).timeout
|
||||
if carrying_items.size() >= max_carrying or nearest == null:
|
||||
await get_tree().create_timer(_pickup_interval).timeout
|
||||
if _carrying_items.size() >= _max_carrying or nearest == null:
|
||||
_go_deposit()
|
||||
return
|
||||
|
||||
@@ -176,26 +176,26 @@ func _pick_up() -> void:
|
||||
|
||||
func _deposit() -> void:
|
||||
await get_tree().create_timer(0.5).timeout
|
||||
while carrying_items.size() > 0:
|
||||
while _carrying_items.size() > 0:
|
||||
if state != State.DEPOSITING:
|
||||
return
|
||||
|
||||
if anthill.space_left() <= 0:
|
||||
if _anthill.space_left() <= 0:
|
||||
state = State.AWAITING
|
||||
await _drop_everything()
|
||||
return
|
||||
|
||||
var item := carrying_items.pop_back() as Honeydew
|
||||
var item := _carrying_items.pop_back() as Honeydew
|
||||
audio_player.play_sound(SoundManager.swoosh())
|
||||
await item.start_moving(anthill.global_position).moved
|
||||
await item.start_moving(_anthill.global_position).moved
|
||||
audio_player.play_sound(SoundManager.tok())
|
||||
item.remove_from_spawner()
|
||||
_erase_honeydew(item)
|
||||
item.queue_free()
|
||||
anthill.deposit_honeydew(1)
|
||||
await get_tree().create_timer(drop_interval).timeout
|
||||
_anthill.deposit_honeydew(1)
|
||||
await get_tree().create_timer(_drop_interval).timeout
|
||||
|
||||
var nearest := _find_nearest(nearby_items.values())
|
||||
var nearest := _find_nearest(_nearby_items.values())
|
||||
if nearest != null:
|
||||
_go_pick_up(nearest)
|
||||
return
|
||||
@@ -205,8 +205,8 @@ func _deposit() -> void:
|
||||
|
||||
|
||||
func _drop_everything() -> void:
|
||||
while carrying_items.size() > 0:
|
||||
var item := carrying_items.pop_back() as Honeydew
|
||||
while _carrying_items.size() > 0:
|
||||
var item := _carrying_items.pop_back() as Honeydew
|
||||
var new_pos := Vector3(
|
||||
randf_range(-DROP_SPREAD, DROP_SPREAD),
|
||||
Honeydew.HEIGHT_OFFSET,
|
||||
@@ -214,7 +214,7 @@ func _drop_everything() -> void:
|
||||
)
|
||||
await item.start_moving(global_position + new_pos).moved
|
||||
item.set_carried(false)
|
||||
await get_tree().create_timer(drop_interval).timeout
|
||||
await get_tree().create_timer(_drop_interval).timeout
|
||||
|
||||
|
||||
func _find_nearest(items: Array) -> Honeydew:
|
||||
@@ -232,10 +232,10 @@ func _find_nearest(items: Array) -> Honeydew:
|
||||
|
||||
func _erase_honeydew(item: Honeydew) -> void:
|
||||
var item_id := item.get_instance_id()
|
||||
if not nearby_items.keys().has(item_id):
|
||||
if not _nearby_items.keys().has(item_id):
|
||||
return
|
||||
|
||||
nearby_items.erase(item_id)
|
||||
_nearby_items.erase(item_id)
|
||||
|
||||
|
||||
func _on_body_entered(item: Node3D) -> void:
|
||||
@@ -243,11 +243,11 @@ func _on_body_entered(item: Node3D) -> void:
|
||||
return
|
||||
|
||||
var item_id := item.get_instance_id()
|
||||
if nearby_items.keys().has(item_id):
|
||||
if _nearby_items.keys().has(item_id):
|
||||
return
|
||||
|
||||
nearby_items[item_id] = item as Honeydew
|
||||
if state == State.AWAITING and anthill.space_left() > 0:
|
||||
_nearby_items[item_id] = item as Honeydew
|
||||
if state == State.AWAITING and _anthill.space_left() > 0:
|
||||
_go_pick_up(item as Honeydew)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user