Tweaked floaty area

This commit is contained in:
2026-02-08 20:59:30 +11:00
parent ab0f5efa30
commit 29ff9b1349
2 changed files with 35 additions and 25 deletions

View File

@@ -15,7 +15,8 @@ var just_bounced: bool = false #allow max bounce height (i.e. ignore jump input)
#floaty field #floaty field
const FLOATY_RISING: int = -10 const FLOATY_RISING: int = -10
const FLOATY_FALLING: int = -30 const FLOATY_FALLING: int = -30
var floaty_counter: int = 0 const FLOATY_HEIGHT_CLEAR: float = 1_000_000_000
var floaty_height: float = FLOATY_HEIGHT_CLEAR
#limits #limits
const MAX_MOVE_SPEED: int = 300 const MAX_MOVE_SPEED: int = 300
@@ -24,49 +25,58 @@ const MAX_FLOATY_SPEED: int = -300
#boilerplate #boilerplate
func _ready(): func _ready():
clear_floaty_height()
_sprite.play("idle", 1) _sprite.play("idle", 1)
#movement #movement
func _physics_process(_delta) -> void: func _physics_process(_delta) -> void:
#print(position.y, " ", floaty_height)
#jump input #jump input
if is_on_floor() and Input.is_action_just_pressed("input_jump"): if is_on_floor() and Input.is_action_just_pressed("input_jump"):
velocity.y -= JUMP_FORCE velocity.y -= JUMP_FORCE
if floaty_counter > 0: #vertical movement
#inverted - could tweak the rise? elif is_floaty() and position.y >= floaty_height: #below the floaty surface
if is_rising(): if is_rising():
velocity.y += FLOATY_RISING velocity.y += FLOATY_RISING
else: else:
velocity.y += FLOATY_FALLING velocity.y += FLOATY_FALLING
#fall faster than you rise
elif is_floaty() and position.y < floaty_height: ##above the floaty surface, but still floaty
if is_rising():
velocity.y += GRAVITY_FALLING #rise slower?
else:
var diff = floaty_height - position.y
position.y += log(diff)
velocity.y = 0
#normally, fall faster than you rise
elif is_rising() and (Input.is_action_pressed("input_jump") or just_bounced): elif is_rising() and (Input.is_action_pressed("input_jump") or just_bounced):
velocity.y += GRAVITY_RISING velocity.y += GRAVITY_RISING
else: else:
velocity.y += GRAVITY_FALLING velocity.y += GRAVITY_FALLING
just_bounced = false just_bounced = false
#terminal velocity #sideways movement
if floaty_counter > 0:
if velocity.y < MAX_FLOATY_SPEED:
velocity.y = MAX_FLOATY_SPEED
elif is_falling() and velocity.y > MAX_FALL_SPEED:
velocity.y = MAX_FALL_SPEED
print(velocity.y)
#input, walking
var move_dir = Input.get_axis("input_left", "input_right") var move_dir = Input.get_axis("input_left", "input_right")
#move with a maximum value
if move_dir: if move_dir:
_sprite.flip_h = move_dir < 0 #fancy _sprite.flip_h = move_dir < 0 #fancy HD 4K graphics
velocity.x += MOVE_FORCE * move_dir velocity.x += MOVE_FORCE * move_dir
if abs(velocity.x) > MAX_MOVE_SPEED:
velocity.x = MAX_MOVE_SPEED * sign(velocity.x)
#no input, slow down #no input, slow down
elif velocity.x != 0: elif velocity.x != 0:
velocity.x = log(abs(velocity.x)) * sign(velocity.x) velocity.x = log(abs(velocity.x)) * sign(velocity.x)
#terminal velocity (in all directions)
if position.y > floaty_height and velocity.y < MAX_FLOATY_SPEED:
velocity.y = MAX_FLOATY_SPEED
if is_falling() and velocity.y > MAX_FALL_SPEED:
velocity.y = MAX_FALL_SPEED
if abs(velocity.x) > MAX_MOVE_SPEED:
velocity.x = MAX_MOVE_SPEED * sign(velocity.x)
#do the thing #do the thing
move_and_slide() move_and_slide()
@@ -87,10 +97,10 @@ func apply_bounce() -> void:
velocity.y = BOUNCE_FORCE velocity.y = BOUNCE_FORCE
just_bounced = true just_bounced = true
func enable_float() -> void: func set_floaty_height(height: float) -> void:
floaty_counter += 1 floaty_height = height
pass
func disable_float() -> void: func clear_floaty_height() -> void:
floaty_counter -= 1 floaty_height = FLOATY_HEIGHT_CLEAR
pass
func is_floaty() -> bool: return floaty_height != FLOATY_HEIGHT_CLEAR

View File

@@ -3,8 +3,8 @@ extends Area2D
func _on_body_entered(body) -> void: func _on_body_entered(body) -> void:
#print("collision") #print("collision")
if body is BoxBoy: if body is BoxBoy:
body.enable_float() body.set_floaty_height(position.y + $CollisionShape2D.shape.get_rect().position.y)
func _on_body_exited(body) -> void: func _on_body_exited(body) -> void:
if body is BoxBoy: if body is BoxBoy:
body.disable_float() body.clear_floaty_height()