탑뷰 슈팅 게임 미니 프로젝트
This commit is contained in:
42
Assets/Scripts/Bullet.cs
Normal file
42
Assets/Scripts/Bullet.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Bullet : MonoBehaviour
|
||||
{
|
||||
public float speed = 10;
|
||||
public float damage = 1;
|
||||
Vector2 direction;
|
||||
public Vector2 Direction
|
||||
{
|
||||
set
|
||||
{
|
||||
direction = value.normalized;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
// <20><EFBFBD>Ʈ <20>ڵ<EFBFBD>
|
||||
//this.Direction = new Vector2(10, 1);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
transform.Translate(direction * speed * Time.deltaTime);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collision)
|
||||
{
|
||||
if (collision.tag == "Wall" || collision.tag == "Enemy")
|
||||
{
|
||||
//// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD><C6AE> Instantiate<74><65> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20>ε<EFBFBD><CEB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
//Destroy(gameObject);
|
||||
|
||||
// ObjectPool<6F><6C> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Instantiate<74><65> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ʱ<EFBFBD>ȭ
|
||||
gameObject.SetActive(false);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Bullet.cs.meta
Normal file
2
Assets/Scripts/Bullet.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 96ac0e9ae3d9187478f3a844c7ce8317
|
||||
53
Assets/Scripts/Character.cs
Normal file
53
Assets/Scripts/Character.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Character : MonoBehaviour
|
||||
{
|
||||
// [Range(0 , 3)]
|
||||
public float MaxHP = 3;
|
||||
public GameObject HPGauge;
|
||||
|
||||
float HP;
|
||||
|
||||
private float HPMaxWidth;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
HP = MaxHP;
|
||||
|
||||
if (HPGauge != null)
|
||||
{
|
||||
HPMaxWidth = HPGauge.GetComponent<RectTransform>().sizeDelta.x;
|
||||
}
|
||||
}
|
||||
|
||||
public void Initalize()
|
||||
{
|
||||
HP = MaxHP;
|
||||
}
|
||||
|
||||
/*
|
||||
* 살아있으면 true를 리턴한다.
|
||||
*/
|
||||
public bool Hit(float damage)
|
||||
{
|
||||
HP -= damage;
|
||||
|
||||
if (HP < 0)
|
||||
{
|
||||
HP = 0;
|
||||
}
|
||||
|
||||
if(HPGauge != null)
|
||||
{
|
||||
HPGauge.GetComponent<RectTransform>().sizeDelta = new Vector2(HP/MaxHP * HPMaxWidth, HPGauge.GetComponent<RectTransform>().sizeDelta.y);
|
||||
}
|
||||
return HP > 0;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Character.cs.meta
Normal file
2
Assets/Scripts/Character.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d0368c6c2ee296f409fb23e40691471e
|
||||
125
Assets/Scripts/EnemyController.cs
Normal file
125
Assets/Scripts/EnemyController.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyController : MonoBehaviour
|
||||
{
|
||||
enum State
|
||||
{
|
||||
Spawning,
|
||||
Moving,
|
||||
Dying
|
||||
}
|
||||
|
||||
[Range(0f, 10f)]
|
||||
public float moveSpeed = 2f;
|
||||
|
||||
public Material flashMaterial;
|
||||
public Material defaultMaterial;
|
||||
|
||||
public AudioClip hitSound;
|
||||
public AudioClip deadSound;
|
||||
|
||||
GameObject target;
|
||||
|
||||
State state;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
//target = GameObject.Find("Player");
|
||||
// target = GameObject.FindGameObjectWithTag("Player");
|
||||
// state = State.Moving;
|
||||
}
|
||||
|
||||
public void Spawn(GameObject target)
|
||||
{
|
||||
this.target = target;
|
||||
state = State.Spawning;
|
||||
GetComponent<Character>().Initalize();
|
||||
GetComponent<Animator>().SetTrigger("Spawn");
|
||||
Invoke("StartMoving", 1f);
|
||||
GetComponent<Collider2D>().enabled = false;
|
||||
}
|
||||
|
||||
void StartMoving()
|
||||
{
|
||||
GetComponent<Collider2D>().enabled = true;
|
||||
state = State.Moving;
|
||||
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (state == State.Moving)
|
||||
{
|
||||
Vector2 direction = target.transform.position - transform.position;
|
||||
transform.Translate(direction.normalized * (moveSpeed * Time.fixedDeltaTime), Space.World);
|
||||
|
||||
if (direction.x < 0)
|
||||
{
|
||||
GetComponent<SpriteRenderer>().flipX = true;
|
||||
}
|
||||
else if (direction.x > 0)
|
||||
{
|
||||
GetComponent<SpriteRenderer>().flipX = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collision)
|
||||
{
|
||||
Debug.Log("Hit");
|
||||
|
||||
if (collision.tag == "Bullet")
|
||||
{
|
||||
float d = collision.gameObject.GetComponent<Bullet>().damage;
|
||||
|
||||
if(GetComponent<Character>().Hit(d))
|
||||
{
|
||||
// 살아있을 때
|
||||
Flash();
|
||||
GetComponent<AudioSource>().PlayOneShot(hitSound);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// 죽었을 때
|
||||
Die();
|
||||
GetComponent<AudioSource>().PlayOneShot(deadSound);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void Flash()
|
||||
{
|
||||
GetComponent<SpriteRenderer>().material = flashMaterial;
|
||||
Invoke("AfterFlash", 0.5f);
|
||||
}
|
||||
|
||||
void AfterFlash()
|
||||
{
|
||||
GetComponent<SpriteRenderer>().material = defaultMaterial;
|
||||
}
|
||||
|
||||
void Die()
|
||||
{
|
||||
state = State.Dying;
|
||||
GetComponent<Animator>().SetTrigger("Die");
|
||||
|
||||
// 일정 시간이 지난 후 호출하는 함수
|
||||
Invoke("AfterDying", 1.4f);
|
||||
}
|
||||
|
||||
void AfterDying()
|
||||
{
|
||||
// Destroy(gameObject);
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/EnemyController.cs.meta
Normal file
2
Assets/Scripts/EnemyController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 99e6322ea18a85e4c9a1fedc39464616
|
||||
53
Assets/Scripts/GameManager.cs
Normal file
53
Assets/Scripts/GameManager.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
public GameObject player;
|
||||
public float spawnTerm = 5;
|
||||
public float fasterEverySpawn = 0.05f;
|
||||
public float minSpawnTerm = 1;
|
||||
public TextMeshProUGUI scoreText;
|
||||
float timeAfterLastSpawn;
|
||||
private float score;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
timeAfterLastSpawn = 0;
|
||||
score = 0;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
timeAfterLastSpawn += Time.deltaTime;
|
||||
score += Time.deltaTime;
|
||||
|
||||
if (timeAfterLastSpawn > spawnTerm)
|
||||
{
|
||||
timeAfterLastSpawn -= spawnTerm;
|
||||
|
||||
SpawnEnemy();
|
||||
|
||||
spawnTerm -= fasterEverySpawn;
|
||||
|
||||
if (spawnTerm < minSpawnTerm)
|
||||
{
|
||||
spawnTerm = minSpawnTerm;
|
||||
}
|
||||
}
|
||||
|
||||
scoreText.text = ((int)score).ToString();
|
||||
}
|
||||
|
||||
void SpawnEnemy()
|
||||
{
|
||||
float x = Random.Range(-12f, 12f);
|
||||
float y = Random.Range(-7f, 8f);
|
||||
|
||||
GameObject obj = GetComponent<ObjectPool>().Get();
|
||||
obj.transform.position = new Vector3(x, y, 0);
|
||||
obj.GetComponent<EnemyController>().Spawn(player);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/GameManager.cs.meta
Normal file
2
Assets/Scripts/GameManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d6302fc59b848224392f6a0051793835
|
||||
27
Assets/Scripts/GameOverManager.cs
Normal file
27
Assets/Scripts/GameOverManager.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class GameOverManager : MonoBehaviour
|
||||
{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnPressPlayAgain()
|
||||
{
|
||||
SceneManager.LoadScene("GameScene");
|
||||
}
|
||||
|
||||
public void OnPressMainMenu()
|
||||
{
|
||||
SceneManager.LoadScene("MainMenuScene");
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/GameOverManager.cs.meta
Normal file
2
Assets/Scripts/GameOverManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 54f57eda84910434c9d5b6b583477cc7
|
||||
27
Assets/Scripts/MenuManager.cs
Normal file
27
Assets/Scripts/MenuManager.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class MenuManager : MonoBehaviour
|
||||
{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnPressGameStart()
|
||||
{
|
||||
SceneManager.LoadScene("GameScene");
|
||||
}
|
||||
|
||||
public void OnPressExit()
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/MenuManager.cs.meta
Normal file
2
Assets/Scripts/MenuManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 53799a2c476e5174c9763628166631ff
|
||||
45
Assets/Scripts/ObjectPool.cs
Normal file
45
Assets/Scripts/ObjectPool.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ObjectPool : MonoBehaviour
|
||||
{
|
||||
public GameObject prefab;
|
||||
public Transform parent;
|
||||
public int maxObject = 30;
|
||||
List<GameObject> _pool;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
_pool = new List<GameObject>();
|
||||
|
||||
for(int i = 0; i < maxObject; i++)
|
||||
{
|
||||
//GameObject obj = Instantiate(prefab);
|
||||
GameObject obj = Instantiate(prefab, parent);
|
||||
obj.SetActive(false); // ??? ?????? ??? ??????? ???? ??? ????
|
||||
_pool.Add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public GameObject Get()
|
||||
{
|
||||
foreach(GameObject obj in _pool)
|
||||
{
|
||||
if(!obj.activeInHierarchy)
|
||||
{
|
||||
obj.SetActive(true);
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/ObjectPool.cs.meta
Normal file
2
Assets/Scripts/ObjectPool.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b277eaec36272684a97a932935f464db
|
||||
147
Assets/Scripts/PlayerController.cs
Normal file
147
Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
public float speed = 3;
|
||||
public GameObject bulletPrefab;
|
||||
Vector3 move;
|
||||
|
||||
public Material flashMaterial;
|
||||
public Material defaultMaterial;
|
||||
|
||||
public AudioClip shotSound;
|
||||
public AudioClip hitSound;
|
||||
public AudioClip deadSound;
|
||||
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
move = Vector3.zero;
|
||||
|
||||
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
|
||||
{
|
||||
//transform.Translate(new Vector3( -speed * Time.deltaTime, 0, 0));
|
||||
move += new Vector3(-1, 0, 0);
|
||||
}
|
||||
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
|
||||
{
|
||||
move += new Vector3(0, 1, 0);
|
||||
}
|
||||
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
|
||||
{
|
||||
move += new Vector3(1, 0, 0);
|
||||
}
|
||||
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
|
||||
{
|
||||
move += new Vector3(0, -1, 0);
|
||||
}
|
||||
|
||||
move = move.normalized;
|
||||
|
||||
if (move.x < 0)
|
||||
{
|
||||
GetComponent<SpriteRenderer>().flipX = true;
|
||||
}
|
||||
|
||||
if (move.x > 0)
|
||||
{
|
||||
GetComponent<SpriteRenderer>().flipX = false;
|
||||
}
|
||||
|
||||
if (move.magnitude > 0)
|
||||
{
|
||||
GetComponent<Animator>().SetTrigger("Move");
|
||||
}
|
||||
else
|
||||
{
|
||||
GetComponent<Animator>().SetTrigger("Stop");
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Shoot();
|
||||
}
|
||||
}
|
||||
|
||||
void Shoot()
|
||||
{
|
||||
GetComponent<AudioSource>().PlayOneShot(shotSound);
|
||||
|
||||
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
// Debug.Log(worldPosition);
|
||||
worldPosition.z = 0;
|
||||
worldPosition -= (transform.position + new Vector3(0, -0.5f, 0));
|
||||
|
||||
|
||||
//GameObject newBullet = Instantiate<GameObject>(bulletPrefab);
|
||||
GameObject newBullet = GetComponent<ObjectPool>().Get();
|
||||
|
||||
if (newBullet != null)
|
||||
{
|
||||
// ??? ??? ???? ??? ??? ????
|
||||
newBullet.transform.position = transform.position + new Vector3(0, -0.5f);
|
||||
//newBullet.GetComponent<Bullet>().Direction = new Vector2(1, 0);
|
||||
newBullet.GetComponent<Bullet>().Direction = worldPosition;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
if (collision.gameObject.tag == "Enemy")
|
||||
{
|
||||
if (GetComponent<Character>().Hit(1))
|
||||
{
|
||||
// 살아있다.
|
||||
GetComponent<AudioSource>().PlayOneShot(hitSound);
|
||||
Flash();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 죽은 상태
|
||||
Die();
|
||||
GetComponent<AudioSource>().PlayOneShot(deadSound);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
transform.Translate(move * speed * Time.fixedDeltaTime);
|
||||
}
|
||||
|
||||
void Flash()
|
||||
{
|
||||
GetComponent<SpriteRenderer>().material = flashMaterial;
|
||||
Invoke("AfterFlash", 0.5f);
|
||||
}
|
||||
|
||||
void AfterFlash()
|
||||
{
|
||||
GetComponent<SpriteRenderer>().material = defaultMaterial;
|
||||
}
|
||||
|
||||
void Die()
|
||||
{
|
||||
GetComponent<Animator>().SetTrigger("Die");
|
||||
|
||||
// 일정 시간이 지난 후 호출하는 함수
|
||||
Invoke("AfterDying", 0.875f);
|
||||
}
|
||||
|
||||
void AfterDying()
|
||||
{
|
||||
// Destroy(gameObject);
|
||||
// gameObject.SetActive(false);
|
||||
SceneManager.LoadScene("GameOverScene");
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/PlayerController.cs.meta
Normal file
2
Assets/Scripts/PlayerController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c0d991982c0b4449beae58cda8358e0
|
||||
Reference in New Issue
Block a user