예제 #1
0
    // Use this for initialization
    void Start()
    {
        startFlag = false;
        readyFlag = false;
        showScore = 0;
        score     = 0;

        hiScoreUpdated = false;

        finished = false;


        ScoreTextFormat = new string('0', ScoreText.text.Length);
        var audio = GetComponents <AudioSource>();

        audioSource = audio[0];
        bgm         = audio[1];

        hiscoreEffects = GameObject.FindGameObjectWithTag("HiScoreEffect").GetComponentsInChildren <ParticleSystem>().ToList();

        // ハイスコア読み込み処理
        savedata = saveDataManager.Load() ?? new SaveData();
        hiScore  = savedata.HiScore;

        HiScoreText.text = hiScore.ToString(ScoreTextFormat);

        // 時間表示の更新
        TimeText.text = MilliSecondsToString.format(GameTimeInMilliSeconds);
        // 背景コントローラーの取得
        backgroundContoroller = GameObject.Find("InfomationBackground").GetComponent <BackgroundController>();

        Score = 0;
        for (var i = 0; i < FieldLength * FieldLength; ++i)
        {
            var obj = Instantiate(blockPrefab);

            float x = i % FieldLength / (BASELENGTH * 2) * BASEX + OffsetXPosition;
            float y = -(i / FieldLength / BASELENGTH * BASEY + OffsetYPosition);
            obj.transform.position = new Vector3(x, y);

            if (i == 0 || i == FieldLength - 1 || i == (FieldLength * (FieldLength - 1)) || i == (FieldLength * FieldLength) - 1)
            {
                // フィールドの角の4つはNONEブロックにする
                obj.Kind = Block.KIND.NONE;
            }
            else
            {
                // ブロックをランダム生成する
                obj.SetRandomBlock();
            }

            if (i % (FieldLength / 2 * (FieldLength * 2)) == 0)
            {
                Debug.Log($"[Center] {x}, {y}");
            }

            obj.name = $"Block[{blocks.Count}](Pos:{x}, {y})";

            blocks.Add(obj);
        }

        cursor = Instantiate(cursorPrefab);
        cursor.SetBlockData(blocks, FieldLength);
        // カーソルの初期位置を設定
        cursor.PositionX = FieldLength / 2;
        cursor.PositionY = FieldLength / 2;

        var settings = new FieldSettings {
            BlockRegenerateFrame = BlockRegenerateTime, FieldWidth = FieldLength
        };

        fieldManager = new FieldManager(blocks, settings);

        // キー操作の定義

        // カーソルを左へ移動
        PlayerInputManager.RegisterOnKeyDownHandler(KEYS.LEFT, (frames) =>
        {
            if (cursor.Hold)
            {
                cursor.SwapLeftBlock();
            }
            else
            {
                cursor.Left();
            }
        });
        PlayerInputManager.RegisterOnKeyDelayHandler(KEYS.LEFT, KeyDelayFrame, null);
        PlayerInputManager.RegisterOnKeyHoldHandler(KEYS.LEFT, KeyRepeatInterval, null);

        // カーソルを右へ移動
        PlayerInputManager.RegisterOnKeyDownHandler(KEYS.RIGHT, (frames) =>
        {
            if (cursor.Hold)
            {
                cursor.SwapRightBlock();
            }
            else
            {
                cursor.Right();
            }
        });
        PlayerInputManager.RegisterOnKeyDelayHandler(KEYS.RIGHT, KeyDelayFrame, null);
        PlayerInputManager.RegisterOnKeyHoldHandler(KEYS.RIGHT, KeyRepeatInterval, null);

        // カーソルを上へ移動
        PlayerInputManager.RegisterOnKeyDownHandler(KEYS.UP, (frames) =>
        {
            if (cursor.Hold)
            {
                cursor.SwapUpBlock();
            }
            else
            {
                cursor.Up();
            }
        });
        PlayerInputManager.RegisterOnKeyDelayHandler(KEYS.UP, KeyDelayFrame, null);
        PlayerInputManager.RegisterOnKeyHoldHandler(KEYS.UP, KeyRepeatInterval, null);

        // カーソルを下へ移動
        PlayerInputManager.RegisterOnKeyDownHandler(KEYS.DOWN, (frames) =>
        {
            if (cursor.Hold)
            {
                cursor.SwapDownBlock();
            }
            else
            {
                cursor.Down();
            }
        });
        PlayerInputManager.RegisterOnKeyDelayHandler(KEYS.DOWN, KeyDelayFrame, null);
        PlayerInputManager.RegisterOnKeyHoldHandler(KEYS.DOWN, KeyRepeatInterval, null);

        // ブロックを選択
        PlayerInputManager.RegisterOnKeyDownHandler(KEYS.BLOCKCHANGE_A, (frames) =>
        {
            if (!startFlag)
            {
                return;
            }
            cursor.Hold = true;
        });
        PlayerInputManager.RegisterOnUpHandler(KEYS.BLOCKCHANGE_A, (frames) =>
        {
            if (!startFlag)
            {
                return;
            }
            cursor.Hold = false;
        });


        // スコアを獲得したときの処理
        fieldManager.onEarnedPointEvent += (r) =>
        {
            Debug.Log($"{r.Reason} {r.Point} {r.Kind}");
            StartCoroutine("BlockDeletePlay", r.Count);
            Score += r.Point;
            backgroundContoroller.ChangeTheme(r.Kind);
        };

        fieldManager.onBlockGenerate += (r) =>
        {
            audioSource.PlayOneShot(BlockRegenerateSE);
        };

        var c = FadeIn(0.05f, () =>
        {
            Debug.Log("Fade finish");
            readyFlag = true;
        });

        StartCoroutine(c);
    }