5.Stage, Cube, CubeMove

개발 일지/Move Cube 2020. 8. 17. 15:50

1.Stage

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class SceneStage : MonoBehaviour
{
    public Cube cube;
 
    public Button btnRegameKey;
    public Button btnHomeKey;
    public Button btnNextStage;
    public Button btnStart;
 
    public GameObject buttons;
    public GameObject clearMessage;
    public GameObject failedMessage;
    public GameObject objTxtMove;
    public GameObject objTxtTime;
 
    public Text txtCurrentMove;
    public Text txtMaxMove;
    public Text txtTimeLimit;
 
    private int currentMoveCount;
    private int maxMoveCount;
    private int timeLimit;
 
    private int stageId;
    private Camera mainCamera;
 
    private bool isClear = false;
    private bool isFaeiled = false;
    
    public void InitStage(int stageId)
    {
        this.stageId = stageId;
        Debug.LogFormat("불러온 스테이지 id : {0}"this.stageId);
        //stageNum에 맞는 스테이지 불러와서 실행
 
        #region Data
        var dataManager = DataManager.GetInstance();
        var stageData = dataManager.dicStageData[this.stageId];
        #endregion
 
        #region Info
        var infoManager = InfoManager.GetInstance();
        var userInfo = infoManager.GetUserInfo();
        var userStageLevel = userInfo.stageLevel;
        #endregion
 
        #region Stage Reset
        this.cube.transform.position = new Vector3(010);
        this.SetStageMap(stageData.path);
        this.SetCamara(stageData.cameraX, stageData.cameraY, stageData.cameraZ);
 
        this.btnStart.gameObject.SetActive(true);
        this.buttons.SetActive(false);
        this.cube.isMoveWait = false;
 
        this.SetDefeat(stageData.maxMove, stageData.timeLimit);
        #endregion Stage Reset
 
        this.cube.onMoveCount = () =>
        {
            this.MoveCounting();
        };
        this.cube.onGameClear = () =>
        {
            if(this.isFaeiled == false)
            {
                this.isClear = true;
                this.objTxtMove.SetActive(false);
                this.objTxtTime.SetActive(false);
                this.StageClear(userStageLevel, stageData.stageLevel);
                this.txtTimeLimit.gameObject.SetActive(false);
                this.txtCurrentMove.gameObject.SetActive(false);
                this.txtMaxMove.gameObject.SetActive(false);
            }
        };
        this.cube.onGameFailed = () =>
        {
            if(this.isClear == false)
            {
                this.isFaeiled = true;
                this.objTxtMove.SetActive(false);
                this.objTxtTime.SetActive(false);
                this.StageFailed(userStageLevel, stageData.stageLevel);
                this.txtTimeLimit.gameObject.SetActive(false);
                this.txtCurrentMove.gameObject.SetActive(false);
                this.txtMaxMove.gameObject.SetActive(false);
            }
        };
 
        #region Button
        this.btnStart.onClick.AddListener(() =>
        {
            this.btnStart.gameObject.SetActive(false);
            this.buttons.SetActive(true);
            this.cube.isMoveWait = true;
            StartCoroutine(this.MoveCamera());
            StartCoroutine(this.Timer());
        });
 
        this.btnRegameKey.onClick.AddListener(() =>
        {
            Debug.Log("다시하기");
            GameSceneManager.GetInstance().LoadScene(4this.stageId);
        });
 
        this.btnHomeKey.onClick.AddListener(() =>
        {
            Debug.Log("난이도 선택");
            GameSceneManager.GetInstance().LoadScene(3);
        });
 
        this.btnNextStage.onClick.AddListener(() =>
        {
            Debug.LogFormat("다음 단계 : {0}"this.stageId + 1);
            GameSceneManager.GetInstance().LoadScene(4this.stageId + 1);
        });
        #endregion
    }
 
    #region 스테이지 클리어 관련
    private IEnumerator Timer()
    {
        while (true)
        {
            if (this.timeLimit == 0)
            {
                Debug.Log("타임아웃");
                this.cube.onGameFailed();
                break;
            }
            yield return new WaitForSeconds(1.0f);
            this.timeLimit--;
            this.txtTimeLimit.text = this.timeLimit.ToString();
        }
        yield return null;
    }
 
    private void MoveCounting()
    {
        if (this.currentMoveCount == this.maxMoveCount)
        {
            Debug.Log("이동아웃");
            this.cube.onGameFailed();
        }
        this.currentMoveCount++;
        this.txtCurrentMove.text = this.currentMoveCount.ToString();
    }
 
    private void SetDefeat(int count, int time)
    {
        if (count == -1)
        {
            this.objTxtTime.SetActive(false);
            this.objTxtMove.SetActive(false);
            this.txtCurrentMove.gameObject.SetActive(false);
            this.txtMaxMove.gameObject.SetActive(false);
            this.currentMoveCount = count * 100;
        }
        else
        {
            this.txtCurrentMove.text = "0";
            this.txtMaxMove.text = "/" + count.ToString();
            this.currentMoveCount = 0;
            this.maxMoveCount = count;
        }
 
        if (time == -1)
        {
            this.txtTimeLimit.gameObject.SetActive(false);
            this.timeLimit = -100;
        }
        else
        {
            this.txtTimeLimit.text = time.ToString();
            this.timeLimit = time;
        }
    }
 
    private void StageClear(int userStageLevel, int currentStageLevel)
    {
        //내 레벨이 스테이지 레벨과 같을 경우 레벨업을 한다.
        if (userStageLevel == currentStageLevel)
        {
            var infoManager = InfoManager.GetInstance();
            infoManager.SaveUserInfo(true);
        }
 
        //클리어 메시지와 클리어 UI 설정
        var dataManager = DataManager.GetInstance();
 
        if (currentStageLevel == dataManager.dicStageData.Count)
        {
            var rtRegameKey = (RectTransform)this.btnRegameKey.transform;
            rtRegameKey.sizeDelta = new Vector3(180180);
            this.btnRegameKey.transform.localPosition = new Vector3(-120-1200);
            var rtHomeKey = (RectTransform)this.btnHomeKey.transform;
            rtHomeKey.sizeDelta = new Vector3(180180);
            this.btnHomeKey.transform.localPosition = new Vector3(120-1200);
        }
        else
        {
            this.btnNextStage.gameObject.SetActive(true);
            var rtRegameKey = (RectTransform)this.btnRegameKey.transform;
            rtRegameKey.sizeDelta = new Vector3(180180);
            this.btnRegameKey.transform.localPosition = new Vector3(-240-1200);
            var rtHomeKey = (RectTransform)this.btnHomeKey.transform;
            rtHomeKey.sizeDelta = new Vector3(180180);
            this.btnHomeKey.transform.localPosition = new Vector3(0-1200);
        }
        this.buttons.SetActive(false);
        this.clearMessage.SetActive(true);
 
    }
 
    private void StageFailed(int userStageLevel, int currentStageLevel)
    {
        //UI설정
        this.buttons.SetActive(false);
        this.failedMessage.SetActive(true);
 
        if (userStageLevel > currentStageLevel)
        {
            this.btnNextStage.gameObject.SetActive(true);
 
            var rtRegameKey = (RectTransform)this.btnRegameKey.transform;
            rtRegameKey.sizeDelta = new Vector3(180180);
            this.btnRegameKey.transform.localPosition = new Vector3(-240-1200);
            var rtHomeKey = (RectTransform)this.btnHomeKey.transform;
            rtHomeKey.sizeDelta = new Vector3(180180);
            this.btnHomeKey.transform.localPosition = new Vector3(0-1200);
        }
        else
        {
            var rtRegameKey = (RectTransform)this.btnRegameKey.transform;
            rtRegameKey.sizeDelta = new Vector3(180180);
            this.btnRegameKey.transform.localPosition = new Vector3(-120-1200);
            var rtHomeKey = (RectTransform)this.btnHomeKey.transform;
            rtHomeKey.sizeDelta = new Vector3(180180);
            this.btnHomeKey.transform.localPosition = new Vector3(120-1200);
        }
    }
    #endregion
 
    #region 스테이지 맵 세팅
    private void SetStageMap(string path)
    {
        Debug.LogFormat("셋 스테이지 맵 경로 : {0}", path);
        var stagePrefabs = Resources.Load<GameObject>(path);
        var stageMap = Instantiate<GameObject>(stagePrefabs);
    }
    #endregion
 
    #region 카메라 셋팅
    private void SetCamara(float x, float y, float z)
    {
        Debug.Log("카메라 셋팅");
        this.mainCamera = FindObjectOfType<Camera>();
        this.mainCamera.transform.position = new Vector3(x, y, z);
    }
 
    private IEnumerator MoveCamera()
    {
        Vector3 cameraPos = this.mainCamera.transform.position;
        Vector3 cameraPoint = new Vector3(4120);
 
        var distancePos = new Vector3(cameraPos.x - cameraPoint.x, cameraPos.y - cameraPoint.y, cameraPos.z - cameraPoint.z);
        if (distancePos.x < 0)
        {
            while (true)
            {
                if (cameraPoint.x - this.mainCamera.transform.position.x < 0)
                {
                    this.mainCamera.transform.position = cameraPoint;//정렬
                    break;
                }
                this.mainCamera.transform.position += new Vector3(-distancePos.x / (Time.deltaTime * 2000), -distancePos.y / (Time.deltaTime * 2000), -distancePos.z / (Time.deltaTime * 2000));
 
                yield return null;
            }
        }
        else
        {
            while (true)
            {
                if (cameraPoint.x - this.mainCamera.transform.position.x > 0)
                {
                    this.mainCamera.transform.position = cameraPoint;//정렬
                    break;
                }
                this.mainCamera.transform.position += new Vector3(-distancePos.x / (Time.deltaTime * 2000), -distancePos.y / (Time.deltaTime * 2000), -distancePos.z / (Time.deltaTime * 2000));
 
                yield return null;
            }
        }
        yield return null;
    }
    #endregion
}
 
cs


2.Cube

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class Cube : MonoBehaviour
{
    [HideInInspector]
    public bool isMoveWait = false;//이게 true면, 이동조작가능, false면 이동조작불가
    public Sensor[] sensors;// 센서들 순서대로 0~4, 위 아래 왼 오 바닥센서(납작한 박스콜라이더로 되어있음)
    public GameObject cubeBody;
    public PanelGoal panelGoal;
 
    public int cubeSpeed = 1;
    private CubeMove cubeMove;
    public System.Action onGameClear;
    public System.Action onGameFailed;
    public System.Action onMoveCount;//이동횟수 알려줌
 
    void Awake()
    {
        this.cubeMove = GetComponent<CubeMove>();
        this.panelGoal.onGameClear = () =>
        {
            StartCoroutine(this.GameClear());
        }; 
    }
 
    #region 특수 타일 감지
    private void OnTriggerEnter(Collider other)//큐브가 올라가는 순간 타일을 읽음
    {
        if (other.tag == "Tile")
        {
            StartCoroutine(this.Tile());
        }
        else if (other.tag == "Goal")
        {
            StartCoroutine(this.Tile());
        }
        else if (other.tag == "OutLine")
        {
            Debug.Log("OutLine");
            this.onGameFailed();
        }
        else if (other.tag == "Trap")
        {
            Debug.Log("Trap");
            this.onGameFailed();
        }
        else if (other.tag == "RotateLeft")
        {
            Debug.Log("RotateLeft");
            StartCoroutine(this.TileRotate(-1));
        }
        else if (other.tag == "RotateRight")
        {
            Debug.Log("RotateRight");
            StartCoroutine(this.TileRotate(1)); ;
        }
        else if (other.tag == "Slide")
        {
            Debug.Log("Slide");
            StartCoroutine(this.TileSlide(other.transform.eulerAngles.y));
        }
        else if (other.tag == "In")
        {
            Debug.Log("In");
            StartCoroutine(this.TileInOut(other.transform.Find("Tile(Out)").transform.position));
        }
    }
    #endregion
 
    #region 특수 타일 효과 모음
    private IEnumerator Tile()
    {
        yield return new WaitForSeconds(1.5f / cubeSpeed);
        this.isMoveWait = true;
    }
 
    private IEnumerator TileInOut(Vector3 tileOut)
    {
        yield return new WaitForSeconds(1.5f / cubeSpeed);
 
        //내려감
        Vector3 dir = new Vector3(010);
        for (int i = 0; i < 30 / this.cubeSpeed; i++)
        {
            this.transform.position += 1 / 30f * -dir * this.cubeSpeed;
            yield return null;
        }
 
        //이동
        this.transform.position = tileOut;
        yield return new WaitForSeconds(1.5f / cubeSpeed);
 
        //올라옴
        for (int i = 0; i < 30 / this.cubeSpeed; i++)
        {
            this.transform.position += 1 / 30f * dir * this.cubeSpeed;
            yield return null;
        }
 
        this.isMoveWait = true;
    }
 
    private IEnumerator TileSlide(float tileDir)//타일의 화살표 위치를 받음
    {
        yield return new WaitForSeconds(1.5f / cubeSpeed);
        Debug.Log(tileDir);
        Vector3 dir = new Vector3(000);//큐브가 이동 할 방향
 
        if (tileDir == 0)
        {
            //위
            dir.x = -1;
        }
        else if (tileDir == 90)
        {
            //오른
            dir.z = 1;
        }
        else if (tileDir == 180)
        {
            //아래
            dir.x = 1;
        }
        else if (tileDir == 270)
        {
            //왼
            dir.z = -1;
        }
 
        Debug.Log(dir);
        for (int i = 0; i < 30 / this.cubeSpeed; i++)
        {
            this.transform.position += 1 / 30f * dir * this.cubeSpeed;
            this.cubeMove.mainCamera.transform.position += 1 / 30f * dir * this.cubeSpeed;
            yield return null;
        }
    }
 
    private IEnumerator TileRotate(int dir)
    {
        yield return new WaitForSeconds(1.5f / cubeSpeed);
 
        for (int i = 0; i < 45 / this.cubeSpeed; i++)
        {
            this.cubeBody.transform.Rotate(0, dir * this.cubeSpeed * 20, Space.World);
            yield return null;
        }
 
        this.isMoveWait = true;
    }
    #endregion
    
    #region 게임 클리어와 실패 시
    private IEnumerator GameClear()
    {
        this.onGameClear();
        yield return new WaitForSeconds(1.5f / cubeSpeed);
 
        float pointA = this.transform.position.y;
 
        while (true)
        {
            if (this.transform.position.y <= pointA - 1.1f)
            {
                break;
            }
            this.transform.Translate(new Vector3(0-0.03f, 0));
            yield return null;
        }
        yield return null;
    }
    #endregion
}
 
cs


3.CubeMove


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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class CubeMove : MonoBehaviour
{
    public GameObject body;
    public Camera mainCamera;
    private Cube cube;
 
    public float cubeSpeed;
    public Coroutine moveRoutine;
 
    public Button btnUp;
    public Button btnDown;
    public Button btnLeft;
    public Button btnRight;
 
    void Awake()
    {
        this.cube = GetComponent<Cube>();
        this.cubeSpeed = this.cube.cubeSpeed;
        this.mainCamera = FindObjectOfType<Camera>();
    }
 
 
    void Start()
    {
        this.moveRoutine = StartCoroutine(this.KeyInput());
 
        #region 모바일 조작 Button(Main)
        this.btnUp.onClick.AddListener(() =>
        {
            if (this.cube.isMoveWait)
            {
                if (this.cube.sensors[0].isMove)
                {
                    this.cube.isMoveWait = false;
                    StartCoroutine(this.Move("z"1));
                    this.MoveReset();
                }
            }
        });
 
        this.btnDown.onClick.AddListener(() =>
        {
            if (this.cube.isMoveWait)
            {
                if (this.cube.sensors[1].isMove)
                {
                    this.cube.isMoveWait = false;
                    StartCoroutine(this.Move("z"-1));
                    this.MoveReset();
                }
            }
        });
 
        this.btnLeft.onClick.AddListener(() =>
        {
            if (this.cube.isMoveWait)
            {
                if (this.cube.sensors[2].isMove)
                {
                    this.cube.isMoveWait = false;
                    StartCoroutine(this.Move("x"-1));
                    this.MoveReset();
                }
            }
        });
 
        this.btnRight.onClick.AddListener(() =>
        {
            if (this.cube.isMoveWait)
            {
                if (this.cube.sensors[3].isMove)
                {
                    this.cube.isMoveWait = false;
                    StartCoroutine(this.Move("x"1));
                    this.MoveReset();
                }
            }
        });
        #endregion
    }
 
    #region 키보드 조작 ArrowKey && WASD
    private IEnumerator KeyInput()
    {
        while (true)
        {
            if (this.cube.isMoveWait)
            {
                #region 키보드 조작
                if (Input.GetKey("up"|| Input.GetKey("w"))
                {
                    if (this.cube.sensors[0].isMove)
                    {
                        this.cube.isMoveWait = false;
                        StartCoroutine(this.Move("z"1));
                        this.MoveReset();
                    }
                }
                else if (Input.GetKey("down"|| Input.GetKey("s"))
                {
                    if (this.cube.sensors[1].isMove)
                    {
                        this.cube.isMoveWait = false;
                        StartCoroutine(this.Move("z"-1));
                        this.MoveReset();
                    }
                }
                else if (Input.GetKey("left"|| Input.GetKey("a"))
                {
                    if (this.cube.sensors[2].isMove)
                    {
                        this.cube.isMoveWait = false;
                        StartCoroutine(this.Move("x"-1));
                        this.MoveReset();
                    }
                }
                else if (Input.GetKey("right"|| Input.GetKey("d"))
                {
                    if (this.cube.sensors[3].isMove)
                    {
                        this.cube.isMoveWait = false;
                        StartCoroutine(this.Move("x"1));
                        this.MoveReset();
                    }
                }
                #endregion
            }
            yield return null;
        }
    }
    #endregion
 
    private void MoveReset()//연달은 벽 통과할때 생기는 OnTriggerExit오류 방지 메서드
    {
        for (int i = 0; i < 4; i++)
        {
            this.cube.sensors[i].isMove = true;
        }
    }
 
    #region 큐브 이동 자연스럽게
    private IEnumerator Move(string shaft, int dir)//shaft = 축(x, y, z), dir = 방향 (-1, 1)
    {
        for (int i = 0; i < 30 / this.cubeSpeed; i++)//돌면서 y값 위로
        {
            this.MoveCube(shaft, dir);
            this.RotateCube(shaft, dir);
            this.NaturalRotate(1);
            yield return null;
        }
 
        for (int i = 0; i < 30 / this.cubeSpeed; i++)//돌면서 y값 아래로
        {
            this.MoveCube(shaft, dir);
            this.RotateCube(shaft, dir);
            this.NaturalRotate(-1);
            yield return null;
        }
        //위의 for문으로 이동시 0.9999.. 로 이동함, 그래서 정수단위 값으로 정렬해서 위치 재설정 코드
        this.cube.transform.position = new Vector3((float)Math.Round(this.cube.transform.position.x), (float)Math.Round(this.cube.transform.position.y), (float)Math.Round(this.cube.transform.position.z));
        this.cube.onMoveCount();//테스트(Test)시 주석처리
    }
 
    private void MoveCube(string shaft, int dir)
    {
        float x = 0, y = 0, z = 0;
        if (shaft == "x")
        {
            z = 1 / 60f * dir * this.cubeSpeed;
        }
        else if (shaft == "z")
        {
            x = 1 / 60f * -dir * this.cubeSpeed;
        }
        this.transform.position += (new Vector3(x, y, z));
        this.mainCamera.transform.position += (new Vector3(x, y, z));
    }
 
    private void RotateCube(string shaft, int dir)
    {
        float x = 0, y = 0, z = 0;
        if (shaft == "x")
        {
            x = 1.5f * dir * this.cubeSpeed;
        }
        else if (shaft == "z")
        {
            z = 1.5f * dir * this.cubeSpeed;
        }
        this.body.transform.Rotate(x, y, z, Space.World);
    }
 
    private void NaturalRotate(int dir)//자연스러운 회전을 위한 회전각 계수
    {
        this.body.transform.position += new Vector3(00.007f * dir * this.cubeSpeed, 0);
    }
    #endregion
}
 
cs




'개발 일지 > Move Cube' 카테고리의 다른 글

6.Google Play Beta.ver//구글 플레이 베타버전 업로드  (0) 2020.08.17
4.Title/StageSelect  (0) 2020.08.17
3.App -> Logo  (0) 2020.08.17
2.Unity Info관리  (0) 2020.08.17
1.Unity GameSceneManager  (0) 2020.08.17
: