Jump/JumpCamera.cs
using System;
/// <summary>
/// 跳一跳的跟随相机:固定角度俯视,平滑跟随棋子。
/// 视线大体沿 +X 方向,平台蛇形前进时在画面上呈现左前/右前的斜线运动。
/// </summary>
public sealed class JumpCamera : Component
{
/// <summary> 相机相对注视点的偏移 </summary>
[Property] public Vector3 Offset { get; set; } = new Vector3( -500f, 0f, 640f );
/// <summary> 注视点相对棋子的前探量(把棋子压到画面下方) </summary>
[Property] public Vector3 LookAhead { get; set; } = new Vector3( 300f, 0f, -40f );
/// <summary> 跟随速度(越大越紧) </summary>
[Property] public float FollowSpeed { get; set; } = 5f;
Vector3 _focus;
bool _hasFocus;
public void SnapTo( Vector3 pos )
{
_focus = pos;
_hasFocus = true;
}
protected override void OnUpdate()
{
var game = JumpGame.Current;
if ( game is null || !_hasFocus ) return;
// 失败(下落/结束)后停住,不跟着棋子坠出画面,让玩家看清失误位置
if ( game.IsGameOver || game.IsFalling ) return;
var t = 1f - MathF.Exp( -FollowSpeed * Time.Delta );
_focus = Vector3.Lerp( _focus, game.PiecePosition, t );
GameObject.WorldPosition = _focus + Offset;
GameObject.WorldRotation = Rotation.LookAt( ( _focus + LookAhead - GameObject.WorldPosition ).Normal );
}
}