坊主ゲームアプリを制作しておりますが、いろいろな機能を盛り込んでいくと、長編になりそうなので、分割して出します。
今回はアプリの骨格ができたので共有します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbManager : MonoBehaviour
{
private GameObject gameManager;
// Start is called before the first frame update
void Start()
{
gameManager = GameObject.Find("GameManager");
}
// Update is called once per frame
void Update()
{
}
public void TouchOrb()
{
if (Input.GetMouseButton(0) == false)
{
return;
}
gameManager.GetComponent<GameManager>().GetOrb();
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TempleManager : MonoBehaviour
{
public Sprite[] templePicture = new Sprite[3];
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void SetTemplePicture(int level)
{
GetComponent<Image>().sprite = templePicture[level];
}
public void SetTempleScale(int score, int nextScore)
{
float scale = 0.5f + (((float)score / (float)nextScore) / 2.0f);
transform.localScale = new Vector3(scale, scale, 1.0f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class GameManager : MonoBehaviour
{
private const int MAX_ORB = 10;
private const int RESPAWN_TIME = 1;
private const int MAX_LEVEL = 2;
public GameObject orbPrefab;
public GameObject smokePrefab;
public GameObject kusudamaPrefab;
public GameObject canvasGame;
public GameObject textScore;
public GameObject imageTemple;
private int score = 0;
private int nextScore = 10;
private int currentOrb = 0;
private int templeLevel = 0;
private DateTime lastDateTime;
private int[] nextScoreTable = new int[] { 10, 10, 10 };
// Start is called before the first frame update
void Start()
{
currentOrb = 10;
for(int i = 0; i < currentOrb; i++)
{
CreateOrb();
}
lastDateTime = DateTime.UtcNow;
nextScore = nextScoreTable[templeLevel];
imageTemple.GetComponent<TempleManager>().SetTemplePicture(templeLevel);
imageTemple.GetComponent<TempleManager>().SetTempleScale(score, nextScore);
RefreshScoreText();
}
// Update is called once per frame
void Update()
{
if(currentOrb < MAX_ORB)
{
TimeSpan timeSpan = DateTime.UtcNow - lastDateTime;
if(timeSpan >= TimeSpan.FromSeconds(RESPAWN_TIME))
{
while(timeSpan >= TimeSpan.FromSeconds(RESPAWN_TIME))
{
CreateNewOrb();
timeSpan -= TimeSpan.FromSeconds(RESPAWN_TIME);
}
}
}
}
public void CreateNewOrb()
{
lastDateTime = DateTime.UtcNow;
if(currentOrb >= MAX_ORB)
{
return;
}
CreateOrb();
currentOrb++;
}
public void CreateOrb()
{
GameObject orb = (GameObject)Instantiate(orbPrefab);
orb.transform.SetParent(canvasGame.transform, false);
orb.transform.localPosition = new Vector3(UnityEngine.Random.Range(-300.0f, 300.0f), UnityEngine.Random.Range(-140.0f, -500.0f));
}
public void GetOrb()
{
score += 1;
if(score > nextScore)
{
score = nextScore;
}
TempleLevelUp();
RefreshScoreText();
imageTemple.GetComponent<TempleManager>().SetTempleScale(score, nextScore);
if((score == nextScore) && (templeLevel == MAX_LEVEL))
{
ClearEffect();
}
currentOrb--;
}
void RefreshScoreText()
{
textScore.GetComponent<Text>().text = "徳:" + score + "/" + nextScore;
}
void TempleLevelUp()
{
if(score >= nextScore)
{
if(templeLevel < MAX_LEVEL)
{
templeLevel++;
score = 0;
TempleLevelUpEffect();
nextScore = nextScoreTable[templeLevel];
imageTemple.GetComponent<TempleManager>().SetTemplePicture(templeLevel);
}
}
}
void TempleLevelUpEffect()
{
GameObject smoke = (GameObject)Instantiate(smokePrefab);
smoke.transform.SetParent(canvasGame.transform, false);
smoke.transform.SetSiblingIndex(2);
Destroy(smoke, 0.5f);
}
void ClearEffect()
{
GameObject kusudama = (GameObject)Instantiate(kusudamaPrefab);
kusudama.transform.SetParent(canvasGame.transform, false);
}
}