make units inherit Interactable

This commit is contained in:
2024-10-06 19:19:41 +10:00
parent 486b193ee7
commit cd4d8d1af6
6 changed files with 27 additions and 30 deletions

View File

@@ -7,7 +7,7 @@
[sub_resource type="BoxShape3D" id="BoxShape3D_ruupa"] [sub_resource type="BoxShape3D" id="BoxShape3D_ruupa"]
[node name="Anthill" type="StaticBody3D"] [node name="Anthill" type="CharacterBody3D"]
script = ExtResource("1_8k02d") script = ExtResource("1_8k02d")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."] [node name="MeshInstance3D" type="MeshInstance3D" parent="."]

View File

@@ -4,7 +4,7 @@
[ext_resource type="PackedScene" uid="uid://cfixshlmwhpmi" path="res://scenes/units/test_unit.tscn" id="2_4bgm6"] [ext_resource type="PackedScene" uid="uid://cfixshlmwhpmi" path="res://scenes/units/test_unit.tscn" id="2_4bgm6"]
[ext_resource type="PackedScene" uid="uid://d4c6ujs1ra1ob" path="res://scenes/units/aphid.tscn" id="3_eh22j"] [ext_resource type="PackedScene" uid="uid://d4c6ujs1ra1ob" path="res://scenes/units/aphid.tscn" id="3_eh22j"]
[ext_resource type="PackedScene" uid="uid://dx544tb0so0b4" path="res://scenes/items/honeydew.tscn" id="4_r46td"] [ext_resource type="PackedScene" uid="uid://dx544tb0so0b4" path="res://scenes/items/honeydew.tscn" id="4_r46td"]
[ext_resource type="PackedScene" uid="uid://clftjlaotf2g2" path="res://scenes/structures/Anthill.tscn" id="7_f30w3"] [ext_resource type="PackedScene" uid="uid://clftjlaotf2g2" path="res://scenes/structures/anthill.tscn" id="7_f30w3"]
[ext_resource type="PackedScene" uid="uid://d8ut24fit87x" path="res://scenes/ui/anthill_info.tscn" id="8_ohyy4"] [ext_resource type="PackedScene" uid="uid://d8ut24fit87x" path="res://scenes/ui/anthill_info.tscn" id="8_ohyy4"]
[sub_resource type="NavigationMesh" id="NavigationMesh_6jq54"] [sub_resource type="NavigationMesh" id="NavigationMesh_6jq54"]

View File

@@ -33,7 +33,6 @@ func spawn_nitwit() -> void:
func _click() -> void: func _click() -> void:
print('AAAAAAAAA')
UiManager.anthill_info.open(self) UiManager.anthill_info.open(self)

View File

@@ -1,4 +1,4 @@
extends PhysicsBody3D extends CharacterBody3D
class_name Interactable class_name Interactable
const MIN_DRAG_DISTANCE: float = 15 const MIN_DRAG_DISTANCE: float = 15
@@ -15,7 +15,7 @@ func _ready() -> void:
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
hovered = HoveringManager.hovered_node == self hovered = HoveringManager.hovered_node == self
_animate() hover_sprite.visible = hovered
func _input(event: InputEvent) -> void: func _input(event: InputEvent) -> void:
@@ -36,7 +36,3 @@ func _input(event: InputEvent) -> void:
func _click() -> void: func _click() -> void:
print(self, ' clicked!') print(self, ' clicked!')
func _animate() -> void:
hover_sprite.visible = hovered

View File

@@ -7,6 +7,7 @@ signal moving_ended
var anthill: Anthill var anthill: Anthill
var spawn_pos: Vector3 var spawn_pos: Vector3
var hovered_rect: bool = false
var selected: bool = false var selected: bool = false
var moving_to_target: bool = false var moving_to_target: bool = false
var ground_plane: Plane = Plane(Vector3.UP, 0) var ground_plane: Plane = Plane(Vector3.UP, 0)
@@ -31,6 +32,12 @@ func _ready() -> void:
super._ready() super._ready()
func _process(delta: float) -> void:
super._process(delta)
selection_sprite.visible = selected
hover_sprite.visible = hovered or hovered_rect
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
super._physics_process(delta) super._physics_process(delta)
if moving_to_target and nav_agent.is_navigation_finished(): if moving_to_target and nav_agent.is_navigation_finished():
@@ -39,6 +46,7 @@ func _physics_process(delta: float) -> void:
func _input(event: InputEvent) -> void: func _input(event: InputEvent) -> void:
super._input(event)
if not is_on_screen: if not is_on_screen:
return return
@@ -49,7 +57,7 @@ func _input(event: InputEvent) -> void:
and button_event.pressed and button_event.pressed
): ):
if HoveringManager.hovered_node is Interactable: if HoveringManager.hovered_node is Interactable:
pass _interact(HoveringManager.hovered_node as Interactable)
else: else:
moving_to_target = true moving_to_target = true
moving_started.emit() moving_started.emit()
@@ -62,6 +70,10 @@ func initialize(from: Anthill, pos: Vector3) -> ControlledUnit:
return self return self
func set_hovered_rect(on: bool) -> void:
hovered_rect = on
static func get_cost() -> int: static func get_cost() -> int:
return 5 return 5
@@ -70,20 +82,19 @@ func set_selected(on: bool) -> void:
selected = on selected = on
func _interact(with: Interactable) -> void:
print(self, ' interacting with ', with)
func _set_target_click(mouse_pos: Vector2) -> void: func _set_target_click(mouse_pos: Vector2) -> void:
var click_position := _click_raycast(mouse_pos) var click_pos := _click_raycast(mouse_pos)
if click_position == null: if click_pos == null:
return return
nav_agent.set_target_position(click_position) nav_agent.set_target_position(click_pos)
func _click_raycast(mouse_pos: Vector2) -> Vector3: func _click_raycast(mouse_pos: Vector2) -> Vector3:
var from := camera.global_position var from := camera.global_position
var to := camera.project_ray_normal(mouse_pos) var to := camera.project_ray_normal(mouse_pos)
return ground_plane.intersects_ray(from, to) return ground_plane.intersects_ray(from, to)
func _animate(delta: float) -> void:
super._animate(delta)
selection_sprite.visible = selected

View File

@@ -1,4 +1,4 @@
extends CharacterBody3D extends Interactable
class_name Unit class_name Unit
const MOVE_SPEED: float = 3 const MOVE_SPEED: float = 3
@@ -8,13 +8,10 @@ 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 hovered_rect: bool = false
var hovered: bool = false
var is_on_screen: bool = false var is_on_screen: bool = false
var wandering_timer: float = 0 var wandering_timer: float = 0
var wandering_center: Vector3 = Vector3.ZERO var wandering_center: Vector3 = Vector3.ZERO
@onready var hover_sprite: Sprite3D = $HoverSprite
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D @onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
@onready var animation_tree: AnimationTree = $AnimationTree @onready var animation_tree: AnimationTree = $AnimationTree
@onready var visibility_notifier: VisibleOnScreenNotifier3D = ( @onready var visibility_notifier: VisibleOnScreenNotifier3D = (
@@ -27,6 +24,7 @@ func _ready() -> void:
assert(nav_agent != null, "nav_agent missing!") assert(nav_agent != null, "nav_agent missing!")
assert(animation_tree != null, "animation_tree missing!") assert(animation_tree != null, "animation_tree missing!")
assert(visibility_notifier != null, "visibility_notifier missing!") assert(visibility_notifier != null, "visibility_notifier missing!")
super._ready()
wandering_center = global_position wandering_center = global_position
nav_agent.max_speed = MOVE_SPEED nav_agent.max_speed = MOVE_SPEED
@@ -41,19 +39,14 @@ func _ready() -> void:
func _process(delta: float) -> void: func _process(delta: float) -> void:
hovered = HoveringManager.hovered_node == self super._process(delta)
_animate(delta) _animate(delta)
hover_sprite.visible = hovered or hovered_rect
func _physics_process(_delta: float) -> void: func _physics_process(_delta: float) -> void:
_navigate() _navigate()
func set_hovered_rect(on: bool) -> void:
hovered_rect = on
func _navigate() -> void: func _navigate() -> void:
if nav_agent.is_navigation_finished(): if nav_agent.is_navigation_finished():
velocity = Vector3.ZERO velocity = Vector3.ZERO
@@ -90,8 +83,6 @@ func _animate(delta: float) -> void:
velocity.length() / MOVE_SPEED, velocity.length() / MOVE_SPEED,
) )
hover_sprite.visible = hovered
func _wander(delta: float) -> void: func _wander(delta: float) -> void:
wandering_timer -= delta wandering_timer -= delta