hero-mark-2/objects/player/player.gd
2022-12-08 23:28:49 -05:00

185 lines
4.6 KiB
GDScript

extends KinematicBody2D
##Children
onready var sprite = $Sprite
onready var climb_ray = $ClimbRay
onready var anims = $AnimationPlayer
onready var sword_sprite = $SwordSprite
onready var sword_hitbox = $SwordArea
##States
enum State {IDLE,WALK,JUMP,FALL,STUNNED,CLIMB,SWORD}
var current_state = State.IDLE
var axis = Vector2.ZERO
##Physics
var velocity = Vector2.ZERO
var walk_speed = 50
var gravity = 12.50
var jump_pressure = 0
var jump_force = 200
var current_ladder = null
##Attacking
func _physics_process(delta):
axis = Vector2(Input.get_axis("ui_left","ui_right"),Input.get_axis("ui_up","ui_down"))
#Check ladder
check_ladder()
match current_state:
State.IDLE:
_process_idle()
continue
State.WALK:
_process_walk()
continue
State.IDLE, State.WALK:
_process_idle_walk()
State.JUMP:
_process_jump()
continue
State.FALL:
_process_fall()
continue
State.CLIMB:
_process_climb()
continue
State.SWORD:
_process_sword()
#Gravity
if current_state != State.CLIMB: velocity.y += gravity
#Apply velocity
move_and_slide(velocity,Vector2.UP)
#Debug
if Input.is_action_pressed("debug_move_player"):
position = get_viewport().get_mouse_position()
print(get_viewport().get_mouse_position())
func _process_idle():
if anims.get_current_animation() != "idle": anims.play("idle")
#Stop
velocity.x = 0
#Goto Walk
if axis.x != 0: current_state = State.WALK
func _process_walk():
if anims.get_current_animation() != "walk": anims.play("walk")
#Move
move(walk_speed,0,true)
#Goto Idle
if axis.x == 0: current_state = State.IDLE
#Push Blocks
for i in get_slide_count():
var collision = get_slide_collision(i)
if collision.get_collider().is_in_group("pushable"):
print(collision.get_collider())
collision.get_collider().push(collision.normal)
func _process_idle_walk():
velocity.y = 0
#Goto Fall
if !is_on_floor(): current_state = State.FALL
#Goto Jump
check_jump()
#Goto Sword
if Input.is_action_just_pressed("sword"):
Game.play_sound(Game.a_sword,Game.ac_jump)
current_state = State.SWORD
return
func _process_jump():
jump_pressure += 1
#Pressure sensitive jump
if jump_pressure == 15 or Input.is_action_just_released("jump"):
velocity.y = -jump_force / 4
#velocity.y = 0
current_state = State.FALL
func _process_fall():
anims.play("jump")
#anims.play("jump")
move(walk_speed,0,true)
#Return to idle
if is_on_floor():
current_state = State.IDLE
return
func _process_climb():
#Graphics
anims.play("climb")
anims.set_speed_scale(abs(axis.y))
#Climb
position.y += axis.y * 0.65
#Sound
if axis.y == -1:
if Game.ac_climb.get_stream() != Game.a_climb_up: Game.play_sound(Game.a_climb_up,Game.ac_climb)
if axis.y == 1:
if Game.ac_climb.get_stream() != Game.a_climb_down: Game.play_sound(Game.a_climb_down,Game.ac_climb)
if axis.y == 0: Game.ac_climb.set_stream(null)
#Manual Jump,, only works when holding neutral or away from ladder
if axis.x != sprite.scale.x: check_jump()
#Auto Jump
if climb_ray.get_collider() == null:
velocity.y = -jump_force
Game.ac_climb.set_stream(null)
Game.play_sound(Game.a_jump,Game.ac_jump)
current_state = State.FALL
return
func _process_sword():
anims.play("stab")
#Stop
velocity.x = 0
sword_sprite.scale.x = sprite.scale.x
#Move hitbox with flip
sword_hitbox.position.x = sprite.scale.x * 10
#Return to idle after animationplayer end anim signal
func check_jump():
if Input.is_action_just_pressed("jump") && !Input.is_action_pressed("ui_down"):
#Detach ladder
if current_state == State.CLIMB:
Game.ac_climb.set_stream(null)
position.x -= sprite.scale.x * 5
anims.set_speed_scale(1)
# Jump
jump_pressure = 0
current_state = State.JUMP
Game.play_sound(Game.a_jump,Game.ac_jump)
anims.play("jump")
velocity.y = -jump_force
move(walk_speed,0,true)
func move(hsp,vsp,flip:bool):
velocity.x = hsp * axis.x
#Flip
if flip: if sign(axis.x) != 0: sprite.scale.x = axis.x
func check_ladder():
if climb_ray.get_collider() != null:
current_ladder = climb_ray.get_collider().get_parent()
#Stop the velocity
velocity = Vector2.ZERO
#Snap to closest side
if position.x < current_ladder.middle:
position.x = current_ladder.left_snap.global_position.x
sprite.scale.x = 1
else:
position.x = current_ladder.right_snap.global_position.x
sprite.scale.x = -1
#Start Climbing
current_state = State.CLIMB
#Move the raycast
climb_ray.position.x = 4 * sprite.scale.x
func die():
get_tree().reload_current_scene()
func _on_AnimationPlayer_animation_finished(anim_name):
#Return to idle after slash
if anim_name == "stab":
current_state = State.IDLE
return