remove test_unit and make proper unit hierarchy
This commit is contained in:
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