Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- const twodspace : Vector2 = Vector2(0, -1)
- #PlayerVars
- var velocity : Vector2 = Vector2()
- var ground_move_speed : int = 400
- var air_move_accel : int = 50
- var dash_speed : int = 500
- var air_max_speed : int = dash_speed
- var dash_var : int = 10
- var gravity : int = 1800
- var jump_velocity : int = 650
- var air_friction : float = 0.8
- var fastfall_speed : int = 500
- var multijump
- var available_jumps : int = 2
- var is_grounded
- #Preloading Raycasts
- onready var groundraycasts = $GroundRays
- func _ready():
- pass
- #mainloop claiing functions
- func _physics_process(delta):
- velocity = move_and_slide_with_snap(velocity, twodspace)
- _get_input()
- velocity.y += gravity * delta
- is_grounded = _check_is_grounded()
- #input tree
- func _input(event):
- if event.is_action_pressed("move_jump") && is_grounded:
- velocity.y = 0
- velocity.y += -jump_velocity
- elif event.is_action_pressed("move_jump") && !is_grounded && multijump > 0:
- velocity.y = 0
- multijump -= 1
- velocity.y += -jump_velocity
- if event.is_action_pressed("move_dash") && is_grounded:
- var move_direction = -int(Input.is_action_pressed("move_left")) + int(Input.is_action_pressed("move_right"))
- velocity.x = lerp(velocity.x, dash_speed* dash_var * move_direction, _get_h_weight())
- if is_grounded:
- multijump = available_jumps
- #movement tree
- func _get_input():
- var move_direction = -int(Input.is_action_pressed("move_left")) + int(Input.is_action_pressed("move_right"))
- if is_grounded:
- velocity.x = lerp(velocity.x, ground_move_speed * move_direction, _get_h_weight())
- if !is_grounded:
- if move_direction > 0 and velocity.x < air_max_speed:
- velocity.x += air_move_accel* _get_h_weight()
- if move_direction < 0 and velocity.x > -air_max_speed:
- velocity.x -= air_move_accel* _get_h_weight()
- else:
- velocity.x = velocity.x
- if !is_grounded and Input.is_action_just_pressed("move_down"):
- if velocity.y >= -(jump_velocity*4):
- velocity.y += fastfall_speed
- if move_direction > 0:
- get_node("Sprite").set_flip_h(true)
- elif move_direction < 0:
- get_node("Sprite").set_flip_h(false)
- #horizontal friction
- func _get_h_weight():
- return 0.3 if is_grounded else 1
- #ground check
- func _check_is_grounded():
- for raycast in groundraycasts.get_children():
- if raycast.is_colliding():
- return true
- #If loop completes but there were no raycasts detected
- return false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement