ダッシュ残像・無敵時間ラボ

ダッシュ距離、速度曲線、残像、無敵時間、砂煙を調整。

ライブプレビュー

被弾可能

ステージのクリックかSpaceでダッシュ。← / →で向きを変更。

パラメーター

172
340
2.5
45
7
340
450
6
8
11

エンジンコード

# DashGhost.gd · attach to a Node2D. Assign a player Sprite2D in the inspector.
# Call dash(Vector2(facing, 0)). Check invincible before resolving contact damage.
# This controls sprite position. For physics, apply motion through your body controller.
extends Node2D

@export var player: Sprite2D
const DISTANCE := 172 * 1.0
const DURATION := 0.34 * 1.0
const CURVE := 2.5 * 1.0
const INTERVAL := 0.045 * 1.0
const GHOSTS := 7
const FADE := 0.34 * 1.0
const TINT := Color("#68e2e4")
const I_FRAMES := 0.45 * 1.0
const BLINK := 6 * 1.0
var invincible := false
var running := false
var origin := Vector2.ZERO
var direction := Vector2.ZERO
var elapsed := 0.0
var next_ghost := 0.0
var base_alpha := 1.0
var ghosts: Array[Sprite2D] = []

func dash(dir: Vector2) -> void:
    if running or dir.is_zero_approx() or not is_instance_valid(player):
        return
    running = true
    invincible = I_FRAMES > 0.0
    origin = player.global_position
    direction = dir.normalized()
    elapsed = 0.0
    next_ghost = 0.0
    base_alpha = player.modulate.a
    dust(origin, -direction, 8)

func pos(age: float) -> Vector2:
    return origin + direction * DISTANCE * (1.0 - pow(1.0 - clampf(age / DURATION, 0.0, 1.0), CURVE))

func _process(delta: float) -> void:
    if not running:
        return
    if not is_instance_valid(player):
        invincible = false
        running = false
        return
    var before := elapsed
    elapsed += delta
    if before < DURATION:
        player.global_position = pos(elapsed)
        while next_ghost < minf(elapsed, DURATION):
            if GHOSTS > 0:
                ghost(pos(next_ghost))
            next_ghost += INTERVAL
        if elapsed >= DURATION:
            dust(player.global_position, direction, 11)
    invincible = elapsed < I_FRAMES
    var dim := invincible and int(elapsed * BLINK * 2.0) % 2 == 1
    player.modulate.a = base_alpha * (0.35 if dim else 1.0)
    running = elapsed < maxf(DURATION, I_FRAMES)

func ghost(at: Vector2) -> void:
    while ghosts.size() >= GHOSTS:
        ghosts.pop_front().queue_free()
    var image := Sprite2D.new()
    image.texture = player.texture
    image.hframes = player.hframes
    image.vframes = player.vframes
    image.frame = player.frame
    image.flip_h = player.flip_h
    image.flip_v = player.flip_v
    image.offset = player.offset
    image.centered = player.centered
    image.z_index = player.z_index - 1
    image.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
    image.modulate = Color(TINT, 0.48)
    add_child(image)
    image.global_transform = player.global_transform
    image.global_position = at
    ghosts.append(image)
    var tween := image.create_tween()
    tween.tween_property(image, "modulate:a", 0.0, FADE)
    tween.tween_callback(func():
        ghosts.erase(image)
        image.queue_free())

func dust(at: Vector2, dir: Vector2, count: int) -> void:
    for i in count:
        var particle := ColorRect.new()
        particle.size = Vector2.ONE * (2 + i % 4)
        particle.color = Color("#d5b5a0")
        particle.mouse_filter = Control.MOUSE_FILTER_IGNORE
        add_child(particle)
        particle.global_position = at
        var target := at + dir * randf_range(6, 30) + Vector2.UP * randf_range(4, 15)
        var tween := particle.create_tween().set_parallel()
        tween.tween_property(particle, "global_position", target, 0.29)
        tween.tween_property(particle, "modulate:a", 0.0, 0.29)
        tween.chain().tween_callback(particle.queue_free)

func _exit_tree() -> void:
    if running and is_instance_valid(player):
        player.modulate.a = base_alpha
ゲームフィール実験室