godot-steering-ai-framework/project/demos/face/Turret.gd
Francois Belair 9f945cbf85 Implement review suggestions to code
Razvan reviewed the code and suggested a number of changes to improve
readability and make facets of the code more explicit and consistent.
2020-01-12 10:20:06 -05:00

60 lines
1.2 KiB
GDScript

extends KinematicBody2D
onready var collision_shape: = $CollisionShape2D
var _radius: = 0.0
var _cannon: Rect2
var _agent: = GSTSteeringAgent.new()
var _accel: = GSTTargetAcceleration.new()
var _angular_velocity: = 0.0
var _angular_drag: = 1.0
var _face: GSTFace
func _ready() -> void:
_radius = collision_shape.shape.radius
_cannon = Rect2(Vector2(-5, 0), Vector2(10, -_radius*2))
func _draw() -> void:
draw_rect(_cannon, Color.blue)
draw_circle(Vector2.ZERO, _radius, Color.teal)
func _physics_process(delta: float) -> void:
if not _face:
return
_accel = _face.calculate_steering(_accel)
_angular_velocity += _accel.angular
if _angular_velocity < 0:
_angular_velocity += _angular_drag * delta
elif _angular_velocity > 0:
_angular_velocity -= _angular_drag * delta
rotation += _angular_velocity * delta
_update_agent()
func setup() -> void:
_face = GSTFace.new(_agent, owner.player.agent)
_face.alignment_tolerance = 0.1
_face.deceleration_radius = PI/2
_agent.max_angular_acceleration = 0.5
_agent.max_angular_speed = 5
_agent.position = Vector3(global_position.x, global_position.y, 0)
_update_agent()
func _update_agent() -> void:
_agent.angular_velocity = _angular_velocity
_agent.orientation = rotation