Add pipe travel
This commit is contained in:
parent
90bd03d194
commit
f13ca3f44f
5 changed files with 485 additions and 22 deletions
|
|
@ -8,21 +8,39 @@ texture_region_size = Vector2i(32, 32)
|
|||
0:0/0 = 0
|
||||
0:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)
|
||||
1:0/0 = 0
|
||||
1:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)
|
||||
1:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, -2, -16, -2)
|
||||
1:0/0/physics_layer_0/polygon_1/points = PackedVector2Array(-16, 2, 16, 2, 16, 16, -16, 16)
|
||||
1:0/0/custom_data_0 = 10
|
||||
2:0/0 = 0
|
||||
2:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)
|
||||
2:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, -2, -16, -2, 16, -16, 16)
|
||||
2:0/0/physics_layer_0/polygon_1/points = PackedVector2Array(2, -16, 16, -16, 16, 16, 2, 16)
|
||||
2:0/0/custom_data_0 = 5
|
||||
3:0/0 = 0
|
||||
3:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)
|
||||
3:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, -2, -16, -2, -2, -16, -2)
|
||||
3:0/0/physics_layer_0/polygon_1/points = PackedVector2Array(2, -16, 16, -16, 16, -2, 2, -2)
|
||||
3:0/0/physics_layer_0/polygon_2/points = PackedVector2Array(-16, 2, -2, 2, -2, 16, -16, 16)
|
||||
3:0/0/physics_layer_0/polygon_3/points = PackedVector2Array(2, 2, 16, 2, 16, 16, 2, 16)
|
||||
3:0/0/custom_data_0 = 15
|
||||
4:0/0 = 0
|
||||
4:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)
|
||||
4:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, -2, -16, -2, 2, 16, 2, 16, 16, -16, 16)
|
||||
4:0/0/physics_layer_0/polygon_1/points = PackedVector2Array(2, -16, 16, -16, 16, -2, 2, -2)
|
||||
4:0/0/custom_data_0 = 3
|
||||
5:0/0 = 0
|
||||
5:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)
|
||||
5:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(2, -16, 16, -16, 16, 16, -16, 16, -16, 2, 2, 2)
|
||||
5:0/0/physics_layer_0/polygon_1/points = PackedVector2Array(-16, -16, -2, -16, -2, -2, -16, -2)
|
||||
5:0/0/custom_data_0 = 9
|
||||
0:1/0 = 0
|
||||
0:1/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)
|
||||
0:1/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, 2, 16, 2, -2, -16, -2)
|
||||
0:1/0/physics_layer_0/polygon_1/points = PackedVector2Array(-16, 2, -2, 2, -2, 16, -16, 16)
|
||||
0:1/0/custom_data_0 = 12
|
||||
1:1/0 = 0
|
||||
1:1/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)
|
||||
1:1/0/physics_layer_0/polygon_0/points = PackedVector2Array(-16, -16, 16, -16, 16, -2, -2, -2, -2, 16, -16, 16)
|
||||
1:1/0/physics_layer_0/polygon_1/points = PackedVector2Array(2, 2, 16, 2, 16, 16, 2, 16)
|
||||
1:1/0/custom_data_0 = 6
|
||||
|
||||
[resource]
|
||||
tile_size = Vector2i(32, 32)
|
||||
physics_layer_0/collision_layer = 1
|
||||
custom_data_layer_0/name = "tube_openings"
|
||||
custom_data_layer_0/type = 2
|
||||
sources/0 = SubResource("TileSetAtlasSource_0cw2b")
|
||||
|
|
|
|||
|
|
@ -44,6 +44,27 @@ const MIN_LAND_SPEED_FOR_SPLAT = 60.0
|
|||
const LAND_PITCH_MIN = 0.87
|
||||
const LAND_PITCH_MAX = 1.15
|
||||
|
||||
# Tubes are tiles too narrow for us, tagged in their TileSet with which sides are
|
||||
# open (the "tube_openings" custom data). Moving into a tube's mouth squeezes us
|
||||
# through as a stream of pixels, and we reform past the other end.
|
||||
const TUBE_DATA_LAYER = "tube_openings"
|
||||
const TUBE_OPEN_UP = 1
|
||||
const TUBE_OPEN_RIGHT = 2
|
||||
const TUBE_OPEN_DOWN = 4
|
||||
const TUBE_OPEN_LEFT = 8
|
||||
const TUBE_WIDTH = 4.0 # The channel drawn through the tiles in LevelTiles.png.
|
||||
const TUBE_ENTRY_REACH = 2.0 # How close our side must be to a tube mouth to go in.
|
||||
const TUBE_MAX_TILES = 256 # Give up following a tube this long.
|
||||
# Coming out of a tube sideways with a floor this close below, land on it
|
||||
# instead of sticking to the wall.
|
||||
const TUBE_EXIT_FLOOR_REACH = 8.0
|
||||
const TUBE_REENTRY_DELAY = 0.3 # So we don't dive straight back in.
|
||||
const TUBE_ENTER_HOLD_TIME = 0.35 # How long to press into a mouth before we squeeze in.
|
||||
# How straight into a mouth we must press (dot product with the way in; 0.9 is
|
||||
# within about 25 degrees). Diagonals don't count, so they can climb walls instead.
|
||||
const TUBE_ENTER_MIN_ALIGNMENT = 0.9
|
||||
const REST_FRAME = 0 # The Idle animation's frame. We reform as it.
|
||||
|
||||
###############
|
||||
## Variables ##
|
||||
###############
|
||||
|
|
@ -86,11 +107,22 @@ var isCrawling := false # Moving along the surface we're on.
|
|||
var isHoldingAtEdge := false
|
||||
var isPressingIntoSurface := false
|
||||
var isReachingAway := false # Holding away from the surface, counting towards letting go/hopping.
|
||||
var isSqueezingIntoTube := false # Holding into a tube's mouth, about to go in.
|
||||
|
||||
var isInTube := false
|
||||
var tubePixels: PixelStream # Our pixels on their way through.
|
||||
var tubeExitPosition := Vector2.ZERO # Where we reappear.
|
||||
var tubeExitNormal := Vector2.UP # The surface we'll be on when we do.
|
||||
var tubeReentryCooldown := 0.0
|
||||
var timeSpentSqueezingIntoTube := 0.0
|
||||
var tubeLayers: Array[TileMapLayer] = [] # TileMapLayers whose tiles can be tubes.
|
||||
var hasFoundTubeLayers := false
|
||||
|
||||
###########
|
||||
## Nodes ##
|
||||
###########
|
||||
@onready var sprite: Sprite2D = $Sprite2D
|
||||
@onready var animationTree: AnimationTree = $AnimationTree
|
||||
@onready var collisionShape: CollisionShape2D = $CollisionShape2D
|
||||
# Edge-finding raycasts, one each side, under a pivot we turn to face the surface we're on.
|
||||
@onready var edgeSensors: Node2D = $EdgeSensors
|
||||
|
|
@ -122,9 +154,14 @@ func _physics_process(delta: float) -> void:
|
|||
# Note: Up is positive y.
|
||||
var moveInput := Vector2(Input.get_axis("moveLeft", "moveRight"), Input.get_axis("moveDown", "moveUp"))
|
||||
|
||||
if isInTube:
|
||||
if tubePixels.isFinished:
|
||||
_exit_tube(delta)
|
||||
return
|
||||
if isWrapping:
|
||||
_update_wrap(delta)
|
||||
return
|
||||
tubeReentryCooldown = maxf(tubeReentryCooldown - delta, 0.0)
|
||||
|
||||
var wasInAir := (_get_surface_normal() == Vector2.ZERO)
|
||||
if not wasInAir:
|
||||
|
|
@ -132,6 +169,8 @@ func _physics_process(delta: float) -> void:
|
|||
|
||||
if isStickingToWall and moveInput.y != 0.0:
|
||||
wallFacingY = -signf(moveInput.y)
|
||||
if _update_tube_squeeze(delta, moveInput):
|
||||
return
|
||||
if _start_edge_wrap(moveInput):
|
||||
return
|
||||
|
||||
|
|
@ -192,9 +231,9 @@ func _update_facing(moveInput: Vector2) -> void:
|
|||
func _update_animation_conditions(moveInput: Vector2) -> void:
|
||||
isOnSurface = _get_surface_normal() != Vector2.ZERO
|
||||
isCrawling = ((moveInput.x != 0.0 and (isStickingToCeiling or is_on_floor()))
|
||||
or (moveInput.y != 0.0 and isStickingToWall))
|
||||
or (moveInput.y != 0.0 and isStickingToWall)) and not isSqueezingIntoTube
|
||||
isHoldingAtEdge = timeSpentHoldingEdge > 0
|
||||
isPressingIntoSurface = _is_pressing_into_surface(moveInput)
|
||||
isPressingIntoSurface = _is_pressing_into_surface(moveInput) or isSqueezingIntoTube
|
||||
isReachingAway = ((is_on_floor() and timeSpentHoldingUpOnFloor > 0)
|
||||
or (isStickingToCeiling and timeSpentUnstickingCeiling > 0)
|
||||
or (isStickingToWall and timeSpentUnstickingWall > 0))
|
||||
|
|
@ -477,9 +516,12 @@ func _find_edge_corner(normal: Vector2, direction: Vector2, reach: float) -> Var
|
|||
return null
|
||||
var corner: Vector2 = sideHit.position + normal * 2
|
||||
|
||||
# 3. A real edge has empty space past it. Tile seams and the foot of a wall don't.
|
||||
# 3. A real edge has empty space past it. Tile seams and the foot of a wall
|
||||
# don't, and a tube's mouth is too narrow to count.
|
||||
if not _is_point_free(corner + direction * 2 - normal * 2):
|
||||
return null
|
||||
if not _is_point_free(corner + direction * radius - normal * 2):
|
||||
return null
|
||||
return corner
|
||||
|
||||
|
||||
|
|
@ -623,14 +665,23 @@ func _rotation_for(normal: Vector2) -> float:
|
|||
|
||||
|
||||
func _finish_wrap(delta: float) -> void:
|
||||
# Close the WRAP_MARGIN gap with move_and_slide() so is_on_floor()/is_on_wall()
|
||||
# describe where we are now, not where we were before the wrap.
|
||||
velocity = -wrapOntoNormal * (WRAP_MARGIN + 1) / delta
|
||||
_settle_onto_surface(wrapOntoNormal, WRAP_MARGIN, delta)
|
||||
# Keep our speed along the new surface so leaving the wrap isn't a sudden stop.
|
||||
velocity = -wrapFromNormal * wrapSpeed
|
||||
isWrapping = false
|
||||
|
||||
|
||||
# After being moved somewhere directly (a wrap or a tube), close the `gap` to the
|
||||
# surface facing `normal` and stick to it.
|
||||
func _settle_onto_surface(normal: Vector2, gap: float, delta: float) -> void:
|
||||
# Close the gap with move_and_slide() so is_on_floor()/is_on_wall() describe
|
||||
# where we are now, not where we were before.
|
||||
velocity = -normal * (gap + 1) / delta
|
||||
|
||||
# Godot still thinks we're on the floor we started from, so it would snap us
|
||||
# down to it and count the wall as floor (floor_block_on_wall). Turn both off
|
||||
# for this move unless we're ending on a floor.
|
||||
var endingOnFloor := wrapOntoNormal == Vector2.UP
|
||||
var endingOnFloor := normal == Vector2.UP
|
||||
var savedFloorSnap := floor_snap_length
|
||||
var savedBlockOnWall := floor_block_on_wall
|
||||
if not endingOnFloor:
|
||||
|
|
@ -640,17 +691,214 @@ func _finish_wrap(delta: float) -> void:
|
|||
floor_snap_length = savedFloorSnap
|
||||
floor_block_on_wall = savedBlockOnWall
|
||||
|
||||
# Keep our speed along the new surface so leaving the wrap isn't a sudden stop.
|
||||
velocity = -wrapFromNormal * wrapSpeed
|
||||
|
||||
isWrapping = false
|
||||
isStickingToCeiling = wrapOntoNormal.y > 0.5
|
||||
isStickingToWall = absf(wrapOntoNormal.x) > 0.5
|
||||
isStickingToCeiling = normal.y > 0.5
|
||||
isStickingToWall = absf(normal.x) > 0.5
|
||||
if isStickingToWall:
|
||||
wallNormalX = wrapOntoNormal.x
|
||||
wallNormalX = normal.x
|
||||
ignoreWallUnstickUntilRelease = true
|
||||
ignoreCeilingUnstickUntilRelease = isStickingToCeiling
|
||||
|
||||
|
||||
# ---- Tubes ----
|
||||
|
||||
# Holding into the mouth of a tube (while stuck to a surface) for
|
||||
# TUBE_ENTER_HOLD_TIME squeezes us through it, if it leads somewhere we fit.
|
||||
# Meanwhile we hold still against the mouth. True while squeezing or once we're in.
|
||||
func _update_tube_squeeze(delta: float, moveInput: Vector2) -> bool:
|
||||
var path := PackedVector2Array()
|
||||
if tubeReentryCooldown <= 0.0 and _get_surface_normal() != Vector2.ZERO:
|
||||
path = _find_tube(moveInput)
|
||||
isSqueezingIntoTube = not path.is_empty()
|
||||
if not isSqueezingIntoTube:
|
||||
timeSpentSqueezingIntoTube = 0.0
|
||||
return false
|
||||
|
||||
timeSpentSqueezingIntoTube += delta
|
||||
if timeSpentSqueezingIntoTube >= TUBE_ENTER_HOLD_TIME:
|
||||
timeSpentSqueezingIntoTube = 0.0
|
||||
isSqueezingIntoTube = false
|
||||
_enter_tube(path)
|
||||
return true
|
||||
|
||||
velocity = Vector2.ZERO
|
||||
_update_facing(moveInput)
|
||||
_update_animation_conditions(moveInput)
|
||||
_update_sprite_rotation(delta)
|
||||
return true
|
||||
|
||||
|
||||
# The path of a tube whose mouth we're pressing straight into, if it leads
|
||||
# somewhere we fit. Empty if there isn't one. Only straight in: on the floor
|
||||
# beside a wall with a mouth in it, up and towards the wall climbs the wall.
|
||||
func _find_tube(moveInput: Vector2) -> PackedVector2Array:
|
||||
if moveInput == Vector2.ZERO:
|
||||
return PackedVector2Array()
|
||||
var inputDirection := Vector2(moveInput.x, -moveInput.y).normalized() # Up is negative y.
|
||||
for direction: Vector2 in [Vector2.UP, Vector2.RIGHT, Vector2.DOWN, Vector2.LEFT]:
|
||||
if inputDirection.dot(direction) < TUBE_ENTER_MIN_ALIGNMENT:
|
||||
continue
|
||||
return _find_tube_towards(direction)
|
||||
return PackedVector2Array()
|
||||
|
||||
|
||||
func _find_tube_towards(direction: Vector2) -> PackedVector2Array:
|
||||
var radius := _get_radius()
|
||||
var across := direction.orthogonal()
|
||||
var reach := radius + TUBE_ENTRY_REACH
|
||||
for layer in _get_tube_layers():
|
||||
# Check the tile ahead of our middle and of each side, in case we're
|
||||
# between two rows of tiles.
|
||||
for side: float in [0.0, -radius, radius]:
|
||||
var probe := global_position + direction * reach + across * side
|
||||
var cell := layer.local_to_map(layer.to_local(probe))
|
||||
if _get_tube_openings(layer, cell) & _tube_side_bit(-direction) == 0:
|
||||
continue
|
||||
# The mouth must be within our width, and touching (or nearly) our side.
|
||||
var toMouth := _tube_cell_point(layer, cell, -direction) - global_position
|
||||
if absf(toMouth.dot(across)) > radius or toMouth.dot(direction) > reach:
|
||||
continue
|
||||
var path := _trace_tube(layer, cell, direction)
|
||||
if not path.is_empty() and not _tube_exit_pose(path).is_empty():
|
||||
return path
|
||||
return PackedVector2Array()
|
||||
|
||||
|
||||
# The tube's path from the mouth of `cell` we go in heading `direction`, round
|
||||
# each bend, to the mouth it comes out of into an empty tile. Empty if it's
|
||||
# blocked or a dead end.
|
||||
func _trace_tube(layer: TileMapLayer, cell: Vector2i, direction: Vector2) -> PackedVector2Array:
|
||||
var path := PackedVector2Array([_tube_cell_point(layer, cell, -direction)])
|
||||
for step in TUBE_MAX_TILES:
|
||||
var openings := _get_tube_openings(layer, cell)
|
||||
if openings & _tube_side_bit(-direction) == 0:
|
||||
return PackedVector2Array() # This tile doesn't connect to the last one.
|
||||
|
||||
# Straight on if we can (through crossings), otherwise round the bend.
|
||||
if openings & _tube_side_bit(direction) == 0:
|
||||
var turn := direction.orthogonal()
|
||||
if openings & _tube_side_bit(turn) == 0:
|
||||
turn = -turn
|
||||
if openings & _tube_side_bit(turn) == 0:
|
||||
return PackedVector2Array() # Dead end.
|
||||
path.append(_tube_cell_point(layer, cell, Vector2.ZERO))
|
||||
direction = turn
|
||||
|
||||
var nextCell := cell + Vector2i(direction)
|
||||
if layer.get_cell_source_id(nextCell) == -1:
|
||||
path.append(_tube_cell_point(layer, cell, direction))
|
||||
return path
|
||||
cell = nextCell
|
||||
return PackedVector2Array()
|
||||
|
||||
|
||||
# Where we come out of the tube along `path`: .position (where the sprite
|
||||
# reforms), .normal (the surface we'll be stuck to) and .appearAt (where we
|
||||
# reappear, a pixel off it). Empty if there's no room for us.
|
||||
func _tube_exit_pose(path: PackedVector2Array) -> Dictionary:
|
||||
var exitMouth := path[path.size() - 1]
|
||||
var exitDirection := (exitMouth - path[path.size() - 2]).normalized().round()
|
||||
var radius := _get_radius()
|
||||
|
||||
# Come out stuck to the face the tube opens from, like after a wrap...
|
||||
var exitNormal := exitDirection
|
||||
var exitPosition := exitMouth + exitDirection * radius
|
||||
# ...unless it opens sideways just above a floor: land on that instead.
|
||||
if exitDirection.y == 0.0:
|
||||
var besideWall := exitMouth + exitDirection * (radius + 1)
|
||||
var floorHit := _cast_ray(besideWall, besideWall + Vector2.DOWN * (radius + TUBE_EXIT_FLOOR_REACH))
|
||||
if not floorHit.is_empty() and floorHit.normal.y < -0.5:
|
||||
exitNormal = Vector2.UP
|
||||
exitPosition = Vector2(besideWall.x, floorHit.position.y - radius)
|
||||
|
||||
# We reappear a pixel off the surface and settle onto it (see _exit_tube).
|
||||
var appearAt := exitPosition + exitNormal
|
||||
if not _is_spot_free(appearAt):
|
||||
return {}
|
||||
return {position = exitPosition, normal = exitNormal, appearAt = appearAt}
|
||||
|
||||
|
||||
# Starts streaming through the tube along `path` (one _find_tube gave us).
|
||||
func _enter_tube(path: PackedVector2Array) -> void:
|
||||
var exitPose := _tube_exit_pose(path)
|
||||
tubeExitPosition = exitPose.appearAt
|
||||
tubeExitNormal = exitPose.normal
|
||||
tubePixels = PixelStream.new()
|
||||
get_parent().add_child(tubePixels)
|
||||
var reformedSprite := Transform2D(_rotation_for(exitPose.normal), (exitPose.position as Vector2).round())
|
||||
tubePixels.start(sprite, path, TUBE_WIDTH, reformedSprite, REST_FRAME)
|
||||
|
||||
isInTube = true
|
||||
velocity = Vector2.ZERO
|
||||
isStickingToWall = false
|
||||
isStickingToCeiling = false
|
||||
isHoppingAcross = false
|
||||
timeSpentHoldingEdge = 0.0
|
||||
timeSpentHoldingUpOnFloor = 0.0
|
||||
timeSpentUnstickingWall = 0.0
|
||||
timeSpentUnstickingCeiling = 0.0
|
||||
hide()
|
||||
collisionShape.set_deferred("disabled", true)
|
||||
wrapSound.play()
|
||||
|
||||
|
||||
func _exit_tube(delta: float) -> void:
|
||||
tubePixels.queue_free()
|
||||
tubePixels = null
|
||||
isInTube = false
|
||||
tubeReentryCooldown = TUBE_REENTRY_DELAY
|
||||
|
||||
# Visible again before anything below, as Godot ignores
|
||||
# reset_physics_interpolation() on hidden nodes.
|
||||
show()
|
||||
global_position = tubeExitPosition
|
||||
collisionShape.disabled = false
|
||||
_set_sprite_rotation(_rotation_for(tubeExitNormal))
|
||||
_settle_onto_surface(tubeExitNormal, 1.0, delta)
|
||||
velocity = Vector2.ZERO
|
||||
# A jump, not movement: don't smooth from where we went in. Without this the
|
||||
# sprite flashes up at the entrance for a frame.
|
||||
reset_physics_interpolation()
|
||||
# Show the frame our pixels just made, and let the AnimationTree carry on from
|
||||
# there. Without start() it would still be in whatever state we went in with.
|
||||
sprite.frame = REST_FRAME
|
||||
_update_animation_conditions(Vector2.ZERO)
|
||||
var playback: AnimationNodeStateMachinePlayback = animationTree.get("parameters/playback")
|
||||
playback.start(&"Idle")
|
||||
wrapSound.play()
|
||||
|
||||
|
||||
func _get_tube_layers() -> Array[TileMapLayer]:
|
||||
if hasFoundTubeLayers:
|
||||
return tubeLayers
|
||||
hasFoundTubeLayers = true
|
||||
for node in get_tree().root.find_children("*", "TileMapLayer", true, false):
|
||||
var layer := node as TileMapLayer
|
||||
if layer.tile_set and layer.tile_set.get_custom_data_layer_by_name(TUBE_DATA_LAYER) != -1:
|
||||
tubeLayers.append(layer)
|
||||
return tubeLayers
|
||||
|
||||
|
||||
# Which sides of the tile at `cell` are open (TUBE_OPEN_... bits). 0 if it isn't a tube.
|
||||
func _get_tube_openings(layer: TileMapLayer, cell: Vector2i) -> int:
|
||||
var data := layer.get_cell_tile_data(cell)
|
||||
if data == null:
|
||||
return 0
|
||||
return data.get_custom_data(TUBE_DATA_LAYER)
|
||||
|
||||
|
||||
func _tube_side_bit(direction: Vector2) -> int:
|
||||
if direction == Vector2.UP: return TUBE_OPEN_UP
|
||||
if direction == Vector2.RIGHT: return TUBE_OPEN_RIGHT
|
||||
if direction == Vector2.DOWN: return TUBE_OPEN_DOWN
|
||||
if direction == Vector2.LEFT: return TUBE_OPEN_LEFT
|
||||
return 0
|
||||
|
||||
|
||||
# The global middle of `cell`'s side facing `side`, or its centre for Vector2.ZERO.
|
||||
func _tube_cell_point(layer: TileMapLayer, cell: Vector2i, side: Vector2) -> Vector2:
|
||||
var halfTile := Vector2(layer.tile_set.tile_size) / 2.0
|
||||
return layer.to_global(layer.map_to_local(cell) + side * halfTile)
|
||||
|
||||
# ---- Effects ----
|
||||
|
||||
func _splat(normal: Vector2) -> void:
|
||||
|
|
|
|||
196
Effects/pixel_stream.gd
Executable file
196
Effects/pixel_stream.gd
Executable file
|
|
@ -0,0 +1,196 @@
|
|||
class_name PixelStream
|
||||
extends Node2D
|
||||
|
||||
# Takes a sprite's current frame apart into its pixels and pours them along a
|
||||
# path as a stream, then puts them back together as a chosen frame somewhere
|
||||
# else. Used for the slime squeezing through tubes.
|
||||
# Every pixel sets off at once: they're drawn into the entry mouth nearest-first,
|
||||
# flow along the path a few abreast, and fly out of the exit mouth so that they
|
||||
# all land at the same moment.
|
||||
# Pixels keep their colours on the way, and blend into the colour of the spot
|
||||
# they land on, so the sprite reforms exactly.
|
||||
# Watch isFinished to know when every pixel is back in place.
|
||||
|
||||
const SPEED = 240.0 # How fast pixels flow along the path, in px/s.
|
||||
const ROW_SPACING = 1.0 # Gap between rows of pixels in the stream.
|
||||
const GATHER_TIME = 0.12 # How long the nearest pixels take to reach the entry mouth.
|
||||
const LAND_TIME = 0.15 # How long the last pixels out take to reach their spot.
|
||||
|
||||
var isFinished := false
|
||||
|
||||
var _startColors := PackedColorArray()
|
||||
var _endColors := PackedColorArray()
|
||||
var _starts := PackedVector2Array() # Where each pixel was in the sprite we took apart.
|
||||
var _ends := PackedVector2Array() # Where it goes in the sprite we're putting back together.
|
||||
var _lanes := PackedFloat32Array() # How far left of the path's middle it flows.
|
||||
var _rows := PackedInt32Array() # Its place in the stream: row 0 goes in first.
|
||||
var _rowCount := 0
|
||||
|
||||
var _path := PackedVector2Array()
|
||||
var _pathDistances := PackedFloat32Array() # How far along the path each point is.
|
||||
var _time := 0.0
|
||||
|
||||
|
||||
# path: global points from the entry mouth to the exit mouth.
|
||||
# width: how wide the tube is. The stream is this many pixels across.
|
||||
# endTransform: the sprite's global transform once it's back together.
|
||||
# endFrame: the frame it reforms as, so it matches what the sprite shows next.
|
||||
func start(sprite: Sprite2D, path: PackedVector2Array, width: float, endTransform: Transform2D, endFrame: int) -> void:
|
||||
top_level = true
|
||||
global_position = Vector2.ZERO
|
||||
z_index = sprite.z_index + 1
|
||||
|
||||
_path = path
|
||||
_pathDistances.append(0.0)
|
||||
for i in range(1, path.size()):
|
||||
_pathDistances.append(_pathDistances[i - 1] + path[i - 1].distance_to(path[i]))
|
||||
|
||||
# Where the sprite is drawn now: snapped to whole pixels like the renderer does.
|
||||
var startTransform := sprite.get_global_transform()
|
||||
startTransform.origin = startTransform.origin.round()
|
||||
var from := _get_frame_pixels(sprite, sprite.frame, startTransform)
|
||||
var to := _get_frame_pixels(sprite, endFrame, endTransform)
|
||||
if from.colors.is_empty() or to.colors.is_empty():
|
||||
isFinished = true
|
||||
return
|
||||
_pair_pixels(from, to, maxi(int(width), 1))
|
||||
|
||||
|
||||
# The frame's opaque pixels (black ones too: they're the eyes) as .positions
|
||||
# (global, through `spriteTransform`) and .colors.
|
||||
func _get_frame_pixels(sprite: Sprite2D, frame: int, spriteTransform: Transform2D) -> Dictionary:
|
||||
var positions := PackedVector2Array()
|
||||
var colors := PackedColorArray()
|
||||
var image := _get_frame_image(sprite, frame)
|
||||
if image != null:
|
||||
image.convert(Image.FORMAT_RGBA8)
|
||||
var size := image.get_size()
|
||||
for y in size.y:
|
||||
for x in size.x:
|
||||
var color := image.get_pixel(x, y)
|
||||
if color.a < 0.5:
|
||||
continue
|
||||
# Pixel centre in the sprite's local space (it's centered).
|
||||
var local := Vector2(x, y) + Vector2.ONE * 0.5 - Vector2(size) / 2.0
|
||||
if sprite.flip_h:
|
||||
local.x = -local.x
|
||||
if sprite.flip_v:
|
||||
local.y = -local.y
|
||||
positions.append(spriteTransform * (local + sprite.offset))
|
||||
colors.append(color)
|
||||
return {positions = positions, colors = colors}
|
||||
|
||||
|
||||
# The pixels of one cell of the sprite's hframes x vframes sheet.
|
||||
static func _get_frame_image(sprite: Sprite2D, frame: int) -> Image:
|
||||
var image := sprite.texture.get_image()
|
||||
if image == null:
|
||||
return null
|
||||
if image.is_compressed():
|
||||
image.decompress()
|
||||
var frameSize := image.get_size() / Vector2i(sprite.hframes, sprite.vframes)
|
||||
var frameCoords := Vector2i(frame % sprite.hframes, floori(float(frame) / sprite.hframes))
|
||||
return image.get_region(Rect2i(frameCoords * frameSize, frameSize))
|
||||
|
||||
|
||||
# Gives each pixel a place in the stream and a spot in the reformed sprite. The
|
||||
# pixels nearest the entry go in first, and the first ones out land furthest
|
||||
# from the exit, so pixels don't cross paths much.
|
||||
# If one frame has more pixels, some of the other's are doubled up so none go
|
||||
# missing from either.
|
||||
func _pair_pixels(from: Dictionary, to: Dictionary, laneCount: int) -> void:
|
||||
var fromOrder := _sorted_by_distance(from.positions, _path[0], false)
|
||||
var toOrder := _sorted_by_distance(to.positions, _path[_path.size() - 1], true)
|
||||
|
||||
var fromCount := fromOrder.size()
|
||||
var toCount := toOrder.size()
|
||||
var count := maxi(fromCount, toCount)
|
||||
for k in count:
|
||||
var i: int = fromOrder[int(float(k) * fromCount / count)]
|
||||
var j: int = toOrder[int(float(k) * toCount / count)]
|
||||
_starts.append(from.positions[i])
|
||||
_startColors.append(from.colors[i])
|
||||
_ends.append(to.positions[j])
|
||||
_endColors.append(to.colors[j])
|
||||
_lanes.append((k % laneCount) - (laneCount - 1) / 2.0)
|
||||
_rows.append(floori(float(k) / laneCount))
|
||||
_rowCount = _rows[count - 1] + 1
|
||||
|
||||
|
||||
# Indices of `points`, nearest to `target` first (or furthest first).
|
||||
func _sorted_by_distance(points: PackedVector2Array, target: Vector2, furthestFirst: bool) -> Array:
|
||||
var order := range(points.size())
|
||||
order.sort_custom(func(a: int, b: int) -> bool:
|
||||
var aDistance := points[a].distance_squared_to(target)
|
||||
var bDistance := points[b].distance_squared_to(target)
|
||||
if furthestFirst:
|
||||
return aDistance > bDistance
|
||||
return aDistance < bDistance)
|
||||
return order
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if isFinished:
|
||||
return
|
||||
_time += delta
|
||||
if _time >= _duration():
|
||||
isFinished = true
|
||||
queue_redraw()
|
||||
|
||||
|
||||
# ---- Timing ----
|
||||
|
||||
# When a row reaches the entry mouth. Each row is ROW_SPACING behind the last.
|
||||
func _enter_time(row: int) -> float:
|
||||
return GATHER_TIME + row * ROW_SPACING / SPEED
|
||||
|
||||
# When a row leaves the exit mouth.
|
||||
func _exit_time(row: int) -> float:
|
||||
return _enter_time(row) + _tube_length() / SPEED
|
||||
|
||||
# When every pixel is back in place.
|
||||
func _duration() -> float:
|
||||
return _exit_time(_rowCount - 1) + LAND_TIME
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
var tubeLength := _tube_length()
|
||||
var duration := _duration()
|
||||
for i in _starts.size():
|
||||
var row := _rows[i]
|
||||
var enterTime := _enter_time(row)
|
||||
var exitTime := _exit_time(row)
|
||||
var spot: Vector2
|
||||
var color := _startColors[i]
|
||||
if _time < enterTime:
|
||||
# Drawn in, speeding up towards the mouth.
|
||||
var progress := _time / enterTime
|
||||
spot = _starts[i].lerp(_point_on_path(0.0, _lanes[i]), progress * progress)
|
||||
elif _time < exitTime:
|
||||
spot = _point_on_path((_time - enterTime) * SPEED, _lanes[i])
|
||||
else:
|
||||
# Out to its spot, slowing down to land with everyone else.
|
||||
var progress := minf((_time - exitTime) / (duration - exitTime), 1.0)
|
||||
var eased := 1.0 - (1.0 - progress) * (1.0 - progress)
|
||||
spot = _point_on_path(tubeLength, _lanes[i]).lerp(_ends[i], eased)
|
||||
color = _startColors[i].lerp(_endColors[i], eased)
|
||||
draw_rect(Rect2(spot.floor(), Vector2.ONE), color)
|
||||
|
||||
|
||||
# The point `distance` along the path, moved `lane` to the left of travel.
|
||||
func _point_on_path(distance: float, lane: float) -> Vector2:
|
||||
var segment := 0
|
||||
while segment < _path.size() - 2 and distance > _pathDistances[segment + 1]:
|
||||
segment += 1
|
||||
var from := _path[segment]
|
||||
var to := _path[segment + 1]
|
||||
var segmentLength := _pathDistances[segment + 1] - _pathDistances[segment]
|
||||
var progress := 0.0
|
||||
if segmentLength > 0.0:
|
||||
progress = clampf((distance - _pathDistances[segment]) / segmentLength, 0.0, 1.0)
|
||||
var left := (to - from).normalized().orthogonal() # Left of travel, with y down.
|
||||
return from.lerp(to, progress) + left * lane
|
||||
|
||||
|
||||
func _tube_length() -> float:
|
||||
return _pathDistances[_pathDistances.size() - 1]
|
||||
1
Effects/pixel_stream.gd.uid
Executable file
1
Effects/pixel_stream.gd.uid
Executable file
|
|
@ -0,0 +1 @@
|
|||
uid://ckliy0rj5o7qj
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
position = Vector2(329, 266)
|
||||
|
||||
[node name="TileMapLayer" type="TileMapLayer" parent="." unique_id=459516426]
|
||||
tile_map_data = PackedByteArray("AAAAAA4AAAAAAAAAAAABAA4AAAAAAAAAAAACAA4AAAAAAAAAAAADAA4AAAAAAAAAAAAEAA4AAAAAAAAAAAAFAA4AAAAAAAAAAAAGAA4AAAAAAAAAAAAHAA4AAAAAAAAAAAAIAA4AAAAAAAAAAAAJAA4AAAAAAAAAAAAKAA4AAAAAAAAAAAALAA4AAAAAAAAAAAAMAA4AAAAAAAAAAAANAA4AAAAAAAAAAAAOAA4AAAAAAAAAAAAPAA4AAAAAAAAAAAAQAA4AAAAAAAAAAAARAA4AAAAAAAAAAAASAA4AAAAAAAAAAAATAA4AAAAAAAAAAAAKAAsAAAAAAAAAAAAJAAsAAAAAAAAAAAAJAAwAAAAAAAAAAAAJAA0AAAAAAAAAAAAKAA0AAAAAAAAAAAAKAAwAAAAAAAAAAAATAA0AAAAAAAAAAAATAAwAAAAAAAAAAAATAAsAAAAAAAAAAAATAAoAAAAAAAAAAAATAAkAAAAAAAAAAAATAAgAAAAAAAAAAAATAAcAAAAAAAAAAAATAAYAAAAAAAAAAAATAAUAAAAAAAAAAAATAAQAAAAAAAAAAAATAAMAAAAAAAAAAAATAAIAAAAAAAAAAAATAAEAAAAAAAAAAAATAAAAAAAAAAAAAAASAAAAAAAAAAAAAAARAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAPAAAAAAAAAAAA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AAAAAAAAAACAAMAAAAAAAAAAAACAAIAAAAAAAAAAAAEAAIAAAAAAAAAAAAEAAMAAAAAAAAAAAAEAAQAAAAAAAAAAAAEAAUAAAAAAAAAAAASAAQAAAAAAAAAAAARAAQAAAAAAAAAAAAQAAQAAAAAAAAAAAAPAAQAAAAAAAAAAAAOAAQAAAACAAAAAAANAAQAAAAAAAAAAAAMAAQAAAAAAAAAAAALAAQAAAAAAAAAAAAOAAMAAAADAAAAAAANAAMAAAABAAAAAAAMAAMAAAAEAAAAAAAMAAIAAAAAAAEAAAALAAIAAAABAAAAAAALAAMAAAAAAAAAAAALAAEAAAAAAAAAAAAPAAMAAAABAAAAAAAQAAMAAAABAAAAAAARAAMAAAAFAAAAAAARAAEAAAAAAAEAAAAOAAEAAAABAAEAAAAQAAEAAAABAAAAAAAPAAEAAAABAAAAAAARAAIAAAACAAAAAAAOAAIAAAACAAAAAAAQAAIAAAAAAAAAAAAPAAIAAAAAAAAAAAASAAEAAAAAAAAAAAASAAIAAAAAAAAAAAASAAMAAAAAAAAAAAANAAIAAAAAAAAAAAAMAAEAAAAAAAAAAAAKAAMAAAAAAAAAAAAJAAMAAAAAAAAAAAAGAAMAAAAAAAAAAAAFAAMAAAAAAAAAAAAIAAMAAAAAAAAAAAAGAAYAAAAAAAAAAAAHAAYAAAAAAAAAAAAIAAYAAAAAAAAAAAANAAEAAAAAAAAAAAAKAAEAAAAAAAAAAAABAA0AAAAEAAAAAAABAAwAAAACAAAAAAABAAsAAAACAAAAAAABAAoAAAACAAAAAAABAAkAAAABAAEAAAADAAkAAAABAAAAAAAEAAkAAAABAAAAAAADAAwAAAAAAAAAAAADAA0AAAABAAAAAAACAA0AAAABAAAAAAACAAkAAAABAAAAAAADAAsAAAAAAAAAAAAEAA0AAAABAAAAAAA=")
|
||||
tile_set = ExtResource("2_4k50d")
|
||||
|
||||
[node name="PauseMenu" parent="." unique_id=904032161 instance=ExtResource("3_pause")]
|
||||
|
|
|
|||
Loading…
Reference in a new issue