Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- #Constants
- const GRAVITY_VEC = Vector2(0, 1500)
- const FLOOR_NORMAL = Vector2(0, -1)
- const SLOPE_SLIDE_STOP = 25.0
- const MIN_ONAIR_TIME = 0.01
- #Normal speed ( walking)
- const WALK_SPEED = 100
- #Running speed (dashing)
- const RUN_SPEED = 350
- const JUMP_SPEED = 500
- const FLOAT_HEIGHT = -200
- var linear_vel = Vector2(0,0)
- var onair_time = 0
- var on_floor = false
- var lands
- #var inhale = ?
- var Anims = ""
- #Run variables
- #Are we running? (Double tapping?)
- var running = false
- #time inbetween key taps
- var run_window = 0.7
- #keep track of double tap count
- var run_press_count = 0
- #Timer for run state to be true
- var run_time = 0
- #Last input
- var lastXInput = 0
- #Try step
- var try_step = false
- #Cache sprite here for fast access
- onready var sprite = $Sprite
- func _physics_process(delta):
- #increment counters
- onair_time += delta
- #Movement
- #Apply gravity
- linear_vel = linear_vel + (delta * GRAVITY_VEC)
- #Move and slide
- linear_vel = move_and_slide(linear_vel, FLOOR_NORMAL)#, SLOPE_SLIDE_STOP)
- #Detect Floor
- if is_on_floor():
- onair_time = 0
- on_floor = onair_time < MIN_ONAIR_TIME
- ### Controls ###
- var target_speed = 0
- #Walk
- if (Input.is_action_pressed("ui_left")):
- target_speed += -1
- target_speed *= WALK_SPEED
- if (Input.is_action_pressed("ui_right")):
- target_speed += 1
- target_speed *= WALK_SPEED
- #cancel out speed if both keys are pressed
- if ((Input.is_action_pressed("ui_right") and (Input.is_action_pressed("ui_left")))):
- target_speed = 0
- #reset timer if we stop pushing keys
- linear_vel.x = lerp(linear_vel.x, target_speed, 1)
- #Run
- #Check for presses
- if (Input.is_action_just_pressed("ui_left")):
- run_press_count -= 1
- if (Input.is_action_just_pressed("ui_right")):
- run_press_count += 1
- #if the direction variable is left or right(-1 left, 1, right) and the input window is appropriate, and the direction variable is the same as the last input..
- #if (run_press_count != 0 and run_time < run_window and run_press_count == lastXInput):
- if (run_press_count <= 2 * lastXInput and run_time < run_window and try_step == true):
- running = true
- if (run_press_count <= 2 * lastXInput and try_step == false):
- run_time = 0
- try_step = true
- #The last input: If negative, left. If positive, right.
- lastXInput = run_press_count
- #We are inputting, and try_step is true, and then a timer starts
- if(try_step == true):
- run_time += delta
- if (run_time >= run_window):
- run_time = 0
- run_press_count = 0
- try_step = false
- if (running):
- target_speed *= RUN_SPEED
- if (not(Input.is_action_pressed("ui_right") and (Input.is_action_pressed("ui_left")))):
- running = false
- print(running)
- print(run_press_count)
- #Jumping
- if ((on_floor) and (Input.is_action_just_pressed("ui_shift"))):
- linear_vel.y = -JUMP_SPEED
- if(not(on_floor) and (Input.is_action_just_pressed("ui_shift"))):
- linear_vel.y = -JUMP_SPEED
- ### Animation ###
- var new_Anims = "Idle"
- #Scale modifier is 2, because our sprite is scaled.
- if (on_floor):
- #Walking
- if (Input.is_action_pressed("ui_left")):
- sprite.scale.x = -2
- new_Anims = "Walk"
- if (Input.is_action_pressed("ui_right")):
- sprite.scale.x = 2
- new_Anims = "Walk"
- #Running
- if (Input.is_action_pressed("ui_left") and (running)):
- sprite.scale.x = -2
- new_Anims = "Run"
- if (Input.is_action_pressed("ui_right") and (running)):
- sprite.scale.x = 2
- new_Anims = "Run"
- else:
- #Kirby can quickly turn around regardless of running
- #or not whether in the air or not
- if (Input.is_action_pressed("ui_left") and not (Input.is_action_pressed("ui_right"))):
- sprite.scale.x = -2
- if (Input.is_action_pressed("ui_right") and not (Input.is_action_pressed("ui_left"))):
- sprite.scale.x = 2
- # In the air
- if linear_vel.y < 0:
- new_Anims = "Jump"
- else:
- new_Anims = "Fall"
- #If the animations aren't playing, play them
- if new_Anims != Anims:
- Anims = new_Anims
- $Anims.play(Anims)
- ### Sounds ###
- #Landing sound
- if (is_on_floor() and not(lands)):
- $Sounds/s_land.play()
- lands = is_on_floor()
- if (is_on_floor()):
- lands = true
- elif (not(is_on_floor())):
- lands = false
- #Jump sound
- if ((on_floor) and (Input.is_action_just_pressed("ui_shift"))):
- $Sounds/s_jump.play()
- if(not(on_floor) and (Input.is_action_just_pressed("ui_shift"))):
- $Sounds/s_jump.play()
- #Walk sound
- #Use a call function in the animationPlayer
- func Step():
- get_node("Sounds/s_walk").play()
- """
- #Temporary:
- func _process(delta):
- motion.y += GRAVITY
- var friction = false
- if (Input.is_action_pressed("ui_right")):
- motion.x = SPEED
- $AnimatedSprite.play("Walk")
- $AnimatedSprite.flip_h = false
- elif (Input.is_action_pressed("ui_left")):
- motion.x = -SPEED
- $AnimatedSprite.play("Walk")
- $AnimatedSprite.flip_h = true
- else:
- motion.x = 0
- friction = true
- $AnimatedSprite.play("Idle")
- if (is_on_floor()):
- if (Input.is_action_just_pressed("ui_shift")):
- motion.y = JUMP_HEIGHT
- if (friction == true):
- motion.x = lerp(motion.x, 0 , 0.2)
- if (not(is_on_floor())):
- if (Input.is_action_just_pressed("ui_shift")):
- motion.y = FLOATH
- else:
- if motion.y < 0 :
- $AnimatedSprite.play("Jump")
- elif motion.y > 0:
- $AnimatedSprite.play("Fall")
- if friction == true:
- motion.x = lerp(motion.x, 0, 0.5)
- motion = move_and_slide(motion, UP)
- print(motion)
- pass
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement