move wandering functionality to Unit class
This commit is contained in:
@@ -5,15 +5,8 @@ enum AphidState {
|
||||
WANDERING,
|
||||
}
|
||||
|
||||
const MAX_WANDER_DISTANCE: float = 5
|
||||
const MIN_WANDER_INTERVAL: float = 0.25
|
||||
const MAX_WANDER_INTERVAL: float = 5
|
||||
|
||||
var wandering_timer: float = 0
|
||||
var state: AphidState = AphidState.WANDERING
|
||||
|
||||
@onready var wandering_center: Vector3 = global_position
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
super._process(delta)
|
||||
@@ -24,13 +17,4 @@ func _handle_wandering(delta: float) -> void:
|
||||
if state != AphidState.WANDERING:
|
||||
return
|
||||
|
||||
wandering_timer -= delta
|
||||
if wandering_timer <= 0:
|
||||
var new_pos_offset := Vector3(
|
||||
randf_range(-MAX_WANDER_DISTANCE, MAX_WANDER_DISTANCE),
|
||||
0,
|
||||
randf_range(-MAX_WANDER_DISTANCE, MAX_WANDER_DISTANCE),
|
||||
)
|
||||
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)
|
||||
_wander(delta)
|
||||
|
||||
@@ -4,8 +4,13 @@ class_name Unit
|
||||
const MOVE_SPEED: float = 3
|
||||
const TURN_SPEED: float = 10
|
||||
|
||||
const MAX_WANDER_DISTANCE: float = 5
|
||||
const MIN_WANDER_INTERVAL: float = 0.25
|
||||
const MAX_WANDER_INTERVAL: float = 5
|
||||
|
||||
var hovered: bool = false
|
||||
var is_on_screen: bool = false
|
||||
var wandering_timer: float = 0
|
||||
|
||||
@onready var hover_sprite: Sprite3D = $HoverSprite
|
||||
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
|
||||
@@ -13,6 +18,7 @@ var is_on_screen: bool = false
|
||||
@onready var visibility_notifier: VisibleOnScreenNotifier3D = (
|
||||
$VisibleOnScreenNotifier3D
|
||||
)
|
||||
@onready var wandering_center: Vector3 = global_position
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -84,6 +90,19 @@ func _animate(delta: float) -> void:
|
||||
hover_sprite.visible = hovered
|
||||
|
||||
|
||||
func _wander(delta: float) -> void:
|
||||
wandering_timer -= delta
|
||||
if wandering_timer <= 0:
|
||||
var new_pos_offset := Vector3(
|
||||
randf_range(-MAX_WANDER_DISTANCE, MAX_WANDER_DISTANCE),
|
||||
0,
|
||||
randf_range(-MAX_WANDER_DISTANCE, MAX_WANDER_DISTANCE),
|
||||
)
|
||||
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)
|
||||
|
||||
|
||||
func _on_nav_agent_velocity_computed(safe_velocity: Vector3) -> void:
|
||||
velocity = safe_velocity
|
||||
move_and_slide()
|
||||
|
||||
Reference in New Issue
Block a user