refactor Interactable and Unit
This commit is contained in:
parent
6cff7362fc
commit
bdf05d5479
@ -1,8 +1,10 @@
|
|||||||
extends CharacterBody3D
|
extends CharacterBody3D
|
||||||
class_name Interactable
|
class_name Interactable
|
||||||
## A base interactable object that can be hovered over and clicked on
|
## A base interactable object that can be hovered over and clicked on.
|
||||||
|
|
||||||
const MIN_DRAG_DISTANCE: float = 15
|
## When releasing the mouse button, how many pixels away from the click.
|
||||||
|
## position the cursor has to be for it to be considered a click and not drag.
|
||||||
|
const MAX_CLICK_RELEASE_DISTANCE: float = 15
|
||||||
|
|
||||||
var _hovered: bool = false
|
var _hovered: bool = false
|
||||||
var _mouse_over: bool = false
|
var _mouse_over: bool = false
|
||||||
@ -19,10 +21,10 @@ func _ready() -> void:
|
|||||||
|
|
||||||
|
|
||||||
func _process(_delta: float) -> void:
|
func _process(_delta: float) -> void:
|
||||||
_hovered = HoveringManager.hovered_node == self and _mouse_over
|
|
||||||
if not _can_interact:
|
if not _can_interact:
|
||||||
_hovered = false
|
_hovered = false
|
||||||
return
|
return
|
||||||
|
_hovered = HoveringManager.hovered_node == self and _mouse_over
|
||||||
hover_indicator.visible = _hovered
|
hover_indicator.visible = _hovered
|
||||||
|
|
||||||
|
|
||||||
@ -39,7 +41,7 @@ func _input(event: InputEvent) -> void:
|
|||||||
else:
|
else:
|
||||||
if (
|
if (
|
||||||
(button_event.position - _click_start_position).length()
|
(button_event.position - _click_start_position).length()
|
||||||
< MIN_DRAG_DISTANCE
|
< MAX_CLICK_RELEASE_DISTANCE
|
||||||
):
|
):
|
||||||
_click()
|
_click()
|
||||||
|
|
||||||
@ -53,4 +55,4 @@ func _on_mouse_entered() -> void:
|
|||||||
|
|
||||||
|
|
||||||
func _on_mouse_exited() -> void:
|
func _on_mouse_exited() -> void:
|
||||||
_mouse_over = false
|
_mouse_over = false
|
||||||
|
|||||||
@ -15,7 +15,8 @@ enum State {
|
|||||||
|
|
||||||
const ANIMATION_SPEED: float = 0.25
|
const ANIMATION_SPEED: float = 0.25
|
||||||
|
|
||||||
var _unit: Unit
|
var unit: Unit
|
||||||
|
|
||||||
var _state: State = State.NONE
|
var _state: State = State.NONE
|
||||||
var _anim_time: float = 0
|
var _anim_time: float = 0
|
||||||
|
|
||||||
@ -30,7 +31,7 @@ func _ready() -> void:
|
|||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
super._process(delta)
|
super._process(delta)
|
||||||
if _unit == null or not visible:
|
if unit == null or not visible:
|
||||||
return
|
return
|
||||||
|
|
||||||
_anim_time += delta
|
_anim_time += delta
|
||||||
@ -40,16 +41,9 @@ func _process(delta: float) -> void:
|
|||||||
|
|
||||||
|
|
||||||
func open(who: Unit) -> void:
|
func open(who: Unit) -> void:
|
||||||
if _unit != null:
|
|
||||||
close()
|
|
||||||
visible = true
|
visible = true
|
||||||
_unit = who
|
unit = who
|
||||||
set_target(_unit.ui_origin)
|
set_target(unit.ui_origin)
|
||||||
|
|
||||||
|
|
||||||
func close() -> void:
|
|
||||||
_unit.toggle_info(false)
|
|
||||||
super.close()
|
|
||||||
|
|
||||||
|
|
||||||
func _handle_pictogram() -> void:
|
func _handle_pictogram() -> void:
|
||||||
@ -59,29 +53,30 @@ func _handle_pictogram() -> void:
|
|||||||
wrapf(_anim_time / ANIMATION_SPEED, 0, 4)
|
wrapf(_anim_time / ANIMATION_SPEED, 0, 4)
|
||||||
) * atlas.region.size.x
|
) * atlas.region.size.x
|
||||||
|
|
||||||
|
|
||||||
func _get_state() -> void:
|
func _get_state() -> void:
|
||||||
if _unit is Aphid:
|
if unit is Aphid:
|
||||||
match (_unit as Aphid).state:
|
match (unit as Aphid).state:
|
||||||
Aphid.State.WANDERING:
|
Aphid.State.WANDERING:
|
||||||
_state = State.APHID_IDLE
|
_state = State.APHID_IDLE
|
||||||
|
|
||||||
if _unit is AntNitwit:
|
if unit is AntNitwit:
|
||||||
match (_unit as AntNitwit).state:
|
match (unit as AntNitwit).state:
|
||||||
AntNitwit.State.WANDERING:
|
AntNitwit.State.WANDERING:
|
||||||
_state = State.ANT_IDLE
|
_state = State.ANT_IDLE
|
||||||
AntNitwit.State.MOVING:
|
AntNitwit.State.MOVING:
|
||||||
_state = State.ANT_MOVING
|
_state = State.ANT_MOVING
|
||||||
AntNitwit.State.GATHERING:
|
AntNitwit.State.GATHERING:
|
||||||
_get_gathering_state((_unit as AntNitwit).gathering.state)
|
_get_gathering_state((unit as AntNitwit).gathering.state)
|
||||||
|
|
||||||
if _unit is AntGatherer:
|
if unit is AntGatherer:
|
||||||
match (_unit as AntGatherer).state:
|
match (unit as AntGatherer).state:
|
||||||
AntGatherer.State.WANDERING:
|
AntGatherer.State.WANDERING:
|
||||||
_state = State.ANT_IDLE
|
_state = State.ANT_IDLE
|
||||||
AntGatherer.State.MOVING:
|
AntGatherer.State.MOVING:
|
||||||
_state = State.ANT_MOVING
|
_state = State.ANT_MOVING
|
||||||
AntGatherer.State.GATHERING:
|
AntGatherer.State.GATHERING:
|
||||||
_get_gathering_state((_unit as AntGatherer).gathering.state)
|
_get_gathering_state((unit as AntGatherer).gathering.state)
|
||||||
|
|
||||||
|
|
||||||
func _get_gathering_state(gather_state: Gathering.State) -> void:
|
func _get_gathering_state(gather_state: Gathering.State) -> void:
|
||||||
|
|||||||
@ -1,22 +1,25 @@
|
|||||||
extends Interactable
|
extends Interactable
|
||||||
class_name Unit
|
class_name Unit
|
||||||
|
|
||||||
const MOVE_SPEED: float = 3
|
|
||||||
const TURN_SPEED: float = 10
|
const TURN_SPEED: float = 10
|
||||||
|
const LOCOMOTION_CHANGE_SPEED: float = 8
|
||||||
|
|
||||||
|
var _move_speed: float = 3
|
||||||
|
|
||||||
var _max_wander_distance: float = 5
|
var _max_wander_distance: float = 5
|
||||||
var _min_wander_interval: float = 0.25
|
var _min_wander_interval: float = 0.25
|
||||||
var _max_wander_interval: float = 5
|
var _max_wander_interval: float = 5
|
||||||
|
|
||||||
var _is_on_screen: bool = false
|
|
||||||
var _wandering_timer: float = 0
|
|
||||||
var _wandering_center: Vector3 = Vector3.ZERO
|
var _wandering_center: Vector3 = Vector3.ZERO
|
||||||
|
var _wandering_timer: float = 0
|
||||||
|
|
||||||
var _spawn_pos: Vector3
|
var _spawn_pos: Vector3
|
||||||
|
|
||||||
var _locomotion_value: float = 0
|
var _locomotion_value: float = 0
|
||||||
var _showing_info: bool = false
|
|
||||||
var _advance_anim_delta_accum: float = 0
|
var _advance_anim_delta_accum: float = 0
|
||||||
|
|
||||||
|
var _showing_info: bool = false
|
||||||
|
|
||||||
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
|
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
|
||||||
@onready var ui_origin: Node3D = $UiOrigin
|
@onready var ui_origin: Node3D = $UiOrigin
|
||||||
@onready var animation_tree: AnimationTree = $AnimationTree
|
@onready var animation_tree: AnimationTree = $AnimationTree
|
||||||
@ -41,11 +44,13 @@ func _ready() -> void:
|
|||||||
anim_advance_indicator.visible = false
|
anim_advance_indicator.visible = false
|
||||||
if _spawn_pos != null and _spawn_pos != Vector3.ZERO:
|
if _spawn_pos != null and _spawn_pos != Vector3.ZERO:
|
||||||
global_position = _spawn_pos
|
global_position = _spawn_pos
|
||||||
|
|
||||||
_wandering_center = global_position
|
_wandering_center = global_position
|
||||||
nav_agent.max_speed = MOVE_SPEED
|
|
||||||
|
nav_agent.max_speed = _move_speed
|
||||||
nav_agent.velocity_computed.connect(_on_nav_agent_velocity_computed)
|
nav_agent.velocity_computed.connect(_on_nav_agent_velocity_computed)
|
||||||
|
|
||||||
set_max_slides(2)
|
set_max_slides(2)
|
||||||
|
|
||||||
visibility_notifier.screen_entered.connect(
|
visibility_notifier.screen_entered.connect(
|
||||||
_on_visibility_notifier_screen_entered,
|
_on_visibility_notifier_screen_entered,
|
||||||
)
|
)
|
||||||
@ -56,23 +61,19 @@ func _ready() -> void:
|
|||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
super._process(delta)
|
super._process(delta)
|
||||||
_animate(delta)
|
_handle_animation(delta)
|
||||||
|
_showing_info = UiManager.unit_info.unit == self
|
||||||
|
|
||||||
|
|
||||||
func _physics_process(_delta: float) -> void:
|
func _physics_process(_delta: float) -> void:
|
||||||
_navigate()
|
_handle_navigation()
|
||||||
|
|
||||||
|
|
||||||
func toggle_info(on: bool) -> void:
|
|
||||||
_showing_info = on
|
|
||||||
|
|
||||||
|
|
||||||
func _click() -> void:
|
func _click() -> void:
|
||||||
UiManager.unit_info.open(self)
|
UiManager.unit_info.open(self)
|
||||||
toggle_info(true)
|
|
||||||
|
|
||||||
|
|
||||||
func _navigate() -> void:
|
func _handle_navigation() -> void:
|
||||||
if nav_agent.is_navigation_finished():
|
if nav_agent.is_navigation_finished():
|
||||||
velocity = Vector3.ZERO
|
velocity = Vector3.ZERO
|
||||||
return
|
return
|
||||||
@ -80,12 +81,12 @@ func _navigate() -> void:
|
|||||||
var next_pos := nav_agent.get_next_path_position()
|
var next_pos := nav_agent.get_next_path_position()
|
||||||
|
|
||||||
var direction := global_position.direction_to(next_pos)
|
var direction := global_position.direction_to(next_pos)
|
||||||
var new_velocity := direction * MOVE_SPEED
|
var new_velocity := direction * _move_speed
|
||||||
nav_agent.set_velocity(new_velocity)
|
nav_agent.set_velocity(new_velocity)
|
||||||
|
|
||||||
|
|
||||||
func _animate(delta: float) -> void:
|
func _handle_animation(delta: float) -> void:
|
||||||
if not _is_on_screen:
|
if not visibility_notifier.is_on_screen():
|
||||||
return
|
return
|
||||||
|
|
||||||
if velocity.length() > 0.01:
|
if velocity.length() > 0.01:
|
||||||
@ -100,8 +101,8 @@ func _animate(delta: float) -> void:
|
|||||||
|
|
||||||
_locomotion_value = move_toward(
|
_locomotion_value = move_toward(
|
||||||
_locomotion_value,
|
_locomotion_value,
|
||||||
velocity.length() / MOVE_SPEED,
|
velocity.length() / _move_speed,
|
||||||
delta * 8
|
delta * LOCOMOTION_CHANGE_SPEED
|
||||||
)
|
)
|
||||||
animation_tree.set("parameters/locomotion/blend_position", _locomotion_value)
|
animation_tree.set("parameters/locomotion/blend_position", _locomotion_value)
|
||||||
|
|
||||||
@ -113,10 +114,12 @@ func _animate(delta: float) -> void:
|
|||||||
)
|
)
|
||||||
var frame := Engine.get_frames_drawn()
|
var frame := Engine.get_frames_drawn()
|
||||||
var advance := (frame + get_instance_id()) % advance_anim_step == 0
|
var advance := (frame + get_instance_id()) % advance_anim_step == 0
|
||||||
anim_advance_indicator.visible = advance and DebugManager.enabled
|
|
||||||
if advance:
|
if advance:
|
||||||
animation_tree.advance(_advance_anim_delta_accum)
|
animation_tree.advance(_advance_anim_delta_accum)
|
||||||
_advance_anim_delta_accum = 0
|
_advance_anim_delta_accum = 0
|
||||||
|
|
||||||
|
anim_advance_indicator.visible = advance and DebugManager.enabled
|
||||||
|
|
||||||
|
|
||||||
func _wander(delta: float) -> void:
|
func _wander(delta: float) -> void:
|
||||||
@ -138,10 +141,8 @@ func _on_nav_agent_velocity_computed(safe_velocity: Vector3) -> void:
|
|||||||
|
|
||||||
|
|
||||||
func _on_visibility_notifier_screen_entered() -> void:
|
func _on_visibility_notifier_screen_entered() -> void:
|
||||||
_is_on_screen = true
|
|
||||||
SelectionManager.add_unit_to_visible(self)
|
SelectionManager.add_unit_to_visible(self)
|
||||||
|
|
||||||
|
|
||||||
func _on_visibility_notifier_screen_exited() -> void:
|
func _on_visibility_notifier_screen_exited() -> void:
|
||||||
_is_on_screen = false
|
|
||||||
SelectionManager.remove_unit_from_visible(self)
|
SelectionManager.remove_unit_from_visible(self)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user