Advertisement
chris33556

state_machine_updated

Feb 12th, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. var velocity = Vector3()
  4. var speed = 5
  5. export (int) var damage := 10
  6. var AttackPoints = 3
  7. var is_Attacking = true
  8.  
  9. onready var anim_player = $AnimatedSprite3D
  10.  
  11. enum State {
  12. IDLE
  13. MOVING
  14. ATTACKING
  15. }
  16.  
  17. var state = State.IDLE
  18.  
  19. func _ready():
  20. $attackArea/CollisionShape.disabled = true
  21.  
  22.  
  23. func _physics_process(delta):
  24. velocity = Vector3()
  25.  
  26. if Input.is_action_pressed("ui_right"):
  27. velocity.x += 1
  28. if Input.is_action_pressed("ui_left"):
  29. velocity.x -= 1
  30. if Input.is_action_pressed("ui_up"):
  31. velocity.z -= 1
  32. if Input.is_action_pressed("ui_down"):
  33. velocity.z += 1
  34.  
  35. if velocity.length() > 0:
  36. velocity = velocity.normalized() * speed
  37. move_and_slide(velocity)
  38. state = State.MOVING
  39. else:
  40. state = State.IDLE
  41.  
  42. if Input.is_action_pressed("attack"):
  43. state = State.ATTACKING
  44. update_state()
  45.  
  46. func update_state():
  47. if state == State.IDLE:
  48. anim_player.play("idle")
  49.  
  50.  
  51. elif state == State.MOVING:
  52. anim_player.play("walking")
  53.  
  54.  
  55. elif state == State.ATTACKING:
  56. if is_Attacking:
  57. $attackArea/CollisionShape.disabled = true
  58. if Input.is_action_just_pressed("attack") && AttackPoints == 3:
  59. $AttackResetTimer.start()
  60. $AnimatedSprite3D.play("Attack_1")
  61. AttackPoints = AttackPoints - 1
  62. $attackArea/CollisionShape.disabled = false
  63.  
  64. elif Input.is_action_just_pressed("attack") && AttackPoints == 2:
  65. $AttackResetTimer.start()
  66. $AnimatedSprite3D.play("Attack_2")
  67. AttackPoints = AttackPoints - 1
  68. $attackArea/CollisionShape.disabled = false
  69.  
  70. elif Input.is_action_just_pressed("attack") && AttackPoints == 1:
  71. $AttackResetTimer.start()
  72. $AnimatedSprite3D.play("Attack_3")
  73. AttackPoints = AttackPoints - 1
  74. $attackArea/CollisionShape.disabled = false
  75.  
  76.  
  77.  
  78. func _on_AttackResetTimer_timeout():
  79. AttackPoints = 3
  80.  
  81. func _on_attackArea_body_entered(body):
  82. if body.has_method("knockback"):
  83. body.knockback(damage)
  84.  
  85.  
  86. func _on_AnimatedSprite3D_animation_finished():
  87. if $AnimatedSprite3D.animation == "Attack_1":
  88. $AnimatedSprite3D.play("idle")
  89. elif $AnimatedSprite3D.animation == "Attack_2":
  90. $AnimatedSprite3D.play("idle")
  91. elif $AnimatedSprite3D.animation == "Attack_3":
  92. $AnimatedSprite3D.play("idle")
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement