mattak's blog

人生を1ミリ進める

ゲームブートキャンプ5日目

16:00-

きょうやること

  • メインゲーム画面作成
    • 4x4マス
    • 敵をたたくと1ポイント
    • ボスをたたくと10ポイント
    • ノイズをたたくと-2ポイント
    • 30sec
    • スコアを競う

マスを作る

f:id:mattaclj:20140309163945p:plain

各マスのボーダーはSprite

オブジェクトをランダムに出現させる

Prefabをコード上から参照するにはResourcesフォルダにいれる必要があるよう・・・ - http://answers.unity3d.com/questions/12003/instantiate-a-prefab-through-code-in-c.html

Prefabって?

  • 再利用可能なゲームオブジェクト
  • 複数のシーンで利用できる

タッチした位置のオブジェクトを消す

using UnityEngine;
using System.Collections;

public class EnemySpawn : MonoBehaviour {

    public GameObject enemy;

    // Use this for initialization
    void Start () {

        // instantinate game object
        for (int n = 0; n < 2; n++) {
            float x = randomIndex (-2, 1) + 0.5f;
            float y = randomIndex (-2, 1) + 0.5f;
            //GameObject enemy = Resources.Load ("EnemyBanana");
            Instantiate (enemy, new Vector3(x, y, 0.0f), Quaternion.identity);
        }
    }

    // generate random index.
    int randomIndex (int begin, int end) {
        int result = (int)(Random.value * (end - begin + 1)) + begin;
        return result;
    }

    // Update is called once per frame
    void Update () {
        // remove enemy
        if (Input.GetMouseButtonDown(0)) {
            Vector3 mousePoint     = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5.0f);
            Vector3 tapPoint       = Camera.main.ScreenToWorldPoint(mousePoint);
            Collider2D collider2d = Physics2D.OverlapPoint (tapPoint);

            Debug.Log ("TapPoint:   " + tapPoint.x + ", " + tapPoint.y + ", ... " + tapPoint.ToString());

            if (collider2d) {

                RaycastHit2D hitObject = Physics2D.Raycast (tapPoint, -Vector2.up);

                if (hitObject) {
                    Debug.Log ("hitObject");
                    print ("hit object is " + hitObject.collider.gameObject.name);
                    Destroy(hitObject.collider.gameObject);
                }
            }
        }
    }
}

わりとはまった。 ポイントは

Vector3 mousePoint     = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5.0f);

で、3次元上のカメラ位置のz軸を反転した値を入れること。

一定時間後に消す

    // Use this for initialization
    void Start () {
        Destroy (this.gameObject, 1.0f);
    }

一定時間ごとに敵をうむ

using UnityEngine;
using System.Collections;

public class EnemySpawn : MonoBehaviour {

        public GameObject enemy;
        public float time;
        private const float SPAWN_TIME = 1.0f;

        // Use this for initialization
        void Start () {
                time = .0f;
        }

        // Update is called once per frame
        void Update () {
                // Spawn with constant time.
                time += Time.deltaTime;
                if (time >= SPAWN_TIME) {
                        print ("SPAWN");
                        Spawn ();
                        time = .0f;
                }

                // Touch GameObject to delete.
                TouchDelete();
        }

        void TouchDelete() {
                // remove enemy
                if (Input.GetMouseButtonDown(0)) {
                        Vector3 mousePoint     = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5.0f);
                        Vector3 tapPoint       = Camera.main.ScreenToWorldPoint(mousePoint);
                        Collider2D collider2d = Physics2D.OverlapPoint (tapPoint);

                        Debug.Log ("TapPoint:   " + tapPoint.x + ", " + tapPoint.y + ", ... " + tapPoint.ToString());

                        if (collider2d) {

                                RaycastHit2D hitObject = Physics2D.Raycast (tapPoint, -Vector2.up);

                                if (hitObject) {
                                        Debug.Log ("hitObject");
                                        print ("hit object is " + hitObject.collider.gameObject.name);
                                        Destroy(hitObject.collider.gameObject);
                                }
                        }
                }
        }

        // generate random index.
        int RandomIndex (int begin, int end) {
                int result = (int)(Random.value * (end - begin + 1)) + begin;
                return result;
        }

        void Spawn() {
                // instantinate game object
                for (int n = 0; n < 2; n++) {
                        float x = RandomIndex (-2, 1) + 0.5f;
                        float y = RandomIndex (-2, 1) + 0.5f;
                        //GameObject enemy = Resources.Load ("EnemyBanana");
                        Instantiate (enemy, new Vector3(x, y, 0.0f), Quaternion.identity);
                }
        }
}

スコアの表示

とりあえず、メインゲームの左上とゲームオーバーの結果シーンにスコアを表示する。

Singletonでスコアはさくせいした。

using UnityEngine;
using System.Collections;

public class ScoreManager : SingletonMonobehaviour<ScoreManager>
{

    private int score;

    public void Awake ()
    {
        if (this != Instance) {
            Destroy(this);
            return;
        }
        DontDestroyOnLoad(this.gameObject);
    }

    public void Start() {
        Reset();
    }

    public void Reset() {
        score = 0;
    }

    public void Add(int _score) {
        this.score += _score;
    }

    public int Get() {
        return score;
    }
}

ゲーム開始時に、ScoreManager.Instance.Reset();して使う。

が、、このインスタンスはシーン遷移後に保持されない

PlayerPrefsをつかってデータの保存をする。 先ほどのScoreManagerを以下のように修正.

public void Add(int _score) {
    this.score += _score;
    Save ();
}

public int Get() {
    return score;
}

public ScoreManager Load() {
    this.score = PlayerPrefs.GetInt ("score");
    return this;
}

public ScoreManager Save() {
    PlayerPrefs.SetInt ("score", score);
    return this;
}

せっかくなので、ベストスコアも管理する。

public ScoreManager Save() {
    PlayerPrefs.SetInt ("score", score);
    UpdateBest (score);
    return this;
}

public int GetBest() {
    int result = PlayerPrefs.GetInt ("score.best");
    return (result <= 0) ? 0 : result;
}

public void UpdateBest(int score) {
    int bestScore = GetBest();
    if (bestScore < score) {
        PlayerPrefs.SetInt ("score.best", score);
    }
}

できたー。。 これでとりあえず、ゲームとして遊べるものが完成した。