godot-steering-ai-framework/project/demos/SeekFlee/SeekFleeDemo.gd
Francois Belair 6e6f27505c Fix init time setters and export Follow variables
The use of `not is_inside_tree()` before setting the setting class' value
meant that the starting values would always be the default. Moving the
value setting before checking for tree readiness fixes the issue.
2020-01-16 12:31:02 -05:00

83 lines
2.0 KiB
GDScript

extends Node2D
# Access helper class for children to access window boundaries.
enum Mode { FLEE, SEEK }
export(Mode) var behavior_mode := Mode.SEEK setget set_behavior_mode
export(float, 0, 2000, 40) var max_linear_speed := 200.0 setget set_max_linear_speed
export(float, 0, 500, 0.5) var max_linear_accel := 10.0 setget set_max_linear_accel
export(float) var player_speed := 600.0 setget set_player_speed
var camera_boundaries: Rect2
onready var player: KinematicBody2D = $Player
onready var spawner: Node2D = $Spawner
func _ready() -> void:
camera_boundaries = Rect2(
Vector2.ZERO,
Vector2(
ProjectSettings["display/window/size/width"],
ProjectSettings["display/window/size/height"]
)
)
var rng := RandomNumberGenerator.new()
rng.randomize()
player.speed = player_speed
for i in range(spawner.entity_count):
var new_pos := Vector2(
rng.randf_range(-camera_boundaries.size.x/2, camera_boundaries.size.x/2),
rng.randf_range(-camera_boundaries.size.y/2, camera_boundaries.size.y/2)
)
var entity: KinematicBody2D = spawner.Entity.instance()
entity.global_position = new_pos
entity.player_agent = player.agent
entity.start_speed = max_linear_speed
entity.start_accel = max_linear_accel
spawner.add_child(entity)
func set_behavior_mode(mode: int) -> void:
behavior_mode = mode
if not is_inside_tree():
return
match mode:
Mode.SEEK:
for child in spawner.get_children():
child.use_seek = true
Mode.FLEE:
for child in spawner.get_children():
child.use_seek = false
func set_max_linear_speed(value: float) -> void:
max_linear_speed = value
if not is_inside_tree():
return
for child in spawner.get_children():
child.agent.max_linear_speed = value
func set_max_linear_accel(value: float) -> void:
max_linear_accel = value
if not is_inside_tree():
return
for child in spawner.get_children():
child.agent.max_linear_acceleration = value
func set_player_speed(value: float) -> void:
player_speed = value
if not is_inside_tree():
return
player.speed = player_speed