Basic Bat enemy AI
This commit is contained in:
parent
656c3278a4
commit
23a693d4a9
@ -47,12 +47,12 @@ region = Rect2( 288, 0, 32, 32 )
|
||||
animations = [ {
|
||||
"frames": [ SubResource( 1 ), SubResource( 2 ), SubResource( 3 ), SubResource( 4 ), SubResource( 5 ), SubResource( 6 ), SubResource( 7 ), SubResource( 8 ), SubResource( 9 ), SubResource( 10 ) ],
|
||||
"loop": true,
|
||||
"name": "Animation",
|
||||
"name": "Animate",
|
||||
"speed": 15.0
|
||||
} ]
|
||||
|
||||
[node name="EnemyDeathEffect" type="AnimatedSprite"]
|
||||
frames = SubResource( 11 )
|
||||
animation = "Animation"
|
||||
animation = "Animate"
|
||||
offset = Vector2( 0, -8 )
|
||||
script = ExtResource( 1 )
|
||||
|
26
ActionRPG-HeartBeast/Effects/HitEffect.tscn
Normal file
26
ActionRPG-HeartBeast/Effects/HitEffect.tscn
Normal file
@ -0,0 +1,26 @@
|
||||
[gd_scene load_steps=6 format=2]
|
||||
|
||||
[ext_resource path="res://Effects/Effect.gd" type="Script" id=1]
|
||||
[ext_resource path="res://Effects/HitEffect.png" type="Texture" id=2]
|
||||
|
||||
[sub_resource type="AtlasTexture" id=1]
|
||||
atlas = ExtResource( 2 )
|
||||
region = Rect2( 0, 0, 24, 24 )
|
||||
|
||||
[sub_resource type="AtlasTexture" id=2]
|
||||
atlas = ExtResource( 2 )
|
||||
region = Rect2( 24, 0, 24, 24 )
|
||||
|
||||
[sub_resource type="SpriteFrames" id=3]
|
||||
animations = [ {
|
||||
"frames": [ SubResource( 1 ), SubResource( 2 ) ],
|
||||
"loop": true,
|
||||
"name": "Animate",
|
||||
"speed": 10.0
|
||||
} ]
|
||||
|
||||
[node name="HitEffect" type="AnimatedSprite"]
|
||||
frames = SubResource( 3 )
|
||||
animation = "Animate"
|
||||
offset = Vector2( 0, -8 )
|
||||
script = ExtResource( 1 )
|
@ -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
|
||||
|
@ -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"]
|
||||
|
13
ActionRPG-HeartBeast/Enemies/PlayerDetectionZone.gd
Normal file
13
ActionRPG-HeartBeast/Enemies/PlayerDetectionZone.gd
Normal 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
|
13
ActionRPG-HeartBeast/Enemies/PlayerDetectionZone.tscn
Normal file
13
ActionRPG-HeartBeast/Enemies/PlayerDetectionZone.tscn
Normal 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"]
|
22
ActionRPG-HeartBeast/Overlap/Hurtbox.gd
Normal file
22
ActionRPG-HeartBeast/Overlap/Hurtbox.gd
Normal file
@ -0,0 +1,22 @@
|
||||
extends Area2D
|
||||
|
||||
export(bool) var show_hit = true
|
||||
|
||||
const HitEffect = preload("res://Effects/HitEffect.tscn")
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
pass # Replace with function body.
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
#func _process(delta):
|
||||
# pass
|
||||
|
||||
|
||||
func _on_Hurtbox_area_entered(area):
|
||||
if show_hit:
|
||||
var effect = HitEffect.instance()
|
||||
var main = get_tree().current_scene
|
||||
main.add_child(effect)
|
||||
effect.global_position = global_position
|
@ -1,7 +1,12 @@
|
||||
[gd_scene format=2]
|
||||
[gd_scene load_steps=2 format=2]
|
||||
|
||||
[ext_resource path="res://Overlap/Hurtbox.gd" type="Script" id=1]
|
||||
|
||||
[node name="Hurtbox" type="Area2D"]
|
||||
collision_layer = 0
|
||||
collision_mask = 0
|
||||
script = ExtResource( 1 )
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
|
||||
[connection signal="area_entered" from="." to="." method="_on_Hurtbox_area_entered"]
|
||||
|
@ -17,6 +17,7 @@ offset = Vector2( -8, -8 )
|
||||
|
||||
[node name="Hurtbox" parent="." instance=ExtResource( 3 )]
|
||||
collision_layer = 8
|
||||
show_hit = false
|
||||
|
||||
[node name="CollisionShape2D" parent="Hurtbox" index="0"]
|
||||
position = Vector2( 8, 8 )
|
||||
|
Loading…
Reference in New Issue
Block a user