Guest User

Untitled

a guest
Aug 11th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3.  
  4. export var MAX_SPEED = 80
  5. export var acceleration = 500
  6. export var friction = 500
  7.  
  8. enum {
  9. MOVE,
  10. ATTACK,
  11. DEAD
  12. }
  13.  
  14. var state = MOVE
  15. var velocity = Vector2.ZERO
  16.  
  17. onready var animationPlayer = $AnimationPlayer
  18.  
  19. #Animation -> Parameters si de acolo setezi in ce pozitie sa inceapa caracterul
  20. # SECOND METHOD
  21. """
  22. var Whatever = null
  23. func _ready():
  24. Whatever = $AnimationWhatever
  25. sunt 2 metode prin care se acceseaza nodul din "main" nush cum sa-i zic
  26. """
  27. var roll_vector = Vector2.RIGHT
  28. var stats = PlayerStats
  29.  
  30. onready var animationTree = $AnimationTree
  31. onready var animationState = animationTree.get("parameters/playback") #animation tree -> playback iei ce e acl
  32. onready var swordHitbox = $HitboxPivot/SwordHitbox
  33. onready var hurtBox = $HurtBox
  34. onready var blinkAnimationPlayer = $BlinkAnimationPlayer
  35. onready var sceneTransition = $SceneTransitionRect
  36.  
  37. const PlayerHurtSound = preload("res://Character/PlayerHurtSound.tscn")
  38.  
  39. func _ready():
  40. #stats.connect("no_health", self, "dead")
  41. animationTree.active = true
  42. swordHitbox.knockback_vector = roll_vector
  43.  
  44. func _physics_process(delta):
  45. match state:
  46. MOVE:
  47. move_state(delta)
  48. ATTACK:
  49. attack_state(delta)
  50. DEAD:
  51. dead_state()
  52. sceneTransition.transition_to("res://Menu/MainMenu.tscn")
  53.  
  54. var input_vector = Vector2.ZERO
  55. var velocity_aux
  56.  
  57. func move_state(delta):
  58. input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
  59. input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
  60. input_vector = input_vector.normalized()
  61.  
  62. if input_vector != Vector2.ZERO:
  63. roll_vector = input_vector
  64. swordHitbox.knockback_vector = input_vector
  65.  
  66. animationTree.set("parameters/Idle/blend_position", input_vector)
  67. animationTree.set("parameters/Run/blend_position", input_vector)
  68. animationTree.set("parameters/Attack/blend_position", input_vector)
  69. #animationTree.set("parameters/DeathAnimation/blend_position", input_vector)
  70. animationState.travel("Run")
  71. velocity = velocity.move_toward(input_vector * MAX_SPEED, acceleration * delta)
  72. else:
  73. animationState.travel("Idle")
  74. if velocity != Vector2.ZERO:
  75. velocity_aux = velocity
  76. velocity = velocity.move_toward(Vector2.ZERO, friction * delta)
  77.  
  78. move()
  79.  
  80. if Input.is_action_just_pressed("attack"):
  81. state = ATTACK
  82.  
  83.  
  84. func attack_state(_delta):
  85. velocity = Vector2.ZERO
  86. animationState.travel("Attack")
  87.  
  88. func attack_animation_ended():
  89. state = MOVE
  90.  
  91. func move():
  92. velocity = move_and_slide(velocity)
  93.  
  94. func dead_state():
  95. animationTree.set("parameters/DeathAnimation/blend_position", velocity_aux)
  96. animationState.travel("DeathAnimation")
  97. $Collision1.disabled = true
  98. $HurtBox/CollisionShape2D.disabled = true
  99.  
  100. func _on_HurtBox_area_entered(area):
  101. stats.health -= area.damage
  102. if stats.health <= 0:
  103. state = DEAD
  104. hurtBox.start_invincibility(0.6)
  105. hurtBox.create_hit_effect()
  106. var playerHurtSounds = PlayerHurtSound.instance()
  107. get_tree().current_scene.add_child(playerHurtSounds)
  108.  
  109. func _on_HurtBox_invincibility_started():
  110. blinkAnimationPlayer.play("Start")
  111.  
  112. func _on_HurtBox_invincibility_ended():
  113. blinkAnimationPlayer.play("Stop")
  114.  
Add Comment
Please, Sign In to add comment