Guest User

Untitled

a guest
Aug 22nd, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 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. onready var control = preload("res://SaveLoad/Control.gd")
  38.  
  39. const PlayerHurtSound = preload("res://Character/PlayerHurtSound.tscn")
  40.  
  41. func _ready():
  42. #stats.connect("no_health", self, "dead")
  43. animationTree.active = true
  44. swordHitbox.knockback_vector = roll_vector
  45. health_update() #PlayerStats e un singleton si nu se actualizeaza in timpul executiei programului,
  46. #deci trebuie sa ii acutalizez viata manual
  47. control.connect("Health_Changed", self , "function")
  48.  
  49. func function():
  50. if SceneSaving.player_position_loaded != null:
  51. global_position = SceneSaving.player_position_loaded
  52. if SceneSaving.health_loaded != null and SceneSaving.max_health_loaded != null:
  53. PlayerStats.health = SceneSaving.health_loaded
  54.  
  55. func _physics_process(delta):
  56. match state:
  57. MOVE:
  58. move_state(delta)
  59. SceneSaving.PlayerPosition = global_position
  60. ATTACK:
  61. attack_state(delta)
  62. DEAD:
  63. dead_state()
  64. sceneTransition.transition_to("res://Menu/MainMenu.tscn")
  65.  
  66.  
  67. var input_vector = Vector2.ZERO
  68. var velocity_aux
  69.  
  70. func move_state(delta):
  71. input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
  72. input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
  73. input_vector = input_vector.normalized()
  74.  
  75. if input_vector != Vector2.ZERO:
  76. roll_vector = input_vector
  77. swordHitbox.knockback_vector = input_vector
  78.  
  79. animationTree.set("parameters/Idle/blend_position", input_vector)
  80. animationTree.set("parameters/Run/blend_position", input_vector)
  81. animationTree.set("parameters/Attack/blend_position", input_vector)
  82. #animationTree.set("parameters/DeathAnimation/blend_position", input_vector)
  83. animationState.travel("Run")
  84. velocity = velocity.move_toward(input_vector * MAX_SPEED, acceleration * delta)
  85. else:
  86. animationState.travel("Idle")
  87. if velocity != Vector2.ZERO:
  88. velocity_aux = velocity
  89. velocity = velocity.move_toward(Vector2.ZERO, friction * delta)
  90.  
  91. move()
  92.  
  93. if Input.is_action_just_pressed("attack"):
  94. state = ATTACK
  95.  
  96.  
  97. func attack_state(_delta):
  98. velocity = Vector2.ZERO
  99. animationState.travel("Attack")
  100.  
  101. func attack_animation_ended():
  102. state = MOVE
  103.  
  104. func move():
  105. velocity = move_and_slide(velocity)
  106.  
  107. func dead_state():
  108. animationTree.set("parameters/DeathAnimation/blend_position", velocity_aux)
  109. animationState.travel("DeathAnimation")
  110. $Collision1.disabled = true
  111. $HurtBox/CollisionShape2D.disabled = true
  112.  
  113. func health_update():
  114. stats.health = stats.max_health
  115.  
  116. func _on_HurtBox_area_entered(area):
  117. stats.health -= area.damage
  118. if stats.health <= 0:
  119. state = DEAD
  120. hurtBox.start_invincibility(0.6)
  121. hurtBox.create_hit_effect()
  122. var playerHurtSounds = PlayerHurtSound.instance()
  123. get_tree().current_scene.add_child(playerHurtSounds)
  124.  
  125. func _on_HurtBox_invincibility_started():
  126. blinkAnimationPlayer.play("Start")
  127.  
  128. func _on_HurtBox_invincibility_ended():
  129. blinkAnimationPlayer.play("Stop")
  130.  
Add Comment
Please, Sign In to add comment