Advertisement
chris33556

player-script_03_03_23

Mar 3rd, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. onready var AnimSprite = $AnimatedSprite3D
  4.  
  5. var velocity = Vector3()
  6. var speed = 3
  7. var isAttacking = false
  8. var isJumping = false
  9. var jump_impulse = 300
  10. var input_vector = Vector3.ZERO
  11. onready var AttackArea = $AttackArea
  12. export (int) var damage := 10
  13. var canMove = false
  14. var AttackPoints = 3
  15. var health := 50
  16. var foo : int = 10
  17. var timer = null
  18. var crouch_delay = 0.5
  19. var isCrouch = true
  20. var current_animation
  21.  
  22. enum states {
  23. IDLE,
  24. WALK,
  25. ATTACK1,
  26. ATTACK2,
  27. ATTACK3,
  28. STUNNED
  29. }
  30.  
  31. var player_state = states.IDLE
  32.  
  33. var state = 0 # removing this line re-enables movement and attack animations plus changing 'match state'
  34. # in line 101 to 'match (player_state)'.
  35.  
  36.  
  37.  
  38. func _ready():
  39. $attackArea/CollisionShape.disabled = true
  40.  
  41.  
  42. func _physics_process(delta):
  43. get_input()
  44. update_animation()
  45. combo()
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. func get_input():
  53. if not isAttacking:
  54. velocity.x = speed * Input.get_axis("ui_left", "ui_right")
  55. velocity.z = speed * Input.get_axis("ui_up", "ui_down")
  56. player_state = states.WALK
  57.  
  58. if velocity.x == 0 and velocity.z == 0:
  59. player_state = states.IDLE
  60. isAttacking = false
  61.  
  62. velocity = move_and_slide(velocity, Vector3.UP)
  63. var motion = velocity.normalized() * speed
  64.  
  65. func combo():
  66. if isAttacking:
  67. $attackArea/CollisionShape.disabled = true
  68. if Input.is_action_just_pressed("attack") && AttackPoints == 3:
  69. $AttackResetTimer.start()
  70. velocity.x = 0
  71. velocity.z = 0
  72. player_state = states.ATTACK1
  73. AttackPoints = AttackPoints - 1
  74. $attackArea/CollisionShape.disabled = false
  75. isAttacking = true
  76.  
  77. elif Input.is_action_just_pressed("attack") && AttackPoints == 2:
  78. $AttackResetTimer.start()
  79. velocity.x = 0
  80. velocity.z = 0
  81. player_state = states.ATTACK2
  82. AttackPoints = AttackPoints - 1
  83. $attackArea/CollisionShape.disabled = false
  84. isAttacking = true
  85. elif Input.is_action_just_pressed("attack") && AttackPoints == 1:
  86. $AttackResetTimer.start()
  87. velocity.x = 0
  88. velocity.z = 0
  89. player_state = states.ATTACK3
  90. AttackPoints = AttackPoints - 1
  91. $attackArea/CollisionShape.disabled = false
  92. isAttacking = true
  93. elif isAttacking == false:
  94. speed = 1.5
  95.  
  96. velocity = move_and_slide(velocity, Vector3.UP)
  97. var motion = velocity.normalized() * speed
  98.  
  99.  
  100. func update_animation():
  101. match (player_state):
  102. states.IDLE:
  103. $AnimatedSprite3D.play("idle")
  104. states.WALK:
  105. $AnimatedSprite3D.play("walking")
  106. states.ATTACK1:
  107. $AnimatedSprite3D.play("Attack_1")
  108. states.ATTACK2:
  109. $AnimatedSprite3D.play("Attack_2")
  110. states.ATTACK3:
  111. $AnimatedSprite3D.play("Attack_3")
  112. states.STUNNED:
  113. $AnimatedSprite3D.play("stunned")
  114.  
  115. func _on_AttackResetTimer_timeout():
  116. AttackPoints = 3
  117.  
  118.  
  119. func _on_AnimatedSprite3D_animation_finished():
  120. if $AnimatedSprite3D.animation == "Attack_1":
  121. $AnimatedSprite3D.play("idle")
  122. elif $AnimatedSprite3D.animation == "Attack_2":
  123. $AnimatedSprite3D.play("idle")
  124. elif $AnimatedSprite3D.animation == "Attack_3":
  125. $AnimatedSprite3D.play("idle")
  126. isAttacking = false
  127.  
  128.  
  129. # THIS IS THE ENEMYS KNOCKBACK FUNCTION WITH PLAYERS CODE TO REGISTER THE ATTACKS-----
  130. func _on_attackArea_body_entered(body):
  131. if body.has_method("Enemyknockback"):
  132. body.Enemyknockback(damage)
  133. if body.health > 0:
  134. if AnimSprite.animation == "Attack_1" or AnimSprite.animation == "Attack_2":
  135. if body.state != body.states.KNOCKBACK:
  136. if body.state != body.states.STUNNED:
  137. body.state = body.states.STUNNED
  138. yield(get_tree().create_timer(0.5), "timeout")
  139. body.state = body.states.IDLE
  140.  
  141. if AnimSprite.animation == "Attack_3":
  142. body.state = body.states.KNOCKBACK
  143. print("knockback")
  144. if body.health <= 0:
  145. body.state = body.states.FINALKNOCKBACK
  146. $CollisionShape.disabled = true
  147.  
  148. # THIS IS THE PLAYERS HITCOUNT BAR SO THE ENEMYS ATTACKS CAN REGISTER ------
  149. func playerknockback(damage: int):
  150. health -= damage
  151. print("player was hit!, current health: " + str(health))
  152.  
  153.  
  154.  
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement