mirror of
https://github.com/Relintai/godot-steering-ai-framework.git
synced 2024-12-24 05:37:15 +01:00
Implement demo for Arrive behavior
This commit is contained in:
parent
546d12b15b
commit
b32446217d
20
project/demos/arrive/Arrive.tscn
Normal file
20
project/demos/arrive/Arrive.tscn
Normal file
@ -0,0 +1,20 @@
|
||||
[gd_scene load_steps=5 format=2]
|
||||
|
||||
[ext_resource path="res://demos/arrive/Arriver.gd" type="Script" id=1]
|
||||
[ext_resource path="res://demos/arrive/ArriveDemo.gd" type="Script" id=2]
|
||||
[ext_resource path="res://demos/arrive/Target.gd" type="Script" id=3]
|
||||
|
||||
[sub_resource type="CircleShape2D" id=1]
|
||||
radius = 15.0
|
||||
|
||||
[node name="Arrive" type="Node2D"]
|
||||
script = ExtResource( 2 )
|
||||
|
||||
[node name="Arriver" type="KinematicBody2D" parent="."]
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Arriver"]
|
||||
shape = SubResource( 1 )
|
||||
|
||||
[node name="Target" type="Node2D" parent="."]
|
||||
script = ExtResource( 3 )
|
8
project/demos/arrive/ArriveDemo.gd
Normal file
8
project/demos/arrive/ArriveDemo.gd
Normal file
@ -0,0 +1,8 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
onready var _target: = $Target
|
||||
|
||||
|
||||
func draw(location: Vector2) -> void:
|
||||
_target.draw(location)
|
44
project/demos/arrive/Arriver.gd
Normal file
44
project/demos/arrive/Arriver.gd
Normal file
@ -0,0 +1,44 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
|
||||
onready var _radius: float = ($CollisionShape2D.shape as CircleShape2D).radius
|
||||
|
||||
onready var _agent: = GSTSteeringAgent.new()
|
||||
onready var _target: = GSTAgentLocation.new()
|
||||
onready var _arrive: = GSTArrive.new(_agent, _target)
|
||||
onready var _accel: = GSTTargetAcceleration.new()
|
||||
|
||||
var _velocity: = Vector2()
|
||||
var _drag: = 1.0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_agent.max_linear_acceleration = 10
|
||||
_agent.max_linear_speed = 200
|
||||
_arrive.arrival_tolerance = 25
|
||||
_arrive.deceleration_radius = 225
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
draw_circle(Vector2.ZERO, _radius, Color.red)
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton:
|
||||
var mb: InputEventMouseButton = event
|
||||
if mb.button_index == BUTTON_LEFT and mb.pressed:
|
||||
_target.position = Vector3(mb.position.x, mb.position.y, 0)
|
||||
owner.draw(Vector2(_target.position.x, _target.position.y))
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
_accel = _arrive.calculate_steering(_accel)
|
||||
_velocity += Vector2(_accel.linear.x, _accel.linear.y)
|
||||
_velocity -= _velocity * _drag * delta
|
||||
_velocity = move_and_slide(_velocity)
|
||||
_update_agent()
|
||||
|
||||
|
||||
func _update_agent() -> void:
|
||||
_agent.position = Vector3(global_position.x, global_position.y, 0)
|
||||
_agent.linear_velocity = Vector3(_velocity.x, _velocity.y, 0)
|
14
project/demos/arrive/Target.gd
Normal file
14
project/demos/arrive/Target.gd
Normal file
@ -0,0 +1,14 @@
|
||||
extends Node2D
|
||||
|
||||
|
||||
var target: = Vector2.ZERO
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
draw_circle(target, 20, Color(1, 1, 0, 0.25))
|
||||
draw_circle(target, 5, Color.yellow)
|
||||
|
||||
|
||||
func draw(location: Vector2) -> void:
|
||||
target = location
|
||||
update()
|
@ -6,7 +6,6 @@ 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
|
||||
|
@ -19,6 +19,11 @@ _global_script_classes=[ {
|
||||
"language": "GDScript",
|
||||
"path": "res://src/behaviors/GSTArrive.gd"
|
||||
}, {
|
||||
"base": "GSTSteeringBehavior",
|
||||
"class": "GSTBlend",
|
||||
"language": "GDScript",
|
||||
"path": "res://src/behaviors/GSTBlend.gd"
|
||||
}, {
|
||||
"base": "GSTPursue",
|
||||
"class": "GSTEvade",
|
||||
"language": "GDScript",
|
||||
@ -45,6 +50,11 @@ _global_script_classes=[ {
|
||||
"path": "res://src/behaviors/GSTMatchOrientation.gd"
|
||||
}, {
|
||||
"base": "GSTSteeringBehavior",
|
||||
"class": "GSTPriority",
|
||||
"language": "GDScript",
|
||||
"path": "res://src/behaviors/GSTPriority.gd"
|
||||
}, {
|
||||
"base": "GSTSteeringBehavior",
|
||||
"class": "GSTPursue",
|
||||
"language": "GDScript",
|
||||
"path": "res://src/behaviors/GSTPursue.gd"
|
||||
@ -77,11 +87,13 @@ _global_script_classes=[ {
|
||||
_global_script_class_icons={
|
||||
"GSTAgentLocation": "",
|
||||
"GSTArrive": "",
|
||||
"GSTBlend": "",
|
||||
"GSTEvade": "",
|
||||
"GSTFace": "",
|
||||
"GSTFlee": "",
|
||||
"GSTLookWhereYouGo": "",
|
||||
"GSTMatchOrientation": "",
|
||||
"GSTPriority": "",
|
||||
"GSTPursue": "",
|
||||
"GSTSeek": "",
|
||||
"GSTSteeringAgent": "",
|
||||
|
Loading…
Reference in New Issue
Block a user