2019-12-19 20:04:08 +01:00
|
|
|
extends Node2D
|
2020-01-02 23:42:41 +01:00
|
|
|
# Access helper class for children to access window boundaries.
|
2019-12-19 20:04:08 +01:00
|
|
|
|
2019-12-21 19:07:43 +01:00
|
|
|
|
2020-01-15 20:15:31 +01:00
|
|
|
enum Mode { FLEE, SEEK }
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
export(Mode) var behavior_mode := Mode.SEEK setget set_behavior_mode
|
2020-01-22 17:55:49 +01:00
|
|
|
export(float, 0, 2000, 40) var linear_speed_max := 200.0 setget set_linear_speed_max
|
|
|
|
export(float, 0, 500, 0.5) var linear_accel_max := 10.0 setget set_linear_accel_max
|
2020-01-16 16:04:27 +01:00
|
|
|
export(float) var player_speed := 600.0 setget set_player_speed
|
2020-01-16 09:44:44 +01:00
|
|
|
|
|
|
|
var camera_boundaries: Rect2
|
2020-01-15 20:15:31 +01:00
|
|
|
|
2019-12-19 20:04:08 +01:00
|
|
|
onready var player: KinematicBody2D = $Player
|
2019-12-21 19:07:43 +01:00
|
|
|
onready var spawner: Node2D = $Spawner
|
2019-12-19 20:04:08 +01:00
|
|
|
|
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
func _ready() -> void:
|
2019-12-19 20:04:08 +01:00
|
|
|
camera_boundaries = Rect2(
|
|
|
|
Vector2.ZERO,
|
|
|
|
Vector2(
|
|
|
|
ProjectSettings["display/window/size/width"],
|
|
|
|
ProjectSettings["display/window/size/height"]
|
|
|
|
)
|
|
|
|
)
|
2020-01-16 09:44:44 +01:00
|
|
|
|
|
|
|
var rng := RandomNumberGenerator.new()
|
2019-12-19 20:04:08 +01:00
|
|
|
rng.randomize()
|
|
|
|
|
2020-01-16 16:04:27 +01:00
|
|
|
player.speed = player_speed
|
|
|
|
|
2019-12-21 19:07:43 +01:00
|
|
|
for i in range(spawner.entity_count):
|
2020-01-16 09:44:44 +01:00
|
|
|
var new_pos := Vector2(
|
2019-12-21 19:07:43 +01:00
|
|
|
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
|
2020-01-22 17:55:49 +01:00
|
|
|
entity.start_speed = linear_speed_max
|
|
|
|
entity.start_accel = linear_accel_max
|
2020-01-27 17:39:06 +01:00
|
|
|
entity.use_seek = behavior_mode == Mode.SEEK
|
2019-12-21 19:07:43 +01:00
|
|
|
spawner.add_child(entity)
|
2020-01-15 20:15:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
func set_behavior_mode(mode: int) -> void:
|
2020-01-16 18:31:02 +01:00
|
|
|
behavior_mode = mode
|
2020-01-16 09:44:44 +01:00
|
|
|
if not is_inside_tree():
|
|
|
|
return
|
2020-01-15 20:15:31 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
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
|
2020-01-15 20:15:31 +01:00
|
|
|
|
|
|
|
|
2020-01-22 17:55:49 +01:00
|
|
|
func set_linear_speed_max(value: float) -> void:
|
|
|
|
linear_speed_max = value
|
2020-01-16 09:44:44 +01:00
|
|
|
if not is_inside_tree():
|
|
|
|
return
|
2020-01-15 20:15:31 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
for child in spawner.get_children():
|
2020-01-22 17:55:49 +01:00
|
|
|
child.agent.linear_speed_max = value
|
2020-01-15 20:15:31 +01:00
|
|
|
|
|
|
|
|
2020-01-22 17:55:49 +01:00
|
|
|
func set_linear_accel_max(value: float) -> void:
|
|
|
|
linear_accel_max = value
|
2020-01-16 09:44:44 +01:00
|
|
|
if not is_inside_tree():
|
|
|
|
return
|
2020-01-15 20:15:31 +01:00
|
|
|
|
2020-01-16 09:44:44 +01:00
|
|
|
for child in spawner.get_children():
|
2020-01-22 17:55:49 +01:00
|
|
|
child.agent.linear_acceleration_max = value
|
2020-01-16 16:04:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
func set_player_speed(value: float) -> void:
|
2020-01-16 18:31:02 +01:00
|
|
|
player_speed = value
|
2020-01-16 16:04:27 +01:00
|
|
|
if not is_inside_tree():
|
|
|
|
return
|
|
|
|
|
|
|
|
player.speed = player_speed
|