カイジのEカード風カードゲームの作成。Part3.選択カード決定と対戦相手カードセット【Unityゲーム制作】

簡単なカードゲーム作成の第3回目を進めていきます。今回は「選択カードの決定」と「対戦相手のカードのセット」をしていきます。

はじめに

今回は第3回目です。第1回目は下記から。

ゲーム作成開始

前回は「カードを複数枚セット」するところまで行ったので、その続きから進めていきます。まずは「選択したカードの決定」からです。

選択カードの決定

カードゲームでは複数のカードから1枚選ぶことになりますが、この選択した時の動作として「カードを場に出す」というのがあります。下図のような感じです。

選択したカードの位置を変更するのでCardScriptのChoiceCard()関数の中でlocalPositionをセットします。下記のようにスクリプトを変更。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CardScript : MonoBehaviour
{
    public void ChoiceCard()
    {
        string strNum = this.transform.Find("Text").GetComponent<Text>().text;
        this.transform.localPosition = new Vector3(-400, 400, 0);
    }
}

これで移動するようになりますが、他のカードを選択すると下図のようにドンドン重なってきます。

これを回避する方法はいくつかあります。例えば、「1回目の選択は仮決定で場所は動かさず、2回目で決定して、即判定に入る」、「他のカードを選択したら、元の位置にカードを戻す」などなど・・・

今回はベーシックに「他のカードを選択したら、元の位置にカードを戻す」を実装していきます。まずはCardsオブジェクトに新しいスクリプトを追加「Cards」という名前にしておきます。

新しく追加したCardsスクリプトの中は下記に変更。元のカード情報を保管するだけです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cards : MonoBehaviour
{
    public GameObject SelectCard;
    public Vector3 InitPosition;


}

次にCardScriptを下記のように変更。「カードが場にあれば元の位置に戻す」、「選択したカード情報を変数に入れる」です。工夫も何もないべたべた書きです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CardScript : MonoBehaviour
{
    public void ChoiceCard()
    {

        GameObject objCards = this.transform.parent.gameObject;
        if (objCards.GetComponent<Cards>().SelectCard != null)
        {
            objCards.GetComponent<Cards>().SelectCard.GetComponent<Transform>().localPosition = objCards.GetComponent<Cards>().InitPosition;
        }
        objCards.GetComponent<Cards>().SelectCard = this.gameObject;
        objCards.GetComponent<Cards>().InitPosition = this.transform.localPosition;

        string strNum = this.transform.Find("Text").GetComponent<Text>().text;
        this.transform.localPosition = new Vector3(-400, 400, 0);

    }
}

これで実行してみて動けばとりあえずカード選択部分はOK。

相手のカード作成

次に相手(敵)のカードを作っていきます。下図の赤枠の位置が相手のカードが並ぶ場所ですね。

まずはEmCardsという名前でオブジェクトを作成。相手カードの親オブジェクトです。

次にOperatorに相手カードを配らせるので、Operatorスクリプトを変更。

EmCardsの変数を追加してパラメータに自分か相手かのオブジェクトを追加。この辺もとりあえずベタ書き。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Operator : MonoBehaviour
{
    public GameObject CardPrefab;
    public GameObject Cards;
    public GameObject EmCards;

    private void Start()
    {
        //Player
        dealCards(1, -500, Cards);
        dealCards(2, -250, Cards);
        dealCards(2, 0, Cards);
        dealCards(2, 250, Cards);
        dealCards(3, 500, Cards);

        //Enemy
        dealCards(1, -500, EmCards);
        dealCards(2, -250, EmCards);
        dealCards(2, 0, EmCards);
        dealCards(2, 250, EmCards);
        dealCards(3, 500, EmCards);

    }
    void dealCards(int iNumber,float fPosition,GameObject objParent)
    {
        GameObject prefab = (GameObject)Instantiate(CardPrefab);
        prefab.transform.Find("Text").GetComponent<Text>().text = iNumber.ToString();
        prefab.transform.SetParent(objParent.transform, false);
        prefab.transform.localPosition = new Vector3(fPosition, 0, 0);
    }

}

OperatorオブジェクトのEmCards変数にEmCardsオブジェクトをセット。

実際に動かしてみて下記のようになればOK。

とりあえず、これで「相手(敵)のカードも設定完了」。直す部分は多いけど、とりあえずの完成が早くできるように進めていきます

タイトルとURLをコピーしました