ItemGenerator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemGanerator : MonoBehaviour
{
public GameObject applePrefab;
public GameObject bombPrefab;
private float span = 1.0f;
private float delta;
private int ratio = 2;
public float speed;
private int state = 0;
// Start is called before the first frame update
void Start()
{
this.state = 1;
}
// Update is called once per frame
void Update()
{
if (this.state == 0) return;
this.delta += Time.deltaTime;
if (this.delta > this.span)
{
this.delta = 0;
GameObject targetPrefab;
int dice = Random.Range(1, 11);
if(dice <= this.ratio)
{
targetPrefab = this.bombPrefab;
}
else
{
targetPrefab = this.applePrefab;
}
var go = Instantiate(targetPrefab);
var x = Random.Range(-1, 2);
var z = Random.Range(-1, 2);
go.transform.position = new Vector3(x, 3, z);
var item = go.GetComponent<ItemController>();
item.speed = this.speed;
}
}
public void SetParameter(float span, float speed, int ratio)
{
this.span = span;
this.speed = speed;
this.ratio = ratio;
}
public void Stop()
{
this.state = 0;
}
}
ItemController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemController : MonoBehaviour
{
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Translate(-this.transform.up * this.speed * Time.deltaTime);
if(this.transform.position.y < -1)
{
Destroy(this.gameObject);
}
}
}
GameDirctor
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirctor : MonoBehaviour
{
public Text textScore;
public Text textTime;
private float time = 5f;
private int score = 0;
private bool isGameOver;
public ItemGanerator itemGen;
private void Start()
{
this.textTime.text = string.Format("{0:0.0}", this.time);
this.textScore.text = string.Format("{0:#,###}", this.score);
}
// Update is called once per frame
void Update()
{
if(this.isGameOver == false)
{
this.time -= Time.deltaTime;
if (this.time <= 0)
{
this.isGameOver = true;
this.itemGen.Stop();
}
this.textTime.text = string.Format("{0:0.0}", this.time);
this.textScore.text = string.Format("{0:#,###}", this.score);
}
}
public void GetApple()
{
this.score += 100;
}
public void GetBomb()
{
this.score /= 2;
}
}
BasketController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasketController : MonoBehaviour
{
public AudioClip appleSfx;
public AudioClip bombSfx;
private AudioSource audioSource;
[SerializeField]
private GameDirctor dirctor;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Apple")
{
Debug.Log("사과");
this.audioSource.PlayOneShot(this.appleSfx);
this.dirctor.GetApple();
}
else
{
Debug.Log("폭탄");
this.audioSource.PlayOneShot(this.bombSfx);
this.dirctor.GetBomb();
}
Destroy(other.gameObject);
}
// Start is called before the first frame update
void Start()
{
this.audioSource = this.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.red, 2f);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.Infinity)) //physics.Raycast 기능을 사용하려면 Collider랑 충돌 필요
{
int x = Mathf.RoundToInt(hit.point.x);
int z = Mathf.RoundToInt(hit.point.z);
var pos = new Vector3(x, this.transform.position.y, z);
this.transform.position = pos;
}
}
}
}