카테고리 없음

2018.04.09 수업내용 (2D 슈팅게임)

일등하이 2018. 5. 15. 21:42
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Main : MonoBehaviour {
 
    public Jet jet;
    public ObjectPool pool;
 
    // Use this for initialization
    void Start () {
        this.pool.PreLoadBullts();
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonUp(0))
        {
            jet.Shoot( pool.Pop());
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Jet : MonoBehaviour {
 
    // Use this for initialization
    private Main main;
 
    void Start () {
        this.main = GameObject.Find("Main").GetComponent<Main>();    
    }
    
    // Update is called once per frame
    void Update () {
        
    }
 
    //미사일을 발사 
    public void Shoot(Bullet bullet)
    {
        Debug.Log("Shoot! : " + bullet);
 
        bullet.transform.parent = null;
        bullet.transform.position = this.transform.position;
        bullet.gameObject.SetActive(true);
 
        ////미사일을 생성 
        //var obj = Resources.Load("bullet");
        //var prefab = Instantiate(obj) as GameObject;
 
        ////위치를 비행기 위치로 설정 
        //prefab.transform.position = this.transform.position;    //0, -4.36, 0
 
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Bullet : MonoBehaviour {
 
    public float speed = 1f;
    private Vector3 pos;
    public System.Action<Bullet> OnBulletDestroy;
 
    // Use this for initialization
    void Start () {
        
    }
 
    // Update is called once per frame
    void Update () {
 
        this.transform.Translate(Vector3.up * speed * Time.deltaTime);
 
        this.pos = this.transform.position;
        //Debug.LogFormat("{0}\t{1}", this.pos.x, this.pos.y);
 
        var screenPoint = Camera.main.WorldToScreenPoint(this.pos);
 
        if (screenPoint.y > Screen.height)
        {
            OnBulletDestroy(this);
        }
 
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
 
public class ObjectPool : MonoBehaviour {
 
    private List<Bullet> bulletList;
    private Object bulletObj;
 
    // Use this for initialization
    void Start () {
        this.bulletList = new List<Bullet>();
    }
    
    // Update is called once per frame
    void Update () {
        
    }
 
    //총알 미리 로드 해서 10개 가지고있는다.
    public void PreLoadBullts(int count = 10)
    {
        this.bulletObj = Resources.Load("bullet");
 
        for(int i = 0; i<count; i++)
        {
            CreateBullet();
        }
    }
 
    private void OnBulletDestory(Bullet bullet)
    {
        this.bulletList.Add(bullet);
 
        bullet.transform.parent = this.transform;
        bullet.gameObject.SetActive(false);
 
    }
 
    private Bullet CreateBullet()
    {
        var prefab = Instantiate(this.bulletObj) as GameObject;
        prefab.SetActive(false);
        prefab.transform.parent = this.transform;
        var bullet = prefab.GetComponent<Bullet>();
 
        //방법 1
        //bullet.OnBulletDestroy = OnBulletDestory;
 
        //방법 2
        bullet.OnBulletDestroy = (destoryedBullet) => {
            this.bulletList.Add(bullet);
 
            bullet.transform.parent = this.transform;
            bullet.gameObject.SetActive(false);
        };
 
 
        this.bulletList.Add(bullet);
 
        return bullet;
    }
 
    public Bullet Pop()
    {
        Bullet bullet = null;
 
        if (this.bulletList.Count == 0)
        {
            //만들어서 줌 
            bullet = CreateBullet();
        }
        else
        {
            //있는걸 꺼내줌 
            bullet = this.bulletList.First();
            this.bulletList.RemoveAt(0);
        }
        
        return bullet;
    }
 
    public void Push(Bullet bullet)
    {
        this.bulletList.Add(bullet);
    }
 
 
    //총알 지급 
}
 
cs



반응형