148 lines
3.6 KiB
C#
148 lines
3.6 KiB
C#
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");
|
|
}
|
|
}
|