145 lines
4.7 KiB
GDScript3
145 lines
4.7 KiB
GDScript3
|
|
@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),
|
||
|
|
])
|