-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
171 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
[gd_scene load_steps=7 format=3 uid="uid://cenq1bawfywu8"] | ||
|
||
[ext_resource type="Script" path="res://scripts/actor/bots/BotInput.gd" id="1_qo7hy"] | ||
[ext_resource type="Script" path="res://scripts/actor/bots/NavigationVisualizer2D.gd" id="2_2yt0t"] | ||
[ext_resource type="Script" path="res://scripts/actor/bots/ProximityAttack.gd" id="3_4n00q"] | ||
[ext_resource type="Script" path="res://scripts/actor/bots/TargetFinder.gd" id="4_y3qb6"] | ||
[ext_resource type="Script" path="res://scripts/actor/bots/TargetDirection.gd" id="5_ekfo4"] | ||
|
||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ixqjj"] | ||
size = Vector2(1000, 500) | ||
|
||
[node name="EnemyAi" type="Node2D"] | ||
script = ExtResource("1_qo7hy") | ||
|
||
[node name="Area2D" type="Area2D" parent="."] | ||
collision_layer = 0 | ||
collision_mask = 2 | ||
monitorable = false | ||
|
||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] | ||
shape = SubResource("RectangleShape2D_ixqjj") | ||
debug_color = Color(0, 0.6, 0.701961, 0) | ||
|
||
[node name="RayCast2D" type="RayCast2D" parent="."] | ||
enabled = false | ||
|
||
[node name="NavigationAgent2D" type="NavigationAgent2D" parent="."] | ||
path_desired_distance = 5.0 | ||
radius = 16.0 | ||
|
||
[node name="NavigationVisualizer" type="Line2D" parent="NavigationAgent2D" node_paths=PackedStringArray("navigation_agent")] | ||
width = 1.0 | ||
script = ExtResource("2_2yt0t") | ||
navigation_agent = NodePath("..") | ||
|
||
[node name="TargetFinder" type="Node" parent="." node_paths=PackedStringArray("area", "bot_input")] | ||
script = ExtResource("4_y3qb6") | ||
area = NodePath("../Area2D") | ||
bot_input = NodePath("..") | ||
|
||
[node name="ProximityAttack" type="Node" parent="." node_paths=PackedStringArray("target_finder", "bot_input")] | ||
script = ExtResource("3_4n00q") | ||
target_finder = NodePath("../TargetFinder") | ||
bot_input = NodePath("..") | ||
|
||
[node name="TargetDirection" type="Node" parent="." node_paths=PackedStringArray("target_finder", "bot_input", "raycast", "navigation_agent")] | ||
script = ExtResource("5_ekfo4") | ||
target_finder = NodePath("../TargetFinder") | ||
bot_input = NodePath("..") | ||
raycast = NodePath("../RayCast2D") | ||
navigation_agent = NodePath("../NavigationAgent2D") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class_name BotInput | ||
extends Node2D | ||
|
||
signal input_update | ||
|
||
@export var enabled:bool = true | ||
## Commands the movement | ||
@export var mover:MoverTopDown2D | ||
|
||
var axis_compensation:Vector2 # top down movement can use different speed for X&Y axis | ||
|
||
## Not using automatic setter functions because they are called before _ready during initialization | ||
func _ready()->void: | ||
# Set to run before mover | ||
process_physics_priority -= 1 | ||
axis_compensation = Vector2.ONE/mover.axis_multiplier | ||
set_enabled(enabled) | ||
|
||
## Toggle processing for animation state machine | ||
func set_enabled(value:bool)->void: | ||
enabled = value | ||
set_physics_process(enabled) | ||
if !enabled: | ||
mover.input_resource.axis = Vector2.ZERO | ||
|
||
## Inputs need to be manipulated here | ||
## Modules use this to time their functions | ||
func _physics_process(_delta:float)->void: | ||
input_update.emit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class_name ProximityAttack | ||
extends Node | ||
|
||
@export var attack_range:float = 16.0 | ||
@export var target_finder:TargetFinder | ||
@export var bot_input:BotInput | ||
|
||
func _ready()->void: | ||
target_finder.target_update.connect(on_target_update) | ||
|
||
func on_target_update()->void: | ||
if target_finder.closest == null: | ||
bot_input.mover.input_resource.set_action(false) | ||
return | ||
var distance:float = (target_finder.closest.global_position - bot_input.global_position).length() | ||
bot_input.mover.input_resource.set_action(distance <= attack_range) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class_name TargetDirection | ||
extends Node | ||
|
||
@export var target_finder:TargetFinder | ||
@export var bot_input:BotInput | ||
@export var attack_distance:float = 16.0 | ||
@export var raycast:RayCast2D | ||
@export var navigation_agent:NavigationAgent2D | ||
|
||
var detected: = false | ||
var last_target_position:Vector2 | ||
var local_direction:Vector2 | ||
|
||
func _ready()->void: | ||
target_finder.target_update.connect(on_target_update) | ||
|
||
|
||
func on_target_update()->void: | ||
if target_finder.closest == null: | ||
return | ||
local_direction = target_finder.closest.global_position - bot_input.global_position | ||
if local_direction.length() > attack_distance && line_of_sight(): | ||
bot_input.mover.input_resource.set_axis((local_direction * bot_input.axis_compensation).normalized()) | ||
bot_input.mover.input_resource.set_aim_direction(bot_input.mover.input_resource.axis) | ||
return | ||
navigation_update() | ||
|
||
## Raycast checks if anything from environment is in the way | ||
func line_of_sight()->bool: | ||
raycast.target_position = local_direction | ||
raycast.force_raycast_update() | ||
return !raycast.is_colliding() | ||
|
||
func navigation_update()->void: | ||
if (navigation_agent.target_position - bot_input.global_position).length() > 16.0: | ||
navigation_agent.target_position = target_finder.closest.global_position | ||
var point:Vector2 = navigation_agent.get_next_path_position() | ||
var direction:Vector2 = (point - bot_input.global_position).normalized() | ||
bot_input.mover.input_resource.set_axis((direction * bot_input.axis_compensation).normalized()) | ||
bot_input.mover.input_resource.set_aim_direction(bot_input.mover.input_resource.axis) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class_name TargetFinder | ||
extends Node | ||
|
||
signal target_update | ||
|
||
@export var area:Area2D | ||
@export var bot_input:BotInput | ||
|
||
var target_list:Array[Node2D] | ||
var closest:Node2D | ||
|
||
func _ready()->void: | ||
bot_input.input_update.connect(on_input_update) | ||
|
||
func on_input_update()->void: | ||
target_list = area.get_overlapping_bodies() | ||
closest = GameMath.get_closest_node_2d(bot_input.global_position, target_list) | ||
|
||
target_update.emit() |