Hi all.
I have an issue I've been trying to solve, but haven't much success. Before I messed with my code a bit, I had a hostile ship that would turn towards the player, and fly towards them. However fidgety it was, it, for the most part, worked.
One attempt at refactoring later, and I've taken a minor step back, returning to the unstopping spinning-top once more. It only turns clockwise, which will be an issue when the player is only a short left turn away, as the ship appears to love right turns.
EnemyTemplate.gd
extends RigidBody2D
signal searching
signal turning
signal moving
enum states {
SEARCH,
ROTATE,
MOVE,
DEAD
}
export var move_thrust = Vector2(175,0)
export var torque = 2500
var has_target = false
var target_list = ["none"]
var current_state = "SEARCH"
var player_pos = Vector2()
var in_pursuit_area = false
var thruster_active = false
var is_turning = false
var is_searching = true
var d
var player
func _ready():
$MaxPursuitCaster.add_exception($SearchRange)
$MaxPursuitCaster.add_exception($PursuitRange)
func _integrate_forces(state):
var ship_pos = self.global_position
var ship_rot = self.global_rotation
var speed = self.get_linear_velocity()
var spin = self.get_angular_velocity()
# SEARCH
if is_searching == true:
if speed < Vector2(0,0) or speed > Vector2(0,0):
self.set_linear_damp(1)
else:
self.set_linear_damp(-1)
if spin < 0 or spin > 0:
self.set_angular_damp(3)
else:
self.set_angular_damp(-1)
# ROTATE
if is_turning == true:
var rot_dir = 0
var angle_rad = self.get_angle_to(player_pos)
var angle_deg = rad2deg(angle_rad)
var w = (d * 1000) / angle_deg
var new_degrees = lerp(0, angle_deg, w)
var new_rad = deg2rad(new_degrees)
if new_rad > 0:
rot_dir = 1
if new_rad < 0:
rot_dir = -1
set_applied_torque(torque * rot_dir)
# MOVE
if thruster_active == true:
self.set_angular_damp(5)
set_applied_force(move_thrust.rotated(rotation))
if thruster_active == false:
set_applied_force(Vector2())
self.set_angular_damp(-1)
func _on_PursuitRange_body_entered(body):
if body.is_in_group("Player"):
in_pursuit_area = true
player = body
func _on_SearchRange_body_entered(body):
if body.is_in_group("Player"):
has_target = true
if target_list.size() == 1:
target_list.append(body)
func _on_PursuitRange_body_exited(body):
if body.is_in_group("Player"):
if target_list.size() > 1:
target_list.pop_back()
has_target = false
in_pursuit_area = false
func _process(delta):
d = delta
if has_target == true and in_pursuit_area == true:
if player_pos != player.global_position:
player_pos = player.get_global_position()
match(current_state):
"SEARCH":
emit_signal("searching")
if target_list.size() > 1:
current_state = "ROTATE"
"ROTATE":
emit_signal("turning")
if target_list.size() == 1:
current_state = "SEARCH"
"MOVE":
if $MaxPursuitCaster.is_colliding() == true and has_target == true:
var collider = $MaxPursuitCaster.get_collider()
if collider.is_in_group("Player") and in_pursuit_area == true:
emit_signal("moving")
is_turning = false
else:
$Thrusters/Engine.emitting = false
thruster_active = false
current_state = "ROTATE"
"DEAD":
self.queue_free()
func _on_EnemyTemplate_body_entered(body):
if body.is_in_group("Singularity"):
current_state = "DEAD"
func _on_EnemyTemplate_searching():
is_searching = true
func _on_EnemyTemplate_turning():
is_turning = true
func _on_EnemyTemplate_moving():
thruster_active = true
$Thrusters/Engine.emitting = true
Now I find myself at a minor impasse. Most of the game is finished, and the movement of the hostile ships is crucial for gameplay. Any suggestions on how to get the ROTATE and MOVE states working again?