Showing
8 changed files
with
342 additions
and
0 deletions
code/MiniGame/MiniGameManager.cs
0 → 100644
1 | +using System.Collections; | ||
2 | +using System.Collections.Generic; | ||
3 | +using UnityEngine; | ||
4 | +using UnityEngine.SceneManagement; | ||
5 | +using UnityEngine.UI; | ||
6 | + | ||
7 | +public enum GameState | ||
8 | +{ | ||
9 | + Ready, | ||
10 | + Play, | ||
11 | + End | ||
12 | +} | ||
13 | + | ||
14 | +public class MiniGameManager : MonoBehaviour | ||
15 | +{ | ||
16 | + public List<Rabbit> m_rabbits; | ||
17 | + static public GameState gameState; | ||
18 | + public GameObject UIButton; | ||
19 | + public Text timeText; | ||
20 | + public Text scoreText; | ||
21 | + | ||
22 | + private int score = 0; | ||
23 | + private float time = 0f; | ||
24 | + private float sec = 0f; | ||
25 | + | ||
26 | + private int playTime = 30; | ||
27 | + | ||
28 | + void Start() | ||
29 | + { | ||
30 | + SetSceneReady(); | ||
31 | + } | ||
32 | + | ||
33 | + public void SetSceneReady() | ||
34 | + { | ||
35 | + gameState = GameState.Ready; | ||
36 | + timeText.text = "00 : " + playTime.ToString("D2"); | ||
37 | + ResetRabbits(); | ||
38 | + } | ||
39 | + | ||
40 | + public void LoadMainScene() | ||
41 | + { | ||
42 | + // 로딩바? | ||
43 | + SceneManager.LoadScene("Main"); | ||
44 | + } | ||
45 | + | ||
46 | + void ResetRabbits() | ||
47 | + { | ||
48 | + for (int i = 0; i < m_rabbits.Count; i++) | ||
49 | + { | ||
50 | + m_rabbits[i].None(); | ||
51 | + } | ||
52 | + } | ||
53 | + | ||
54 | + void ExitRabbits() | ||
55 | + { | ||
56 | + for (int i = 0; i < m_rabbits.Count; i++) | ||
57 | + { | ||
58 | + if (m_rabbits[i].state != RabbitState.Die && m_rabbits[i].state != RabbitState.None) | ||
59 | + m_rabbits[i].Exit(); | ||
60 | + } | ||
61 | + } | ||
62 | + | ||
63 | + public void ShowScore() | ||
64 | + { | ||
65 | + gameState = GameState.End; | ||
66 | + scoreText.text = score + " 마리를 잡았습니다!"; | ||
67 | + SetGamePanel.Instance.SetPanelOn(); | ||
68 | + scoreText.gameObject.SetActive(true); | ||
69 | + UIButton.SetActive(true); | ||
70 | + } | ||
71 | + | ||
72 | + | ||
73 | + void Update() | ||
74 | + { | ||
75 | + if (gameState == GameState.Play) | ||
76 | + { | ||
77 | + time += Time.deltaTime; | ||
78 | + sec = Mathf.Ceil(playTime - time); | ||
79 | + timeText.text = "00 : " + ((int)sec).ToString("D2"); | ||
80 | + | ||
81 | + if (sec <= 0) | ||
82 | + { | ||
83 | + ExitRabbits(); | ||
84 | + ShowScore(); | ||
85 | + } | ||
86 | + | ||
87 | + if (Input.GetMouseButtonDown(0)) | ||
88 | + { | ||
89 | + Vector2 touchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); | ||
90 | + RaycastHit2D hit = Physics2D.Raycast(touchPos, Vector2.zero); | ||
91 | + | ||
92 | + if (hit) | ||
93 | + { | ||
94 | + int num = int.Parse(hit.collider.gameObject.name); | ||
95 | + if (m_rabbits[num].state == RabbitState.Idle) | ||
96 | + { | ||
97 | + score += 1; | ||
98 | + m_rabbits[num].Die(); | ||
99 | + } | ||
100 | + } | ||
101 | + } | ||
102 | + } | ||
103 | + else | ||
104 | + return; | ||
105 | + } | ||
106 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
code/MiniGame/MiniGameManager.cs.meta
0 → 100644
code/MiniGame/Rabbit.cs
0 → 100644
1 | +using System.Collections; | ||
2 | +using System.Collections.Generic; | ||
3 | +using UnityEngine; | ||
4 | + | ||
5 | + | ||
6 | +public enum RabbitState | ||
7 | +{ | ||
8 | + None, | ||
9 | + Pop, | ||
10 | + Idle, | ||
11 | + Die, | ||
12 | + Exit | ||
13 | +} | ||
14 | + | ||
15 | +public class Rabbit : MonoBehaviour | ||
16 | +{ | ||
17 | + private Animator m_animator; | ||
18 | + | ||
19 | + public RabbitState state = RabbitState.None; | ||
20 | + public GameObject body; | ||
21 | + | ||
22 | + private float m_resetTime = 0f; | ||
23 | + private float m_exitTime = 0f; | ||
24 | + | ||
25 | + private void Awake() | ||
26 | + { | ||
27 | + m_animator = GetComponent<Animator>(); | ||
28 | + SetResetTime(); | ||
29 | + } | ||
30 | + | ||
31 | + | ||
32 | + private void SetResetTime() | ||
33 | + { | ||
34 | + m_resetTime = Random.Range(1.5f, 6f); | ||
35 | + } | ||
36 | + | ||
37 | + | ||
38 | + public void None() | ||
39 | + { | ||
40 | + state = RabbitState.None; | ||
41 | + body.SetActive(false); | ||
42 | + m_animator.SetBool("dying", false); | ||
43 | + m_animator.SetBool("exiting", false); | ||
44 | + SetResetTime(); | ||
45 | + } | ||
46 | + | ||
47 | + public void Pop() | ||
48 | + { | ||
49 | + state = RabbitState.Pop; | ||
50 | + body.SetActive(true); | ||
51 | + m_animator.SetBool("poping", true); | ||
52 | + } | ||
53 | + | ||
54 | + public void Idle() | ||
55 | + { | ||
56 | + state = RabbitState.Idle; | ||
57 | + m_animator.SetBool("poping", false); | ||
58 | + } | ||
59 | + | ||
60 | + public void Die() | ||
61 | + { | ||
62 | + state = RabbitState.Die; | ||
63 | + m_animator.SetBool("dying", true); | ||
64 | + } | ||
65 | + | ||
66 | + public void Exit() | ||
67 | + { | ||
68 | + state = RabbitState.Exit; | ||
69 | + m_exitTime = 0f; | ||
70 | + m_animator.SetBool("exiting", true); | ||
71 | + } | ||
72 | + | ||
73 | + | ||
74 | + private void Update() | ||
75 | + { | ||
76 | + if (MiniGameManager.gameState == GameState.Play) | ||
77 | + { | ||
78 | + float dt = Time.deltaTime; | ||
79 | + | ||
80 | + if (state == RabbitState.None) | ||
81 | + { | ||
82 | + body.SetActive(false); | ||
83 | + m_resetTime -= dt; | ||
84 | + | ||
85 | + if (m_resetTime <= 0) | ||
86 | + Pop(); | ||
87 | + | ||
88 | + return; | ||
89 | + } | ||
90 | + else if (state == RabbitState.Idle) | ||
91 | + { | ||
92 | + m_exitTime += dt; | ||
93 | + if (m_exitTime >= 2) | ||
94 | + Exit(); | ||
95 | + | ||
96 | + return; | ||
97 | + } | ||
98 | + } | ||
99 | + else | ||
100 | + return; | ||
101 | + } | ||
102 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
code/MiniGame/Rabbit.cs.meta
0 → 100644
code/MiniGame/SetGamePanel.cs
0 → 100644
1 | +using System.Collections; | ||
2 | +using System.Collections.Generic; | ||
3 | +using UnityEngine; | ||
4 | +using UnityEngine.UI; | ||
5 | + | ||
6 | +public class SetGamePanel : MonoBehaviour | ||
7 | +{ | ||
8 | + static public SetGamePanel Instance; | ||
9 | + private Animator m_animator; | ||
10 | + private Image m_image; | ||
11 | + | ||
12 | + public GameObject backPanel; | ||
13 | + | ||
14 | + private void Awake() | ||
15 | + { | ||
16 | + Instance = this; | ||
17 | + } | ||
18 | + | ||
19 | + private void Start() | ||
20 | + { | ||
21 | + m_image = GetComponent<Image>(); | ||
22 | + m_animator = GetComponent<Animator>(); | ||
23 | + m_animator.SetTrigger("Play"); | ||
24 | + } | ||
25 | + | ||
26 | + | ||
27 | + public void SetPanelOn() | ||
28 | + { | ||
29 | + backPanel.SetActive(true); | ||
30 | + } | ||
31 | + public void SetPanelOff() | ||
32 | + { | ||
33 | + m_image = null; | ||
34 | + backPanel.SetActive(false); | ||
35 | + MiniGameManager.gameState = GameState.Play; | ||
36 | + } | ||
37 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
code/MiniGame/SetGamePanel.cs.meta
0 → 100644
code/MiniGame/SetMiniGame.cs
0 → 100644
1 | +using System.Collections; | ||
2 | +using System.Collections.Generic; | ||
3 | +using UnityEngine; | ||
4 | +using UnityEngine.SceneManagement; | ||
5 | +using UnityEngine.UI; | ||
6 | + | ||
7 | +public class SetMiniGame : MonoBehaviour | ||
8 | +{ | ||
9 | + static public SetMiniGame Instance; | ||
10 | + public GameObject UIButton; | ||
11 | + public Image backPanel; | ||
12 | + | ||
13 | + private Color m_color; | ||
14 | + | ||
15 | + private float fadeTime = 1f; | ||
16 | + float start; | ||
17 | + float end; | ||
18 | + float time = 0f; | ||
19 | + | ||
20 | + private void Awake() | ||
21 | + { | ||
22 | + Instance = this; | ||
23 | + } | ||
24 | + | ||
25 | + | ||
26 | + public void SetButton(bool open) | ||
27 | + { | ||
28 | + UIButton.SetActive(open); | ||
29 | + } | ||
30 | + | ||
31 | + public void StartMiniGameScene() | ||
32 | + { | ||
33 | + StartCoroutine(ImageFadeOut()); | ||
34 | + } | ||
35 | + | ||
36 | + | ||
37 | + protected IEnumerator ImageFadeOut() | ||
38 | + { | ||
39 | + m_color.a = 0f; | ||
40 | + m_color = backPanel.color; | ||
41 | + start = 0f; end = 1f; time = 0f; | ||
42 | + | ||
43 | + backPanel.gameObject.SetActive(true); | ||
44 | + while (m_color.a < 1f) | ||
45 | + { | ||
46 | + time += Time.deltaTime / fadeTime; | ||
47 | + m_color.a = Mathf.Lerp(start, end, time); | ||
48 | + backPanel.color = m_color; | ||
49 | + yield return null; | ||
50 | + } | ||
51 | + SceneManager.LoadScene("MiniGameScene"); | ||
52 | + } | ||
53 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
code/MiniGame/SetMiniGame.cs.meta
0 → 100644
-
Please register or login to post a comment