Last active
April 23, 2021 16:10
-
-
Save fy0/f142447836037181aa34537bf307fa90 to your computer and use it in GitHub Desktop.
史尔特尔BUG伪代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 来源: https://www.bilibili.com/video/BV1LB4y1A7VX | |
/** | |
生命周期示意图: | |
https://edotor.net/?engine=dot#digraph%20g%7B%0A%20%20rankdir%3DLR%3B%0A%20%20node%20%5Bshape%3Dbox%5D%3B%0A%0A%20%20%22onAwake%22%20-%3E%20%22onAction%22%20-%3E%20%22onDestroy%22%0A%20%20%0A%20%20%22Buff%E5%8A%A0%E5%85%A5%E7%AE%A1%E7%90%86%E5%99%A8%EF%BC%8C%E8%B0%83%E7%94%A8%22%20-%3E%20%22onAwake%22%20-%3E%20%22%E5%88%B0%E8%BE%BE%E6%89%A7%E8%A1%8C%E6%97%B6%E6%9C%BA%EF%BC%8C%E8%B0%83%E7%94%A8%22%20-%3E%20%22onAction%22%20-%3E%20%22%E5%88%B0%E8%BE%BE%E9%94%80%E6%AF%81%E6%97%B6%E6%9C%BA%EF%BC%8C%E8%B0%83%E7%94%A8%22%20-%3E%20%22onDestroy%22%0A%7D | |
digraph g{ | |
rankdir=LR; | |
node [shape=box]; | |
"onAwake" -> "onAction" -> "onDestroy" | |
"Buff加入管理器,调用" -> "onAwake" -> "到达执行时机,调用" -> "onAction" -> "到达销毁时机,调用" -> "onDestroy" | |
} | |
*/ | |
// 单位通用类 | |
class Unit { | |
buffs = []; // 当前的buff | |
} | |
// Buff 通用类 | |
class Buff { | |
static recycle: Buff[] = []; | |
// 创建buff,设置所有者,将自己添加到单位buff列表 | |
static create(owner, spellID) { | |
const buff = Buff.getRecycle(); | |
buff.owner = owner; | |
buff.spellID = spellID; | |
buff.owner.buffs.add(this); | |
buff.onAwake(); | |
} | |
addToRecycle() { | |
Buff.recycle.push(this); | |
} | |
static getRecycle() { | |
return Buff.recycle.pop() || new Buff(); | |
} | |
} | |
// 这里为了省事两个效果写一起了,估计实际是两个不同buff | |
class 巫恋娃娃_自动撤回_mix extends Buff { | |
influences: Buff[]; // 影响的buff | |
onAwake() { | |
// 巫恋娃娃:启动时使周围单位技能失效(如果是两个类,这里是巫恋娃娃技能的onAction) | |
// 选择周围单位 | |
const units = pickUnits(); | |
// 将这些单位的buff储存并移除 | |
for (let i of units) { | |
for (let b of i.buffs) { | |
this.influences.push({ owner: b.owner, spellID: b.spellID }); | |
b.remove(); | |
} | |
i.buffs.clear(); // 从单位buff列表中清除持有的buff | |
} | |
} | |
// 触发自动删除时调用,也就是故事的开始 | |
onAction() { | |
// 自动撤回:时间到了,被触发 | |
this.remove(); // 删除自己,这里会走到下面的 onDestroy() | |
/* | |
// onDestroy 中一段这样的代码被执行了 | |
this.owner.buffs.remove(this); // 从刻俄柏buff列表中移除自己 | |
this.active = false; | |
this.addToRecycle(); // 塞入回收站 | |
*/ | |
for (const i of this.influences) { | |
Buff.create(i.owner, i.spellID); // 这里独行长路重用了当前buff的槽,现在刻俄柏的BUFF列表中又有当前BUFF了 | |
} | |
} | |
remove() { | |
// 先后两次调用: | |
// 1. onAction触发调用,这个应该只涉及自动撤回类 | |
// 2. 倒计时结束后,remove会被二次调用,这个是全局机制 | |
this.onDestroy(); | |
} | |
onDestroy() { | |
// BUFF移除之后 | |
// BUG点: 收尾工作与之前操作的冲突 | |
// 这其实是一个通用的正常操作,但是不应该被做两次(另外这段代码应该属于BUFF类,为了容易理解写这里了) | |
this.owner.buffs.remove(this); // 从刻俄柏buff列表中移除自己 | |
this.active = false; // 理论上不会出现一个buff是可被重用状态,同时又出现在角色的buff列表中。这里做到了 | |
this.addToRecycle(); // 塞入回收站 | |
// 离谱的地方就在于此处,刻俄柏的buff列表中持有当前Buff容器,但是被标为无效。 | |
// 所以当刻俄柏的技能下一次被无效化,其buff列表中的那个技能(现在不知道是谁在用了)会受到影响(加入influences) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment