Advertisement
Guest User

Code

a guest
Aug 14th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. const PlayerHurtSound = preload("res://Player/PlayerHurtSound.tscn")
  4.  
  5. export var ACCELERATION = 500
  6. export var MAX_SPEED = 80
  7. export var ROLL_SPEED = 120
  8. export var FRICTION = 500
  9.  
  10. enum {
  11.     MOVE,
  12.     ROLL,
  13.     ATTACK
  14. }
  15.  
  16. var state = MOVE
  17. var velocity = Vector2.ZERO
  18. var roll_vector = Vector2.DOWN
  19. var stats = PlayerStats
  20.  
  21. onready var animationPlayer = $AnimationPlayer
  22. onready var animationTree = $AnimationTree
  23. onready var animationState = animationTree.get("parameters/playback")
  24. onready var swordHitbox = $HitboxPivot/SwordHitbox
  25. onready var hurtbox = $Hurtbox
  26. onready var blinkAnimationPlayer = $BlinkAnimationPlayer
  27.  
  28. func _ready():
  29.     randomize()
  30.     stats.connect("no_health", self, "queue_free")
  31.     animationTree.active = true
  32.     swordHitbox.knockback_vector = roll_vector
  33.  
  34. func _physics_process(delta):
  35.     match state:
  36.         MOVE:
  37.             move_state(delta)
  38.        
  39.         ROLL:
  40.             roll_state()
  41.        
  42.         ATTACK:
  43.             attack_state()
  44.    
  45. func move_state(delta):
  46.     var input_vector = Vector2.ZERO
  47.     input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
  48.     input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
  49.     input_vector = input_vector.normalized()
  50.    
  51.     if input_vector != Vector2.ZERO:
  52.         roll_vector = input_vector
  53.         swordHitbox.knockback_vector = input_vector
  54.         animationTree.set("parameters/Idle/blend_position", input_vector)
  55.         animationTree.set("parameters/Run/blend_position", input_vector)
  56.         animationTree.set("parameters/Attack/blend_position", input_vector)
  57.         animationTree.set("parameters/Roll/blend_position", input_vector)
  58.         animationState.travel("Run")
  59.         velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
  60.     else:
  61.         animationState.travel("Idle")
  62.         velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
  63.    
  64.     move()
  65.    
  66.     if Input.is_action_just_pressed("roll"):
  67.         state = ROLL
  68.    
  69.     if Input.is_action_just_pressed("attack"):
  70.         state = ATTACK
  71.  
  72. func roll_state():
  73.     velocity = roll_vector * ROLL_SPEED
  74.     animationState.travel("Roll")
  75.     move()
  76.  
  77. func attack_state():
  78.     velocity = Vector2.ZERO
  79.     animationState.travel("Attack")
  80.  
  81. func move():
  82.     velocity = move_and_slide(velocity)
  83.  
  84. func roll_animation_finished():
  85.     velocity = velocity * 0.8
  86.     state = MOVE
  87.  
  88. func attack_animation_finished():
  89.     state = MOVE
  90.  
  91. func _on_Hurtbox_area_entered(area):
  92.     stats.health -= area.damage
  93.     hurtbox.start_invincibility(0.6)
  94.     hurtbox.create_hit_effect()
  95.     var playerHurtSound = PlayerHurtSound.instance()
  96.     get_tree().current_scene.add_child(playerHurtSound)
  97.    
  98. func _on_Hurtbox_invincibility_started():
  99.     blinkAnimationPlayer.play("Start")
  100.  
  101. func _on_Hurtbox_invincibility_ended():
  102.     blinkAnimationPlayer.play("Stop")
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement