refactor ControlledUnit
This commit is contained in:
@@ -2,13 +2,13 @@ extends Unit
|
|||||||
class_name ControlledUnit
|
class_name ControlledUnit
|
||||||
|
|
||||||
signal moving_started
|
signal moving_started
|
||||||
signal moving_ended
|
signal moving_finished
|
||||||
|
|
||||||
var _anthill: Anthill
|
var _anthill: Anthill
|
||||||
|
|
||||||
var _hovered_rect: bool = false
|
var _hovered_rect: bool = false
|
||||||
var _selected: bool = false
|
var _selected: bool = false
|
||||||
var _is_relocating: bool = false
|
var _is_moving: bool = false
|
||||||
var _ground_plane: Plane = Plane(Vector3.UP, 0)
|
var _ground_plane: Plane = Plane(Vector3.UP, 0)
|
||||||
|
|
||||||
@onready var selection_indicator: VisualInstance3D = $SelectionIndicator
|
@onready var selection_indicator: VisualInstance3D = $SelectionIndicator
|
||||||
@@ -38,9 +38,9 @@ func _process(delta: float) -> void:
|
|||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
super._physics_process(delta)
|
super._physics_process(delta)
|
||||||
if _is_relocating and nav_agent.is_navigation_finished():
|
if _is_moving and nav_agent.is_navigation_finished():
|
||||||
_is_relocating = false
|
_is_moving = false
|
||||||
moving_ended.emit()
|
moving_finished.emit()
|
||||||
|
|
||||||
|
|
||||||
func _input(event: InputEvent) -> void:
|
func _input(event: InputEvent) -> void:
|
||||||
@@ -55,8 +55,12 @@ func _input(event: InputEvent) -> void:
|
|||||||
if HoveringManager.hovered_node is Interactable:
|
if HoveringManager.hovered_node is Interactable:
|
||||||
_interact(HoveringManager.hovered_node as Interactable)
|
_interact(HoveringManager.hovered_node as Interactable)
|
||||||
else:
|
else:
|
||||||
|
var click_pos := _click_raycast(button_event.position)
|
||||||
|
if click_pos == null:
|
||||||
|
return
|
||||||
|
|
||||||
|
navigate(click_pos, true)
|
||||||
moving_started.emit()
|
moving_started.emit()
|
||||||
_set_target_click(button_event.position)
|
|
||||||
|
|
||||||
|
|
||||||
func initialize(from: Anthill, pos: Vector3) -> ControlledUnit:
|
func initialize(from: Anthill, pos: Vector3) -> ControlledUnit:
|
||||||
@@ -73,8 +77,8 @@ func set_selected(on: bool) -> void:
|
|||||||
_selected = on
|
_selected = on
|
||||||
|
|
||||||
|
|
||||||
func navigate(to: Vector3, relocating: bool = false) -> void:
|
func navigate(to: Vector3, moving: bool = false) -> void:
|
||||||
_is_relocating = relocating
|
_is_moving = moving
|
||||||
nav_agent.set_target_position(to)
|
nav_agent.set_target_position(to)
|
||||||
|
|
||||||
|
|
||||||
@@ -82,14 +86,6 @@ func _interact(with: Interactable) -> void:
|
|||||||
print(self, " interacting with ", with)
|
print(self, " interacting with ", with)
|
||||||
|
|
||||||
|
|
||||||
func _set_target_click(mouse_pos: Vector2) -> void:
|
|
||||||
var click_pos := _click_raycast(mouse_pos)
|
|
||||||
if click_pos == null:
|
|
||||||
return
|
|
||||||
|
|
||||||
navigate(click_pos, true)
|
|
||||||
|
|
||||||
|
|
||||||
func _click_raycast(mouse_pos: Vector2) -> Vector3:
|
func _click_raycast(mouse_pos: Vector2) -> Vector3:
|
||||||
var from := StaticNodesManager.main_camera.global_position
|
var from := StaticNodesManager.main_camera.global_position
|
||||||
var to := StaticNodesManager.main_camera.project_ray_normal(mouse_pos)
|
var to := StaticNodesManager.main_camera.project_ray_normal(mouse_pos)
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func _ready() -> void:
|
|||||||
assert(gathering != null, "gathering missing!")
|
assert(gathering != null, "gathering missing!")
|
||||||
super._ready()
|
super._ready()
|
||||||
moving_started.connect(_on_moving_started)
|
moving_started.connect(_on_moving_started)
|
||||||
moving_ended.connect(_on_moving_ended)
|
moving_finished.connect(_on_moving_ended)
|
||||||
nav_agent.navigation_finished.connect(gathering.on_nav_agent_navigation_finished)
|
nav_agent.navigation_finished.connect(gathering.on_nav_agent_navigation_finished)
|
||||||
var item_bones: Array[int] = []
|
var item_bones: Array[int] = []
|
||||||
for i in MAX_CARRY:
|
for i in MAX_CARRY:
|
||||||
@@ -34,7 +34,7 @@ func _ready() -> void:
|
|||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
super._process(delta)
|
super._process(delta)
|
||||||
if _is_relocating:
|
if _is_moving:
|
||||||
state = State.MOVING
|
state = State.MOVING
|
||||||
|
|
||||||
_handle_wandering(delta)
|
_handle_wandering(delta)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func _ready() -> void:
|
|||||||
assert(gathering != null, "gathering missing!")
|
assert(gathering != null, "gathering missing!")
|
||||||
super._ready()
|
super._ready()
|
||||||
moving_started.connect(_on_moving_started)
|
moving_started.connect(_on_moving_started)
|
||||||
moving_ended.connect(_on_moving_ended)
|
moving_finished.connect(_on_moving_ended)
|
||||||
nav_agent.navigation_finished.connect(gathering.on_nav_agent_navigation_finished)
|
nav_agent.navigation_finished.connect(gathering.on_nav_agent_navigation_finished)
|
||||||
var item_bones: Array[int] = []
|
var item_bones: Array[int] = []
|
||||||
for i in gathering.DEFAULT_MAX_CARRYING:
|
for i in gathering.DEFAULT_MAX_CARRYING:
|
||||||
@@ -33,7 +33,7 @@ func _ready() -> void:
|
|||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
super._process(delta)
|
super._process(delta)
|
||||||
if _is_relocating:
|
if _is_moving:
|
||||||
state = State.MOVING
|
state = State.MOVING
|
||||||
|
|
||||||
_handle_wandering(delta)
|
_handle_wandering(delta)
|
||||||
|
|||||||
Reference in New Issue
Block a user