序列模式-更新方法
参考章节:https://gpp.tkchu.me/update-method.html
脑内画面
更新方法把"每帧要做的事情"分散到每个对象自己身上。游戏世界像一支队伍,主循环每帧点名,每个对象向前走一步。
#mermaid-svg-dH0wtQVDy2vjEdEA{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-dH0wtQVDy2vjEdEA .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-dH0wtQVDy2vjEdEA .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-dH0wtQVDy2vjEdEA .error-icon{fill:#552222;}#mermaid-svg-dH0wtQVDy2vjEdEA .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dH0wtQVDy2vjEdEA .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-dH0wtQVDy2vjEdEA .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dH0wtQVDy2vjEdEA .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dH0wtQVDy2vjEdEA .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-dH0wtQVDy2vjEdEA .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dH0wtQVDy2vjEdEA .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dH0wtQVDy2vjEdEA .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dH0wtQVDy2vjEdEA .marker.cross{stroke:#333333;}#mermaid-svg-dH0wtQVDy2vjEdEA svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dH0wtQVDy2vjEdEA p{margin:0;}#mermaid-svg-dH0wtQVDy2vjEdEA .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-dH0wtQVDy2vjEdEA .cluster-label text{fill:#333;}#mermaid-svg-dH0wtQVDy2vjEdEA .cluster-label span{color:#333;}#mermaid-svg-dH0wtQVDy2vjEdEA .cluster-label span p{background-color:transparent;}#mermaid-svg-dH0wtQVDy2vjEdEA .label text,#mermaid-svg-dH0wtQVDy2vjEdEA span{fill:#333;color:#333;}#mermaid-svg-dH0wtQVDy2vjEdEA .node rect,#mermaid-svg-dH0wtQVDy2vjEdEA .node circle,#mermaid-svg-dH0wtQVDy2vjEdEA .node ellipse,#mermaid-svg-dH0wtQVDy2vjEdEA .node polygon,#mermaid-svg-dH0wtQVDy2vjEdEA .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-dH0wtQVDy2vjEdEA .rough-node .label text,#mermaid-svg-dH0wtQVDy2vjEdEA .node .label text,#mermaid-svg-dH0wtQVDy2vjEdEA .image-shape .label,#mermaid-svg-dH0wtQVDy2vjEdEA .icon-shape .label{text-anchor:middle;}#mermaid-svg-dH0wtQVDy2vjEdEA .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-dH0wtQVDy2vjEdEA .rough-node .label,#mermaid-svg-dH0wtQVDy2vjEdEA .node .label,#mermaid-svg-dH0wtQVDy2vjEdEA .image-shape .label,#mermaid-svg-dH0wtQVDy2vjEdEA .icon-shape .label{text-align:center;}#mermaid-svg-dH0wtQVDy2vjEdEA .node.clickable{cursor:pointer;}#mermaid-svg-dH0wtQVDy2vjEdEA .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-dH0wtQVDy2vjEdEA .arrowheadPath{fill:#333333;}#mermaid-svg-dH0wtQVDy2vjEdEA .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-dH0wtQVDy2vjEdEA .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-dH0wtQVDy2vjEdEA .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dH0wtQVDy2vjEdEA .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-dH0wtQVDy2vjEdEA .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dH0wtQVDy2vjEdEA .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-dH0wtQVDy2vjEdEA .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-dH0wtQVDy2vjEdEA .cluster text{fill:#333;}#mermaid-svg-dH0wtQVDy2vjEdEA .cluster span{color:#333;}#mermaid-svg-dH0wtQVDy2vjEdEA div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-dH0wtQVDy2vjEdEA .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-dH0wtQVDy2vjEdEA rect.text{fill:none;stroke-width:0;}#mermaid-svg-dH0wtQVDy2vjEdEA .icon-shape,#mermaid-svg-dH0wtQVDy2vjEdEA .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dH0wtQVDy2vjEdEA .icon-shape p,#mermaid-svg-dH0wtQVDy2vjEdEA .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-dH0wtQVDy2vjEdEA .icon-shape .label rect,#mermaid-svg-dH0wtQVDy2vjEdEA .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dH0wtQVDy2vjEdEA .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-dH0wtQVDy2vjEdEA .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-dH0wtQVDy2vjEdEA :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 游戏循环
World.Update(dt)
Player.Update
Projectile.Update
Enemy.Update
Effect.Update
它解决的问题
游戏里大量对象都有跨帧行为:移动、冷却、动画、生命周期、AI。更新方法让对象把自己的时间推进逻辑封装起来,主循环只负责统一调度。
C# 示例
csharp
public interface IUpdatable
{
bool IsAlive { get; }
void Update(float dt, GameWorld world);
}
public sealed class GameWorld
{
private readonly List<IUpdatable> _objects = new();
private readonly List<IUpdatable> _pendingAdd = new();
public void Add(IUpdatable obj)
{
_pendingAdd.Add(obj);
}
public void Update(float dt)
{
foreach (var obj in _objects)
{
obj.Update(dt, this);
}
_objects.RemoveAll(obj => !obj.IsAlive);
_objects.AddRange(_pendingAdd);
_pendingAdd.Clear();
}
}
public sealed class Projectile : IUpdatable
{
private float _x;
private float _speed;
private float _life;
public bool IsAlive => _life > 0f;
public Projectile(float x, float speed, float life)
{
_x = x;
_speed = speed;
_life = life;
}
public void Update(float dt, GameWorld world)
{
_x += _speed * dt;
_life -= dt;
}
}
什么时候用
- 对象行为需要跨帧持续推进。
- 主循环不应该知道每种对象的细节。
- 对象数量和类型经常变化。
使用时的锋利边
更新顺序会影响结果。对象 A 先更新还是对象 B 先更新,可能改变碰撞、AI 感知或触发事件的结果。需要确定性时,要明确排序规则。
更新列表遍历时不要直接增删当前集合。使用延迟添加、死亡标记或命令队列,可以避免迭代器失效和跳过对象。