您当前的位置: 首页 >  游戏

【03】

暂无认证

  • 4浏览

    0关注

    196博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

cocos小游戏实战-05-NPC与角色攻击逻辑

【03】 发布时间:2022-07-26 09:57:06 ,浏览量:4

添加NPC攻击逻辑与动画

assets/Scripts/WoodenSkeleton/WoodenSkeletonManager.ts

判断角色是否在NPC的上下左右四个格子中;当前X坐标相等,且Y坐标相距小于等于1格,或者Y坐标相等,X坐标小于等于1格


async init() {
	// 攻击检测,角色出现在NPC附近,NPC主动发起攻击
  EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END, this.onAttack, this)
}

// NPC攻击角色检测
onAttack() {
  const { x: playerX, y: playerY } = DataManager.Instance.player
  // 判断角色是否在NPC的上下左右四个格子中
  const disX = Math.abs(this.x - playerX)
  const disY = Math.abs(this.y - playerY)
  // 当前X坐标相等,且Y坐标相距小于等于1格,或者Y坐标相等,X坐标小于等于1格
  if ((this.x === playerX && disY  name.includes(v))) {
        // 统一state入口
        this.node.getComponent(EntityManager).state = ENTITY_STATE_ENUM.IDLE
        // this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.IDLE)
      }
    })
  }

run() {
	//...
	case this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK):
	if (this.params.get(PARAMS_NAME_ENUM.ATTACK).value) {
          this.currentState = this.stateMachines.get(PARAMS_NAME_ENUM.ATTACK)
        } else if...

}
添加角色死亡逻辑与动画

assets/Scripts/WoodenSkeleton/WoodenSkeletonManager.ts

在NPC攻击处通知角色死亡

// ...

// NPC攻击角色检测
  onAttack() {
    const { x: playerX, y: playerY, state: playState } = DataManager.Instance.player
    // 判断角色是否在NPC的上下左右四个格子中
    const disX = Math.abs(this.x - playerX)
    const disY = Math.abs(this.y - playerY)
    // 当前X坐标相等,且Y坐标相距小于等于1格,或者Y坐标相等,X坐标小于等于1格,且判断当前角色不是死亡状态,不然会出现鞭尸现象
    if (
      ((this.x === playerX && disY             
关注
打赏
1657344724
查看更多评论
0.0368s