Skip to content

Commit

Permalink
Use a pivot node instead of flipping the player sprite (a-little-org-…
Browse files Browse the repository at this point in the history
…called-mario#378)

* Use a pivot node instead of flipping the player sprite

Also fixes an issue with crouching in reverse gravity (a-little-org-called-mario#280)

* Fix bus positioning

* post-merge

* Fix issue with gravity switching while crouched
  • Loading branch information
paulloz authored Apr 18, 2022
1 parent b4524d0 commit 1f1c24b
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 173 deletions.
4 changes: 2 additions & 2 deletions scenes/levels/Flappy/env/cloud.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ func _physics_process(delta: float) -> void:
queue_free()


func _on_Area2D_body_entered(body):
func _on_Area2D_body_entered(_body):
$EnterAudio.play()


func _on_Area2D_body_exited(body):
func _on_Area2D_body_exited(_body):
$ExitAudio.play()
2 changes: 1 addition & 1 deletion scenes/levels/paulloz/002.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ follow_vertically = true
position = Vector2( 96, 480 )
player_scene = ExtResource( 10 )

[node name="EndPortal2" parent="." instance=ExtResource( 4 )]
[node name="EndPortal" parent="." instance=ExtResource( 4 )]
position = Vector2( 3296, 1184 )

[node name="GravityZones" type="Node2D" parent="."]
Expand Down
36 changes: 17 additions & 19 deletions scenes/platformer/characters/Bus.gd
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
extends Node
extends Sprite

export(NodePath) var collision_shape: NodePath

var inventory = preload("res://scripts/resources/PlayerInventory.tres")
onready var player : Player = owner
onready var sprite : Sprite = player.get_node("BusSprite")
onready var collision : CollisionShape2D = player.get_node("BusCollision")

onready var player: Player = owner
onready var collision: CollisionShape2D = get_node(collision_shape)


func _ready() -> void:
EventBus.connect("bus_collected", self, "_on_bus_collected")
if inventory.has_bus:
_activate_bus()
else:
set_process(false)
call_deferred("_activate_bus", inventory.has_bus)


func _process(_delta: float) -> void:
sprite.flip_h = !player.sprite.flip_h
if inventory.has_bus:
player.powerupspeed = 4
player.powerupaccel = 2
Expand All @@ -28,19 +25,20 @@ func _process(_delta: float) -> void:
func _on_bus_collected(data: Dictionary) -> void:
if data.has("collected"):
inventory.has_bus = data["collected"]
_activate_bus()
call_deferred("_activate_bus", inventory.has_bus)


func _activate_bus() -> void:
sprite.visible = true
collision.set_deferred("disabled", false)
call_deferred("_update_player")
set_process(true)
func _activate_bus(active: bool) -> void:
# Sprites
visible = active
player.sprite.visible = !active

# Collision
collision.disabled = !active
player.collision.disabled = active

func _update_player() -> void:
player.sprite.visible = false
player.get_node("CollisionShape2D").set_deferred("disabled", true)
var trail: Line2D = player.get_node_or_null("Trail")
if trail != null:
trail.height = 15
trail.height = 15 if active else 30

set_process(active)
82 changes: 45 additions & 37 deletions scenes/platformer/characters/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ var super_jump_timer = 0.0
var powerupspeed = 1
var powerupaccel = 1

onready var sprite = $Sprite
onready var anim = $Sprite/Anims
onready var tween = $Tween
onready var pivot: Node2D = $Pivot
onready var sprite := $Pivot/Sprite
onready var anim: AnimationPlayer = $Pivot/Sprite/Anims
onready var tween: Tween = $Tween
onready var collision: CollisionShape2D = $Collision

onready var original_collision_extents: Vector2 = collision.shape.extents

onready var original_scale = sprite.scale
onready var squash_scale = Vector2(original_scale.x * 1.4, original_scale.y * 0.4)
Expand Down Expand Up @@ -136,7 +140,8 @@ func _physics_process(delta: float) -> void:
crouch()
else:
grounded = false
crouching = false
if crouching:
uncrouch()
coyote_timer -= delta
# while we're holding the jump button we should jump higher
if not super_jumping:
Expand All @@ -147,12 +152,10 @@ func _physics_process(delta: float) -> void:
anim.playAnim("Jump")

if crouching and not Input.is_action_pressed("down"):
crouching = false
set_hitbox_crouching(false)
unsquash()
uncrouch()

y_motion.set_accel(gravity.strength * gravity_multiplier)
sprite.flip_v = gravity.direction.y < 0
pivot.scale.y = gravity.direction.y

var move_and_slide_result = move_and_slide(
y_motion.update_motion() + x_motion.update_motion(), Vector2.UP
Expand All @@ -169,11 +172,17 @@ func _physics_process(delta: float) -> void:
y_motion.set_motion(move_and_slide_result)


func _is_on_floor() -> bool:
return (
(gravity.direction.y == Vector2.DOWN.y and is_on_floor())
or (gravity.direction.y == Vector2.UP.y and is_on_ceiling())
)


func try_slip(angle: float):
if angle == 0:
return false
var axis = "x" if is_equal_approx(angle, PI) else "y"
# is_equal_approx(abs(collision_angle - PI), PI/2)
var original_v = position[axis] # remember original value on axis
# check collisions in nearby positions within SLIP_RANGE
for r in range(1, SLIP_RANGE):
Expand All @@ -193,11 +202,26 @@ func crouch():
squash()


func uncrouch():
crouching = false
set_hitbox_crouching(false)
unsquash()


func set_hitbox_crouching(is_crouching: bool):
if is_crouching:
collision.shape.extents.y = original_collision_extents.y * 0.4
collision.position.y = (original_collision_extents.y - collision.shape.extents.y) * gravity.direction.y
else:
collision.shape.extents.y = original_collision_extents.y
collision.position.y = 0


func jump():
stats.jump_xp += 1
tween.stop_all()
anticipating_jump = true
set_hitbox_crouching(false)
uncrouch()
squash(0.03, 0, 0.5)
yield(tween, "tween_all_completed")
stretch(0.2, 0, 0.5, 1.2)
Expand Down Expand Up @@ -229,11 +253,11 @@ func land():


func look_right():
sprite.flip_h = false
pivot.scale.x = 1;


func look_left():
sprite.flip_h = true
pivot.scale.x = -1


func squash(time = 0.1, _returnDelay = 0, squash_modifier = 1.0):
Expand Down Expand Up @@ -298,6 +322,14 @@ func unsquash(time = 0.1, _returnDelay = 0, squash_modifier = 1.0):
tween.start()


func bounce(strength = 1100):
squash(0.075)
yield(tween, "tween_all_completed")
stretch(0.15)
coyote_timer = 0
y_motion.set_speed(-strength)


func jerk_left(jerk: float):
if x_motion.get_accel() > -MINACCEL:
x_motion.set_accel(-MINACCEL)
Expand All @@ -318,21 +350,6 @@ func reset() -> void:
_end_flash_sprite()


func bounce(strength = 1100):
squash(0.075)
yield(tween, "tween_all_completed")
stretch(0.15)
coyote_timer = 0
y_motion.set_speed(-strength)


func _is_on_floor() -> bool:
return (
(gravity.direction.y == Vector2.DOWN.y and is_on_floor())
or (gravity.direction.y == Vector2.UP.y and is_on_ceiling())
)


func _on_heart_change(delta: int, total: int):
if delta < 0:
$HurtSFX.play()
Expand All @@ -353,7 +370,7 @@ func _on_enemy_hit_fireball():
func flash_sprite(duration: float = 0.05) -> void:
var material: ShaderMaterial = sprite.material as ShaderMaterial
if material != null:
$Sprite.material.set_shader_param("flash_modifier", 1.0)
material.set_shader_param("flash_modifier", 1.0)
$HitFlashTimer.wait_time = duration
$HitFlashTimer.start()

Expand All @@ -362,12 +379,3 @@ func _end_flash_sprite() -> void:
var material: ShaderMaterial = sprite.material as ShaderMaterial
if material != null:
material.set_shader_param("flash_modifier", 0.0)


func set_hitbox_crouching(value: bool):
if value:
$CollisionShape2D.shape.extents.y = 27 * 0.4
$CollisionShape2D.position.y = 21
else:
$CollisionShape2D.shape.extents.y = 27
$CollisionShape2D.position.y = 5
Loading

0 comments on commit 1f1c24b

Please sign in to comment.