refactor Interactable and Unit

This commit is contained in:
Teatov 2024-10-13 21:27:53 +10:00
parent 6cff7362fc
commit bdf05d5479
3 changed files with 44 additions and 46 deletions

View File

@ -1,8 +1,10 @@
extends CharacterBody3D
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 _mouse_over: bool = false
@ -19,10 +21,10 @@ func _ready() -> void:
func _process(_delta: float) -> void:
_hovered = HoveringManager.hovered_node == self and _mouse_over
if not _can_interact:
_hovered = false
return
_hovered = HoveringManager.hovered_node == self and _mouse_over
hover_indicator.visible = _hovered
@ -39,7 +41,7 @@ func _input(event: InputEvent) -> void:
else:
if (
(button_event.position - _click_start_position).length()
< MIN_DRAG_DISTANCE
< MAX_CLICK_RELEASE_DISTANCE
):
_click()
@ -53,4 +55,4 @@ func _on_mouse_entered() -> void:
func _on_mouse_exited() -> void:
_mouse_over = false
_mouse_over = false

View File

@ -15,7 +15,8 @@ enum State {
const ANIMATION_SPEED: float = 0.25
var _unit: Unit
var unit: Unit
var _state: State = State.NONE
var _anim_time: float = 0
@ -30,7 +31,7 @@ func _ready() -> void:
func _process(delta: float) -> void:
super._process(delta)
if _unit == null or not visible:
if unit == null or not visible:
return
_anim_time += delta
@ -40,16 +41,9 @@ func _process(delta: float) -> void:
func open(who: Unit) -> void:
if _unit != null:
close()
visible = true
_unit = who
set_target(_unit.ui_origin)
func close() -> void:
_unit.toggle_info(false)
super.close()
unit = who
set_target(unit.ui_origin)
func _handle_pictogram() -> void:
@ -59,29 +53,30 @@ func _handle_pictogram() -> void:
wrapf(_anim_time / ANIMATION_SPEED, 0, 4)
) * atlas.region.size.x
func _get_state() -> void:
if _unit is Aphid:
match (_unit as Aphid).state:
if unit is Aphid:
match (unit as Aphid).state:
Aphid.State.WANDERING:
_state = State.APHID_IDLE
if _unit is AntNitwit:
match (_unit as AntNitwit).state:
if unit is AntNitwit:
match (unit as AntNitwit).state:
AntNitwit.State.WANDERING:
_state = State.ANT_IDLE
AntNitwit.State.MOVING:
_state = State.ANT_MOVING
AntNitwit.State.GATHERING:
_get_gathering_state((_unit as AntNitwit).gathering.state)
_get_gathering_state((unit as AntNitwit).gathering.state)
if _unit is AntGatherer:
match (_unit as AntGatherer).state:
if unit is AntGatherer:
match (unit as AntGatherer).state:
AntGatherer.State.WANDERING:
_state = State.ANT_IDLE
AntGatherer.State.MOVING:
_state = State.ANT_MOVING
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:

View File

@ -1,22 +1,25 @@
extends Interactable
class_name Unit
const MOVE_SPEED: float = 3
const TURN_SPEED: float = 10
const LOCOMOTION_CHANGE_SPEED: float = 8
var _move_speed: float = 3
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 _wandering_timer: float = 0
var _spawn_pos: Vector3
var _locomotion_value: float = 0
var _showing_info: bool = false
var _advance_anim_delta_accum: float = 0
var _showing_info: bool = false
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
@onready var ui_origin: Node3D = $UiOrigin
@onready var animation_tree: AnimationTree = $AnimationTree
@ -41,11 +44,13 @@ func _ready() -> void:
anim_advance_indicator.visible = false
if _spawn_pos != null and _spawn_pos != Vector3.ZERO:
global_position = _spawn_pos
_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)
set_max_slides(2)
visibility_notifier.screen_entered.connect(
_on_visibility_notifier_screen_entered,
)
@ -56,23 +61,19 @@ func _ready() -> void:
func _process(delta: float) -> void:
super._process(delta)
_animate(delta)
_handle_animation(delta)
_showing_info = UiManager.unit_info.unit == self
func _physics_process(_delta: float) -> void:
_navigate()
func toggle_info(on: bool) -> void:
_showing_info = on
_handle_navigation()
func _click() -> void:
UiManager.unit_info.open(self)
toggle_info(true)
func _navigate() -> void:
func _handle_navigation() -> void:
if nav_agent.is_navigation_finished():
velocity = Vector3.ZERO
return
@ -80,12 +81,12 @@ func _navigate() -> void:
var next_pos := nav_agent.get_next_path_position()
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)
func _animate(delta: float) -> void:
if not _is_on_screen:
func _handle_animation(delta: float) -> void:
if not visibility_notifier.is_on_screen():
return
if velocity.length() > 0.01:
@ -100,8 +101,8 @@ func _animate(delta: float) -> void:
_locomotion_value = move_toward(
_locomotion_value,
velocity.length() / MOVE_SPEED,
delta * 8
velocity.length() / _move_speed,
delta * LOCOMOTION_CHANGE_SPEED
)
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 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
anim_advance_indicator.visible = advance and DebugManager.enabled
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:
_is_on_screen = true
SelectionManager.add_unit_to_visible(self)
func _on_visibility_notifier_screen_exited() -> void:
_is_on_screen = false
SelectionManager.remove_unit_from_visible(self)