Untitled
unknown
plain_text
a year ago
2.4 kB
7
Indexable
extends CharacterBody2D
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var right_leg: RayCast2D = $RayCasts/RightLeg
@onready var left_leg: RayCast2D = $RayCasts/LeftLeg
@onready var left_side: RayCast2D = $RayCasts/LeftSide
@onready var right_side: RayCast2D = $RayCasts/RightSide
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var collision_shape_2d: CollisionShape2D = $HitBoxArea/CollisionShape2D
var health: int = 3
var speed: int = 75
var direction: int = 1
var dead: bool = false
var taking_damage: bool = false
var player_chase: bool = false
var facing_right: bool = false
var facing_wall: bool = false
var player = null
func _physics_process(delta) -> void:
if dead:
return
if !right_leg.is_colliding() or right_side.is_colliding():
direction = -1
elif !left_leg.is_colliding() or left_side.is_colliding():
direction = 1
if player_chase:
if is_on_wall():
facing_wall = true
if (player.global_position.x < global_position.x) and !facing_wall:
direction = -1
elif (player.global_position.x > global_position.x) and !facing_wall:
direction = 1
if abs(player.global_position.x - global_position.x) <= 30 and !facing_wall:
if is_on_wall():
return
direction = 0
animation_player.play("attack")
position.x += direction * speed * delta
if direction <= -1:
animation_player.play("walk")
sprite_2d.flip_h = true
sprite_2d.offset.x = 8.585
facing_right = false
update_collision_area_attack()
elif direction >= 1:
animation_player.play("walk")
sprite_2d.flip_h = false
sprite_2d.offset.x = 0
facing_right = true
update_collision_area_attack()
elif direction == 0 and is_on_floor():
animation_player.play("idle")
func take_damage() -> void:
health -= 1
if health <= 0:
dead = true
direction = 0
animation_player.play("death")
func _on_detection_area_body_entered(body) -> void:
player = body
player_chase = true
func _on_detection_area_body_exited(_body) -> void:
player = null
player_chase = false
func _on_hit_box_area_body_entered(body) -> void:
if body.has_method("take_damage"):
body.take_damage()
func update_collision_area_attack() -> void:
if(facing_right):
collision_shape_2d.position.x = 42.5
else:
collision_shape_2d.position.x = -42.5
collision_shape_2d.disabled = true
Editor is loading...
Leave a Comment