Basic Bat enemy AI

This commit is contained in:
Alan Youngblood
2024-11-19 17:04:41 -05:00
parent 656c3278a4
commit 23a693d4a9
9 changed files with 135 additions and 6 deletions

View File

@ -2,13 +2,51 @@ extends KinematicBody2D
const EnemyDeathEffect = preload("res://Effects/EnemyDeathEffect.tscn")
var knockback = Vector2.ZERO
export var ACCELERATION = 300
export var MAX_SPEED = 50
export var FRICTION = 200
enum {
IDLE,
WANDER,
CHASE
}
var knockback = Vector2.ZERO
var velocity = Vector2.ZERO
var state = IDLE
onready var sprite = $AnimatedSprite
onready var stats = $Stats
onready var playerDetectionZone = $PlayerDetectionZone
func _physics_process(delta):
knockback = knockback.move_toward(Vector2.ZERO, 200 * delta)
knockback = move_and_slide(knockback)
match state:
IDLE:
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
seek_player()
WANDER:
pass
CHASE:
var player = playerDetectionZone.player
if player != null:
var direction = (player.global_position - global_position).normalized()
velocity = velocity.move_toward(direction * MAX_SPEED, ACCELERATION * delta)
else:
state = IDLE
sprite.flip_h = velocity.x < 0
velocity = move_and_slide(velocity)
func seek_player():
if playerDetectionZone.can_see_player():
state = CHASE
func _on_Hurtbox_area_entered(area):
stats.health -= area.damage

View File

@ -1,10 +1,11 @@
[gd_scene load_steps=14 format=2]
[gd_scene load_steps=16 format=2]
[ext_resource path="res://Enemies/Bat.png" type="Texture" id=1]
[ext_resource path="res://Shadows/SmallShadow.png" type="Texture" id=2]
[ext_resource path="res://Overlap/Hurtbox.tscn" type="PackedScene" id=3]
[ext_resource path="res://Enemies/Bat.gd" type="Script" id=4]
[ext_resource path="res://Stats.tscn" type="PackedScene" id=5]
[ext_resource path="res://Enemies/PlayerDetectionZone.tscn" type="PackedScene" id=6]
[sub_resource type="AtlasTexture" id=1]
atlas = ExtResource( 1 )
@ -41,6 +42,9 @@ radius = 4.12311
radius = 7.0
height = 6.0
[sub_resource type="CircleShape2D" id=9]
radius = 61.0328
[node name="Bat" type="KinematicBody2D"]
collision_layer = 16
script = ExtResource( 4 )
@ -48,7 +52,7 @@ script = ExtResource( 4 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 6 )
animation = "Fly"
frame = 4
frame = 1
playing = true
offset = Vector2( 0, -12 )
@ -68,7 +72,14 @@ shape = SubResource( 8 )
[node name="Stats" parent="." instance=ExtResource( 5 )]
max_health = 2
[node name="PlayerDetectionZone" parent="." instance=ExtResource( 6 )]
[node name="CollisionShape2D" parent="PlayerDetectionZone" index="0"]
modulate = Color( 1, 1, 1, 0.258824 )
shape = SubResource( 9 )
[connection signal="area_entered" from="Hurtbox" to="." method="_on_Hurtbox_area_entered"]
[connection signal="no_health" from="Stats" to="." method="_on_Stats_no_health"]
[editable path="Hurtbox"]
[editable path="PlayerDetectionZone"]

View File

@ -0,0 +1,13 @@
extends Area2D
var player = null
# Called when the node enters the scene tree for the first time.
func can_see_player():
return player != null
func _on_PlayerDetectionZone_body_entered(body):
player = body
func _on_PlayerDetectionZone_body_exited(body):
player = null

View File

@ -0,0 +1,13 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Enemies/PlayerDetectionZone.gd" type="Script" id=1]
[node name="PlayerDetectionZone" type="Area2D"]
collision_layer = 0
collision_mask = 2
script = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
[connection signal="body_entered" from="." to="." method="_on_PlayerDetectionZone_body_entered"]
[connection signal="body_exited" from="." to="." method="_on_PlayerDetectionZone_body_exited"]