//全ての敵に行動させる public void AllAction(MapPresenter mapPresenter) { // Dictionary<int, EnemyPresenter> plauerInSideFloorEnemy = enemyListPresenter.Select(enemy => // enemy.Value.status.floorId == 0).ToDictionary(enemy => ); Dictionary <int, CharacterPresenter> plauerInSideFloorEnemy = new Dictionary <int, CharacterPresenter>(); Dictionary <int, CharacterPresenter> plauerOutSideFloorEnemy = new Dictionary <int, CharacterPresenter>(); /// TODO全プレイヤーで検索する CharacterPresenter player = characterListPresenter.FirstOrDefault(presenter => presenter.Value.status.type == TileModel.CharaType.Player).Value; //プレイヤーと同じフロアにいるかで分ける foreach (KeyValuePair <int, CharacterPresenter> enemy in characterListPresenter.Where(presenter => presenter.Value.status.type == TileModel.CharaType.Enemy)) { enemy.Value.beforeStatus = enemy.Value.status; if (enemy.Value.status.floorId == player.status.floorId) { plauerInSideFloorEnemy.Add(enemy.Key, enemy.Value); } else { plauerOutSideFloorEnemy.Add(enemy.Key, enemy.Value); } } // プレイヤーに近い順に並べて行動させるようにソートする。 foreach (KeyValuePair <int, CharacterPresenter> enemy in plauerInSideFloorEnemy) { //GetFirstPositionAStar(enemy.Value.status.position, playerPresenter.status.position, mapPresenter); } var around1 = new DirectionUtil().GetAroundDirection(1); var around100 = new DirectionUtil().GetAroundDirection(100); foreach (KeyValuePair <int, CharacterPresenter> enemy in characterListPresenter.Where(presenter => presenter.Value.status.type == TileModel.CharaType.Enemy)) { CharacterPresenter characterPresenter = enemy.Value; // 周りにプレイヤーがいれば攻撃 var searchDirection1 = around1.Select(i => i + characterPresenter.status.position.GetVector2Int()); var hitEnemyDirection = searchDirection1.Where(i => mapPresenter.SearchCharaType(i, TileModel.CharaType.Player)); if (hitEnemyDirection.Any()) { Vector2Int direction = hitEnemyDirection.First() - characterPresenter.status.position.GetVector2Int(); characterPresenter.SetDirection(direction.GetVector2Int()); characterPresenter.Attack(mapPresenter, characterListPresenter); characterPresenter.SetIsAction(true); continue; } // 攻撃できなければランダムアクション int actionType = characterPresenter.GetAction(); if (actionType == 1) { InputAxis axis = InputAxis.GetRandomAxis(); //キャッシュに残るもので移動できるか、移動できなければ再計算 if (characterPresenter.cacheNextDestination.Count > 0 && mapPresenter.IsCanMove(characterPresenter.cacheNextDestination.First() - characterPresenter.status.position.GetVector2Int(), characterPresenter.status.position.GetVector2Int(), characterPresenter.status.type)) { axis = new InputAxis(characterPresenter.cacheNextDestination.First() - characterPresenter.status.position.GetVector2Int()); characterPresenter.cacheNextDestination.RemoveAt(0); } else { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); //通路を検索 var to = mapPresenter.GetFloorModel(characterPresenter.status.floorId).innerPath.GetRandom(); if (to != Vector2Int.zero && to != characterPresenter.status.position.GetVector2Int()) { var wayList = GetDestinationWayList(characterPresenter.status.position.GetVector2Int(), to, mapPresenter, characterPresenter); if (wayList.Length > 0) { axis = new InputAxis(wayList.First() - characterPresenter.status.position.GetVector2Int()); characterPresenter.cacheNextDestination = wayList.ToList(); } } sw.Stop(); Debug.Log(characterPresenter.status.guid + ",from:" + characterPresenter.status.position.GetVector2Int() + ",to:" + to + ",time:" + sw.ElapsedMilliseconds); } if (axis.I == new Vector2Int(0, 0)) { //移動先がなければ行動済みにする。 characterPresenter.SetIsAction(true); continue; } if (!mapPresenter.IsCanMove(axis.I, characterPresenter.status.position.GetVector2Int(), characterPresenter.status.type)) { // TODO 移動先に邪魔なものがあれば縦か横移動をする。 characterPresenter.SetIsAction(true); continue; } Vector2Int beforePosition = new Vector2Int((int)characterPresenter.status.position.x, (int)characterPresenter.status.position.z); Vector2Int afterPosition = new Vector2Int(beforePosition.x + axis.I.x, beforePosition.y + axis.I.y); characterPresenter.Move(axis.F.x, axis.F.y); //移動元と移動先にキャラクター情報を設定 mapPresenter.SetUserModel(beforePosition, null); mapPresenter.SetUserModel(afterPosition, characterPresenter.status); characterPresenter.SetMapData( mapPresenter.GetTileModel(afterPosition).floorId, new Vector3(afterPosition.x, 0, afterPosition.y), new Vector3(axis.I.x, 0, axis.I.y) ); } else { //GetNearCharacterPosition(GameConfig.SearchType.Around8, TileModel.CharaType.Player, mapPresenter.map); characterPresenter.SetIsAction(true); } } }