remove test_unit and make proper unit hierarchy
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://cfixshlmwhpmi"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/units/test_unit.gd" id="1_e76lg"]
|
||||
[ext_resource type="PackedScene" uid="uid://bi231xk2sp410" path="res://assets/models/ant.glb" id="1_ff64u"]
|
||||
[ext_resource type="Texture2D" uid="uid://dehqm00kiljut" path="res://assets/textures/selection_unit_decal.png" id="2_5725f"]
|
||||
[ext_resource type="Script" path="res://scripts/units/controlled_unit.gd" id="2_gj6f7"]
|
||||
[ext_resource type="Texture2D" uid="uid://duf132faskeid" path="res://assets/textures/selection_unit_hover_decal.png" id="4_umx1w"]
|
||||
|
||||
[sub_resource type="SphereShape3D" id="SphereShape3D_e8dcp"]
|
||||
@@ -10,7 +10,7 @@ radius = 0.25
|
||||
|
||||
[node name="TestUnit" instance=ExtResource("1_ff64u")]
|
||||
collision_mask = 0
|
||||
script = ExtResource("1_e76lg")
|
||||
script = ExtResource("2_gj6f7")
|
||||
|
||||
[node name="AnimationPlayer" parent="." index="1"]
|
||||
deterministic = true
|
||||
@@ -39,5 +39,3 @@ avoidance_enabled = true
|
||||
height = 0.5
|
||||
radius = 0.25
|
||||
neighbor_distance = 10.0
|
||||
debug_enabled = true
|
||||
debug_use_custom = true
|
||||
|
||||
@@ -106,12 +106,13 @@ func _set_selection_state(hover: bool) -> void:
|
||||
unit.global_position
|
||||
+ (Vector3.UP * UNIT_SELECT_OFFSET)
|
||||
)
|
||||
if unit is TestUnit:
|
||||
if unit is ControlledUnit:
|
||||
var controlled_unit := unit as ControlledUnit
|
||||
if hover:
|
||||
(unit as TestUnit).set_hovered(rect_abs.has_point(point))
|
||||
controlled_unit.set_hovered(rect_abs.has_point(point))
|
||||
else:
|
||||
(unit as TestUnit).set_selected(rect_abs.has_point(point))
|
||||
(unit as TestUnit).set_hovered(false)
|
||||
controlled_unit.set_selected(rect_abs.has_point(point))
|
||||
controlled_unit.set_hovered(false)
|
||||
|
||||
|
||||
func _on_frustrum_area_unit_entered(unit: Node3D) -> void:
|
||||
|
||||
46
scripts/units/controlled_unit.gd
Normal file
46
scripts/units/controlled_unit.gd
Normal file
@@ -0,0 +1,46 @@
|
||||
extends Unit
|
||||
class_name ControlledUnit
|
||||
|
||||
var selected: bool = false
|
||||
var ground_plane: Plane = Plane(Vector3.UP, 0)
|
||||
|
||||
@onready var camera: Camera3D = get_viewport().get_camera_3d()
|
||||
@onready var selection_sprite: Sprite3D = $SelectionSprite
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
set_selected(false)
|
||||
super._ready()
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton and selected:
|
||||
var button_event := event as InputEventMouseButton
|
||||
if (
|
||||
button_event.button_index == MOUSE_BUTTON_RIGHT
|
||||
and button_event.pressed
|
||||
):
|
||||
_set_target_click(button_event.position)
|
||||
|
||||
|
||||
func set_selected(on: bool) -> void:
|
||||
selected = on
|
||||
|
||||
|
||||
func _set_target_click(mouse_pos: Vector2) -> void:
|
||||
var click_position := _click_raycast(mouse_pos)
|
||||
if click_position == null:
|
||||
return
|
||||
|
||||
nav_agent.set_target_position(click_position)
|
||||
|
||||
|
||||
func _click_raycast(mouse_pos: Vector2) -> Vector3:
|
||||
var from := camera.global_position
|
||||
var to := camera.project_ray_normal(mouse_pos)
|
||||
return ground_plane.intersects_ray(from, to)
|
||||
|
||||
|
||||
func _animate(delta: float) -> void:
|
||||
super._animate(delta)
|
||||
selection_sprite.visible = selected
|
||||
@@ -1,22 +1,18 @@
|
||||
extends CharacterBody3D
|
||||
class_name TestUnit
|
||||
class_name Unit
|
||||
|
||||
const MOVE_SPEED: float = 3
|
||||
const TURN_SPEED: float = 10
|
||||
|
||||
var selected: bool = false
|
||||
var hovered: bool = false
|
||||
var ground_plane: Plane = Plane(Vector3.UP, 0)
|
||||
|
||||
@onready var camera: Camera3D = get_viewport().get_camera_3d()
|
||||
@onready var selection_sprite: Sprite3D = $SelectionSprite
|
||||
@onready var hover_sprite: Sprite3D = $HoverSprite
|
||||
@onready var nav_agent: NavigationAgent3D = $NavigationAgent3D
|
||||
@onready var animation_player: AnimationPlayer = $AnimationPlayer
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
set_selected(false)
|
||||
set_hovered(false)
|
||||
nav_agent.max_speed = MOVE_SPEED
|
||||
nav_agent.velocity_computed.connect(_on_nav_agent_velocity_computed)
|
||||
set_max_slides(2)
|
||||
@@ -32,37 +28,9 @@ func _physics_process(_delta: float) -> void:
|
||||
_navigate()
|
||||
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton and selected:
|
||||
var button_event := event as InputEventMouseButton
|
||||
if (
|
||||
button_event.button_index == MOUSE_BUTTON_RIGHT
|
||||
and button_event.pressed
|
||||
):
|
||||
_set_target_click(button_event.position)
|
||||
|
||||
|
||||
func set_selected(on: bool) -> void:
|
||||
selected = on
|
||||
|
||||
|
||||
func set_hovered(on: bool) -> void:
|
||||
hovered = on
|
||||
|
||||
|
||||
func _set_target_click(mouse_pos: Vector2) -> void:
|
||||
var click_position := _click_raycast(mouse_pos)
|
||||
if click_position == null:
|
||||
return
|
||||
|
||||
nav_agent.set_target_position(click_position)
|
||||
|
||||
|
||||
func _click_raycast(mouse_pos: Vector2) -> Vector3:
|
||||
var from := camera.global_position
|
||||
var to := camera.project_ray_normal(mouse_pos)
|
||||
return ground_plane.intersects_ray(from, to)
|
||||
|
||||
func _navigate() -> void:
|
||||
if nav_agent.is_navigation_finished():
|
||||
velocity = Vector3.ZERO
|
||||
@@ -89,7 +57,6 @@ func _animate(delta: float) -> void:
|
||||
else:
|
||||
animation_player.play('idle')
|
||||
|
||||
selection_sprite.visible = selected
|
||||
hover_sprite.visible = hovered
|
||||
|
||||
|
||||
Reference in New Issue
Block a user