Megalomaniak the code may be messed up, but i'm willing to learn and understand. Thank you.
extends KinematicBody2D
const GRAVITY = 15
const SPEED = 40
const CHASE_SPEED = 60
var left = true
var motion = Vector2.ZERO
var is_attacking := false
var can_move := true
var is_chasing := false
var attack_name = 'combo0'
onready var player = get_node('/root/World/Player')
func _ready():
$AnimatedSprite.play('walk')
func _process(delta):
if is_attacking: return
chase_player()
detect_floor()
if can_move and !is_chasing:
move_character()
if motion.x == 0:$AnimatedSprite.play('idle')
else:$AnimatedSprite.play('walk')
if is_chasing:
if motion.x == 0:$AnimatedSprite.play('idle')
else:$AnimatedSprite.play('walk')
func move_character():
motion.x = -SPEED if left else SPEED
motion.y += GRAVITY
motion = move_and_slide(motion,Vector2.UP)
func detect_floor():
if (not $HoleChecker.is_colliding() and is_on_floor() and !is_chasing):
left = !left
scale.x = -scale.x
$HoleChecker.enabled = true
if is_chasing and motion.x !=0:
if motion.x > 0:
$AnimatedSprite.flip_h = true
$Vision.rotation_degrees = 90
$PlayerChecker.rotation_degrees = 0
else:
$AnimatedSprite.flip_h = false
$Vision.rotation_degrees = -90
$PlayerChecker.rotation_degrees = 180
func chase_player():
var direction = (player.position - position).normalized()
if $Vision.is_colliding() and is_on_floor():
is_chasing = true
if is_chasing:
motion.x = direction.x * CHASE_SPEED
motion.y += GRAVITY
motion = move_and_slide(motion,Vector2.UP)
if abs(direction.x) > 0.9998:
is_chasing = false
func attack(attack_name,delay):
can_move = false
$AttackTimer.set_wait_time(delay)
$AttackTimer.start()
$AnimatedSprite.play(attack_name)
is_attacking = true
func _on_PlayerChecker_body_entered(body):
attack(attack_name,1.1)
func _on_PlayerChecker_body_exited(body):
is_attacking = false
attack_name = 'combo0'
func _on_AttackTimer_timeout():
if attack_name == 'combo0' and is_attacking:
attack_name = 'combo1'
attack(attack_name,1.6)
elif is_attacking:
attack_name = 'combo0'
attack(attack_name,1.1)
func _on_AnimatedSprite_animation_finished():
can_move = true