refactor gathering

This commit is contained in:
Teatov 2024-10-13 22:35:48 +10:00
parent 58a231d02a
commit a4b4ff89eb
8 changed files with 86 additions and 149 deletions

View File

@ -76,13 +76,11 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -58.3389, 0, -63.0179)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -49.7297, 0, -59.3123)
gizmo_extents = 2.71
script = ExtResource("3_vkmt5")
amount = 3
[node name="AphidsBunch2" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -24.0519, -1.90735e-06, -58.8914)
gizmo_extents = 1.38
script = ExtResource("3_vkmt5")
amount = 2
[node name="AphidsBunch3" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 7.61883, -1.90735e-06, -56.5091)
@ -93,61 +91,51 @@ script = ExtResource("3_vkmt5")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 51.9648, -1.90735e-06, -56.633)
gizmo_extents = 2.9
script = ExtResource("3_vkmt5")
amount = 3
[node name="AphidsBunch5" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 31.7378, -1.90735e-06, -19.6778)
gizmo_extents = 10.0
script = ExtResource("3_vkmt5")
amount = 8
[node name="AphidsBunch6" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 47.8955, -2.86102e-06, 1.05166)
gizmo_extents = 1.15
script = ExtResource("3_vkmt5")
amount = 2
[node name="AphidsBunch7" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -15.9318, 9.53674e-07, -10.1347)
gizmo_extents = 4.35
script = ExtResource("3_vkmt5")
amount = 3
[node name="AphidsBunch8" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -53.994, 9.53674e-07, -41.5218)
gizmo_extents = 4.35
script = ExtResource("3_vkmt5")
amount = 2
[node name="AphidsBunch9" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -59.0157, 9.53674e-07, -13.4608)
gizmo_extents = 5.74
script = ExtResource("3_vkmt5")
amount = 2
[node name="AphidsBunch10" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -42.632, 9.53674e-07, -7.0495)
gizmo_extents = 5.74
script = ExtResource("3_vkmt5")
amount = 2
[node name="AphidsBunch11" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.70586, 9.53674e-07, 7.39888)
gizmo_extents = 2.39
script = ExtResource("3_vkmt5")
amount = 4
[node name="AphidsBunch12" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -64.3237, 2.86102e-06, 13.9106)
gizmo_extents = 2.39
script = ExtResource("3_vkmt5")
amount = 2
[node name="AphidsBunch13" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -38.2669, 2.86102e-06, 37.6487)
gizmo_extents = 8.47
script = ExtResource("3_vkmt5")
amount = 8
[node name="AphidsBunch14" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -40.4441, 2.86102e-06, 65.3472)
@ -158,31 +146,26 @@ script = ExtResource("3_vkmt5")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -1.03778, 2.86102e-06, 62.3291)
gizmo_extents = 4.8
script = ExtResource("3_vkmt5")
amount = 4
[node name="AphidsBunch16" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.1906, 3.8147e-06, 30.336)
gizmo_extents = 4.8
script = ExtResource("3_vkmt5")
amount = 3
[node name="AphidsBunch17" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 27.3633, 1.90735e-06, 45.8938)
gizmo_extents = 10.0
script = ExtResource("3_vkmt5")
amount = 15
[node name="AphidsBunch18" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 32.9824, 1.90735e-06, 18.079)
gizmo_extents = 5.75
script = ExtResource("3_vkmt5")
amount = 4
[node name="AphidsBunch19" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 54.9313, 1.90735e-06, 29.1094)
gizmo_extents = 5.75
script = ExtResource("3_vkmt5")
amount = 3
[node name="AphidsBunch20" type="Marker3D" parent="Bunches/Aphids"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 56.3303, 1.90735e-06, 59.7978)
@ -195,8 +178,8 @@ script = ExtResource("3_vkmt5")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -50.9454, -8.10623e-06, -60.0162)
gizmo_extents = 4.7
script = ExtResource("3_vkmt5")
what = 1
amount = 0
_what = 1
_amount = 30
[node name="UI" type="Node" parent="."]

View File

@ -46,6 +46,11 @@ func open(who: Unit) -> void:
set_target(unit.ui_origin)
func close() -> void:
super.close()
unit = null
func _handle_pictogram() -> void:
texture_rect.visible = _state != State.NONE
atlas.region.position.y = (_state - 1) * atlas.region.size.y

View File

@ -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()

View File

@ -4,7 +4,7 @@ class_name ControlledUnit
signal moving_started
signal moving_finished
var _anthill: Anthill
var anthill: Anthill
var _hovered_rect: bool = false
var _selected: bool = false
@ -64,7 +64,7 @@ func _input(event: InputEvent) -> void:
func initialize(from: Anthill, pos: Vector3) -> ControlledUnit:
_anthill = from
anthill = from
_spawn_pos = pos
return self

View File

@ -4,6 +4,8 @@ class_name Unit
const TURN_SPEED: float = 10
const LOCOMOTION_CHANGE_SPEED: float = 8
var showing_info: bool = false
var _move_speed: float = 3
var _max_wander_distance: float = 5
@ -18,7 +20,6 @@ var _spawn_pos: Vector3
var _locomotion_value: float = 0
var _advance_anim_delta_accum: float = 0
var _showing_info: bool = false
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
@onready var ui_origin: Node3D = $UiOrigin
@ -62,7 +63,7 @@ func _ready() -> void:
func _process(delta: float) -> void:
super._process(delta)
_handle_animation(delta)
_showing_info = UiManager.unit_info.unit == self
showing_info = UiManager.unit_info.unit == self
func _physics_process(_delta: float) -> void:

View File

@ -24,12 +24,10 @@ func _ready() -> void:
super._ready()
moving_started.connect(_on_moving_started)
moving_finished.connect(_on_moving_ended)
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)
gathering.navigate_to.connect(_on_gathering_navigate_to)
item_bones.append(skeleton.find_bone(ITEM_BONE_NAME + str(i)))
gathering.initialize(self, skeleton, item_bones, MAX_CARRY, 0.4, 1)
func _process(delta: float) -> void:
@ -38,7 +36,6 @@ func _process(delta: float) -> void:
state = State.MOVING
_handle_wandering(delta)
_handle_gathering()
func _interact(with: Interactable) -> void:
@ -54,22 +51,9 @@ func _handle_wandering(delta: float) -> void:
_wander(delta)
func _handle_gathering() -> void:
gathering.handle_gathering(_showing_info)
func _on_moving_ended() -> void:
state = State.WANDERING
func _on_moving_started() -> void:
if state == State.GATHERING:
gathering.stop_gathering()
state = State.MOVING
func _on_gathering_navigate_to(pos: Vector3) -> void:
if state != State.GATHERING:
return
navigate(pos)

View File

@ -8,6 +8,7 @@ enum State {
}
const ITEM_BONE_NAME = "Nitwit_item_"
const MAX_CARRY: int = 3
var state: State = State.WANDERING
@ -23,12 +24,10 @@ func _ready() -> void:
super._ready()
moving_started.connect(_on_moving_started)
moving_finished.connect(_on_moving_ended)
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)
gathering.navigate_to.connect(_on_gathering_navigate_to)
for i in MAX_CARRY:
item_bones.append(skeleton.find_bone(ITEM_BONE_NAME + str(i)))
gathering.initialize(self, skeleton, item_bones, MAX_CARRY, 0.25, 0.5)
func _process(delta: float) -> void:
@ -37,7 +36,6 @@ func _process(delta: float) -> void:
state = State.MOVING
_handle_wandering(delta)
_handle_gathering()
func _interact(with: Interactable) -> void:
@ -53,24 +51,9 @@ func _handle_wandering(delta: float) -> void:
_wander(delta)
func _handle_gathering() -> void:
gathering.handle_gathering(_showing_info)
func _on_moving_ended() -> void:
state = State.WANDERING
func _on_moving_started() -> void:
if state == State.GATHERING:
gathering.stop_gathering()
state = State.MOVING
func _on_gathering_navigate_to(pos: Vector3) -> void:
print("_on_gathering_navigate_to")
if state != State.GATHERING:
return
print("_on_gathering_navigate_to 2")
navigate(pos)

View File

@ -1,12 +1,6 @@
extends Area3D
class_name Gathering
signal navigate_to(pos: Vector3)
const DEFAULT_MAX_CARRYING = 3
const DEFAULT_DROP_INTERVAL = 0.25
const DEFAULT_PICKUP_INTERVAL = 0.5
const DROP_SPREAD: float = 0.1
const ANTHILL_DEPOSIT_RADIUS: float = 0.5
enum State {
@ -18,16 +12,17 @@ enum State {
var state: State = State.STOP
var _unit: ControlledUnit
var _nearby_items: Dictionary = {}
var _carrying_items: Array[Honeydew] = []
var _max_carrying: int = DEFAULT_MAX_CARRYING
var _max_carrying: int = 0
var _target: Honeydew
var _anthill: Anthill
var _skeleton: Skeleton3D
var _drop_interval: float = DEFAULT_DROP_INTERVAL
var _pickup_interval: float = DEFAULT_PICKUP_INTERVAL
var _drop_interval: float = 0
var _pickup_interval: float = 0
var _item_bones: Array[int] = []
var _showing_after_set: bool = false
@ -53,15 +48,20 @@ func _process(_delta: float) -> void:
for i in range(_carrying_items.size()):
var item := _carrying_items[i]
item.global_position = _get_nth_pile_pos(i)
collision_shape.global_position = gathering_center
collision_shape.global_rotation = Vector3.ZERO
radius_indicator.visible = (
(state != State.STOP and _unit.showing_info)
or _showing_after_set
)
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:
var button_event := event as InputEventMouseButton
if not button_event.pressed:
@ -74,86 +74,67 @@ func _input(event: InputEvent) -> void:
func initialize(
from: Anthill,
unit: ControlledUnit,
skeleton_3d: Skeleton3D,
bones: Array[int],
max_carry: int = DEFAULT_MAX_CARRYING,
drop_interv: float = DEFAULT_DROP_INTERVAL,
pickup_interv: float = DEFAULT_PICKUP_INTERVAL,
max_carry: int,
drop_interv: float,
pickup_interv: float,
) -> void:
_anthill = from
_unit = unit
_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:
collision_shape.global_position = gathering_center
collision_shape.global_rotation = Vector3.ZERO
radius_indicator.visible = (
(state != State.STOP and showing_info)
or _showing_after_set
_unit.moving_started.connect(_on_unit_moving_started)
_unit.nav_agent.navigation_finished.connect(
_on_nav_agent_navigation_finished
)
func start_gathering(item: Honeydew) -> void:
gathering_center = item.global_position
_showing_after_set = true
state = State.AWAITING
_go_pick_up(item)
func stop_gathering() -> void:
state = State.STOP
_target = null
func on_nav_agent_navigation_finished() -> void:
if state == State.PICKING_UP:
_pick_up()
if (
state == State.DEPOSITING
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 _unit.anthill.space_left() <= 0:
state = State.AWAITING
return
if _carrying_items.size() >= _max_carrying:
_go_deposit()
return
_target = item
state = State.PICKING_UP
navigate_to.emit(item.global_position)
_unit.navigate(item.global_position)
func _go_deposit() -> void:
if _unit.anthill.space_left() <= 0:
state = State.AWAITING
return
state = State.DEPOSITING
var dir := _anthill.global_position.direction_to(global_position)
navigate_to.emit(
_anthill.global_position
var dir := _unit.anthill.global_position.direction_to(global_position)
_unit.navigate(
_unit.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:
state = State.AWAITING
if nearest != null:
_go_pick_up(nearest)
if _nearby_items.size() != 0:
_go_pick_up(_find_nearest(_nearby_items.values()))
elif _carrying_items.size() > 0:
_go_deposit()
return
@ -167,11 +148,11 @@ func _pick_up() -> void:
audio_player.play_sound(SoundManager.pop())
await get_tree().create_timer(_pickup_interval).timeout
if _carrying_items.size() >= _max_carrying or nearest == null:
if _carrying_items.size() >= _max_carrying or _nearby_items.size() == 0:
_go_deposit()
return
_go_pick_up(nearest)
_go_pick_up(_find_nearest(_nearby_items.values()))
func _deposit() -> void:
@ -180,57 +161,44 @@ func _deposit() -> void:
if state != State.DEPOSITING:
return
if _anthill.space_left() <= 0:
if _unit.anthill.space_left() <= 0:
state = State.AWAITING
await _drop_everything()
return
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(_unit.anthill.global_position).moved
audio_player.play_sound(SoundManager.tok())
item.remove_from_spawner()
_erase_honeydew(item)
_remove_honeydew_from_nearby(item)
item.queue_free()
_anthill.deposit_honeydew(1)
_unit.anthill.deposit_honeydew(1)
await get_tree().create_timer(_drop_interval).timeout
var nearest := _find_nearest(_nearby_items.values())
if nearest != null:
_go_pick_up(nearest)
if _nearby_items.size() == 0:
state = State.AWAITING
_unit.navigate(gathering_center)
return
state = State.AWAITING
navigate_to.emit(gathering_center)
func _drop_everything() -> void:
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,
randf_range(-DROP_SPREAD, DROP_SPREAD),
)
await item.start_moving(global_position + new_pos).moved
item.set_carried(false)
await get_tree().create_timer(_drop_interval).timeout
_go_pick_up(_find_nearest(_nearby_items.values()))
func _find_nearest(items: Array) -> Honeydew:
var nearest: Node3D = null
var nearest: Honeydew = null
var nearest_distance: float = INF
for item: Honeydew in items:
if item.carried:
continue
var distance := global_position.distance_to(item.global_position)
var distance := global_position.distance_squared_to(item.global_position)
if distance < nearest_distance:
nearest_distance = distance
nearest = item
return nearest
func _erase_honeydew(item: Honeydew) -> void:
func _remove_honeydew_from_nearby(item: Honeydew) -> void:
var item_id := item.get_instance_id()
if not _nearby_items.keys().has(item_id):
return
@ -247,7 +215,7 @@ func _on_body_entered(item: Node3D) -> void:
return
_nearby_items[item_id] = item as Honeydew
if state == State.AWAITING and _anthill.space_left() > 0:
if state == State.AWAITING:
_go_pick_up(item as Honeydew)
@ -255,4 +223,17 @@ func _on_body_exited(item: Node3D) -> void:
if item is not Honeydew:
return
_erase_honeydew(item as Honeydew)
_remove_honeydew_from_nearby(item as Honeydew)
func _on_unit_moving_started() -> void:
state = State.STOP
_target = null
func _on_nav_agent_navigation_finished() -> void:
if state == State.PICKING_UP:
_pick_up()
if state == State.DEPOSITING:
_deposit()