-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd06ce1
commit 6b1f192
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
extends Node3D | ||
class_name Trader | ||
|
||
## AI for a ship that behaves like a trader, avoiding combat. | ||
## | ||
## [b]This script expects the parent node to be a [Ship].[/b] | ||
|
||
# TODO: Deduplicate code with Pirate class. | ||
|
||
## For thrusting, the tolerance (in degrees) for being slightly off-rotated. | ||
@export var direction_tolerance_deg: float = 10.0 | ||
|
||
## The tolerance (in m) for hitting the destination point, before selecting a new one. | ||
@export var destination_tolerance: float = 1.0 | ||
|
||
@onready var _ship := self.get_parent() as Ship | ||
var _direction_tolerance_rad: float | ||
var _destination: Node3D = null | ||
|
||
func _ready() -> void: | ||
self._direction_tolerance_rad = deg_to_rad(self.direction_tolerance_deg) | ||
|
||
func _select_new_destination() -> void: | ||
var planets: Array[Node3D] = [] | ||
planets.assign(self.get_tree().get_nodes_in_group("planets")) | ||
if self._destination: | ||
planets.erase(self._destination) | ||
|
||
self._destination = planets.pick_random() if planets else null | ||
|
||
func _desired_direction() -> Vector3: | ||
if not self._destination: | ||
return Vector3.ZERO | ||
|
||
return (self._destination.global_position - self._ship.global_position).normalized() | ||
|
||
func _pointing_in_direction(direction: Vector3) -> bool: | ||
var current_direction := - self._ship.global_transform.basis.z | ||
return current_direction.angle_to(direction) <= self._direction_tolerance_rad | ||
|
||
func _distance_to_destination() -> float: | ||
if not self._destination: | ||
return 0.0 | ||
|
||
return self._ship.global_position.distance_to(self._destination.global_position) | ||
|
||
func _physics_process(_delta: float) -> void: | ||
if not self._destination: | ||
self._select_new_destination() | ||
|
||
var desired_direction := self._desired_direction() | ||
self._ship.rigid_body_direction.direction = desired_direction | ||
|
||
if not self._pointing_in_direction(desired_direction): | ||
self._ship.rigid_body_thruster.throttle = 0.0 | ||
return | ||
|
||
if self._distance_to_destination() > self.destination_tolerance: | ||
self._ship.rigid_body_thruster.throttle = 1.0 | ||
else: | ||
self._ship.rigid_body_thruster.throttle = 0.0 | ||
self._select_new_destination() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[gd_scene load_steps=2 format=3 uid="uid://dpwohjd3mf1e"] | ||
|
||
[ext_resource type="Script" path="res://actors/trader.gd" id="1_1k1lj"] | ||
|
||
[node name="Trader" type="Node3D"] | ||
script = ExtResource("1_1k1lj") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters