no message

This commit is contained in:
2025-06-11 17:54:55 +02:00
parent 4105e75357
commit 69a456db88
38 changed files with 2398 additions and 159 deletions

View File

@ -1,113 +1,114 @@
extends CharacterBody2D
@export var speed = 350
@export var gravity = 40
@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)
@export var crouch_mult = 1
@export var sprint_mult = 1
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 crouch_raycast1 = $crouchrc1
@onready var crouch_raycast2 = $crouchrc2
@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 wallcheck = $wallcheck
@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 is_crouching = false
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 wallrunning := false
var pushingoffwall := false
var wallrun_direction := 0 # -1 = links, 1 = rechts
var standing_cshape = preload("res://resources/player standing cshape.tres")
var crouching_cshape = preload("res://resources/player crouching cshape.tres")
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):
var horizontal_direction = Input.get_axis("move_left", "move_right")
if !is_on_floor():
velocity.y += gravity
if velocity.y > fallvelocity_cap:
velocity.y = fallvelocity_cap
else:
air_jumps_done = 0
wallrun_available = true
if Input.is_action_just_pressed("jump") && Input.is_action_pressed("move_right") && wall_detected_right() && wallrun_available:
start_wallrun(1)
elif Input.is_action_just_pressed("jump") && Input.is_action_pressed("move_left") && wall_detected_left() && wallrun_available:
start_wallrun(-1)
elif Input.is_action_just_pressed("jump"):
if is_on_floor():
velocity.y = -jump_force
elif air_jumps_done < max_air_jumps:
velocity.y = -jump_force
air_jumps_done += 1
if wallrunning:
wallrun_timer += delta
velocity.y = wallrun_speed # Bewegt nach oben
if wallrun_direction > 0 && !wallrun_raycast_right2.is_colliding():
ledgedetected()
if wallrun_direction < 1 && !wallrun_raycast_left2.is_colliding():
ledgedetected()
elif wallrun_timer >= wallrun_time:
wallpushoff()
elif pushingoffwall:
wallpushoff_timer += delta
velocity.x = speed * 0.3 * horizontal_direction + (-wallrun_direction * wallpushoff_force)
if wallpushoff_timer >= wallpushoff_time:
pushingoffwall = false
else:
velocity.x = speed * horizontal_direction * crouch_mult
if horizontal_direction != 0:
switchdirection(horizontal_direction)
if Input.is_action_just_pressed("crouch"):
crouch()
elif Input.is_action_just_released("crouch"):
if above_head_empty():
stand()
else:
if stuck_under_object != true:
stuck_under_object = true
print("player stuck under object")
if stuck_under_object && above_head_empty():
if !Input.is_action_pressed("crouch"):
stand()
stuck_under_object = false
move_and_slide()
update_animation(horizontal_direction)
func above_head_empty() -> bool:
var result = !crouch_raycast1.is_colliding() && !crouch_raycast2.is_colliding()
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:
@ -117,67 +118,42 @@ func wall_detected_left() -> bool:
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 start_wallrun(direction: int):
wallrunning = true
wallrun_available = false
wallrun_direction = direction
velocity.x = 1000 * wallrun_direction # An Wand haften
print("start wallrun called")
func transitionrun() -> bool:
var result = Input.get_axis("move_left", "move_right") != 0 && is_on_floor()
return result
func wallpushoff():
wallrunning = false
pushingoffwall = true
wallpushoff_timer = 0.0
wallrun_timer = 0.0
print("stop wallrun called")
func transitionjump() -> bool:
var result = Input.is_action_just_pressed("jump") && is_on_floor()
return result
func ledgedetected():
wallrunning = false
wallrun_timer = 0.0
func transitiondoublejump() -> bool:
var result = Input.is_action_just_pressed("jump") && !is_on_floor() && (air_jumps_done < max_air_jumps)
return result
func checkledgegrab() -> bool:
var result = wallcheck.is_colliding() && !floorcheck.is_colliding() && velocity.y == 0
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 update_animation(horizontal_direction):
if is_on_floor():
if horizontal_direction == 0:
if is_crouching:
ap.play("crouch idle")
else:
ap.play("idle")
else:
if is_crouching:
ap.play("crouch walk")
else:
ap.play("run")
else:
if velocity.y < 0:
if air_jumps_done == 0 :
ap.play("jump")
elif air_jumps_done == 1:
ap.play("double jump")
elif velocity.y > 0:
ap.play("fall")
func crouch():
if is_crouching:
return
is_crouching = true
crouch_mult = 0.4
cshape.shape = crouching_cshape
cshape.position.y = -31
func transitioncrouch() -> bool:
var result = is_on_floor() && Input.is_action_pressed("crouch")
return result
func stand():
if is_crouching == false:
return
is_crouching = false
crouch_mult = 1
cshape.shape = standing_cshape
cshape.position.y = -44
func transitioncrouchwalk() -> bool:
var result = is_on_floor() && Input.is_action_pressed("crouch") && Input.get_axis("move_left", "move_right") != 0
return result
func switchdirection(horizontal_direction):
sprite.flip_h = (horizontal_direction == -1)
sprite.position.x = horizontal_direction * 4
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