Wicked-Depths/plr.gd

202 lines
4.5 KiB
GDScript3
Raw Permalink Normal View History

extends CharacterBody2D
const JUMP_VELOCITY = -260
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var dir = "none"
var airjump = false
var ignore = Vector2(0,0)
var wall = "none"
var can_jump = true
var first_air = false
var tweener = ""
var defspeed = 100
var sprintspeed = 150
var SPEED = defspeed
var can_move = true
@export var on_ladder = false
@export var ladder : CharacterBody2D
var cool = 0.6
func _physics_process(delta):
if on_ladder:
if not Input.is_action_pressed("sprint") and SPEED != defspeed:
SPEED = defspeed
tweener = get_tree().create_tween()
velocity = Vector2(0,0)
$Left/CPUParticles2D.emitting = false
$right/CPUParticles2D.emitting = false
$AnimatedSprite2D.play("idle")
if Input.is_action_pressed("ui_up"):
velocity.y += -1 * SPEED * 0.5
if Input.is_action_pressed("ui_down"):
velocity.y += 1 * SPEED * 0.5
if Input.is_action_just_pressed("ui_accept"):
velocity.y = JUMP_VELOCITY
on_ladder = false
move_and_slide()
return
if Input.is_action_just_pressed("ui_left") or Input.is_action_just_pressed("ui_right"):
on_ladder = false
airjump = true
$LedgeAssistWall.start()
move_and_slide()
return
if not can_move:
if not Input.is_action_pressed("sprint") and SPEED != defspeed:
SPEED = defspeed
tweener = get_tree().create_tween()
return
if Input.is_action_just_pressed("sprint"):
if not str(tweener) == "":
tweener.stop()
tweener = get_tree().create_tween()
tweener.tween_property(self,"SPEED",sprintspeed,1)
elif Input.is_action_just_released("sprint"):
if not str(tweener) == "":
tweener.stop()
tweener = get_tree().create_tween()
tweener.tween_property(self,"SPEED",defspeed,0.3)
if not is_on_floor():
velocity.y += gravity * delta
if can_jump and first_air:
first_air = false
$LedgeAssist.start(0.05)
if is_on_floor():
ignore = Vector2(0,0)
can_jump = true
first_air = is_on_floor() or is_on_wall_only()
if not is_on_wall_only():
wall = "none"
# Handle Jump.
if Input.is_action_just_pressed("ui_accept") and can_jump or airjump and Input.is_action_just_pressed("ui_accept"):
can_jump = false
velocity.y = JUMP_VELOCITY
$AnimatedSprite2D.play("jump")
if airjump:
if wall == "right":
ignore.y = 1
ignore.x = 0
$leftt.stop()
$rightt.start(cool)
elif wall == "left":
ignore.x = 1
ignore.y = 0
$rightt.stop()
$leftt.start(cool)
wall = "none"
airjump = false
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction == -1:
dir = "left"
if wall == "none":
$AnimatedSprite2D.flip_h = true
else:
$AnimatedSprite2D.flip_h = false
elif direction == 1:
dir = "right"
if wall == "none":
$AnimatedSprite2D.flip_h = false
else:
$AnimatedSprite2D.flip_h = true
else:
dir = "none"
if velocity.y == 0:
$AnimatedSprite2D.play("idle")
elif not $AnimatedSprite2D.animation == "jump":
$AnimatedSprite2D.play("jump")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if not velocity.x == 0 and velocity.y == 0:
$AnimatedSprite2D.play("move")
if not is_on_floor() and not on_ladder:
var left_collide = $Left.get_overlapping_bodies()
var right_collide = $right.get_overlapping_bodies()
if dir == "left" and len($Left.get_overlapping_bodies()) >= 1 and not ignore.x:
velocity.y *= 0.6
wall = "left"
$Left/CPUParticles2D.emitting = true
$right/CPUParticles2D.emitting = false
$AnimatedSprite2D.play("wall")
airjump = true
elif dir == "right" and len($right.get_overlapping_bodies()) >= 1 and not ignore.y:
velocity.y *= 0.6
wall = "right"
$right/CPUParticles2D.emitting = true
$Left/CPUParticles2D.emitting = false
$AnimatedSprite2D.play("wall")
airjump = true
else:
$Left/CPUParticles2D.emitting = false
$right/CPUParticles2D.emitting = false
if airjump:
$LedgeAssistWall.start()
else:
airjump = false
else:
$Left/CPUParticles2D.emitting = false
$right/CPUParticles2D.emitting = false
move_and_slide()
func _on_leftt_timeout():
ignore.x = 0
func _on_rightt_timeout():
ignore.y = 0
func _on_ledge_assist_timeout():
can_jump = false
func _on_ledge_assist_wall_timeout():
airjump = false