141 lines
4.0 KiB
GDScript
141 lines
4.0 KiB
GDScript
class_name Enemy extends KinematicBody2D
|
|
|
|
enum STATE {IDLE, WANDER, JUMP, FALL, HURT, ATTACK}
|
|
enum STATE_SECONDARY {NONE, SHOOT}
|
|
|
|
#const WALK_FORCE = 600
|
|
const WALK_MAX_SPEED = (30*2)
|
|
#const STOP_FORCE = 1300
|
|
const JUMP_SPEED = 270
|
|
|
|
var velocity = Vector2()
|
|
|
|
var gravity = 60*9.8
|
|
var player_state = STATE.IDLE
|
|
var player_last_state = null
|
|
var animation_play_finished = true
|
|
var flip_direction = false
|
|
|
|
onready var _animated_sprite = $AnimatedSprite
|
|
|
|
|
|
func _physics_process(delta):
|
|
# Horizontal movement
|
|
if player_state != STATE.WANDER and $IdleTimeout.time_left == 0:
|
|
if flip_direction:
|
|
velocity.x = (1 * WALK_MAX_SPEED)
|
|
flip_direction = false
|
|
else:
|
|
velocity.x = (-1 * WALK_MAX_SPEED)
|
|
flip_direction = true
|
|
|
|
# Vertical movement code. Apply gravity.
|
|
velocity.y += gravity * delta
|
|
|
|
# Move based on the velocity and snap to the ground.
|
|
#velocity = move_and_slide_with_snap(velocity, Vector2.DOWN, Vector2.UP)
|
|
velocity = move_and_slide(velocity, Vector2(0, -1))
|
|
|
|
# Animate based on last state and velocity
|
|
if velocity.x > 0 and _animated_sprite.flip_h != true:
|
|
_animated_sprite.flip_h = true
|
|
_animated_sprite.offset.x = 0
|
|
$PlayerDetection.cast_to = Vector2(20,0)
|
|
if velocity.x < 0 and _animated_sprite.flip_h != false:
|
|
_animated_sprite.flip_h = false
|
|
_animated_sprite.offset.x = -18
|
|
$PlayerDetection.cast_to = Vector2(-20,0)
|
|
|
|
# forced state changes like falling an gitting hurt
|
|
if velocity.y > 0 and player_state != STATE.JUMP and player_state != STATE.FALL:
|
|
print("STATE_TRANSITION: Fall")
|
|
player_state = STATE.FALL
|
|
|
|
if $PlayerDetection.is_colliding():
|
|
player_state = STATE.ATTACK
|
|
player_last_state = null
|
|
velocity.x = 0
|
|
|
|
# Each state should trigger its own animation
|
|
match player_state:
|
|
STATE.IDLE:
|
|
# Transitions
|
|
if velocity.y == -JUMP_SPEED:
|
|
print("STATE_TRANSITION: Jump")
|
|
player_last_state = STATE.IDLE
|
|
player_state = STATE.JUMP
|
|
elif velocity.x != 0:
|
|
print("STATE_TRANSITION: Wander")
|
|
player_last_state = STATE.IDLE
|
|
player_state = STATE.WANDER
|
|
# Animation
|
|
if player_last_state != STATE.IDLE:
|
|
_animated_sprite.play("idle")
|
|
player_last_state = STATE.IDLE
|
|
$IdleTimeout.start()
|
|
|
|
STATE.JUMP:
|
|
# Transitions
|
|
if is_on_floor():
|
|
print("STATE_TRANSITION: Idle")
|
|
_animated_sprite.play("idle")
|
|
player_last_state = STATE.JUMP
|
|
player_state = STATE.IDLE
|
|
# Animation
|
|
if player_last_state != STATE.JUMP:
|
|
_animated_sprite.play("jump")
|
|
player_last_state = STATE.JUMP
|
|
|
|
STATE.FALL:
|
|
# Transitions
|
|
if is_on_floor():
|
|
print("STATE_TRANSITION: Idle")
|
|
_animated_sprite.play("idle")
|
|
player_last_state = STATE.FALL
|
|
player_state = STATE.IDLE
|
|
# Animation
|
|
if player_last_state != STATE.FALL:
|
|
#_animated_sprite.animation = "jump"
|
|
_animated_sprite.frame = 8
|
|
_animated_sprite.stop()
|
|
player_last_state = STATE.FALL
|
|
|
|
STATE.WANDER:
|
|
# print (_animated_sprite.animation ) # apparentl playing doesn't stop when loop is disabled.
|
|
# Transitions
|
|
if velocity.y == -JUMP_SPEED:
|
|
print("STATE_TRANSITION: Jump")
|
|
player_last_state = STATE.WANDER
|
|
player_state = STATE.JUMP
|
|
elif velocity.x == 0:
|
|
print("STATE_TRANSITION: Idle")
|
|
player_last_state = STATE.WANDER
|
|
player_state = STATE.IDLE
|
|
# Animation
|
|
if player_last_state != STATE.WANDER:
|
|
$IdleTimeout.start()
|
|
_animated_sprite.play("walk")
|
|
player_last_state = STATE.WANDER
|
|
if $IdleTimeout.time_left == 0:
|
|
$IdleTimeout.start()
|
|
velocity.x = 0
|
|
player_last_state = STATE.WANDER
|
|
player_state = STATE.IDLE
|
|
|
|
STATE.ATTACK:
|
|
# Animation
|
|
if player_last_state != STATE.ATTACK:
|
|
$IdleTimeout.start()
|
|
_animated_sprite.play("attack")
|
|
player_last_state = STATE.ATTACK
|
|
if $IdleTimeout.time_left == 0:
|
|
$IdleTimeout.start()
|
|
velocity.x = 0
|
|
player_last_state = STATE.ATTACK
|
|
player_state = STATE.IDLE
|
|
|
|
|
|
func _on_AnimatedSprite_animation_finished():
|
|
animation_play_finished = true
|
|
#print($AnimatedSprite.animation," anim over ", $IdleTimeout.time_left, animation_play_finished)
|