added buggy crouching
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
const SPEED = 600.0
|
||||
var SPEED = 600.0
|
||||
const REGSPEED = 600.0
|
||||
const JUMP_VELOCITY = -1000.0
|
||||
const MAX_JUMP_DURATION = 0.3
|
||||
const MIN_JUMP_DURATION = 0.01
|
||||
@@ -9,54 +10,83 @@ const FALL_GRAVITY_MULTIPLIER = 2
|
||||
|
||||
var isFacingRight: bool
|
||||
|
||||
# jumping vars
|
||||
var jump_timer = 0.0
|
||||
var is_jumping = false
|
||||
|
||||
# crouching vars
|
||||
var is_crouching = false
|
||||
var can_stand_up = true
|
||||
var crouch_speed = 100
|
||||
|
||||
# dialouge vars
|
||||
@export var is_talking = false
|
||||
|
||||
@onready var actionableFinder: Area2D = $Marker2D/ActionableFinder
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("crouch"):
|
||||
is_crouching = true
|
||||
update_crouch_state()
|
||||
elif(event.is_action_released("crouch")):
|
||||
is_crouching = false
|
||||
update_crouch_state()
|
||||
|
||||
func update_crouch_state() -> void:
|
||||
if is_crouching:
|
||||
#slow speed
|
||||
SPEED = crouch_speed
|
||||
#shorten hitbox
|
||||
$CollisionShape2D.scale.y = .5
|
||||
elif(can_stand_up):
|
||||
SPEED = REGSPEED # resotre normal speed
|
||||
$CollisionShape2D.scale.y = 1 # restore collision
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if Input.is_action_just_pressed("ui_accept"):
|
||||
var actionables = actionableFinder.get_overlapping_areas()
|
||||
if actionables.size() > 0:
|
||||
actionables[0].action()
|
||||
is_talking = true
|
||||
actionables[0].action(is_talking)
|
||||
return
|
||||
func _physics_process(delta):
|
||||
# Apply gravity
|
||||
if not is_on_floor():
|
||||
var gravity_multiplier = FALL_GRAVITY_MULTIPLIER if velocity.y > 0 else 2
|
||||
velocity.y += GRAVITY * gravity_multiplier * delta
|
||||
if !is_talking:
|
||||
# Handle Jump
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
start_jump()
|
||||
|
||||
if is_jumping:
|
||||
if Input.is_action_pressed("jump"):
|
||||
continue_jump(delta)
|
||||
else:
|
||||
end_jump()
|
||||
|
||||
# Handle Jump
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
start_jump()
|
||||
|
||||
if is_jumping:
|
||||
if Input.is_action_pressed("jump"):
|
||||
continue_jump(delta)
|
||||
# Handle horizontal movement
|
||||
var direction = Input.get_axis("move_left", "move_right")
|
||||
|
||||
if direction < 0:
|
||||
isFacingRight = false
|
||||
elif direction > 0:
|
||||
isFacingRight = true
|
||||
|
||||
if isFacingRight:
|
||||
$Marker2D.scale.x = 1
|
||||
$AnimatedSprite2D.scale.x = 1 * 0.2
|
||||
else:
|
||||
end_jump()
|
||||
$Marker2D.scale.x = -1
|
||||
$AnimatedSprite2D.scale.x = -1 * 0.2
|
||||
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
# Handle horizontal movement
|
||||
var direction = Input.get_axis("move_left", "move_right")
|
||||
|
||||
if direction < 0:
|
||||
isFacingRight = false
|
||||
elif direction > 0:
|
||||
isFacingRight = true
|
||||
|
||||
if isFacingRight:
|
||||
$Marker2D.scale.x = 1
|
||||
$AnimatedSprite2D.scale.x = 1 * 0.2
|
||||
else:
|
||||
$Marker2D.scale.x = -1
|
||||
$AnimatedSprite2D.scale.x = -1 * 0.2
|
||||
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
move_and_slide()
|
||||
|
||||
func start_jump():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
Reference in New Issue
Block a user