Exemplo n.º 1
0
    // Use this for initialization
    void Start()
    {
        floor = GetComponent <Transform> ();

        // Object setart position
        objPositions[playerName] = new int[] { 0, 0 };
        objPositions[startName]  = new int[] { 0, 0 };
        objPositions[goalName]   = new int[] { dx - 1, dz - 1 };
        objPositions[enemyName]  = new int[] { Mathf.RoundToInt(dx / 2), Mathf.RoundToInt(dz / 2) };

        //blocks
        blockPreb.GetComponent <Transform>().localScale =
            new Vector3(floor.localScale.x / dx, 1f, floor.localScale.z / dz);

        blocks = new Blocks(blockPreb, floor, dx, dz, "map");
        blocks.Init(objPositions);


        // Goal
        GameObject goal = GameObject.Find(goalName);

        goal.name = goalName;
        goal.GetComponent <Transform>().position = blocks.GetBlockPosition(
            objPositions[goalName][0], objPositions[goalName][1]);

        // walls
        Vector3 scale = blockPreb.GetComponent <Transform>().localScale;

        for (int angle = 0; angle < 360; angle += 90)
        {
            float x = Mathf.Cos(Mathf.Deg2Rad * angle);
            float z = Mathf.Sin(Mathf.Deg2Rad * angle);

            blockPreb.GetComponent <Transform>().localScale = new Vector3(
                Mathf.RoundToInt(z * 10) == 0 ? 0.01f : floor.localScale.x,
                scale.y,
                Mathf.RoundToInt(x * 10) == 0 ? 0.01f : floor.localScale.z
                );

            float px = x * floor.localScale.x / 2f;
            float pz = z * floor.localScale.z / 2f;
            float py = floor.localScale.y / 2f + floor.position.y + scale.y / 2f;
            Instantiate(blockPreb, new Vector3(px, py, pz), Quaternion.identity);
        }
        blockPreb.GetComponent <Transform>().localScale = scale;

        // player, enemy
        new string[] { playerName, enemyName }.Select((v, i) => new { v, i }).All(
            item => {
            GameObject obj      = Instantiate(playerPrefab);
            obj.name            = item.v;
            Transform transform = obj.GetComponent <Transform> ();
            Vector3 p           = blocks.GetBlockPosition(objPositions [item.v] [0], objPositions [item.v] [1]);
            //TODO ???
            p.y = floor.position.y + floor.localScale.y / 2f + transform.localScale.y * transform.Find("Body").localScale.y / 2f;
            transform.position        = p;
            PlayerCellController ctrl = obj.GetComponent <PlayerCellController> ();

            if (item.v == playerName)
            {
                player = obj;
                ctrl.AutoMovingSpan = 0f;

                ctrl.AddTriggerAction(goalName, () => {
                    ctrl.CancelMotions();
                    dlg.DoModal(name => {}, timer.ToString("0.0"));
                    timer = 0.0f;

                    transform.position      = blocks.GetBlockPosition(objPositions[startName][0], objPositions[startName][1]);
                    transform.localRotation = Quaternion.identity;

                    audio_source_effects.PlayOneShot(audio_goal);
                });

                ctrl.AddTriggerAction(enemyName, () => {
                    ctrl.CancelMotions();
                    timer += 5.0f;

                    if (timerColor != null)
                    {
                        StopCoroutine(timerColor);
                    }
                    timerColor = StartCoroutine(TimerColor(Color.red, Color.black, 5f));

                    audio_source_effects.PlayOneShot(audio_damage);

                    //transform.position = blocks.GetBlockPosition(objPositions[startName][0], objPositions[startName][1]);
                    //transform.localRotation = Quaternion.identity;
                });
            }
            else if (item.v == enemyName)
            {
                ctrl.AutoMovingSpan = 5f;
                ctrl.SetColor(new Color32(165, 35, 86, 255));
            }
            return(true);
        }
            );

        birdEye            = GameObject.FindWithTag("MainCamera").GetComponent <Camera> ();
        birdEye.enabled    = false;
        playersEye         = player.GetComponent <Transform> ().Find("Camera").GetComponent <Camera> ();
        playersEye.enabled = true;
        SetPlayerActionType();
        if (start_bird_view == true)
        {
            ChangeCamera();
        }
        timerText = GameObject.Find("TimerText");

        // Modal
        dlg = GameObject.Find("Canvas").GetComponent <ModalDialog>();

        //audio
        audio_source_effects        = gameObject.AddComponent <AudioSource>();
        audio_source_effects.volume = volume_effects;
        audio_source_bgm            = gameObject.AddComponent <AudioSource>();
        audio_source_bgm.loop       = true;
        audio_source_bgm.volume     = volume_bgm;

        audio_bgms = new Dictionary <string, AudioClip>()
        {
            { "Default", audio_bgm },
            { "Bird", audio_bird },
            { "Navi", audio_navi },
            { "Warning", audio_warning },
            { "Distance", audio_distance },
        };
        BGM("Default");
    }
Exemplo n.º 2
0
    // Use this for initialization
    void Start()
    {
        floor = GetComponent <Transform>();

        // Object start position
        objPositions[playerName] = new int[] { 0, 0 };
        objPositions[startName]  = new int[] { 0, 0 };
        objPositions[goalName]   = new int[] { dx - 1, dz - 1 };
        objPositions[enemyName]  = new int[] { Mathf.RoundToInt(dx / 2), Mathf.RoundToInt(dz / 2) };

        // blocks
        blockPreb.GetComponent <Transform>().localScale =
            new Vector3(floor.localScale.x / dx, 1f, floor.localScale.z / dz);
        blocks = new Blocks(blockPreb, floor, dx, dz, "map");
        blocks.Init(objPositions);

        // Goal
        GameObject goal = GameObject.Find(goalName);

        goal.name = goalName;
        goal.GetComponent <Transform>().position = blocks.GetBlockPosition(
            objPositions[goalName][0], objPositions[goalName][1]);

        // walls
        Vector3 scale = blockPreb.GetComponent <Transform>().localScale;

        for (int angle = 0; angle < 360; angle += 90)
        {
            float x = Mathf.Cos(Mathf.Deg2Rad * angle);
            float z = Mathf.Sin(Mathf.Deg2Rad * angle);

            blockPreb.GetComponent <Transform>().localScale = new Vector3(
                Mathf.RoundToInt(z * 10) == 0 ? 0.01f : floor.localScale.x,
                scale.y,
                Mathf.RoundToInt(x * 10) == 0 ? 0.01f : floor.localScale.z
                );

            float px = x * floor.localScale.x / 2f;
            float pz = z * floor.localScale.z / 2f;
            float py = floor.localScale.y / 2f + floor.position.y + scale.y / 2f;
            Instantiate(blockPreb, new Vector3(px, py, pz), Quaternion.identity);
        }
        blockPreb.GetComponent <Transform>().localScale = scale;


        // player, enemy
        new string[] { playerName, enemyName }.Select((v, i) => new { v, i }).All(
            item =>
        {
            GameObject obj      = Instantiate(playerPrefab);
            obj.name            = item.v;
            Transform transform = obj.GetComponent <Transform>();
            Vector3 p           = blocks.GetBlockPosition(objPositions[item.v][0], objPositions[item.v][1]);
            p.y = floor.localScale.y / 2f + floor.position.y + transform.localScale.y * transform.Find("Body").localScale.y / 2f;
            transform.position        = p;
            PlayerCellController ctrl = obj.GetComponent <PlayerCellController>();

            if (item.v == playerName)
            {
                player = obj;
                ctrl.AutoMovingSpan = 0f;

                ctrl.AddTriggerAction(goalName, () => {
                    ctrl.CancelMotions();
                    dlg.DoModal(name => { }, timer.ToString("0.0"));
                    timer = 0.0f;

                    transform.position      = blocks.GetBlockPosition(objPositions[startName][0], objPositions[startName][1]);
                    transform.localRotation = Quaternion.identity;

                    audio_source_effects.PlayOneShot(audio_goal);
                });

                ctrl.AddTriggerAction(enemyName, () => {
                    timer += 5.0f;

                    if (timerColor != null)
                    {
                        StopCoroutine(timerColor);
                    }
                    timerColor = StartCoroutine(TimerColor(Color.red, Color.black, 5f));

                    audio_source_effects.PlayOneShot(audio_damage);
                });
            }
            else if (item.v == enemyName)
            {
                ctrl.AutoMovingSpan = 5f;
                ctrl.SetColor(new Color32(165, 35, 86, 255));

                ctrl.SetLayer(LayerMask.NameToLayer("Ignore Raycast"));
                ctrl.TrackingObject = player.GetComponent <Transform>();
            }
            return(true);
        }
            );

        birdEye            = GameObject.FindWithTag("MainCamera").GetComponent <Camera>();
        birdEye.enabled    = false;
        playersEye         = player.GetComponent <Transform>().Find("Camera").GetComponent <Camera>();
        playersEye.enabled = true;
        SetPlayerActionType();
        if (start_bird_view == true)
        {
            ChangeCamera();
        }
        timerText = GameObject.Find("TimerText");

        dlg = GameObject.Find("Canvas").GetComponent <ModalDialog>();

        audio_source_effects        = gameObject.AddComponent <AudioSource>();
        audio_source_effects.volume = volume_effects;
        audio_source_bgm            = gameObject.AddComponent <AudioSource>();
        audio_source_bgm.loop       = true;
        audio_source_bgm.volume     = volume_bgm;

        audio_bgms = new Dictionary <string, AudioClip>()
        {
            { "Default", audio_bgm },
            { "Bird", audio_bird },
            { "Navi", audio_navi },
            { "Warning", audio_warning },
            { "Distance", audio_distance },
        };
        BGM("Default");

        routeRenderer = gameObject.AddComponent <RouteRenderer>();

        blocks.All((x, y) => {
            GameObject o = Instantiate(textPrefab, blocks.GetBlockPosition(x, y), Quaternion.Euler(90, 0, 0));
            o.GetComponent <TextMesh>().text = Mathf.Sqrt(
                Mathf.Pow(objPositions[goalName][0] - x, 2) + Mathf.Pow(objPositions[goalName][1] - y, 2))
                                               .ToString("0.0");
            o.SetActive(false);
            distances.Add(o);
            return(true);
        });

        items = new Dictionary <ItemNames, Item>()
        {
            { ItemNames.Bird, new Item(
                  10f,
                  () => {
                    BGM("Bird");
                    ChangeCamera();
                },
                  () => {
                    BGM("Default");
                    ChangeCamera();
                }
                  ) },
            { ItemNames.BreakWall, new Item(
                  0f,
                  () => {
                    if (toBreakBlockXZ[0] != -1)
                    {
                        audio_source_effects.PlayOneShot(audio_break);
                        blocks.RemoveBlock(toBreakBlockXZ[0], toBreakBlockXZ[1], false);
                        reRoute = true;
                    }
                },
                  () => {
                }
                  ) },
            { ItemNames.Distance, new Item(
                  4f,
                  () => {
                    BGM("Distance");
                    distances.ForEach(o => o.SetActive(true));
                },
                  () => {
                    BGM("Default");
                    distances.ForEach(o => o.SetActive(false));
                }
                  ) },
            { ItemNames.Navi, new Item(
                  3f,
                  () => {
                    BGM("Navi");
                    reRoute = true;
                },
                  () => {
                    BGM("Default");
                    routeRenderer.Clear();
                },
                  () => {
                    if (reRoute)
                    {
                        reRoute          = false;
                        List <int> route = blocks.Solve(blocks.xz2i(objPositions[playerName]), blocks.xz2i(objPositions[goalName]));
                        routeRenderer.Render(route, i => blocks.GetBlockPosition(i));
                    }
                }
                  ) },
        };
    }
Exemplo n.º 3
0
    // Start is called before the first frame update
    void Start()
    {
        floor = GetComponent <Transform>();

        //object start position 最初の位置を初期化
        objPositions[playerName] = new int[] { 0, 0 };
        objPositions[startName]  = new int[] { 0, 0 };
        //フロアの長さ-1の大きさ、にゴールを配置
        objPositions[goalName]  = new int[] { dx - 1, dz - 1 };
        objPositions[enemyName] = new int[] { Mathf.RoundToInt(dx / 2), Mathf.RoundToInt(dz / 2) };

        //blocks ブロックの大きさをフロアーの長さからマスでわって決める
        blockPreb.GetComponent <Transform>().localScale = new Vector3(floor.localScale.x / dx, 1f, floor.localScale.z / dz);
        blocks = new Blocks(blockPreb, floor, dx, dz, "map");
        blocks.Init(objPositions);

        // goal
        GameObject goal = GameObject.Find(goalName);

        goal.name = goalName;
        goal.GetComponent <Transform>().position = blocks.GetBlockPosition(objPositions[goalName][0], objPositions[goalName][1]);

        // walls
        // ブロックを置くときに壁が置かれてしまう  最後にscaleを代入し直して解決
        Vector3 scale = blockPreb.GetComponent <Transform>().localScale;

        for (int angle = 0; angle < 360; angle += 90)
        // 90度ずつの角度をループ
        {
            // Mathf.Deg2Rad * angleは角度をラジアンになおしている
            float x = Mathf.Cos(Mathf.Deg2Rad * angle);
            float z = Mathf.Sin(Mathf.Deg2Rad * angle);

            blockPreb.GetComponent <Transform>().localScale = new Vector3(
                // Mathf.Cosとかはたまに1ちょうどにならないことがあるので10をかけて0,1の位を四捨五入しようとしている
                Mathf.RoundToInt(z * 10) == 0? 0.01f : floor.localScale.x,
                scale.y,
                Mathf.RoundToInt(x * 10) == 0? 0.01f : floor.localScale.z
                );
            // 座標の計算
            float px = x * floor.localScale.x / 2f;
            float pz = z * floor.localScale.z / 2f;
            float py = floor.localScale.y / 2f + floor.position.y + scale.y / 2f;
            Instantiate(blockPreb, new Vector3(px, py, pz), Quaternion.identity);
        }
        blockPreb.GetComponent <Transform>().localScale = scale;

        // player, enemy
        new string[] { playerName, enemyName }.Select((v, i) => new { v, i }).All(
            item =>
        {
            GameObject obj      = Instantiate(playerPrefab);
            obj.name            = item.v;
            Transform transform = obj.GetComponent <Transform>();
            Vector3 p           = blocks.GetBlockPosition(objPositions[item.v][0], objPositions[item.v][1]);
            p.y = floor.localScale.y / 2f + floor.position.y + transform.localScale.y * transform.Find("Body").localScale.y / 2f;
            transform.position        = p;
            PlayerCellController ctrl = obj.GetComponent <PlayerCellController>();

            if (item.v == playerName)
            {
                player = obj;
                ctrl.AutoMovingSpan = 0f;
                // 引数なしのラムダ式がよくわかってない↓
                ctrl.AddTriggerAction(goalName, () => {
                    ctrl.CancelMotions();
                    dlg.DoModal(name => {}, timer.ToString("0.0"));
                    timer = 0.0f;
                    // 最初のスタート位置に戻る
                    transform.position      = blocks.GetBlockPosition(objPositions[startName][0], objPositions[startName][1]);
                    transform.localRotation = Quaternion.identity;

                    audio_source_effects.PlayOneShot(audio_goal);
                });

                ctrl.AddTriggerAction(enemyName, () => {
                    timer += 5.0f;

                    if (timerColor != null)
                    {
                        StopCoroutine(timerColor);
                    }
                    timerColor = StartCoroutine(TimerColor(Color.red, Color.black, 5.0f));

                    audio_source_effects.PlayOneShot(audio_damage);
                });
            }
            else if (item.v == enemyName)
            {
                ctrl.AutoMovingSpan = 2f;
                ctrl.SetColor(new Color32(165, 35, 86, 255));

                ctrl.SetLayer(LayerMask.NameToLayer("Ignore Raycast"));
                ctrl.TrackingObject = player.GetComponent <Transform>();
            }
            return(true);
        }
            );

        birdEye            = GameObject.FindWithTag("MainCamera").GetComponent <Camera>();
        birdEye.enabled    = false;
        playersEye         = player.GetComponent <Transform>().Find("Camera").GetComponent <Camera>();
        playersEye.enabled = true;
        SetPlayerActionType();
        if (start_bird_view == true)
        {
            ChangeCamera();
        }
        timerText = GameObject.Find("TimerText");

        dlg = GameObject.Find("Canvas").GetComponent <ModalDialog>();

        audio_source_effects        = gameObject.AddComponent <AudioSource>();
        audio_source_effects.volume = volume_effects;
        audio_source_bgm            = gameObject.AddComponent <AudioSource>();
        audio_source_bgm.loop       = true;
        audio_source_bgm.volume     = volume_bgm;

        audio_bgms = new Dictionary <string, AudioClip>()
        {
            { "Default", audio_bgm },
            { "Bird", audio_bird },
            { "Navi", audio_navi },
            { "Warning", audio_warning },
            { "Distance", audio_distance },
        };
        BGM("Default");

        routeRenderer = gameObject.AddComponent <RouteRenderer>();

        // ゴールまでの位置の表示
        // 全てのブロックのセルに対して処理を行っていく
        blocks.All((x, y) => {
            GameObject o = Instantiate(textPrefab, blocks.GetBlockPosition(x, y), Quaternion.Euler(90, 0, 0));
            o.GetComponent <TextMesh>().text = Mathf.Sqrt(Mathf.Pow(objPositions[goalName][0] - x, 2) + Mathf.Pow(objPositions[goalName][1] - y, 2)).ToString("0.0");
            o.SetActive(false);
            distances.Add(o);
            return(true);
        });



        items = new Dictionary <ItemNames, Item>()
        {
                         //Itemの引数 実行時間と行うアクションと終わったあとのアクションを引数として渡している
            {
                ItemNames.Bird, new Item(
                    // 5fはどこに行っているのだろうか →おそらくスタートのところに入っている
                    5f,
                    () => {
                    BGM("Bird");
                    ChangeCamera();
                },
                    () => {
                    BGM("Default");
                    ChangeCamera();
                }

                    )
            },
            { ItemNames.BreakWall, new Item(
                  0f,
                  () => {
                    if (toBreakBlockXZ[0] != -1)
                    {
                        audio_source_effects.PlayOneShot(audio_break);
                        //falseはゲーム内に保存するオプション
                        blocks.RemoveBlock(toBreakBlockXZ[0], toBreakBlockXZ[1], false);
                        reRoute = true;
                    }
                },
                  () => {
                }
                  ) },
            { ItemNames.Distance, new Item(
                  5f,
                  () => {
                    BGM("Distance");
                    distances.ForEach(o => o.SetActive(true));
                },
                  () => {
                    BGM("Default");
                    distances.ForEach(o => o.SetActive(false));
                }
                  ) },
            { ItemNames.Navi, new Item(
                  5f,
                  () => {
                    BGM("Navi");
                    reRoute = true;
                },
                  () => {
                    BGM("Default");
                    routeRenderer.clear();
                },
                  () => {
                    if (reRoute)
                    {
                        reRoute          = false;
                        List <int> route = blocks.Solve(blocks.xz2i(objPositions[playerName]), blocks.xz2i(objPositions[goalName]));
                        routeRenderer.Render(route, i => blocks.GetBlockPosition(i));
                    }
                }
                  ) },
        };
    }