godot-steering-ai-framework/project/demos/face/Turret.gd

60 lines
1.3 KiB
GDScript3
Raw Normal View History

2019-12-20 19:17:27 +01:00
extends KinematicBody2D
onready var collision_shape: = $CollisionShape2D
var _cannon: Rect2
var _agent: = GSTSteeringAgent.new()
var _accel: = GSTTargetAcceleration.new()
2019-12-20 19:17:27 +01:00
var _angular_velocity: = 0.0
var _angular_drag: = 0.01
2019-12-20 19:17:27 +01:00
var _face: GSTFace
func _ready() -> void:
var radius = collision_shape.shape.radius
_cannon = Rect2(Vector2(-5, 0), Vector2(10, -radius*2))
2019-12-20 19:17:27 +01:00
func _draw() -> void:
draw_rect(_cannon, Color.blue)
func _physics_process(delta: float) -> void:
if not _face:
2019-12-22 20:08:53 +01:00
return
2019-12-20 19:17:27 +01:00
_accel = _face.calculate_steering(_accel)
_angular_velocity += _accel.angular
_angular_velocity = lerp(_angular_velocity, 0, _angular_drag)
2019-12-20 19:17:27 +01:00
rotation += _angular_velocity * delta
_update_agent()
func setup(
align_tolerance: float,
deceleration_radius: float,
max_angular_accel: float,
max_angular_speed: float
) -> void:
2019-12-20 19:17:27 +01:00
_face = GSTFace.new(_agent, owner.player.agent)
_face.alignment_tolerance = align_tolerance
_face.deceleration_radius = deceleration_radius
2019-12-20 19:17:27 +01:00
_agent.max_angular_acceleration = max_angular_accel
_agent.max_angular_speed = max_angular_speed
2019-12-20 19:17:27 +01:00
_agent.position = Vector3(global_position.x, global_position.y, 0)
_update_agent()
2019-12-22 20:08:53 +01:00
func _update_agent() -> void:
_agent.angular_velocity = _angular_velocity
_agent.orientation = rotation