###Godot version
extends CharacterBody2D
class_name Player
var current_state : States = States.JUMPING
enum States{
IDLE,
RUNNING,
JUMPING,
KICKING,
FALLING,
FAST_FALLING,
RUNNINGKICK,
RUNNINGJUMP,
BLOCKING,
DASHING,
CHATTING,
TAKE_DAMAGE
}
@onready var movement_sprite : AnimatedSprite2D = $Idle
@onready var jump_sprite : AnimatedSprite2D = $Jump
@onready var kick_sprite : AnimatedSprite2D = $Kick
@onready var slidingkick_sprite : AnimatedSprite2D = $SlidingKick
@export var speed : float = 100
@export var jump_velocity : float = -260.0
@export var acceleration : int = 5
@export var knockbackX : int = -20
@export var knockbackY : int = -20
var is_facing = Vector2.ZERO
#Coyote Time
@export var jump_buffer_time: int = 15
@export var coyote_time: int = 15
var jump_buffer_counter : int = 0
var coyote_counter: int = 0
var maximum_health = 10
var health = 5
var damaged = false
signal deal_damage
#Dashing
var dashDirection = Input.get_vector("Left", "Right", "Up", "Down")
var canDash = false
var kick_finished = false
var slidingkick_finished = false
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var direction : Vector2 = Vector2.ZERO
#Connection to GameManager to allow death
func _ready():
GameManager.player = self
func revive_player():
health = maximum_health
func facing():
if Input.is_action_pressed("Left"):
var is_facing = Vector2.LEFT
# print(is_facing)
elif Input.is_action_pressed("Right"):
var is_facing = Vector2.RIGHT
# print(is_facing)
func take_damage():
damaged = true
func _physics_process(delta):
print(health)
if is_on_floor() and Input.is_action_just_pressed("kick") and direction.x == 0:
current_state = States.KICKING
if direction.x == 0 and is_on_floor() and not Input.is_action_just_pressed("kick") and not kick_sprite.is_playing():
current_state = States.IDLE
if direction.x != 0 and is_on_floor() and Input.is_action_just_pressed("kick"):
current_state = States.RUNNINGKICK
if direction.x != 0 and is_on_floor() and not Input.is_action_pressed("kick") and not Input.is_action_pressed("Dash"):
current_state = States.RUNNING
if direction.x != 0 and is_on_floor() and Input.is_action_pressed("Dash"):
current_state = States.DASHING
if Input.is_action_just_pressed("Up") and is_on_floor() and velocity.y <= 0 and not Input.is_action_pressed("Dash"):
current_state = States.JUMPING
if Input.is_action_just_pressed("Up") and is_on_floor() and velocity.y <= 0 and Input.is_action_pressed("Dash"):
current_state = States.RUNNINGJUMP
if damaged:
current_state = States.TAKE_DAMAGE
##########################################################################
if current_state == States.TAKE_DAMAGE:
print("TAKE_DAMAGE")
velocity.y = knockbackY * direction.y
velocity.x = knockbackX * direction.x
health -= 1
damaged = false
#################################################################
if current_state == States.DASHING:
print("DASHING")
kick_sprite.hide()
movement_sprite.show()
slidingkick_sprite.hide()
jump_sprite.hide()
if direction.x > 0:
direction.x = 1
if direction.x < 0:
direction.x = -1
velocity.x = direction.x * speed * 2
if direction.x > 0:
movement_sprite.play("Run_R")
if direction.x < 0:
movement_sprite.play("Run_L")
############################################################
if current_state == States.KICKING:
var overlapping_object = $KickAttack.get_overlapping_areas()
for area in overlapping_object:
var parent = area.get_parent()
#emit_signal("deal_damage")
parent.take_damage()
print(parent.name)
print("Kicking")
movement_sprite.hide()
slidingkick_sprite.hide()
jump_sprite.hide()
kick_sprite.show()
kick_sprite.play("kick")
if not kick_finished:
movement_sprite.hide()
if kick_finished:
print("kick_done")
#kick_sprite.hide()
kick_finished = false
###############################################################
if current_state == States.RUNNINGKICK:
direction.x = 0
var overlapping_object = $KickAttack.get_overlapping_areas()
for area in overlapping_object:
var parent = area.get_parent()
#emit_signal("deal_damage")
parent.take_damage()
print(parent.name)
if Input.is_action_pressed("Left"):
$KickAttack.scale.x = abs($KickAttack.scale.x) * -1
$SlidingKick.scale.x = abs($SlidingKick.scale.x) * -1
print("Runningkicking")
movement_sprite.hide()
kick_sprite.hide()
jump_sprite.hide()
slidingkick_sprite.show()
slidingkick_sprite.play("slidingkick")
if not slidingkick_finished:
movement_sprite.hide()
if slidingkick_finished:
print("kick_done")
#kick_sprite.hide()
slidingkick_finished = false
else:
$KickAttack.scale.x = abs($KickAttack.scale.x) * 1
$SlidingKick.scale.x = abs($SlidingKick.scale.x) * 1
print("Runningkicking")
movement_sprite.hide()
kick_sprite.hide()
jump_sprite.hide()
slidingkick_sprite.show()
slidingkick_sprite.play("slidingkick")
if not slidingkick_finished:
movement_sprite.hide()
if slidingkick_finished:
print("kick_done")
#kick_sprite.hide()
slidingkick_finished = false
#####################################################################
if current_state == States.IDLE:
print("Idle")
movement_sprite.play("Idle")
kick_sprite.hide()
movement_sprite.show()
slidingkick_sprite.hide()
jump_sprite.hide()
################################################################
if current_state == States.RUNNING:
kick_sprite.hide()
movement_sprite.show()
slidingkick_sprite.hide()
jump_sprite.hide()
print("RUNNING")
if direction.x > 0:
direction.x = 1
if direction.x < 0:
direction.x = -1
velocity.x = direction.x * speed #* acceleration
if direction.x > 0:
movement_sprite.play("Run_R")
if direction.x < 0:
movement_sprite.play("Run_L")
###################################################################
if current_state == States.JUMPING:
jump_sprite.show()
movement_sprite.hide()
slidingkick_sprite.hide()
kick_sprite.hide()
print("Jumping")
velocity.x = direction.x * speed * 1.1#* acceleration
if is_on_floor():
velocity.y = jump_velocity
if not is_on_floor():
if velocity.y < 1:
if direction.x >= 0:
jump_sprite.play("Jumping(R)")
if direction.x < 0:
jump_sprite.play("Jumping(L)")
if velocity.y > 200:
if direction.x >= 0:
jump_sprite.play("Falling(R)")
if direction.x < 0:
jump_sprite.play("Falling(L)")
if Input.is_action_pressed("Up"):
pass
velocity.y += gravity * delta * 0.7
if velocity.y > 2000:
velocity.y = 2000
#########################################################################
if current_state == States.RUNNINGJUMP:
jump_sprite.show()
movement_sprite.hide()
slidingkick_sprite.hide()
kick_sprite.hide()
print("RunningJumping")
velocity.x = direction.x * speed * 2
if is_on_floor():
velocity.y = jump_velocity
if not is_on_floor():
if velocity.y < 1:
if direction.x >= 0:
jump_sprite.play("Jumping(R)")
if direction.x < 0:
jump_sprite.play("Jumping(L)")
if velocity.y > 200:
if direction.x >= 0:
jump_sprite.play("Falling(R)")
if direction.x < 0:
jump_sprite.play("Falling(L)")
if Input.is_action_pressed("Up"):
pass
velocity.y += gravity * delta * 0.7
if velocity.y > 2000:
velocity.y = 2000
#########################################################################
direction = Input.get_vector("Left", "Right", "Up", "Down")
if not is_on_floor() and current_state != States.JUMPING:# or States.RUNNINGJUMP:
velocity.y += gravity * delta * 0.7
if velocity.y > 2000:
velocity.y = 2000
if is_on_floor():
coyote_counter = coyote_time
if not is_on_floor():
if coyote_counter > 0:
coyote_counter -= 1
if Input.is_action_just_pressed("Up"):
jump_buffer_counter = jump_buffer_time
if jump_buffer_counter > 0:
jump_buffer_counter -= 1
if jump_buffer_counter > 0 and coyote_counter > 0:
velocity.y = jump_velocity
jump_buffer_counter = 0
coyote_counter = 0
else:
velocity.x = move_toward(velocity.x,0,8)
#velocity.x = clamp(velocity.x, -speed, speed)
if Input.is_action_pressed("Die") or health == 0:
die()
move_and_slide()
func die():
GameManager.respawn_player()
func _on_kick_animation_finished():
kick_finished = true
func _on_sliding_kick_animation_finished():
slidingkick_finished = true
Question
Okay I know the code looks like a mess full of various redundancies. My original intent was just to use AnimatedSprite2D until I could figure out AnimationPlayer, as I had seen a comment that said you could pretty easily convert AnimatedSprite2D into animation player, then clean everything up afterwards. I’m just wondering if anyone has any info on how to translate AnimatedSprite2D to AnimationPlayer and still maintain the original effects of the code. The various videos and old posts I’ve found have been either extremely basic or completely unrelated to what I’m trying to do.