Advertisement
dd0

Untitled

dd0
Dec 8th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. extends Node
  2.  
  3. class_name StateMachines
  4. var state = null setget set_state
  5. var previous_state =null
  6. var states ={}
  7. var jump_charge=1 # remaining charge of jumps
  8. var dash_charge=1 # remaining dash charges
  9. onready var parent = get_parent()
  10. onready var jump_window: Timer =$jump_window # time to jump
  11.  
  12.  
  13. func _physics_process(delta):
  14. if state != null:
  15. _state_logic(delta)
  16. var transition = _get_transition(delta)
  17. if transition!=null:
  18. set_state(transition)
  19.  
  20.  
  21.  
  22.  
  23. func _ready():
  24. add_state("idle")
  25. add_state("run")
  26. add_state("jump")
  27. add_state("fall")
  28. add_state("dash")
  29. call_deferred("set_state",state.idle)
  30.  
  31.  
  32.  
  33.  
  34. func get_direction() -> Vector2 :
  35. return Vector2(
  36. Input.get_action_strength("ui_right") -
  37. Input.get_action_strength("ui_left") ,
  38. -1.0 if Input.get_action_strength("ui_up")
  39. and is_on_floor() else 0.0
  40. )
  41.  
  42.  
  43.  
  44. func _state_logic(delta):#for calc velocity
  45. parent.get_direction()
  46.  
  47. parent.apply_gravity(delta)
  48. parent.apply_mouvement(delta)
  49. if state==states.dash :
  50. parent.papply_speeed_up(delta)
  51.  
  52. func _get_transition(delta):
  53. var is_input:bool
  54. if parent.Input.is_action_just_pressed("ui_up") or parent.Input.is_action_just_pressed("ui_right") or parent.Input.is_action_just_pressed("ui_left"):
  55. is_input=true
  56. else :
  57. is_input=false
  58.  
  59. match state:
  60.  
  61.  
  62. states.jump:
  63. if Input.is_action_just_pressed("DASH") and is_input:
  64. return states.dash
  65. if parent.is_on_floor() :
  66. if not parent.velocity.x==0 :
  67. return states.run
  68.  
  69. else :
  70. return states.idle
  71.  
  72. if parent.velocity.y>0:
  73. return states.fall
  74.  
  75. states.fall:
  76. if parent.is_on_floor() :
  77.  
  78. if not parent.velocity.x==0 :
  79. return states.run
  80. else :
  81. return states.idle
  82.  
  83. if parent.velocity.x<0 :
  84. return states.jump
  85. if Input.is_action_just_pressed("DASH") and dash_charge>0 and is_input:
  86. return states.dash
  87.  
  88.  
  89. states.idle :
  90. if not parent.is_on_floor() and parent.velocity.y<0:
  91. return states.jump
  92. elif not parent.velocity.x==0:
  93. return states.run
  94.  
  95. if Input.is_action_just_pressed("DASH") and dash_charge>0 and is_input:
  96. return states.dash
  97. states.run :
  98.  
  99. if Input.is_action_just_pressed("DASH") and dash_charge>0 and is_input :
  100.  
  101. return states.dash
  102.  
  103. if Input.is_action_just_pressed("GRAB"):
  104. return states.grab
  105.  
  106.  
  107. states.DASH:
  108. if dash_charge<1:
  109. return states.jump
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. func Jump_recharge(jump_charge):
  122.  
  123. if jump_charge ==1 :
  124. pass
  125. elif parent.is_on_floor():
  126. jump_charge=+1
  127. return jump_charge
  128.  
  129. func dash_recharge(dash_charge):
  130. if dash_charge ==1 :
  131. pass
  132. elif parent.is_on_floor() and dash_charge<1:
  133. dash_charge=+1
  134.  
  135. else :
  136. dash_charge =1
  137. return dash_charge
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. func apply_speeed_up(delta):
  145. var speedboost=Vector2(5,5)
  146. var dir_dash = Vector2(0,0)
  147. if jump_window.time_left==0:
  148. jump_window.start()
  149. dir_dash=parent.direction
  150.  
  151. parent.velocity.y+=dir_dash.y*parent.speed.y*speedboost.y
  152. parent.velocity.x=parent.speed.x * dir_dash.x*speedboost.x
  153.  
  154.  
  155. func apply_mouvement(delta):
  156.  
  157.  
  158.  
  159.  
  160. if jump_charge>0:
  161. parent.velocity.y+=parent.speed.y*parent.direction.y
  162. jump_charge+=-1
  163.  
  164.  
  165.  
  166.  
  167. if parent.is_jump_interrupted and not states.dash:
  168.  
  169. parent.velocity.y= 0
  170.  
  171.  
  172.  
  173.  
  174. parent.velocity.x=(parent.speed.x * parent.direction.x + parent.wind_speed.x)
  175. parent.velocity.y += parent.gravity * get_physics_process_delta_time() + parent.wind_speed.y
  176. parent.velocity= parent.move_and_slide(parent.velocity,parent.FLOOR_NORMAL)
  177.  
  178. func apply_gravity(delta) :
  179. parent.velocity.y+=parent.gravity*delta#apply gravity
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188. ######################################################from a normal state to new one
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. func _enter_state(new_state, old_state):
  196. # match new_state:
  197. # states.idle:
  198. # parent.anim_player.play("idle")
  199. # states.run:
  200. # parent.anim_player.play("run")
  201. # states.jump:
  202. # parent.anim_player.play("jump")
  203. # states.fall:
  204. # parent.anim_player.play("fall")
  205. # states.dash:
  206. # parent.anim_player.play("dash")
  207. pass
  208.  
  209.  
  210.  
  211.  
  212. func _exit_state(old_state, new_state):
  213. pass
  214.  
  215.  
  216. func set_state(new_state):
  217. previous_state=state
  218. state = new_state
  219.  
  220. if previous_state!=null:
  221. _exit_state(previous_state,new_state)
  222. if new_state!= null:
  223. _enter_state(new_state,previous_state)
  224.  
  225. func add_state(state_name):
  226. states[state_name]= states.size()
  227.  
  228.  
  229.  
  230. ########################Timer o the dash :
  231.  
  232.  
  233. func _on_Sjump_window_timeout() :
  234. if not parent.is_on_floor():
  235. dash_charge-=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement