Files

93 lines
2.3 KiB
GDScript

class_name CPUController extends BaseController
func _process(_delta: float) -> void:
if not jets_collection: return
for jet: Jet in jets_collection.get_children():
if Globals.ControlMode.PLAYER_MODE == jet.controller.control_mode:
continue
process_jet_plan(jet)
func _controller_message(event: BaseEvent) -> void:
if event is InitSpawnsLoadedEvent:
handle_init_spawns_loaded_event()
func handle_init_spawns_loaded_event() -> void:
for jet: Jet in jets_collection.get_children():
if Globals.ControlMode.PLAYER_MODE == jet.controller.control_mode:
continue
create_jet_plan(jet)
func create_jet_plan(jet: Jet) -> void:
assign_jet_target(jet)
func assign_jet_target(jet: Jet) -> void:
for _jet: Jet in jets_collection.get_children():
if _jet.faction == jet.faction: continue
if _jet.targeted_by: continue
_jet.targeted_by = jet
jet.target = _jet
break
func process_jet_plan(jet: Jet) -> void:
if not is_instance_valid(jet.target): return
var direction: Vector3 = (
jet.target.global_position - jet.global_position
).normalized()
var target_basis = Basis.looking_at(direction, Vector3.UP)
var target_rotation = target_basis.get_euler()
var current_rotation = jet.global_rotation
var yaw_error = wrapf(
target_rotation.y - current_rotation.y,
-PI,
PI
)
var pitch_error = wrapf(
target_rotation.x - current_rotation.x,
-PI,
PI
)
var deadzone = deg_to_rad(2.0)
if yaw_error > deadzone:
jet.controller.cpu.yaw_right()
elif yaw_error < -deadzone:
jet.controller.cpu.yaw_left()
else:
jet.controller.cpu.no_action()
if pitch_error > deadzone:
jet.controller.cpu.pitch_down()
elif pitch_error < -deadzone:
jet.controller.cpu.pitch_up()
#func process_jet_plan(jet: Jet) -> void:
#var direction = (jet.target.global_position - jet.global_position).normalized()
#var target_basis = jet.looking_at(direction, Vector3.UP)
#var target_rotation = target_basis.get_euler()
#var direction = jet.target.global_position - jet.global_position
#var desired_rotation = jet.look_rotation(direction)
#var rotation_error = desired_rotation - jet.rotation
#jet.pitch_input = calculate_pitch(rotation_error)
#jet.yaw_input = calculate_yaw(rotation_error)
#jet.roll_input = calculate_roll(rotation_error)
#jet.throttle = calculate_throttle(distance)