Prototype: timed piston hazard that shatters the slime into pixels
The piston winds up, slams down, holds and retracts on a timer. Catching the slime under the head breaks its current frame into one particle per non-black pixel, which bounce off and settle on the level. The level reloads afterwards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f1fc198a5b
commit
38d7e6d761
7 changed files with 332 additions and 0 deletions
|
|
@ -43,6 +43,7 @@ const ROTATE_TO_SURFACE_SPEED = 25.0
|
|||
const MIN_LAND_SPEED_FOR_SPLAT = 60.0
|
||||
const LAND_PITCH_MIN = 0.87
|
||||
const LAND_PITCH_MAX = 1.15
|
||||
const RESPAWN_DELAY = 2.5 # Prototype: reloads the level this long after shattering.
|
||||
|
||||
###############
|
||||
## Variables ##
|
||||
|
|
@ -76,6 +77,8 @@ var wrapArcStartAngle := 0.0 # Angle around the corner where the arc begins.
|
|||
var wrapSpeed := 0.0
|
||||
var wrapDistanceTravelled := 0.0
|
||||
|
||||
var isShattered := false
|
||||
|
||||
###########
|
||||
## Nodes ##
|
||||
###########
|
||||
|
|
@ -642,6 +645,22 @@ func _splat(normal: Vector2) -> void:
|
|||
landSound.pitch_scale = randf_range(LAND_PITCH_MIN, LAND_PITCH_MAX)
|
||||
landSound.play()
|
||||
|
||||
# Bursts into a particle per (non-black) pixel of the current frame.
|
||||
# push: the direction we were hit towards. exclude: bodies the pixels pass through.
|
||||
func shatter(push: Vector2, exclude: Array[RID] = []) -> void:
|
||||
if isShattered:
|
||||
return
|
||||
isShattered = true
|
||||
|
||||
var burst := PixelBurst.new()
|
||||
get_parent().add_child(burst)
|
||||
burst.burst_from_sprite(sprite, push, exclude)
|
||||
|
||||
hide()
|
||||
set_physics_process(false)
|
||||
collisionShape.set_deferred("disabled", true)
|
||||
get_tree().create_timer(RESPAWN_DELAY).timeout.connect(get_tree().reload_current_scene)
|
||||
|
||||
# ---- Physics queries (all ignore the slime itself) ----
|
||||
|
||||
func _get_radius() -> float:
|
||||
|
|
|
|||
139
Effects/pixel_burst.gd
Normal file
139
Effects/pixel_burst.gd
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
class_name PixelBurst
|
||||
extends Node2D
|
||||
|
||||
# Breaks a sprite's current frame into one particle per pixel. Black (outline)
|
||||
# and transparent pixels are skipped. Particles fall, bounce off the level and
|
||||
# come to rest on it.
|
||||
|
||||
const GRAVITY = 700.0
|
||||
const MAX_FALL_SPEED = 400.0
|
||||
const BOUNCE = 0.35 # Speed kept (and reversed) when hitting something.
|
||||
const FLOOR_FRICTION = 0.6 # Sideways speed kept when bouncing off a floor.
|
||||
const SETTLE_SPEED = 15.0 # Slower than this on a floor and the pixel stops for good.
|
||||
const MAX_PUSH_OUT = 16 # Pixels spawned inside the level move out by up to this.
|
||||
const BLACK_THRESHOLD = 0.1 # Pixels with all channels below this count as black.
|
||||
|
||||
# How hard pixels squirt out sideways from under whatever pushed them, and back against it.
|
||||
const SIDEWAYS_SPEED_MIN = 40.0
|
||||
const SIDEWAYS_SPEED_MAX = 220.0
|
||||
const BACK_SPEED_MIN = 20.0
|
||||
const BACK_SPEED_MAX = 180.0
|
||||
|
||||
var positions := PackedVector2Array()
|
||||
var velocities := PackedVector2Array()
|
||||
var colors := PackedColorArray()
|
||||
var settled := PackedByteArray()
|
||||
var excluded: Array[RID] = []
|
||||
|
||||
|
||||
# push: the direction the sprite was hit towards (e.g. DOWN for a piston from above).
|
||||
# exclude: bodies the pixels shouldn't collide with (e.g. the piston head).
|
||||
func burst_from_sprite(sprite: AnimatedSprite2D, push: Vector2, exclude: Array[RID] = []) -> void:
|
||||
top_level = true
|
||||
global_position = Vector2.ZERO
|
||||
z_index = sprite.z_index + 1
|
||||
excluded = exclude
|
||||
|
||||
var texture := sprite.sprite_frames.get_frame_texture(sprite.animation, sprite.frame)
|
||||
var image := _get_image(texture)
|
||||
if image == null:
|
||||
return
|
||||
|
||||
var size := image.get_size()
|
||||
var spriteTransform := sprite.get_global_transform()
|
||||
var center := sprite.global_position
|
||||
var sideways := push.orthogonal()
|
||||
|
||||
for y in size.y:
|
||||
for x in size.x:
|
||||
var color := image.get_pixel(x, y)
|
||||
if color.a < 0.5:
|
||||
continue
|
||||
if color.r < BLACK_THRESHOLD and color.g < BLACK_THRESHOLD and color.b < BLACK_THRESHOLD:
|
||||
continue
|
||||
|
||||
# Pixel centre in the sprite's local space (it's centered).
|
||||
var localX := x - size.x / 2.0 + 0.5
|
||||
var localY := y - size.y / 2.0 + 0.5
|
||||
if sprite.flip_h:
|
||||
localX = -localX
|
||||
if sprite.flip_v:
|
||||
localY = -localY
|
||||
var worldPoint := spriteTransform * (Vector2(localX, localY) + sprite.offset)
|
||||
|
||||
var side := signf((worldPoint - center).dot(sideways))
|
||||
if side == 0.0:
|
||||
side = [-1.0, 1.0].pick_random()
|
||||
var velocity := sideways * side * randf_range(SIDEWAYS_SPEED_MIN, SIDEWAYS_SPEED_MAX)
|
||||
velocity -= push * randf_range(BACK_SPEED_MIN, BACK_SPEED_MAX)
|
||||
|
||||
positions.append(_push_out_of_solid(worldPoint, -push))
|
||||
velocities.append(velocity)
|
||||
colors.append(color)
|
||||
settled.append(0)
|
||||
|
||||
queue_redraw()
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
for i in positions.size():
|
||||
if settled[i]:
|
||||
continue
|
||||
|
||||
var oldPosition := positions[i]
|
||||
var velocity := velocities[i]
|
||||
velocity.y = minf(velocity.y + GRAVITY * delta, MAX_FALL_SPEED)
|
||||
var newPosition := oldPosition + velocity * delta
|
||||
|
||||
# Pixels that start inside something (squashed into it) move freely until they're out.
|
||||
if not _is_solid(oldPosition) and _is_solid(newPosition):
|
||||
if _is_solid(Vector2(newPosition.x, oldPosition.y)):
|
||||
newPosition.x = oldPosition.x
|
||||
velocity.x *= -BOUNCE
|
||||
if _is_solid(Vector2(newPosition.x, newPosition.y)):
|
||||
var hitFloor := velocity.y > 0.0
|
||||
newPosition.y = oldPosition.y
|
||||
velocity.y *= -BOUNCE
|
||||
if hitFloor:
|
||||
velocity.x *= FLOOR_FRICTION
|
||||
if velocity.length() < SETTLE_SPEED:
|
||||
velocity = Vector2.ZERO
|
||||
settled[i] = 1
|
||||
|
||||
positions[i] = newPosition
|
||||
velocities[i] = velocity
|
||||
|
||||
queue_redraw()
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
for i in positions.size():
|
||||
draw_rect(Rect2(positions[i].floor(), Vector2.ONE), colors[i])
|
||||
|
||||
|
||||
# Moves a point that starts inside the level out along `direction`, a pixel at a time.
|
||||
func _push_out_of_solid(point: Vector2, direction: Vector2) -> Vector2:
|
||||
for step in MAX_PUSH_OUT:
|
||||
if not _is_solid(point):
|
||||
break
|
||||
point += direction
|
||||
return point
|
||||
|
||||
|
||||
func _is_solid(point: Vector2) -> bool:
|
||||
var query := PhysicsPointQueryParameters2D.new()
|
||||
query.position = point
|
||||
query.exclude = excluded
|
||||
return not get_world_2d().direct_space_state.intersect_point(query, 1).is_empty()
|
||||
|
||||
|
||||
func _get_image(texture: Texture2D) -> Image:
|
||||
var image: Image
|
||||
if texture is AtlasTexture:
|
||||
var atlas := texture as AtlasTexture
|
||||
image = atlas.atlas.get_image().get_region(Rect2i(atlas.region))
|
||||
else:
|
||||
image = texture.get_image()
|
||||
if image != null and image.is_compressed():
|
||||
image.decompress()
|
||||
return image
|
||||
1
Effects/pixel_burst.gd.uid
Normal file
1
Effects/pixel_burst.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://hmc5cy1srfxt
|
||||
144
Hazards/piston.gd
Normal file
144
Hazards/piston.gd
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
@tool
|
||||
extends Node2D
|
||||
|
||||
# A piston that slams down on a timer. Anything with a shatter() method caught
|
||||
# under the head while it slams (or is down) gets shattered.
|
||||
# The node's origin is the bottom of the housing; the head hangs just below it.
|
||||
# Rotate the node to slam in other directions.
|
||||
|
||||
enum State { WAITING, WINDUP, SLAMMING, HOLDING, RETRACTING }
|
||||
|
||||
@export var slamDistance := 80.0: # How far the head travels down from rest.
|
||||
set(value):
|
||||
slamDistance = value
|
||||
_rebuild()
|
||||
@export var headSize := Vector2(48, 16):
|
||||
set(value):
|
||||
headSize = value
|
||||
_rebuild()
|
||||
@export var startDelay := 0.0 # Offset the timer, to stagger several pistons.
|
||||
@export var waitTime := 1.5 # Retracted time between slams.
|
||||
@export var windupTime := 0.35 # A short lift and shake before slamming, as a warning.
|
||||
@export var slamSpeed := 900.0
|
||||
@export var holdTime := 0.4
|
||||
@export var retractSpeed := 80.0
|
||||
|
||||
const WINDUP_LIFT = 4.0
|
||||
const WINDUP_SHAKE = 1.0
|
||||
const HOUSING_HEIGHT = 24.0
|
||||
const SHAFT_WIDTH = 10.0
|
||||
const SHAFT_OVERLAP = 4.0 # How far the shaft starts up inside the housing.
|
||||
const CRUSH_ZONE_DEPTH = 4.0 # How far below the head the crush check reaches.
|
||||
const CRUSH_ZONE_INSET = 2.0 # Narrower than the head, so brushing its side is safe.
|
||||
|
||||
const HEAD_COLOR = Color(0.55, 0.57, 0.62)
|
||||
const HEAD_STRIPE_COLOR = Color(0.85, 0.65, 0.1)
|
||||
const SHAFT_COLOR = Color(0.35, 0.36, 0.4)
|
||||
const HOUSING_COLOR = Color(0.25, 0.26, 0.3)
|
||||
|
||||
var state := State.WAITING
|
||||
var stateTime := 0.0
|
||||
var headOffset := 0.0 # How far the head is below rest.
|
||||
|
||||
@onready var head: AnimatableBody2D = $Head
|
||||
@onready var headShape: CollisionShape2D = $Head/CollisionShape2D
|
||||
@onready var crushZone: Area2D = $Head/CrushZone
|
||||
@onready var crushShape: CollisionShape2D = $Head/CrushZone/CollisionShape2D
|
||||
@onready var plate: Polygon2D = $Head/Plate
|
||||
@onready var stripe: Polygon2D = $Head/Stripe
|
||||
@onready var shaft: Polygon2D = $Shaft
|
||||
@onready var housing: Polygon2D = $Housing
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_rebuild()
|
||||
stateTime = -startDelay
|
||||
_place_head(0.0, 0.0)
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if Engine.is_editor_hint():
|
||||
return
|
||||
|
||||
stateTime += delta
|
||||
match state:
|
||||
State.WAITING:
|
||||
if stateTime >= waitTime:
|
||||
_set_state(State.WINDUP)
|
||||
State.WINDUP:
|
||||
var progress := minf(stateTime / windupTime, 1.0)
|
||||
var shake := randf_range(-WINDUP_SHAKE, WINDUP_SHAKE)
|
||||
_place_head(-WINDUP_LIFT * progress, shake)
|
||||
if stateTime >= windupTime:
|
||||
_set_state(State.SLAMMING)
|
||||
State.SLAMMING:
|
||||
_place_head(minf(headOffset + slamSpeed * delta, slamDistance), 0.0)
|
||||
_crush()
|
||||
if headOffset >= slamDistance:
|
||||
_set_state(State.HOLDING)
|
||||
State.HOLDING:
|
||||
_crush() # Overlaps from the last slam frame show up a frame late.
|
||||
if stateTime >= holdTime:
|
||||
_set_state(State.RETRACTING)
|
||||
State.RETRACTING:
|
||||
_place_head(maxf(headOffset - retractSpeed * delta, 0.0), 0.0)
|
||||
if headOffset <= 0.0:
|
||||
_set_state(State.WAITING)
|
||||
|
||||
|
||||
func _set_state(newState: State) -> void:
|
||||
state = newState
|
||||
stateTime = 0.0
|
||||
|
||||
|
||||
func _crush() -> void:
|
||||
var slamDirection := Vector2.DOWN.rotated(global_rotation)
|
||||
var passThrough: Array[RID] = [head.get_rid()]
|
||||
for body in crushZone.get_overlapping_bodies():
|
||||
if body.has_method("shatter"):
|
||||
body.shatter(slamDirection, passThrough)
|
||||
|
||||
|
||||
func _place_head(offset: float, shakeX: float) -> void:
|
||||
headOffset = offset
|
||||
head.position = Vector2(shakeX, headOffset + headSize.y / 2.0)
|
||||
# The shaft is a 1px tall rect stretched from inside the housing down to the head.
|
||||
shaft.scale.y = maxf(headOffset, 0.0) + SHAFT_OVERLAP
|
||||
|
||||
|
||||
# Sizes the shapes and visuals from the exports. Runs in the editor too, as a preview.
|
||||
func _rebuild() -> void:
|
||||
if not is_node_ready():
|
||||
return
|
||||
|
||||
var headRect := RectangleShape2D.new()
|
||||
headRect.size = headSize
|
||||
headShape.shape = headRect
|
||||
|
||||
var crushRect := RectangleShape2D.new()
|
||||
crushRect.size = Vector2(headSize.x - CRUSH_ZONE_INSET * 2, headSize.y + CRUSH_ZONE_DEPTH)
|
||||
crushShape.shape = crushRect
|
||||
crushShape.position = Vector2(0, CRUSH_ZONE_DEPTH / 2.0)
|
||||
|
||||
var half := headSize / 2.0
|
||||
plate.polygon = _rect_polygon(-half, half)
|
||||
plate.color = HEAD_COLOR
|
||||
stripe.polygon = _rect_polygon(Vector2(-half.x, half.y - 3), Vector2(half.x, half.y - 1))
|
||||
stripe.color = HEAD_STRIPE_COLOR
|
||||
|
||||
shaft.polygon = _rect_polygon(Vector2(-SHAFT_WIDTH / 2, 0), Vector2(SHAFT_WIDTH / 2, 1))
|
||||
shaft.color = SHAFT_COLOR
|
||||
shaft.position = Vector2(0, -SHAFT_OVERLAP)
|
||||
|
||||
var housingHalfWidth := maxf(headSize.x / 2.0 - 6.0, SHAFT_WIDTH)
|
||||
housing.polygon = _rect_polygon(Vector2(-housingHalfWidth, -HOUSING_HEIGHT), Vector2(housingHalfWidth, 0))
|
||||
housing.color = HOUSING_COLOR
|
||||
|
||||
_place_head(headOffset, 0.0)
|
||||
|
||||
|
||||
func _rect_polygon(topLeft: Vector2, bottomRight: Vector2) -> PackedVector2Array:
|
||||
return PackedVector2Array([
|
||||
topLeft, Vector2(bottomRight.x, topLeft.y),
|
||||
bottomRight, Vector2(topLeft.x, bottomRight.y),
|
||||
])
|
||||
1
Hazards/piston.gd.uid
Normal file
1
Hazards/piston.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://11w13udqki1x
|
||||
24
Hazards/piston.tscn
Normal file
24
Hazards/piston.tscn
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
[gd_scene format=3]
|
||||
|
||||
[ext_resource type="Script" path="res://Hazards/piston.gd" id="1_piston"]
|
||||
|
||||
[node name="Piston" type="Node2D"]
|
||||
script = ExtResource("1_piston")
|
||||
|
||||
[node name="Shaft" type="Polygon2D" parent="."]
|
||||
|
||||
[node name="Head" type="AnimatableBody2D" parent="."]
|
||||
position = Vector2(0, 8)
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Head"]
|
||||
|
||||
[node name="Plate" type="Polygon2D" parent="Head"]
|
||||
|
||||
[node name="Stripe" type="Polygon2D" parent="Head"]
|
||||
|
||||
[node name="CrushZone" type="Area2D" parent="Head"]
|
||||
monitorable = false
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Head/CrushZone"]
|
||||
|
||||
[node name="Housing" type="Polygon2D" parent="."]
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
[ext_resource type="PackedScene" uid="uid://uaxviwb40nn8" path="res://Characters/player.tscn" id="1_ormqy"]
|
||||
[ext_resource type="TileSet" uid="uid://tuiytikmuvdy" path="res://Assets/level_tiles.tres" id="2_4k50d"]
|
||||
[ext_resource type="PackedScene" path="res://UI/pause_menu.tscn" id="3_pause"]
|
||||
[ext_resource type="PackedScene" path="res://Hazards/piston.tscn" id="4_piston"]
|
||||
|
||||
[node name="TestLevel" type="Node2D" unique_id=1824208737]
|
||||
|
||||
|
|
@ -13,6 +14,9 @@ position = Vector2(329, 266)
|
|||
tile_map_data = PackedByteArray("AAAAAA4AAAAAAAAAAAABAA4AAAAAAAAAAAACAA4AAAAAAAAAAAADAA4AAAAAAAAAAAAEAA4AAAAAAAAAAAAFAA4AAAAAAAAAAAAGAA4AAAAAAAAAAAAHAA4AAAAAAAAAAAAIAA4AAAAAAAAAAAAJAA4AAAAAAAAAAAAKAA4AAAAAAAAAAAALAA4AAAAAAAAAAAAMAA4AAAAAAAAAAAANAA4AAAAAAAAAAAAOAA4AAAAAAAAAAAAPAA4AAAAAAAAAAAAQAA4AAAAAAAAAAAARAA4AAAAAAAAAAAASAA4AAAAAAAAAAAATAA4AAAAAAAAAAAAKAAsAAAAAAAAAAAAJAAsAAAAAAAAAAAAJAAwAAAAAAAAAAAA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AgAAAAAAAAAAAADAAgAAAAAAAAAAAAEAAgAAAAAAAAAAAACAAYAAAAAAAAAAAADAAYAAAAAAAAAAAAEAAYAAAAAAAAAAAACAAUAAAAAAAAAAAACAAQAAAAAAAAAAAACAAMAAAAAAAAAAAACAAIAAAAAAAAAAAAEAAIAAAAAAAAAAAAEAAMAAAAAAAAAAAAEAAQAAAAAAAAAAAAEAAUAAAAAAAAAAAASAAQAAAAAAAAAAAARAAQAAAAAAAAAAAAQAAQAAAAAAAAAAAAPAAQAAAAAAAAAAAAOAAQAAAACAAAAAAANAAQAAAAAAAAAAAAMAAQAAAAAAAAAAAALAAQAAAAAAAAAAAAOAAMAAAADAAAAAAANAAMAAAABAAAAAAAMAAMAAAAEAAAAAAAMAAIAAAAAAAEAAAALAAIAAAABAAAAAAALAAMAAAAAAAAAAAALAAEAAAAAAAAAAAAPAAMAAAABAAAAAAAQAAMAAAABAAAAAAARAAMAAAAFAAAAAAARAAEAAAAAAAEAAAAOAAEAAAABAAEAAAAQAAEAAAABAAAAAAAPAAEAAAABAAAAAAARAAIAAAACAAAAAAAOAAIAAAACAAAAAAAQAAIAAAAAAAAAAAAPAAIAAAAAAAAAAAASAAEAAAAAAAAAAAASAAIAAAAAAAAAAAASAAMAAAAAAAAAAAANAAIAAAAAAAAAAAAMAAEAAAAAAAAAAAAKAAMAAAAAAAAAAAAJAAMAAAAAAAAAAAAGAAMAAAAAAAAAAAAFAAMAAAAAAAAAAAAIAAMAAAAAAAAAAAAGAAYAAAAAAAAAAAAHAAYAAAAAAAAAAAAIAAYAAAAAAAAAAAANAAEAAAAAAAAAAAAKAAEAAAAAAAAAAAA=")
|
||||
tile_set = ExtResource("2_4k50d")
|
||||
|
||||
[node name="Piston" parent="." instance=ExtResource("4_piston")]
|
||||
position = Vector2(448, 352)
|
||||
|
||||
[node name="PauseMenu" parent="." unique_id=904032161 instance=ExtResource("3_pause")]
|
||||
visible = false
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue