using UnityEngine;
using System.Collections;
// 플레이어의 움직임을 처리하는 클래스
public class Player_2 : MonoBehaviour {
// 플레이어의 속도
public float speed = 10.0f;
// 플레이어 HP
public int PlayerHp = 10;
// 플레이어 컨트롤러(터치 or 키보드)
public bool IsTouch = false;
// 플레이어의 최대 최소 이동범위를 위한 변수들
Vector2 Screen_Size;
Vector2 Player_SpriteSize;
// 플레이어의 총알
public GameObject bullet;
// 총알 발사 딜레이(자동)
public float Bullet_Delay = 0.0f;
float Bullet_DeltaTime = 0.0f;
// Use this for initialization
void Start () {
Screen_Size = new Vector2 (Screen.width / 100.0f, Screen.height / 100.0f);
Player_SpriteSize = this.gameObject.GetComponent<SpriteRenderer> ().sprite.textureRect.center/100.0f;
}
// Update is called once per frame
void Update () {
if (PlayerHp < 1) {
GameObject.Destroy(this.gameObject);
}
if (IsTouch == false) {
KeyBoardControl();
}
Bullet_DeltaTime += Time.deltaTime;
if (Bullet_DeltaTime > Bullet_Delay) {
Instantiate (bullet, this.gameObject.transform.position, Quaternion.identity);
Bullet_DeltaTime = 0.0f;
}
}
void KeyBoardControl(){
// 캐릭터 좌우 이동, 화면 밖으로 나갔을때 처리
float dirX = Input.GetAxis ("Horizontal");
this.gameObject.transform.Translate (Vector3.right * dirX * speed*Time.deltaTime);
// 캐릭터가 화면 왼쪽으로 나갔을때 X 최소값으로 되돌린다.
if (this.gameObject.transform.position.x < -Screen_Size.x+(Player_SpriteSize.x)) {
this.gameObject.transform.position = new Vector3(-Screen_Size.x+(Player_SpriteSize.x),this.gameObject.transform.position.y, this.gameObject.transform.position.z);
}
// 캐릭터가 화면 오른쪽으로 나갔을때 X 최대값으로 되돌린다.
else if(this.gameObject.transform.position.x > Screen_Size.x-(Player_SpriteSize.x)){
this.gameObject.transform.position = new Vector3(Screen_Size.x-(Player_SpriteSize.x),this.gameObject.transform.position.y, this.gameObject.transform.position.z);
}
// 캐릭터 상하 이동, 화면 밖으로 나갔을때 처리
float dirY = Input.GetAxis ("Vertical");
this.gameObject.transform.Translate (Vector3.up * dirY * speed*Time.deltaTime);
// 캐릭터가 화면 아래로 나갔을때 Y최소값으로 되돌린다.
if (this.gameObject.transform.position.y < -Screen_Size.y+(Player_SpriteSize.y)) {
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, -Screen_Size.y+(Player_SpriteSize.y), this.gameObject.transform.position.z);
}
// 캐릭터가 화면 아래로 나갔을때 Y최대값으로 되돌린다.
else if(this.gameObject.transform.position.y > Screen_Size.y-(Player_SpriteSize.y)){
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, Screen_Size.y-(Player_SpriteSize.y), this.gameObject.transform.position.z);
}
}
// 플레이어가 다른 오브젝트와 충돌 되었을 경우
void OnTriggerEnter2D(Collider2D other) {
// 플레이어와 적이 충돌 되었을 경우
if (other.gameObject.tag == "Enemy") {
//플레이어의 체력을 깍는다.
PlayerHp--;
}
}
}
using UnityEngine;
using System.Collections;
// 총알의 움직임을 직선으로 처리하는 클래스
// X, Y의 방향을 설정해주면 해당 방향으로 이동함
public class BulletMoveLine : MonoBehaviour {
// 이동할 X,Y방향
public float moveX = 0.0f;
public float moveY = 0.0f;
// 총알의 속도
public float speed = 0.0f;
// 총알의 이동 각도
float Angle = 0.0f;
// 화면 안의 거리를 체크
Vector2 LimitDistance;
// Use this for initialization
void Start () {
LimitDistance = new Vector2 (Screen.width / 100.0f, Screen.height / 100.0f);
}
// Update is called once per frame
void Update () {
// 일직선으로 이동
this.gameObject.transform.Translate (new Vector3 (moveX, moveY, 0) * speed * Time.deltaTime);
// 화면 밖일 경우 제거
if (this.gameObject.transform.position.y < -LimitDistance.y || this.gameObject.transform.position.y > LimitDistance.y
|| this.gameObject.transform.position.x < -LimitDistance.x || this.gameObject.transform.position.x > LimitDistance.x){
Destroy(this.gameObject);
}
}
// 총알이 다른 오브젝트와 충돌 되었을 경우
void OnTriggerEnter2D(Collider2D other) {
// 현재 총알의 태그가 적 일경우
if (this.gameObject.tag == "Enemy") {
// 플레이어와 닿으면
if (other.gameObject.tag == "Player") {
// 총알 삭제
GameObject.Destroy (this.gameObject);
}
}
// 현재 총알의 태그가 플레이어일 경우
else if (this.gameObject.tag == "Player") {
// 적과 닿으면
if (other.gameObject.tag == "Enemy") {
// 총알 삭제
GameObject.Destroy (this.gameObject);
}
}
}
}
using UnityEngine;
using System.Collections;
public class EnemyInfo : MonoBehaviour {
public int Hp = 1;
//public GameObject AttackPattern;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// 적 캐릭터가 다른 오브젝트와 충돌 했을 경우
void OnTriggerEnter2D(Collider2D other) {
// 충돌한 오브젝트가 플레이어 태그일 경우
if (other.gameObject.tag == "Player") {
// 적캐릭터의 Hp 하락
Hp--;
}
}
}
using UnityEngine;
using System.Collections;
// 적 캐릭터의 이동을 처리하는 클래스
// 지정한 패스의 순서대로 이동하게 설계
public class EnemyMove : MonoBehaviour {
// 인스펙터 창에서 Empty GameObject를 등록해서 패스를 사용
public GameObject[] Path;
// 이동할 적 캐릭터를 인스펙터 창에서 등록
public GameObject Enemy;
// 적의 이동 속도 조절
public float Speed = 0.0f;
// 현재 가지고 있는 패스
int NowPathNum = 0;
// 캐릭터의 이동 벡터
Vector3 DirectionVector;
// Use this for initialization
void Start () {
// 패스가 0~1개로 등록 됬을 경우 이동을 금지
// 즉 무조건 패스는 2개 이상 등록
if (Path.Length > 1) {
// 적의 시작 위치를 패스의 0번으로 초기화
Enemy.transform.position = Path [NowPathNum].gameObject.transform.position;
// 현재 위치에서 다음 위치까지의 방향벡터 구함
DirectionVector = (Path [NowPathNum+1].gameObject.transform.position - Path [NowPathNum].gameObject.transform.position).normalized;
}
}
// Update is called once per frame
void Update () {
// 적 캐릭터의 Hp가 0일 경우
if (Enemy.GetComponent<EnemyInfo>().Hp < 1) {
// 해당 적 캐릭터 삭제
GameObject.Destroy(this.gameObject);
}
if (Path.Length > 1) {
// 캐릭터 이동
Enemy.transform.position += (DirectionVector * Speed * Time.deltaTime);
// 만약 다음 가야할 패스가 마지막이 아니고
if (Path.Length > NowPathNum + 1) {
// 다음 가야할 위치와 현재 적의 위치 사이의 거리가 일정 범위 이하 일때
if ((Path [NowPathNum + 1].gameObject.transform.position - Enemy.transform.position).magnitude < 1.0f) {
NowPathNum++; // 현재 패스를 다음 패스로 바꿈.
if (Path.Length != NowPathNum) {
// 현재 위치에서 다음 위치까지의 방향벡터 구함
DirectionVector = (Path [NowPathNum + 1].gameObject.transform.position - Path [NowPathNum].gameObject.transform.position).normalized;
}
}
}
else {
// 더 이상 이동할 패스가 없을때 적 캐릭터 삭제
GameObject.Destroy (this.gameObject);
}
}
}
}
using UnityEngine;
using System.Collections;
// 이펙트 애니메이션의 프레임이 마지막 프레임일때 이펙트 오브젝트를 삭제하는 클래스
public class Effect : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void Destroy(){
Destroy(this.gameObject);
}
}
using UnityEngine;
using System.Collections;
// 충돌 시 이펙트를 발생 시키는 것을 처리하는 클래스
public class Crash : MonoBehaviour {
// 발생 시킬 이펙트
public GameObject effectObj;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
// 현재 오브젝트가 Player 태그 이고
if( this.gameObject.tag == "Player" )
{
// 충돌한 오브젝트가 Enemy 태그 일때
if( other.gameObject.tag == "Enemy" )
{
// 이펙트를 현재 오브젝트의 위치에 소환
Vector3 spawn = this.gameObject.transform.position;
spawn.z = effectObj.gameObject.transform.position.z;
Instantiate(effectObj, spawn, Quaternion.identity);
}
}
// 현재 오브젝트가 Enemy 태그 이고
if( this.gameObject.tag == "Enemy" )
{
// 충돌한 오브젝트가 Player 태그 일때
if( other.gameObject.tag == "Player")
{
// 이펙트를 현재 오브젝트의 위치에 소환
Vector3 spawn = this.gameObject.transform.position;
spawn.z = effectObj.gameObject.transform.position.z;
Instantiate(effectObj, spawn, Quaternion.identity);
}
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// 현재 플레이어의 체력을 UI로 보여주기 위한 클래스
public class ShowHp : MonoBehaviour {
// UI에 보여질 현재 플레이어의 체력
int NowPlayerHp;
//플레이어 오브젝트를 담는 변수
GameObject Player;
// Use this for initialization
void Start () {
// 플레이어를 찾아서 담는다.
Player = GameObject.Find ("Player");
// 플레이어의 체력을 UI에 보여질 현재 플레이어의 체력에 넣는다.
NowPlayerHp = Player.GetComponent<Player>().PlayerHp;
// 유아이로 보여준다.
this.gameObject.GetComponent<Text>().text = "PlayerHP : " + NowPlayerHp.ToString();
}
// Update is called once per frame
void Update () {
// 현재 보여지고 있는 체력과 플레이어의 체력이 다를 경우
if (NowPlayerHp != Player.GetComponent<Player> ().PlayerHp) {
// UI에 보여질 체력을 갱신하고
NowPlayerHp = Player.GetComponent<Player> ().PlayerHp;
// 유아이로 보여준다.
this.gameObject.GetComponent<Text>().text = "PlayerHP : " + NowPlayerHp.ToString();
}
}
}