160 lines
5.1 KiB
GDScript
160 lines
5.1 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@export var speed = 350
|
|
@export var grav = 40
|
|
@export var jump_force = 800
|
|
@export var fallvelocity_cap = 1000 # How fast the player can get when falling
|
|
@export var max_air_jumps = 1 # Number of total jumps allowed (1 = normal, 2 = double jump)
|
|
|
|
var input_direction = 0
|
|
var direction_locked = false
|
|
var facing_direction = 1 # 1 = right, -1 = left
|
|
|
|
@export var sprint_mult = 1.5
|
|
@export var crouch_mult = 0.4
|
|
|
|
@onready var ap = $AnimationPlayer
|
|
@onready var sprite = $Sprite2D
|
|
@onready var cshape = $mainbody
|
|
@onready var topcheck1 = $topcheck1
|
|
@onready var topcheck2 = $topcheck2
|
|
|
|
@onready var statemachine = $StateMachine
|
|
|
|
@onready var wallrun_raycast_left1 = $wallrunrcleft1
|
|
@onready var wallrun_raycast_left2 = $wallrunrcleft2
|
|
@onready var wallrun_raycast_right1 = $wallrunrcright1
|
|
@onready var wallrun_raycast_right2 = $wallrunrcright2
|
|
@onready var wallcheckright = $wallcheckright
|
|
@onready var wallcheckleft = $wallcheckleft
|
|
@onready var floorcheck = $floorcheck
|
|
|
|
var horizontal_direction = 0
|
|
var v_mult = 1 #velocity multiplier
|
|
var v_push = 0 #velocity push in either direction
|
|
|
|
var air_jumps_done = 0
|
|
|
|
var stuck_under_object = false
|
|
|
|
var wallrun_available := true
|
|
var wallrun_timer := 0.0 #:= bedeutet, dass der Typ fest festgelegt ist (z.B int, bool etc.)
|
|
var wallrun_time := 1 # Dauer des Wallruns in Sekunden
|
|
var wallrun_speed := -250.0 # Geschwindigkeit nach oben (negativ wegen y-Achse)
|
|
|
|
var wallpushoff_force := 450 # Stärke des Abstoßens
|
|
var wallpushoff_mult := 0.3 # schränkt die velocity.x Kontrolle des Spielers ein.
|
|
var wallpushoff_timer := 0.0
|
|
var wallpushoff_time := 0.5 # dauer des pushoffs
|
|
|
|
var climbup_pos1 = Vector2(0, 0)
|
|
var climbup_pos2 = Vector2(0, 0)
|
|
var climbup_pos1_reached = false
|
|
var climbup_speed_y = 100
|
|
var climbup_speed_x = 500
|
|
|
|
func _process(delta):
|
|
#turnmovement()
|
|
pass
|
|
|
|
|
|
func _physics_process(delta):
|
|
|
|
move_and_slide()
|
|
input_direction = Input.get_axis("move_left", "move_right")
|
|
|
|
#print(statemachine.current_state.get_state_name()) # zum debuggen aktivieren
|
|
|
|
if statemachine.current_state.get_state_name() not in ["PlayerWallrun", "PlayerClimbUp"]:
|
|
gravity()
|
|
|
|
if statemachine.current_state.get_state_name() not in ["PlayerWallrun", "PlayerWallrunPushoff", "PlayerLedgeGrab", "PlayerClimbUp"]:
|
|
turnmovement()
|
|
|
|
|
|
$ledgecollision.disabled = statemachine.current_state.get_state_name() in ["PlayerIdle", "PlayerRun", "PlayerCrouch", "PlayerCrouchWalk", "PlayerJump", "PlayerWallrun", "PlayerClimbUp"] or velocity.y < 0 or Input.is_action_pressed("crouch") or(topcheck1.is_colliding() and topcheck2.is_colliding() and statemachine.current_state.get_state_name() != "PlayerLedgeGrab")
|
|
|
|
func movement():
|
|
velocity.x = speed * input_direction * v_mult + v_push
|
|
|
|
func turnmovement():
|
|
movement()
|
|
|
|
if direction_locked:
|
|
return
|
|
|
|
if input_direction != 0 and sign(input_direction) != facing_direction:
|
|
switch_direction(sign(input_direction))
|
|
|
|
func switch_direction(new_direction):
|
|
facing_direction = new_direction
|
|
sprite.flip_h = (facing_direction == -1)
|
|
|
|
func set_facing_direction(dir: int): #manual flip
|
|
if direction_locked:
|
|
return
|
|
if dir in [-1, 1] and dir != facing_direction:
|
|
switch_direction(dir)
|
|
|
|
func lock_direction():
|
|
direction_locked = true
|
|
|
|
func unlock_direction():
|
|
direction_locked = false
|
|
|
|
func gravity():
|
|
velocity.y += grav
|
|
if velocity.y > fallvelocity_cap:
|
|
velocity.y = fallvelocity_cap
|
|
|
|
func head_clear() -> bool:
|
|
var result = !topcheck1.is_colliding() && !topcheck2.is_colliding()
|
|
return result
|
|
|
|
func wall_detected_left() -> bool:
|
|
var result = wallrun_raycast_left1.is_colliding() && wallrun_raycast_left2.is_colliding()
|
|
return result
|
|
|
|
func wall_detected_right() -> bool:
|
|
var result = wallrun_raycast_right1.is_colliding() && wallrun_raycast_right2.is_colliding()
|
|
return result
|
|
|
|
# transition functions
|
|
|
|
func transitionidle() -> bool:
|
|
var result = Input.get_axis("move_left", "move_right") == 0 && is_on_floor()
|
|
return result
|
|
|
|
func transitionrun() -> bool:
|
|
var result = Input.get_axis("move_left", "move_right") != 0 && is_on_floor()
|
|
return result
|
|
|
|
func transitionjump() -> bool:
|
|
var result = Input.is_action_just_pressed("jump") && is_on_floor()
|
|
return result
|
|
|
|
func transitiondoublejump() -> bool:
|
|
var result = Input.is_action_just_pressed("jump") && !is_on_floor() && (air_jumps_done < max_air_jumps)
|
|
return result
|
|
|
|
func transitionfall() -> bool:
|
|
var result = velocity.y > 0 && !is_on_floor()
|
|
return result
|
|
|
|
func transitionledgegrab() -> bool:
|
|
var result = !floorcheck.is_colliding() && velocity.y - grav == 0 && (wallcheckright.is_colliding() or wallcheckleft.is_colliding())
|
|
return result
|
|
|
|
func transitioncrouch() -> bool:
|
|
var result = is_on_floor() && Input.is_action_pressed("crouch")
|
|
return result
|
|
|
|
func transitioncrouchwalk() -> bool:
|
|
var result = is_on_floor() && Input.is_action_pressed("crouch") && Input.get_axis("move_left", "move_right") != 0
|
|
return result
|
|
|
|
func transitionwallrun() -> bool:
|
|
var result = Input.is_action_just_pressed("jump") and ((Input.is_action_pressed("move_right") && wall_detected_right()) or (Input.is_action_pressed("move_left") && wall_detected_left())) and wallrun_available
|
|
return result
|
|
|