背景
前面已经完成了主要的玩法和特效,接下来就是游戏的胜利与失败的界定。当前游戏主要是休闲游戏,且与三消类似,所以大致也会参照三消的模式。之前的第一版中,敌方会像植物大战僵尸一样,不断靠近我们的,但是这个玩法不够休闲。所以目前的设计主要是偏休闲的固定消除,会在家园模式中后期添加防守类的模式。
目标
胜利条件:击杀任务指定数量的敌对单位
失败条件:超出关卡设置的“出鞘”上限
开发过程
先在关卡设置那里配置胜利和失败的配置
{
"1": {
"name": "关卡001",
"describe": "新手关卡说明",
"helpinfo": "帮助提示",
"mapNo": "map001",
"winCondition": [
{
"name": "role001",
"event": "unit_die",
"value": 2,
"finished": 0
}
],
"loseCondition": [
{
"name": "player1",
"event": "chu_qiao",
"value": 2,
"finished": 0
}
],
.....
}
}
接着在游戏开始后,添加2个胜利与失败的组件
// 胜利与失败条件
let battleInfo = entity.getComponent(BattleResComp).battleInfo
let _BattleOverWinComp = PooledComponent.requestComponent(BattleOverWinComp)
_BattleOverWinComp.conditionItems = battleInfo.winCondition
_BattleOverWinComp.callback = () => {
console.log('todo显示胜利结算页面')
}
ooxh.game.battleEntity.attachComponent(_BattleOverWinComp);
let _BattleOverLoseComp = PooledComponent.requestComponent(BattleOverLoseComp)
_BattleOverLoseComp.conditionItems = battleInfo.loseCondition
_BattleOverLoseComp.callback = () => {
console.log('todo显示失败结算页面')
}
ooxh.game.battleEntity.attachComponent(_BattleOverLoseComp);
胜利条件组件
主要监听单位死亡
export class BattleOverWinSystem {
apply(entity: Entity) {
}
static listenWinEvent(comp: BattleOverWinComp) {
ooxh.eventBus.on(EventBusType.UnitDie, BattleOverWinSystem.onUnitDie, comp)
}
static unListenWinEvent(comp: BattleOverWinComp) {
ooxh.eventBus.off(EventBusType.UnitDie, BattleOverWinSystem.onUnitDie, comp)
}
static onUnitDie(unitNo, arg2, arg3) {
let comp: BattleOverWinComp = (this as unknown as BattleOverWinComp)
if (comp) {
let conditionFinishedNum = 0;
comp.conditionItems.forEach((_item) => {
if (_item.event == EventBusType.UnitDie && _item.name == unitNo) {
_item.finished++
}
if (_item.finished >= _item.value) {
conditionFinishedNum++
}
})
console.log('onUnitDie', unitNo)
if (conditionFinishedNum == comp.conditionItems.length) {
if (ooxh.game.battleEntity.getComponent(BattleStateComp).isGameOver == false) {
ooxh.game.battleEntity.getComponent(BattleStateComp).isGameOver = true
comp.callback && comp.callback() // 组件完成回调
}
}
}
}
}
export class BattleOverWinComp extends PooledComponent {
callback: Function = null
conditionItems: IOverConditionItem[] = []
reset() {
this.callback = null
this.conditionItems = []
}
onAttach(entity) {
this.entity = entity
console.log('实体', entity, '挂载了 BattleOverWinComp')
BattleOverWinSystem.listenWinEvent(this)
}
onDetach(entity) {
this.entity = null
console.log('实体', entity, '移除了 BattleOverWinComp')
BattleOverWinSystem.unListenWinEvent(this)
}
}
失败条件组件
主要监听玩家的“出鞘”次数
export class BattleOverLoseSystem {
apply(entity: Entity) {
}
static listenWinEvent(comp: BattleOverLoseComp) {
ooxh.eventBus.on(EventBusType.ChuQiao, BattleOverLoseSystem.onChuQiao, comp)
}
static unListenWinEvent(comp: BattleOverLoseComp) {
ooxh.eventBus.off(EventBusType.ChuQiao, BattleOverLoseSystem.onChuQiao, comp)
}
static onChuQiao(arg1, arg2, arg3) {
let comp: BattleOverLoseComp = (this as unknown as BattleOverLoseComp)
if (comp) {
let conditionFinishedNum = 0;
comp.conditionItems.forEach((_item) => {
if (_item.event == EventBusType.ChuQiao) {
_item.finished++
}
if (_item.finished >= _item.value) {
conditionFinishedNum++
}
})
console.log('onChuQiao', conditionFinishedNum, comp.conditionItems)
if (conditionFinishedNum >= comp.conditionItems.length) {
if (ooxh.game.battleEntity.getComponent(BattleStateComp).isGameOver == false) {
ooxh.game.battleEntity.getComponent(BattleStateComp).isGameOver = true
comp.callback && comp.callback() // 组件完成回调
}
}
}
}
}
export class BattleOverLoseComp extends PooledComponent {
callback: Function = null
conditionItems: IOverConditionItem[] = []
reset() {
this.callback = null
this.conditionItems = []
}
onAttach(entity) {
this.entity = entity
console.log('实体', entity, '挂载了 BattleOverLoseComp')
BattleOverLoseSystem.listenWinEvent(this)
}
onDetach(entity) {
this.entity = null
console.log('实体', entity, '移除了 BattleOverLoseComp')
}
}
接下来就是找到事件的触发点,埋放事件
BattleChuQiaoSystem.upSwordItems(entity, () => {
ooxh.eventBus.emit(EventBusType.ChuQiao)
this.callback && this.callback()
})
subHp(hp: number) {
if (this._hp > hp) {
this.hp -= hp
} else {
this.hp = 0
ooxh.eventBus.emit(EventBusType.UnitDie, this.unitNo)
UnitItem.removeItem(this)
}
}
测试
在测试阶段,发现了一些数据回收的问题,因为每次结束战役,需要将战役内的节点和资源回收
onDetach(entity) {
this.entity = null
console.log('实体', entity, '移除了 BattleGridComp')
for (let [key, blockItem] of this.blockItemMap.entries()) {
BlockItem.removeItem(blockItem)
}
// 剩下的destroy
ooxh.game.blocksNode.children.forEach((_node) => {
if (_node.name == 'SwordsNode') {
// todo 待优化
} else {
_node.destroy()
}
})
}
下一个开发计划
与服务端交互,获取奖励资源