前回の記事はこちら
今回は前回に続いてユニット関係のものを作っていきます。
ジェネレータっていうのはあまり言葉として適していないかもしれませんがご了承ください。
前回からの変更
まず、UnitScript.csを次のように変更します。これはユニットを生成するときのための準備です。
UnitScript.cs
using System.Collections;
using System.Collections.Generic;
using Tokens;
using UnityEngine;
public class UnitScript : MonoBehaviour
{
public double speed;
private double angle;
private string code;
public Interpreter interpreter;
private string lang;
private int id;
public static int nowId = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (lang == "Garnet")
transform.position += transform.right * -(float)speed * 1.0f; //二次元では前はrightの反対
else if (lang == "Topaz")
transform.position += transform.right * -(float)speed * 1.5f;
else if (lang == "Turquoise")
transform.position += transform.right * -(float)speed * 0.75f;
}
public void Rotate(double addAngle)
{
angle += addAngle;
}
public void setCodeAndLangAndStart(string code, string lang)
{
gameObject.name = "Unit" + nowId;
id = nowId;
nowId++;
angle = 0.0;
this.code = code;
this.lang = lang;
if (lang == "Garnet")
{
interpreter = new GarnetInterpreter(id);
}
else if (lang == "Topaz")
{
interpreter = new TopazInterpreter(id);
}
else if (lang == "Turquoise")
{
interpreter = new TurquoiseInterpreter(id);
}
else
{
throw new System.Exception("Please select right language.");
}
StartCoroutine("ExecuteAndWait");
}
private IEnumerator ExecuteAndWait()
{
while (true)
{
interpreter.execute(code);
Vector3 rotate = transform.eulerAngles;
rotate.z = (float)angle;
transform.eulerAngles = rotate;
yield return new WaitForSeconds(1f); //1秒待つ
}
}
}
プログラムを見ればわかる通り、トパーズ言語は移動が速くなり、ターコイズ言語は遅くなります。
これで、好きな言語で動かせるようになります。IEnumerator
とかがわからない人は、"コルーチン"と検索してみてください。
ジェネレータのプログラム
string[] languages = new string[] { "None", "Garnet", "Topaz", "Turquoise" };
GameObject newUnit = Instantiate(unit, gameObject.transform.position, Quaternion.identity);
newUnit.GetComponent<UnitScript>().setCodeAndLangAndStart(コード, languages[インデックス]);
こんな感じのコードをどっかで書いてあげればユニットを生成できます。(ユニットはプレファブ化しておいてください)
ちなみに、私はボタンのイベント処理にくっつけました。
追記:コードはこちらです。
Generator.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class GeneratorScript : MonoBehaviour
{
string programCode;
public GameObject unit;
public GameObject textBox;
public GameObject dropdown;
// Start is called before the first frame update
void Start()
{
textBox = GameObject.Find("Code");
}
// Update is called once per frame
void Update()
{
}
public void DropdownValueChanged(int value)
{
string[] languages = new string[] { "None", "Garnet", "Topaz", "Turquoise" };
GameObject newUnit = Instantiate(unit, gameObject.transform.position, Quaternion.identity);
newUnit.GetComponent<UnitScript>().setCodeAndLangAndStart(textBox.GetComponent<TMP_InputField>().text, languages[value]);
Destroy(this.gameObject);
}
}
動かしてみる
・コードをRotate(180)
・インデックスを3(ターコイズ言語)
にしてあげると、ユニットができてくるくる回るようになります。
最後に
次回は、オンライン系の処理を作っていきたいと思います。
次回の記事はこちら