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

53 lines
1.2 KiB
GDScript3
Raw Normal View History

2019-12-20 19:17:27 +01:00
extends KinematicBody2D
onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius
onready var _cannon: = Rect2(Vector2(-5, 0), Vector2(10, -_radius*2))
onready var _agent: = GSTSteeringAgent.new()
onready var _accel: = GSTTargetAcceleration.new()
var _angular_velocity: = 0.0
var _angular_drag: = 1.0
var _face: GSTFace
func _draw() -> void:
draw_rect(_cannon, Color.blue)
draw_circle(Vector2.ZERO, _radius, Color.teal)
2019-12-22 20:08:53 +01:00
2019-12-20 19:17:27 +01:00
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
if _angular_velocity < 0:
_angular_velocity += _angular_drag * delta
elif _angular_velocity > 0:
_angular_velocity -= _angular_drag * delta
rotation += _angular_velocity * delta
_update_agent()
2019-12-22 20:08:53 +01:00
func setup() -> void:
2019-12-20 19:17:27 +01:00
_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()
2019-12-22 20:08:53 +01:00
func _update_agent() -> void:
_agent.angular_velocity = _angular_velocity
_agent.orientation = rotation