Пример #1
0
    private void DefaultAction(CharacterPresenter characterPresenter)
    {
        InputAxis axis    = InputAxis.GetInputAxis();
        bool      isShift = Input.GetKey(KeyCode.LeftShift);
        bool      isZ     = Input.GetKey(KeyCode.Z);

        //攻撃
        if (Input.GetKeyDown(KeyCode.X) && characterPresenter.status.isAction == false && !characterPresenter.isMove)
        {
            characterPresenter.Attack(mapPresenter, characterListPresenter.characterListPresenter);
            characterListPresenter.Delete();
            return;
        }

        if (axis.F.x != 0 || axis.F.y != 0)
        {
            if (isZ && (axis.F.x == 0 || axis.F.y == 0))
            {
                return;
            }
            // 向き変更
            if (!characterPresenter.isMove)
            {
                characterPresenter.SetDirection(new Vector3(axis.I.x, 0, axis.I.y));
                // shiftが押されていたら方向転換だけ
                if (isShift)
                {
                    return;
                }
            }

            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);

            if (mapPresenter.IsCanMove(axis.I, beforePosition, characterPresenter.status.type))
            {
                if (!characterPresenter.isMove && characterPresenter.status.isAction == false)
                {
                    if (!isShift)
                    {
                        characterPresenter.Move(axis.F.x, axis.F.y);

                        //アイテムがあれば取得
                        if (mapPresenter.GetTileModel(afterPosition.x, afterPosition.y).itemGuid != 0)
                        {
                            ItemPresenter itemPresenter = itemsListPresenter.Find(mapPresenter.GetTileModel(afterPosition.x, afterPosition.y).itemGuid);

                            if (itemPresenter != null && characterPresenter.SetItem(itemPresenter.status))
                            {
                                itemsListPresenter.Delete(itemPresenter);

                                mapPresenter.SetItemModel(afterPosition.x, afterPosition.y, null);
                            }
                            else
                            {
                                Debug.Log("Cant Delete:null");
                            }
                        }

                        //階段あれば移動
                        if (mapPresenter.GetTileModel(afterPosition)
                            .tileType == TileModel.TileType.Stairs)
                        {
                            mapPresenter.Regenerate();
                        }

                        //移動元と移動先にキャラクター情報を設定
                        mapPresenter.SetUserModel(beforePosition, null);
                        mapPresenter.SetUserModel(afterPosition, characterPresenter.status);

                        characterPresenter.SetPosition(new Vector3(afterPosition.x, 0, afterPosition.y));
                    }
                }
            }
        }
    }
Пример #2
0
    //全ての敵に行動させる
    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);
            }
        }
    }