public bool GetStageDataType(int _playerId, Common.MOVE_TYPE pos) { CharacterModel.Data playerId = GetCharacterData(_playerId); switch (pos) { case Common.MOVE_TYPE.UP: if (stageData[(int)playerId.BlockPos.y - 1, (int)playerId.BlockPos.x] == (int)Define.Stage.BLOCK_TYPE.OBSTACLE) { return(false); } break; case Common.MOVE_TYPE.DOWN: if (stageData[(int)playerId.BlockPos.y + 1, (int)playerId.BlockPos.x] == (int)Define.Stage.BLOCK_TYPE.OBSTACLE) { return(false); } break; case Common.MOVE_TYPE.LEFT: if (stageData[(int)playerId.BlockPos.y, (int)playerId.BlockPos.x - 1] == (int)Define.Stage.BLOCK_TYPE.OBSTACLE) { return(false); } break; case Common.MOVE_TYPE.RIGHT: if (stageData[(int)playerId.BlockPos.y, (int)playerId.BlockPos.x + 1] == (int)Define.Stage.BLOCK_TYPE.OBSTACLE) { return(false); } break; } return(true); }
//--------------------------------------------------- // GetVecToMoveType //--------------------------------------------------- public Vector2 GetMoveTypeToVec(Common.MOVE_TYPE type) { Vector2 outVec = Vector2.zero; switch (type) { case Common.MOVE_TYPE.DOWN: outVec = Vector2.down; break; case Common.MOVE_TYPE.UP: outVec = Vector2.up; break; case Common.MOVE_TYPE.RIGHT: outVec = Vector2.right; break; case Common.MOVE_TYPE.LEFT: outVec = Vector2.left; break; } return(outVec); }
//--------------------------------------------------- // GetMoveVec //--------------------------------------------------- public Vector3 GetMoveVec(Common.MOVE_TYPE type) { Vector3 outVec = Vector3.zero; switch (type) { case Common.MOVE_TYPE.DOWN: outVec = Vector3.back; break; case Common.MOVE_TYPE.UP: outVec = Vector3.forward; break; case Common.MOVE_TYPE.RIGHT: outVec = Vector3.right; break; case Common.MOVE_TYPE.LEFT: outVec = Vector3.left; break; } return(outVec * Define.Battle.BLOCK_SIZE); }
/// <summary> /// 指定したアクターの移動 /// </summary> /// <remarks> /// 指定したアクターIDのオブジェクトを移動させる<br> /// 移動はステージのブロックにそってのみ行われる<br> /// 斜め移動はなし、上下左右のみ移動命令が行える<br> /// 1ブロックの移動スピードはアクターに依存する<br> /// 移動中のアクター、もしくは制御下にないアクターは移動命令を受け付けない /// </remarks> /// <returns> 移動可否 </returns> /// <param name="actorId">移動させたいアクターID</param> public bool Move(int actorId, Common.MOVE_TYPE type) { ActorData?actorData = _actorManager.GetActor(actorId); Character character = actorData.Value.Actor.gameObject.GetComponent <Character>(); if (actorData == null) { return(false); } if (character == null || character.CharacterModel.IsMoveUpdate || character.CharacterModel.TeamId != _teamType) { return(false); } Vector2 blockData = _battleSystem.StageManager.getPositionBlock(character.gameObject.transform.position + _battleSystem.StageManager.GetMoveVec(type)); Define.Stage.BLOCK_TYPE blockType = _battleSystem.StageManager.GetBlockType(blockData); if (blockType != (int)Define.Stage.BLOCK_TYPE.NORMAL) { return(false); } Vector3 targetPos = _battleSystem.StageManager.GetBlockPosition((int)blockData.x, (int)blockData.y); targetPos.y = character.transform.position.y; if (targetPos == character.transform.position) { return(false); } character.CharacterModel.TargetPos = targetPos; return(true); }
//--------------------------------------------------- // UpdateAI //--------------------------------------------------- override public void UpdateAI() { var PlayerDataList = GetTeamCharacterDataList(TEAM_TYPE.PLAYER); var EnemyDataList = GetTeamCharacterDataList(TEAM_TYPE.ENEMY); int[,] StageData = GetStageData(); //データの更新 foreach (var data in PlayerDataList) { Players[data.ActorId] = data; var oldPos = OldPos[data.ActorId]; if (oldPos != data.BlockPos) { //方向の更新 if (data.BlockPos.x > oldPos.x) { Dir[data.ActorId] = Common.MOVE_TYPE.RIGHT; } else if (data.BlockPos.x < oldPos.x) { Dir[data.ActorId] = Common.MOVE_TYPE.LEFT; } else if (data.BlockPos.y > oldPos.y) { Dir[data.ActorId] = Common.MOVE_TYPE.UP; } else if (data.BlockPos.y < oldPos.y) { Dir[data.ActorId] = Common.MOVE_TYPE.DOWN; } OldPos[data.ActorId] = data.BlockPos; } } foreach (var data in EnemyDataList) { Enemys[data.ActorId] = data; var oldPos = OldPos[data.ActorId]; if (oldPos != data.BlockPos) { //方向の更新 if (data.BlockPos.x > oldPos.x) { Dir[data.ActorId] = Common.MOVE_TYPE.RIGHT; } else if (data.BlockPos.x < oldPos.x) { Dir[data.ActorId] = Common.MOVE_TYPE.LEFT; } else if (data.BlockPos.y > oldPos.y) { Dir[data.ActorId] = Common.MOVE_TYPE.UP; } else if (data.BlockPos.y < oldPos.y) { Dir[data.ActorId] = Common.MOVE_TYPE.DOWN; } OldPos[data.ActorId] = data.BlockPos; } } //アイテム取得 foreach (var item in ItemPos) { bool bget = false; float itemlen = 20.0f; int id = -1; foreach (var player in PlayerDataList) { //アイテム取得 if (player.BlockPos == item) { ItemPos.Remove(item); bget = true; break; } //範囲 float range = 0.0f; if (player.BlockPos.y < item.y) { range += item.y - player.BlockPos.y; } if (player.BlockPos.y > item.y) { range += player.BlockPos.y - item.y; } if (player.BlockPos.x < item.x) { range += item.x - player.BlockPos.x; } if (player.BlockPos.x > item.x) { range += player.BlockPos.x - item.x; } if (range < itemlen) { itemlen = range; id = player.ActorId; } } if (id != -1) { var player = GetCharacterData(id); //最短距離 if (player.BlockPos.y < item.y && (int)player.BlockPos.y + 1 < 15 && StageData[(int)player.BlockPos.y + 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.UP); } else if (player.BlockPos.y > item.y && (int)player.BlockPos.y - 1 > -1 && StageData[(int)player.BlockPos.y - 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.DOWN); } else if (player.BlockPos.x < item.x && (int)player.BlockPos.x + 1 < 15 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x + 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.RIGHT); } else if (player.BlockPos.x > item.x && (int)player.BlockPos.x - 1 > -1 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x - 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.LEFT); } //ランダム else { var rand = Random.Range(0, 4); if (rand == 0) { Move(player.ActorId, Common.MOVE_TYPE.UP); } if (rand == 1) { Move(player.ActorId, Common.MOVE_TYPE.DOWN); } if (rand == 2) { Move(player.ActorId, Common.MOVE_TYPE.RIGHT); } if (rand == 3) { Move(player.ActorId, Common.MOVE_TYPE.LEFT); } } } if (bget == true) { break; } } //プレイヤー処理 foreach (var player in PlayerDataList) { //移動中か //if (player.isMoveEnable == false) //{ // continue; //} //攻撃 Common.MOVE_TYPE attackDir = Common.MOVE_TYPE.NONE; float Length = 20.0f; bool bAttack = false; int DangerEnemy = -1; //範囲判定 bool bRangeAction = false; foreach (var enemy in EnemyDataList) { //範囲 float range = 0.0f; if (player.BlockPos.y < enemy.BlockPos.y) { range += enemy.BlockPos.y - player.BlockPos.y; } if (player.BlockPos.y > enemy.BlockPos.y) { range += player.BlockPos.y - enemy.BlockPos.y; } if (player.BlockPos.x < enemy.BlockPos.x) { range += enemy.BlockPos.x - player.BlockPos.x; } if (player.BlockPos.x > enemy.BlockPos.x) { range += player.BlockPos.x - enemy.BlockPos.x; } //近いとき無敵 if (range <= 7.0f) { Action(player.ActorId, Define.Battle.ACTION_TYPE.INVINCIBLE); } //近いとき近距離攻撃 if (range <= 2.0f) { Action(player.ActorId, Define.Battle.ACTION_TYPE.ATTACK_SHORT); } } foreach (var enemy in EnemyDataList) { //重なり //if (player.BlockPos == enemy.BlockPos) //{ // continue; //} //直線上にいるか //上下 if (player.BlockPos.x == enemy.BlockPos.x) { //上に敵 if (player.BlockPos.y < enemy.BlockPos.y) { //ブロック判定 bool isBlock = false; for (int nCnt = 1; nCnt < enemy.BlockPos.y - player.BlockPos.y; nCnt++) { if (StageData[(int)player.BlockPos.y + nCnt, (int)player.BlockPos.x] == 1) { isBlock = true; break; } } if (isBlock == true) { continue; } //向き合っているか if (Dir[enemy.ActorId] == Common.MOVE_TYPE.DOWN) { DangerEnemy = enemy.ActorId; break; } if (Dir[player.ActorId] == Common.MOVE_TYPE.UP) { float len = enemy.BlockPos.y - player.BlockPos.y; if (len < Length) { Length = len; attackDir = Common.MOVE_TYPE.UP; } } } //下に敵 if (player.BlockPos.y > enemy.BlockPos.y) { //ブロック判定 bool isBlock = false; for (int nCnt = 1; nCnt < player.BlockPos.y - enemy.BlockPos.y; nCnt++) { if (StageData[(int)player.BlockPos.y - nCnt, (int)player.BlockPos.x] == 1) { isBlock = true; break; } } if (isBlock == true) { continue; } //向き合っているか if (Dir[enemy.ActorId] == Common.MOVE_TYPE.UP) { DangerEnemy = enemy.ActorId; break; } if (Dir[player.ActorId] == Common.MOVE_TYPE.DOWN) { float len = player.BlockPos.y - enemy.BlockPos.y; if (len < Length) { Length = len; attackDir = Common.MOVE_TYPE.DOWN; } } } } //左右 if (player.BlockPos.y == enemy.BlockPos.y) { //右に敵 if (player.BlockPos.x < enemy.BlockPos.x) { //ブロック判定 bool isBlock = false; for (int nCnt = 1; nCnt < enemy.BlockPos.x - player.BlockPos.x; nCnt++) { if (StageData[(int)player.BlockPos.y, (int)player.BlockPos.x + nCnt] == 1) { isBlock = true; break; } } if (isBlock == true) { continue; } //向き合っているか if (Dir[enemy.ActorId] == Common.MOVE_TYPE.LEFT) { DangerEnemy = enemy.ActorId; break; } if (Dir[player.ActorId] == Common.MOVE_TYPE.RIGHT) { float len = enemy.BlockPos.x - player.BlockPos.x; if (len < Length) { Length = len; attackDir = Common.MOVE_TYPE.RIGHT; } } } //左に敵 if (player.BlockPos.x > enemy.BlockPos.x) { //ブロック判定 bool isBlock = false; for (int nCnt = 1; nCnt < player.BlockPos.x - enemy.BlockPos.x; nCnt++) { if (StageData[(int)player.BlockPos.y, (int)player.BlockPos.x - nCnt] == 1) { isBlock = true; break; } } if (isBlock == true) { continue; } //向き合っているか if (Dir[enemy.ActorId] == Common.MOVE_TYPE.RIGHT) { DangerEnemy = enemy.ActorId; break; } if (Dir[player.ActorId] == Common.MOVE_TYPE.LEFT) { float len = player.BlockPos.x - enemy.BlockPos.x; if (len < Length) { Length = len; attackDir = Common.MOVE_TYPE.LEFT; } } } } } //end foreach enemy //攻撃 if (attackDir != Common.MOVE_TYPE.NONE || DangerEnemy == -1) { bool CanShot = false; //近距離 if (Length < 3) { CanShot = Action(player.ActorId, Define.Battle.ACTION_TYPE.ATTACK_SHORT); } //中距離 if (CanShot == false && Length < 8) { CanShot = Action(player.ActorId, Define.Battle.ACTION_TYPE.ATTACK_MIDDLE); } //長距離 if (CanShot == false) { CanShot = Action(player.ActorId, Define.Battle.ACTION_TYPE.ATTACK_LONG); } bAttack = CanShot; } //移動 if (bAttack == false) { //にげる if (DangerEnemy != -1) { //下から if (Dir[DangerEnemy] == Common.MOVE_TYPE.UP) { if ((int)player.BlockPos.x + 1 < 15 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x + 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.RIGHT); } else if ((int)player.BlockPos.x - 1 > -1 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x - 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.LEFT); } else if ((int)player.BlockPos.y + 1 < 15 && StageData[(int)player.BlockPos.y + 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.UP); } else { Move(player.ActorId, Common.MOVE_TYPE.DOWN); } } //上から if (Dir[DangerEnemy] == Common.MOVE_TYPE.DOWN) { if ((int)player.BlockPos.x + 1 < 15 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x + 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.RIGHT); } else if ((int)player.BlockPos.x - 1 > -1 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x - 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.LEFT); } else if ((int)player.BlockPos.y - 1 > -1 && StageData[(int)player.BlockPos.y - 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.DOWN); } else { Move(player.ActorId, Common.MOVE_TYPE.UP); } } //右から if (Dir[DangerEnemy] == Common.MOVE_TYPE.LEFT) { if ((int)player.BlockPos.y + 1 < 15 && StageData[(int)player.BlockPos.y + 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.UP); } else if ((int)player.BlockPos.y - 1 > -1 && StageData[(int)player.BlockPos.y - 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.DOWN); } else if ((int)player.BlockPos.x - 1 > -1 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x - 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.LEFT); } else { Move(player.ActorId, Common.MOVE_TYPE.RIGHT); } } //左から if (Dir[DangerEnemy] == Common.MOVE_TYPE.RIGHT) { if ((int)player.BlockPos.y + 1 < 15 && StageData[(int)player.BlockPos.y + 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.UP); } else if ((int)player.BlockPos.y - 1 > -1 && StageData[(int)player.BlockPos.y - 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.DOWN); } else if ((int)player.BlockPos.x + 1 < 15 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x + 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.RIGHT); } else { Move(player.ActorId, Common.MOVE_TYPE.LEFT); } } } //攻撃対象に向けて移動 else { Vector2 enemyPos = new Vector2(); //移動対象の位置 foreach (var enemy in EnemyDataList) { if (enemy.Hp > 0) { enemyPos = enemy.BlockPos; break; } } //最短距離 if (player.BlockPos.y < enemyPos.y && (int)player.BlockPos.y + 1 < 15 && StageData[(int)player.BlockPos.y + 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.UP); } else if (player.BlockPos.y > enemyPos.y && (int)player.BlockPos.y - 1 > -1 && StageData[(int)player.BlockPos.y - 1, (int)player.BlockPos.x] == 0) { Move(player.ActorId, Common.MOVE_TYPE.DOWN); } else if (player.BlockPos.x < enemyPos.x && (int)player.BlockPos.x + 1 < 15 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x + 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.RIGHT); } else if (player.BlockPos.x > enemyPos.x && (int)player.BlockPos.x - 1 > -1 && StageData[(int)player.BlockPos.y, (int)player.BlockPos.x - 1] == 0) { Move(player.ActorId, Common.MOVE_TYPE.LEFT); } //ランダム else { var rand = Random.Range(0, 4); if (rand == 0) { Move(player.ActorId, Common.MOVE_TYPE.UP); } if (rand == 1) { Move(player.ActorId, Common.MOVE_TYPE.DOWN); } if (rand == 2) { Move(player.ActorId, Common.MOVE_TYPE.RIGHT); } if (rand == 3) { Move(player.ActorId, Common.MOVE_TYPE.LEFT); } } //遠回り //else if ((int)player.BlockPos.x + 1 < 15 && // StageData[(int)player.BlockPos.y, (int)player.BlockPos.x + 1] == 0) //{ // Move(player.ActorId, Common.MOVE_TYPE.RIGHT); //} //else if ((int)player.BlockPos.x - 1 > -1 && // StageData[(int)player.BlockPos.y, (int)player.BlockPos.x - 1] == 0) //{ // Move(player.ActorId, Common.MOVE_TYPE.LEFT); //} //else if ((int)player.BlockPos.y + 1 < 15 && // StageData[(int)player.BlockPos.y + 1, (int)player.BlockPos.x] == 0) //{ // Move(player.ActorId, Common.MOVE_TYPE.UP); //} //else if ((int)player.BlockPos.y - 1 > -1 && // StageData[(int)player.BlockPos.y - 1, (int)player.BlockPos.x] == 0) //{ // Move(player.ActorId, Common.MOVE_TYPE.DOWN); //} } //var rand = Random.Range(0, 4); //if( rand == 0) //{ // Move(player.ActorId, Common.MOVE_TYPE.UP); //} //if (rand == 1) //{ // Move(player.ActorId, Common.MOVE_TYPE.DOWN); //} //if (rand == 2) //{ // Move(player.ActorId, Common.MOVE_TYPE.RIGHT); //} //if (rand == 3) //{ // Move(player.ActorId, Common.MOVE_TYPE.LEFT); //} } } }