add blightwing's behavior

This commit is contained in:
Chris Cromer 2022-08-04 00:59:10 -04:00
parent 89f1ac5303
commit a72954b578
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
2 changed files with 90 additions and 20 deletions

View File

@ -1,27 +1,82 @@
extends KinematicBody2D
# Declare member variables here. Examples:
# var a: int = 2
# var b: String = "text"
signal player_touched
export var direction = -1
export var speed = 50
export var follow_path = false
var target: Vector2 = Vector2(0, 0)
var target_id = 0
var start_position = Vector2()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
start_position = position
if follow_path:
if $Path.get_child_count() == 0:
follow_path = false
else:
target = $Path.get_child(0).position
if direction == 1:
$AnimatedSprite.flip_h = true
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
var velocity = Vector2()
velocity.x += 1 * 50
#velocity.y -= 1 * 50
velocity = move_and_slide(velocity, Vector2(0, 0), true, 2)
for i in get_slide_count():
var collision = get_slide_collision(i)
#if collision.collider.name == "Player":
# print("Blightwing: " + String(OS.get_unix_time()) + " " + collision.collider.name)
if $LeftWallChecker.is_colliding():
wall_checker_collided($LeftWallChecker)
elif $RightWallChecker.is_colliding():
wall_checker_collided($RightWallChecker)
if not follow_path:
var target_position = position
target_position.x *= 2 * direction
position = position.move_toward(target_position, round(speed * delta))
var velocity = get_velocity_towards_target(delta)
var collision = move_and_collide(velocity, true, true, true)
if collision and collision.collider.name != "Player":
var _collision = move_and_collide(velocity)
else:
var velocity = get_velocity_towards_target(delta)
var collision = move_and_collide(velocity, true, true, true)
if collision and collision.collider.name != "Player":
var _collision = move_and_collide(velocity)
else:
position = position.move_toward(start_position + target, round(speed * delta))
if position == start_position + target:
if $Path.get_child_count() - 1 == target_id:
target_id = -1
target = $Path.position
else:
target_id += 1
target = $Path.get_child(target_id).position
if start_position.x + target.x > position.x:
$AnimatedSprite.flip_h = true
elif start_position.x + target.x < position.x:
$AnimatedSprite.flip_h = false
func _on_Area2D_body_entered(body: Node) -> void:
print(body.name)
func get_velocity_towards_target(delta: float) -> Vector2:
var velocity = Vector2(0, 0)
if start_position.x + target.x > position.x:
velocity.x = speed * delta
elif start_position.x + target.x < position.x:
velocity.x = -speed * delta
if start_position.y + target.y > position.y:
velocity.y = speed * delta
elif start_position.y + target.y < position.y:
velocity.y = -speed * delta
return velocity
func wall_checker_collided(wall_checker: RayCast2D) -> void:
if wall_checker.get_collider().name == "Player":
emit_signal("player_touched")
direction *= -1
$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=7 format=2]
[gd_scene load_steps=8 format=2]
[ext_resource path="res://assets/characters/characters.png" type="Texture" id=1]
[ext_resource path="res://characters/enemies/blightwing/Blightwing.gd" type="Script" id=2]
[sub_resource type="AtlasTexture" id=1]
atlas = ExtResource( 1 )
@ -23,18 +24,32 @@ animations = [ {
} ]
[sub_resource type="RectangleShape2D" id=5]
extents = Vector2( 5, 5 )
extents = Vector2( 4.5, 5 )
[node name="Blightwing" type="KinematicBody2D" groups=["enemy"]]
[node name="Blightwing" type="KinematicBody2D" groups=["enemy", "rideable"]]
collision_layer = 8
collision_mask = 11
script = ExtResource( 2 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
frames = SubResource( 4 )
animation = "fly"
frame = 3
playing = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 3.47893e-06, 0.999998 )
position = Vector2( 1.66894e-06, 1.5 )
rotation = 1.5708
shape = SubResource( 5 )
[node name="LeftWallChecker" type="RayCast2D" parent="."]
rotation = 1.5708
enabled = true
cast_to = Vector2( 0, 10 )
collision_mask = 11
[node name="RightWallChecker" type="RayCast2D" parent="."]
rotation = -1.5708
enabled = true
cast_to = Vector2( 0, 10 )
collision_mask = 11