Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- class_name StateMachines
- var state = null setget set_state
- var previous_state =null
- var states ={}
- var jump_charge=1 # remaining charge of jumps
- var dash_charge=1 # remaining dash charges
- onready var parent = get_parent()
- onready var jump_window: Timer =$jump_window # time to jump
- func _physics_process(delta):
- if state != null:
- _state_logic(delta)
- var transition = _get_transition(delta)
- if transition!=null:
- set_state(transition)
- func _ready():
- add_state("idle")
- add_state("run")
- add_state("jump")
- add_state("fall")
- add_state("dash")
- call_deferred("set_state",state.idle)
- func get_direction() -> Vector2 :
- return Vector2(
- Input.get_action_strength("ui_right") -
- Input.get_action_strength("ui_left") ,
- -1.0 if Input.get_action_strength("ui_up")
- and is_on_floor() else 0.0
- )
- func _state_logic(delta):#for calc velocity
- parent.get_direction()
- parent.apply_gravity(delta)
- parent.apply_mouvement(delta)
- if state==states.dash :
- parent.papply_speeed_up(delta)
- func _get_transition(delta):
- var is_input:bool
- 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"):
- is_input=true
- else :
- is_input=false
- match state:
- states.jump:
- if Input.is_action_just_pressed("DASH") and is_input:
- return states.dash
- if parent.is_on_floor() :
- if not parent.velocity.x==0 :
- return states.run
- else :
- return states.idle
- if parent.velocity.y>0:
- return states.fall
- states.fall:
- if parent.is_on_floor() :
- if not parent.velocity.x==0 :
- return states.run
- else :
- return states.idle
- if parent.velocity.x<0 :
- return states.jump
- if Input.is_action_just_pressed("DASH") and dash_charge>0 and is_input:
- return states.dash
- states.idle :
- if not parent.is_on_floor() and parent.velocity.y<0:
- return states.jump
- elif not parent.velocity.x==0:
- return states.run
- if Input.is_action_just_pressed("DASH") and dash_charge>0 and is_input:
- return states.dash
- states.run :
- if Input.is_action_just_pressed("DASH") and dash_charge>0 and is_input :
- return states.dash
- if Input.is_action_just_pressed("GRAB"):
- return states.grab
- states.DASH:
- if dash_charge<1:
- return states.jump
- func Jump_recharge(jump_charge):
- if jump_charge ==1 :
- pass
- elif parent.is_on_floor():
- jump_charge=+1
- return jump_charge
- func dash_recharge(dash_charge):
- if dash_charge ==1 :
- pass
- elif parent.is_on_floor() and dash_charge<1:
- dash_charge=+1
- else :
- dash_charge =1
- return dash_charge
- func apply_speeed_up(delta):
- var speedboost=Vector2(5,5)
- var dir_dash = Vector2(0,0)
- if jump_window.time_left==0:
- jump_window.start()
- dir_dash=parent.direction
- parent.velocity.y+=dir_dash.y*parent.speed.y*speedboost.y
- parent.velocity.x=parent.speed.x * dir_dash.x*speedboost.x
- func apply_mouvement(delta):
- if jump_charge>0:
- parent.velocity.y+=parent.speed.y*parent.direction.y
- jump_charge+=-1
- if parent.is_jump_interrupted and not states.dash:
- parent.velocity.y= 0
- parent.velocity.x=(parent.speed.x * parent.direction.x + parent.wind_speed.x)
- parent.velocity.y += parent.gravity * get_physics_process_delta_time() + parent.wind_speed.y
- parent.velocity= parent.move_and_slide(parent.velocity,parent.FLOOR_NORMAL)
- func apply_gravity(delta) :
- parent.velocity.y+=parent.gravity*delta#apply gravity
- ######################################################from a normal state to new one
- func _enter_state(new_state, old_state):
- # match new_state:
- # states.idle:
- # parent.anim_player.play("idle")
- # states.run:
- # parent.anim_player.play("run")
- # states.jump:
- # parent.anim_player.play("jump")
- # states.fall:
- # parent.anim_player.play("fall")
- # states.dash:
- # parent.anim_player.play("dash")
- pass
- func _exit_state(old_state, new_state):
- pass
- func set_state(new_state):
- previous_state=state
- state = new_state
- if previous_state!=null:
- _exit_state(previous_state,new_state)
- if new_state!= null:
- _enter_state(new_state,previous_state)
- func add_state(state_name):
- states[state_name]= states.size()
- ########################Timer o the dash :
- func _on_Sjump_window_timeout() :
- if not parent.is_on_floor():
- dash_charge-=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement