Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class_name PlayerMovement extends CharacterBody2D
- ###Class for everything pretaining to player physics and input.
- ##export varibles
- #Anything realted to physics that we cannot know an extact value for and needs
- #to be tweaked through playtesting.
- @export_category("Physics Properties")
- @export var constantJumpForce : float
- @export var downForce : float
- @export var maxSpeed : float
- @export var acceleration : float
- @export var friction : float
- @export var weight : float
- @export var terminalFall : float
- var initJumpForce : float = constantJumpForce
- @export_category("Hit Box")
- @export var normalHitBoxSize : int
- @export var crouchingHitBoxSize : int
- @export_category("Sprite Properties")
- @export var offSetWhileCrouching : Vector2
- #instance of playerStates so we can emit the current state to the state machine
- var playerStates = preload("res://Scripts/Player/Player_State_Machine.gd").playerStates
- var currentPlayerState = playerStates.standing
- signal change_player_state(currentPlayerState : PlayerStateMachine.playerStates)
- ##functions
- #_physics_process(delta) runs at a fixed rate every cycle
- func _physics_process(delta):
- #Calculate horrizontal vector. Stops the player holding left and right at the same time.
- var direction : Vector2
- direction.x = Input.get_axis("move_left" , "move_right")
- #normalise direction for joysticks
- direction= direction.normalized()
- #set flip h based on axis.
- %SpriteHandle.flip_h = direction.x < 0 if direction.x != 0 else %SpriteHandle.flip_h
- #are we moving?
- if direction.x != 0:
- velocity.x = clamp(velocity.x + direction.x * acceleration * delta, -maxSpeed, maxSpeed)
- #are we also crouching?
- if Input.is_action_pressed("crouch") && self.is_on_floor():
- currentPlayerState = playerStates.crouchwalking
- else:
- currentPlayerState = playerStates.running
- #have we stopped?
- elif direction.x == 0:
- #gradual deceleration
- velocity.x = direction.x * max(abs(velocity.x) - friction, 0)
- #are we also crouching?
- if Input.is_action_pressed("crouch") && self.is_on_floor():
- currentPlayerState = playerStates.crouching
- else:
- currentPlayerState = playerStates.standing
- ##Subroutine for basic jump.
- if self.is_on_floor() == true:
- #disable gravity
- velocity.y = 0
- currentPlayerState= playerStates.standing
- if Input.is_action_just_pressed("jump"):
- currentPlayerState = playerStates.jumping
- #reduce jump force by down force every
- #physics process we are in the air for
- initJumpForce -= downForce
- var jump : Vector2= initJumpForce * up_direction
- velocity += jump
- #i forgot what this is there for but Im scared to toutch it
- if velocity.y > 0:
- velocity.y = -2
- #start timer to set state back to falling
- %JumpTimer.start()
- if !self.is_on_floor() && currentPlayerState != playerStates.jumping:
- start_falling()
- #if player releases jump early they should be able to fall sooner also set
- #state to falling if player isn't current toutching the ground
- if Input.is_action_just_released("jump") && !self.is_on_floor():
- start_falling()
- #player remains in crouched state if sandwiched
- #between ceiling and floor. Ray casting has to be used
- #over is_on_ceiling and floor because its impossible for the
- #hitbox to be the right size to both fit through these gaps and
- #toutch the edges of them.
- elif %CeilingRay.is_colliding() && !Input.is_action_pressed("crouch"):
- currentPlayerState = playerStates.crouching
- #update player state needs to be done every tick for physics purposes.
- change_player_state.emit(currentPlayerState)
- move_and_slide()
- ##Sets player to falling state when timer runs out.
- ##Stops them from being in the air too long.
- func start_falling():
- currentPlayerState = playerStates.falling
- #make falling velocity increase linearly over time
- velocity += weight * Vector2.DOWN
- #cap fall speed to allow player to control their fall
- if velocity.y >= terminalFall:
- velocity.y = terminalFall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement