using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
[SerializeField]
private Text txtTime;
[SerializeField]
private Text txtScore;
private float time = 10.0f;
private int score = 0;
void Start()
{
this.UpdateScoreUI();
// 노란색 바구니가 선택된 경우 시간 +10초
if (GameManager.instance.selectedBasketType == BasketType.Yellow)
{
this.time += 10.0f;
}
}
public void UpdateScoreUI()
{
this.txtScore.text = string.Format("{0} Point", score);
}
public void IncreaseScore(int score)
{
this.score += score;
}
public void DecreaseScore(int score)
{
this.score -= score;
}
// 시간을 늘리는 메서드
public void IncreaseTime(float addedTime)
{
this.time += addedTime;
}
public float GetCurrentTime()
{
return this.time;
}
void Update()
{
this.time -= Time.deltaTime; // 매프레임마다 감소된 시간을 표시
this.txtTime.text = this.time.ToString("F1"); // 소수점 1자리까지 표시
GameManager.instance.Score = this.score;
if (this.time <= 0)
{
SceneManager.LoadScene("GameOver");
}
}
}
using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager instance = null;
public GameObject selectedBasket; // 선택된 바구니
public Material selectedMaterial; // 선택된 바구니의 Material
public BasketType selectedBasketType; // 선택된 바구니의 유형
public int Score;
void Awake()
{
if (instance == null)
{
instance = this;
selectedBasket = null; // 선택된 바구니 초기화
selectedMaterial = null; // 선택된 바구니의 Material 초기화
selectedBasketType = BasketType.Red; // 초기 바구니 유형 설정
DontDestroyOnLoad(gameObject); // 씬 변경 시 파괴 방지
Score = 0;
}
else if (instance != this)
{
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class initGameManager : MonoBehaviour
{
public void InitGameManager()
{
GameManager.instance.Score = 0;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemController : MonoBehaviour
{
// Start is called before the first frame update
private float moveSpeed = 1f;
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Vector3.down*this.moveSpeed*Time.deltaTime);
if(this.transform.position.y < -0)
{
Destroy(this.gameObject);
}
}
}
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject basket; // 바구니 게임 오브젝트를 참조하는 변수
// Start is called before the first frame update
void Start()
{
if (GameManager.instance != null && GameManager.instance.selectedMaterial != null)
{
Renderer renderer = GetComponent<Renderer>();
if (renderer == null) // 바구니 게임 오브젝트에 Renderer 컴포넌트가 없다면
{
renderer = GetComponentInChildren<Renderer>(); // 자식 게임 오브젝트의 Renderer 컴포넌트를 참조
}
if (renderer != null)
{
renderer.material = GameManager.instance.selectedMaterial; // Material 변경
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemGenerator : MonoBehaviour
{
private float elasedTime; //경과 시간
private float spawnTime = 1f; //1초에 한번씩
//생성하기 위해서 Prefab이 필요함
public GameObject applePrefab;
public GameObject bombPrefab;
[SerializeField]
private GameDirector gameDirector;
void Start()
{
}
// Update is called once per frame
void Update()
{
//시간 재기
//변수에 Time.deltaTime더해라
this.elasedTime += Time.deltaTime;
// 현재 남은 시간을 가져오기
float remainingTime = gameDirector.GetCurrentTime();
// 남은 시간에 따라 아이템 생성 간격 조정
if (remainingTime >= 40f)
{
this.spawnTime = 1.5f;
Debug.Log("남은시간 40초 이상 생성주기 1.5");
}
else if (remainingTime >= 20f)
{
this.spawnTime = 1f;
Debug.Log("남은시간 20초 이상 생성주기 1.0");
}
else
{
this.spawnTime = 0.5f;
Debug.Log("남은시간 0초 이상 생성주기 0.5");
}
//1초가 지났다면
if (this.elasedTime >= this.spawnTime)
{
//아이템을 생성
this.CreateItem();
//초기화
this.elasedTime = 0;
}
}
private void CreateItem()
{
// 남은 시간에 따른 폭탄 생성 확률 조정
float remainingTime = gameDirector.GetCurrentTime();
int bombProbability;
if (remainingTime >= 40f)
{
bombProbability = 2; // 20% 확률
}
else if (remainingTime >= 20f)
{
bombProbability = 3; // 30% 확률
}
else
{
bombProbability = 4; // 40% 확률
}
//사과 또는 폭탄
int rand = Random.Range(1, 11); //1 ~ 10
GameObject itemGo = null;
if (rand > bombProbability)
{
//사과
itemGo = Instantiate(this.applePrefab);
}
else
{
//폭탄
itemGo = Instantiate(this.bombPrefab);
}
//위치 설정
//x: -1, 1
//z: -1, 1
int x = Random.Range(-1, 2); //-1, 0, 1
int z = Random.Range(-1, 2);
//위치를 설정
itemGo.transform.position = new Vector3(x, 3.5f, z);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeScnenController : MonoBehaviour
{
public void LoadScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}
'KDT > 유니티 기초' 카테고리의 다른 글
[SimpleRPG] 플레이어 이동과 공격 + 몬스터 공격시 피격 구현 (0) | 2023.08.09 |
---|---|
[Apple Catch] High Score 구현 (0) | 2023.08.07 |
[MoveHunt] 몬스터를 클릭하면 이동 후 공격, 바닥을 클릭하면 해당 위치로 이동 구현 (1) | 2023.08.06 |
[UnityChan] 캐릭터 화면 클릭 시 해당 위치로 이동하기 (0) | 2023.08.04 |
[밤 송이 던지기] (0) | 2023.08.04 |