VioletaBabel

29. 테트리스 본문

BCA/4. Unity
29. 테트리스
Beabletoet 2018. 4. 4. 17:19
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
//TimeManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TimeManager : MonoBehaviour
{
    static public float GameTime = 0;
    static public bool bTimeStop = false;
    // Use this for initialization
    void Start ()
    {
        
    }
    
    // Update is called once per frame
    void Update ()
    {
        if (bTimeStop)
            GameTime = 0;
        else
            GameTime = Time.deltaTime;
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Singleton.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T _instance;
    public static T instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = FindObjectOfType(typeof(T)) as T;
            }
            return _instance;
        }
    }
}
 
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
//ButtonScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
 
public class ButtonScript : MonoBehaviour
{
    public AudioClip buttonEnterSound = null;
    
    private AudioSource myAudio;
    // Use this for initialization
    void Start()
    {
        myAudio = GetComponent<AudioSource>();
    }
 
    // Update is called once per frame
    void Update()
    {
 
    }
 
    private void OnMouseEnter()
    {
        transform.DOScale(0.75f, 0.2f);
        PlaySound(buttonEnterSound);
    }
    private void OnMouseExit()
    {
        transform.DOScale(0.5f, 0.2f);
    }
    private void OnMouseDown()
    {
        transform.DOScale(0.625f, 0.2f);
    }
    private void OnMouseUp()
    {
        transform.DOScale(0.75f, 0.2f);
        GameMap.instance.GameInit();
        gameObject.SetActive(false);
        transform.DOScale(0.5f, 0);
    }
    public void PlaySound(AudioClip a)
    {
        myAudio.PlayOneShot(a,0.7f);
    }
}
 
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
//CameraScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CameraScript : MonoBehaviour
{
    Vector3 origins;
    private bool shake = false;
    private float shakeTime = 0.2f;
    // Use this for initialization
    void Start()
    {
        origins = transform.position;
    }
 
    // Update is called once per frame
    void Update()
    {
        if(shake)
        {
            transform.position = origins + Random.insideUnitSphere * 0.2f;
            shakeTime -= TimeManager.GameTime;
            if(shakeTime <= 0)
            {
                shakeTime = 0.2f;
                shake = false;
            }
        }
    }
    public void shakeOn()
    {
        shake = true;
    }
}
 
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
//ScoreDisplay.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class ScoreDisplay : Singleton<ScoreDisplay>
{
    public GameObject[] numSprite = null;
    public GameObject[] scoreNum = null;
    public GameObject levelNum = null;
    public GameObject holdNum = null;
    public GameObject[] comboNum = null;
    // Use this for initialization
    void Start()
    {
 
    }
 
    // Update is called once per frame
    void Update()
    {
 
    }
 
    public void setScore(int score)
    {
        int[] nowScore = new int[5];
        if (scoreNum[0== null)
        {
            for (int i = 0; i < 5++i)
                nowScore[i] = 0;
        }
        else
        {
            for (int i = 0; i < 5++i)
                Destroy(scoreNum[i]);
        }
        for (int i = 10000, j = score, k = 0; i > 0; i /= 10++k)
        {
            nowScore[k] = j / i;
            j %= i;
        }
        for(int i = 0; i < 5++i)
        {
            scoreNum[i] = Instantiate(numSprite[nowScore[i]], new Vector3(8.1f + i * 0.8f, 23.5f, 0), Quaternion.identity);
        }
    }
 
    public void setLevel(int level)
    {
        if(levelNum != null)
        {
            Destroy(levelNum);
        }
        levelNum = Instantiate(numSprite[level], new Vector3(3.8f, 23.43f, 0), Quaternion.identity);
    }
 
    public void setHold(int hold)
    {
        if (holdNum != null)
            Destroy(holdNum);
        holdNum = Instantiate(numSprite[hold], new Vector3(2.2f, 220), Quaternion.identity);
    }
 
    public void setCombo(int combo)
    {
        int[] nowCombo = new int[2];
        nowCombo[0= combo / 10;
        nowCombo[1= combo % 10;
        if(comboNum[0!= null)
        {
            Destroy(comboNum[0]);
            Destroy(comboNum[1]);
        }
        comboNum[0= Instantiate(numSprite[nowCombo[0]], new Vector3(7.9f, 22.5f, 0), Quaternion.identity);
        comboNum[1= Instantiate(numSprite[nowCombo[1]], new Vector3(8.8f, 22.5f, 0), Quaternion.identity);
    }
}
 
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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//GameMap.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
 
public class GameMap : Singleton<GameMap>
{
    public GameObject[] Mino = null;
    public GameObject[] ShadowMino = null;
    public GameObject[] nowMapObject = new GameObject[240];
 
    public GameObject checkBlock = null;
    public GameObject[] checkBlocks = null;
 
    public bool[] nowMap = new bool[240];
    public int score = 0;
 
    public int combo = 0;
 
    public GameObject[] miniaturePrefab = null;
    public GameObject[] miniature = new GameObject[7];
    public bool firstBlock = true;
    public int nextBlockType = 0;
    public int holdBlockType = 0;
    public int nowBlockType = 0;
    public GameObject[] holdMiniature = new GameObject[7];
    public bool firstHold = true;
    public GameObject nowBlock = null;
 
 
    public int[] bombMap = new int[240];
    public GameObject[] bombMapObject = new GameObject[240];
    public GameObject[] bombTimeSprite = new GameObject[6];
    public GameObject[] bombTimeMap = new GameObject[240];
    public GameObject bombSpirte = null;
 
    public AudioClip removeLineSound = null;
    public AudioClip gameOverSound = null;
    public AudioClip bombSound = null;
    public AudioClip buttonClickSound = null;
    public AudioClip bgm = null;
    private AudioSource myAudio;
    private AudioSource bgmAudio;
 
    public GameObject camera = null;
    public GameObject playButton = null;
 
    
 
    public int level = 1;
    public int hold = 5;
    // Use this for initialization
    void Start()
    {
        myAudio = GetComponent<AudioSource>();
        bgmAudio = GetComponent<AudioSource>();
        //for (int i = 0; i < 240; ++i)
        //{//데이터 확인용
        //    checkBlocks[i] = Instantiate(checkBlock, new Vector3((i % 10), (i / 10), 0), Quaternion.identity);
        //    checkBlocks[i].SetActive(false);
        //}
        for (int i = 0; i < 7++i)
        {
            miniature[i] = Instantiate(miniaturePrefab[i]);
            Vector3 vec = miniature[i].transform.position;
            vec.x += 2.3f;
            holdMiniature[i] = Instantiate(miniaturePrefab[i], vec, Quaternion.identity);
            miniature[i].SetActive(false);
            holdMiniature[i].SetActive(false);
        }
    }
 
    public void GameInit()
    {
        PlaySound(buttonClickSound);
        score = 0;
        level = 1;
        hold = 5;
        for (int i = 0; i < 7++i)
        {
            miniature[i].SetActive(false);
            holdMiniature[i].SetActive(false);
        }
        for (int i = 0; i < 240++i)
        {
            nowMap[i] = false;
            if (nowMapObject[i] != null)
                Destroy(nowMapObject[i]);
            nowMapObject[i] = null;
            bombMap[i] = -1;
            if (bombMapObject[i] != null)
                Destroy(bombMapObject[i]);
            bombMapObject[i] = null;
            if (bombTimeMap[i] != null)
                Destroy(bombTimeMap[i]);
            bombTimeMap[i] = null;
        }
        ScoreDisplay.instance.setScore(score);
        makeNewBlock();
        ScoreDisplay.instance.setLevel(level);
        ScoreDisplay.instance.setHold(hold);
        ScoreDisplay.instance.setCombo(combo);
    }
 
    // Update is called once per frame
    void Update()
    {
        if(!bgmAudio.isPlaying)
            bgmAudio.PlayOneShot(bgm,1);
        //데이터 확인용
        //for (int i = 0; i < 240; ++i)
        //{
        //    if (nowMap[i])
        //    {
        //        checkBlocks[i].SetActive(true);
        //    }
        //    else
        //        checkBlocks[i].SetActive(false);
        //}
        if (hold > 0)
            if (Input.GetKeyDown(KeyCode.LeftAlt))
            {
                if (firstHold)
                {
                    holdBlockType = nowBlockType;
                    for (int i = 0; i < 7++i)
                    {
                        if (i.Equals(holdBlockType))
                            holdMiniature[i].SetActive(true);
                        else
                            holdMiniature[i].SetActive(false);
                    }
                    Destroy(nowBlock);
                    nowBlock.GetComponent<Block>().DestroyGhost();
                    makeNewBlock();
                    firstHold = false;
                }
                else
                {
                    nowBlock.GetComponent<Block>().DestroyGhost();
                    Destroy(nowBlock);
                    nowBlock = Instantiate(Mino[holdBlockType], new Vector3(4210), Quaternion.identity);
                    int t = holdBlockType;
                    holdBlockType = nowBlockType;
                    nowBlockType = t;
                    for (int i = 0; i < 7++i)
                    {
                        if (i.Equals(holdBlockType))
                            holdMiniature[i].SetActive(true);
                        else
                            holdMiniature[i].SetActive(false);
                    }
                }
                --hold;
                ScoreDisplay.instance.setHold(hold);
            }
    }
 
    public bool sideTest(Transform t, bool left = true)
    {//이동 시 옆면이 벽이나 블록을 뚫지 않는지 검사
        bool result = true;
        int num = -100;
        if (left)
            num = 100;
        foreach (Transform child in t)
        {
            if (child.name.Contains("hadow"))
                continue;
            int _x = Convert.ToInt32(child.position.x);
            int _y = Convert.ToInt32(child.position.y);
            if (left)
            {
                num = (child.position.x < num) ? Convert.ToInt32(child.position.x) : num;
                if (_x != 0)
                {
                    if (nowMap[_y * 10 + _x - 1])
                        return false;
                }
            }
            else
            {
                num = (child.position.x > num) ? Convert.ToInt32(child.position.x) : num;
                if (_x != 9)
                {
                    if (nowMap[_y * 10 + _x + 1])
                        return false;
                }
            }
        }
        if (left)
        {
            if (num.Equals(0))
            {
                return false;
            }
        }
        else
        {
            if (num.Equals(9))
            {
                return false;
            }
        }
 
        foreach (Transform child in t)
        {
            if (child.name.Contains("hadow"))
                continue;
            int _x = Convert.ToInt32(child.position.x);
            if (num == _x)
            {
                int _y = Convert.ToInt32(child.position.y);
                if (left)
                {
                    if (nowMap[_y * 10 + _x - 1== true)
                        result = false;
                }
                else
                {
                    if (nowMap[_y * 10 + _x + 1== true)
                        result = false;
                }
            }
        }
        return result;
    }
 
    public bool downTest(Transform t)
    {//이동 시 벽이나 아랫 블록을 뚫지 않는지 검사
        bool result = true;
        foreach (Transform child in t)
        {
            if (child.name.Contains("hadow"))
                continue;
            int _x = Convert.ToInt32(child.position.x);
            int _y = Convert.ToInt32(child.position.y);
 
            if (_y == 0)
                return false;
 
            if (nowMap[(_y - 1* 10 + _x] == true)
            {
                result = false;
            }
        }
        return result;
    }
 
    public bool checkGameOver()
    {
        for (int i = 190; i < 200++i)
            if (nowMap[i])
            {
                PlaySound(gameOverSound);
                playButton.SetActive(true);
                camera.GetComponent<CameraScript>().shakeOn();
                return true;
            }
        return false;
    }
 
    public void StopBlock(Transform t)
    { // 블록이 정지할 때
        foreach (Transform child in t)
        {
            if (child.name.Contains("Bomb"))
            {
                int _x = Convert.ToInt32(child.position.x);
                int _y = Convert.ToInt32(child.position.y);
                bombMap[_y * 10 + _x] = 5;
                bombMapObject[_y * 10 + _x] = child.gameObject;
            }
            else if (child.name.Contains("Shadow"))
            {
                Destroy(child.gameObject);
            }
            else
            {
                int _x = Convert.ToInt32(child.position.x);
                int _y = Convert.ToInt32(child.position.y);
                nowMap[_y * 10 + _x] = true;
                nowMapObject[_y * 10 + _x] = child.gameObject;
            }
        }
        removeBlock();
        checkBombTime();
    }
 
    public void makeNewBlock()
    { // 새 블록을 만듦
 
        if (firstBlock)
        {
            nextBlockType = UnityEngine.Random.Range(07);
            firstBlock = false;
        }
        nowBlock = Instantiate(Mino[nextBlockType], new Vector3(4210), Quaternion.identity);
        nowBlockType = nextBlockType;
        nextBlockType = UnityEngine.Random.Range(07);
        for (int i = 0; i < 7++i)
        {
            if (nextBlockType.Equals(i))
                miniature[i].SetActive(true);
            else
                miniature[i].SetActive(false);
        }
    }
 
    public void removeBlock()
    { // 줄 삭제
        bool deleteLineThisTurn = false;
        int nowScore = 1;
        for (int i = 190; i > -1; i -= 10)
        {
            bool a = true;
            for (int j = 0; j < 10++j)
            {
                if (!nowMap[i + j])
                {
                    a = false;
                }
            }
            if (a)
            {
                deleteLineThisTurn = true;
                score += nowScore++ * 100;
                for (int j = 0; j < 10++j)
                {
                    Destroy(nowMapObject[i + j]);
                    if (!bombMap[i + j].Equals(-1))
                    {
                        Destroy(bombMapObject[i + j]);
                        bombMapObject[i + j] = null;
                        bombMap[i + j] = -1;
                        Destroy(bombTimeMap[i + j]);
                        bombTimeMap[i + j] = null;
                    }
                }
                for (int j = i; j < 200++j)
                {
                    if (j > 189)
                    {
                        nowMapObject[j] = null;
                        nowMap[j] = false;
                        bombMapObject[j] = null;
                        bombMap[j] = -1;
                        bombTimeMap[j] = null;
                    }
                    else
                    {
                        nowMap[j] = nowMap[j + 10];
                        bombMap[j] = bombMap[j + 10];
                        nowMapObject[j] = nowMapObject[j + 10];
                        bombMapObject[j] = bombMapObject[j + 10];
                        bombTimeMap[j] = bombTimeMap[j + 10];
                        if (nowMap[j])
                        {
                            Vector3 vec = nowMapObject[j].transform.position;
                            vec.y -= 1;
                            nowMapObject[j].transform.position = vec;
                        }
                        if (!bombMap[j].Equals(-1))
                        {
                            Vector3 vec = bombMapObject[j].transform.position;
                            vec.y -= 1;
                            bombMapObject[j].transform.position = vec;
                            if (bombTimeMap[j] != null)
                                bombTimeMap[j].transform.position = vec;
                        }
                    }
                }
            }
        }
        if (deleteLineThisTurn)
        {
            PlaySound(removeLineSound);
            if (++combo > 1)
            {
                score += combo * 10;
            }
        }
        else
            combo = 0;
        ScoreDisplay.instance.setScore(score);
        level = (score / 1000 > 4) ? 5 : (score / 1000+ 1;
        ScoreDisplay.instance.setLevel(level);
        ScoreDisplay.instance.setCombo(combo);
    }
 
    public void checkBombTime()
    {
        for (int i = 0; i < 200++i)
        {
            if (bombMap[i].Equals(5))
            {//5일때
                bombTimeMap[i] = Instantiate(bombTimeSprite[5], new Vector3(i % 10, i / 100)/*nowBlock.GetComponent<Block>().bombBlock.transform*/, Quaternion.identity);
 
                --bombMap[i];
            }
            else if (!bombMap[i].Equals(-1))
            {
                if (!bombMap[i].Equals(0))
                {//1,2,3,4일때
                    int _x = Convert.ToInt32(bombTimeMap[i].transform.position.x);
                    int _y = Convert.ToInt32(bombTimeMap[i].transform.position.y);
                    Destroy(bombTimeMap[i]);
                    bombTimeMap[i] = Instantiate(bombTimeSprite[bombMap[i]], new Vector3(_x, _y, 0), Quaternion.identity);
                    --bombMap[i];
                }
                else
                {//0일때
                    PlaySound(bombSound);
                    camera.GetComponent<CameraScript>().shakeOn();
                    bombMap[i] = -1;
                    int _x = Convert.ToInt32(bombTimeMap[i].transform.position.x);
                    int _y = Convert.ToInt32(bombTimeMap[i].transform.position.y);
                    Destroy(bombTimeMap[i]);
                    bombTimeMap[i] = null;
                    Destroy(bombMapObject[i]);
                    bombMapObject[i] = null;
                    for (int j = -2; j < 3++j)
                    {
                        for (int k = -2; k < 3++k)
                        {
                            int absX = (k < 0) ? -k : k;
                            int absY = (j < 0) ? -j : j;
                            if (_x + k >= 0 && _x + k <= 9 && _y + j >= 0 && _y + j <= 19)
                                if (absX + absY <= 2)
                                {
                                    int number = (_y + j) * 10 + (_x + k);
                                    Destroy(nowMapObject[number]);
                                    if (!bombMap[number].Equals(-1))
                                    {
                                        Destroy(bombMapObject[number]);
                                        Destroy(bombTimeMap[number]);
                                        bombMapObject[number] = null;
                                        bombTimeMap[number] = null;
                                    }
                                    bombMap[number] = -1;
 
                                    if (absX + absY == 0 || absX + absY == 2)
                                    {//bombBlock 만듦
                                        nowMap[number] = true;
                                        nowMapObject[number] = Instantiate(bombSpirte, new Vector3(_x + k, _y + j, 0), Quaternion.identity);
                                    }
                                    else
                                    {//빵꾸
                                        nowMap[(_y + j) * 10 + (_x + k)] = false;
                                    }
                                }
                        }
                    }
                }
            }
        }
    }
    public void PlaySound(AudioClip a)
    {
        myAudio.PlayOneShot(a,0.7f);
    }
}
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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//Block.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
 
public class Block : MonoBehaviour
{
    GameMap TheGameMap;
 
    public int level = 1;
    public float downSpeed;
    private float downTime = 0;
 
    private bool nowTurn = true;
 
    private bool left = false;
    private bool right = false;
    private bool down = false;
    private float sideMoveTime = 0;
    private float downMoveTime = 0;
    private const float moveSpeed = 0.1f;
 
    public int rot = 0;
    public int type = 0;
 
    public GameObject shadowPrefab = null;
    private GameObject shadow = null;
 
    private GameObject center = null;
 
    public GameObject ghostPrefab = null;
    private GameObject ghost = null;
    // Use this for initialization
 
    public bool bombOn = false;
    public GameObject bombPrefab = null;
    public GameObject bombBlock = null;
 
    public AudioClip moveSound = null;
    public AudioClip dropSound = null;
    private AudioSource myAudio;
    void Start()
    {
        TheGameMap = GameMap.instance;
        level = TheGameMap.level;
        nowTurn = true;
        downSpeed = 1 / (float)level;
        foreach (Transform child in transform)
        {
            if (child.tag.Contains("Center"))
            {
                center = child.gameObject;
            }
        }
        shadow = Instantiate(shadowPrefab, transform);
        shadow.transform.Rotate(0090);
 
        MakeGhost();
 
        int maxR = 25;
        switch (level)
        {
            case 5: maxR = 3break;
            case 4: maxR = 6break;
            case 3: maxR = 9break;
            case 2: maxR = 12break;
            case 1: maxR = 15break;
        }
        int r = UnityEngine.Random.Range(0, maxR); // 확률
        if (r.Equals(0))
        {
            bombOn = true;
        }
        if (bombOn == true)
        {
            int a = UnityEngine.Random.Range(04);
            int count = 0;
            foreach (Transform t in transform)
            {
                if (!t.name.Contains("hadow"))
                    if (count.Equals(a))
                    {
                        bombBlock = Instantiate(bombPrefab, t.transform.position, Quaternion.identity, transform);
                        break;
                    }
                    else
                        ++count;
 
                if (count >= 10)
                    break;
            }
        }
 
        myAudio = GetComponent<AudioSource>();
    }
 
    // Update is called once per frame
    void Update()
    {
        if (nowTurn)
        {
            downTime += TimeManager.GameTime;
            if (downTime >= downSpeed)
            {
                bool a = TheGameMap.downTest(transform);
                if (a)
                {
                    downTime = 0;
                    Vector3 vec = transform.position;
                    vec.y -= 1;
                    transform.position = vec;
                    shadow.transform.position = transform.position;
                    PlaySound(moveSound);
                }
                else
                {
                    nowTurn = false;
                }
            }
 
            if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                left = true;
                sideMoveTime = moveSpeed;
            }
            else if (Input.GetKeyUp(KeyCode.LeftArrow))
            {
                left = false;
                sideMoveTime = 0;
            }
 
            if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                right = true;
                sideMoveTime = moveSpeed;
            }
            else if (Input.GetKeyUp(KeyCode.RightArrow))
            {
                right = false;
                sideMoveTime = 0;
            }
 
            if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                down = true;
                downMoveTime = moveSpeed;
            }
            else if (Input.GetKeyUp(KeyCode.DownArrow))
            {
                down = false;
                downMoveTime = 0;
            }
 
            bool attachBlock = false;
            Move();
            foreach (Transform t in transform)
            {
                int _x = Convert.ToInt32(t.position.x);
                int _y = Convert.ToInt32(t.position.y);
                if (_x < 0)
                {
                    Vector3 vec = transform.position;
                    vec.x += 1;
                    transform.position = vec;
                }
                else if (_x > 9)
                {
                    Vector3 vec = transform.position;
                    vec.x -= 1;
                    transform.position = vec;
                }
            }
            foreach (Transform t in shadow.transform)
            {
                int _x = Convert.ToInt32(t.position.x);
                int _y = Convert.ToInt32(t.position.y);
                if (_x < 0)
                {
                    Vector3 vec = shadow.transform.position;
                    vec.x += 1;
                    shadow.transform.position = vec;
                }
                else if (_x > 9)
                {
                    Vector3 vec = shadow.transform.position;
                    vec.x -= 1;
                    shadow.transform.position = vec;
                }
 
                int rx = Convert.ToInt32(transform.position.x);
                int ry = Convert.ToInt32(transform.position.y);
 
                if (_y >= 0 && _y <= 19 && _x >= 0 && _x <= 9)
                    if (TheGameMap.nowMap[_y * 10 + _x])
                    {
                        if (_x < rx)
                        {
                            Vector3 vec = shadow.transform.position;
                            vec.x += 1;
                            shadow.transform.position = vec;
                            attachBlock = true;
                        }
                        else
                        {
                            Vector3 vec = shadow.transform.position;
                            vec.x -= 1;
                            shadow.transform.position = vec;
                            attachBlock = true;
                        }
                    }
 
            }
 
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                bool canRotate = true;
                foreach (Transform t in shadow.transform)
                {
                    int _x = Convert.ToInt32(t.position.x);
                    int _y = Convert.ToInt32(t.position.y);
                    if (_y >= 0 && _y <= 19 && _x >= 0 && _x <= 9)
                        if (TheGameMap.nowMap[_y * 10 + _x])
                            canRotate = false;
                }
                if (canRotate)
                {
                    if (attachBlock)
                    {
                        transform.position = shadow.transform.position;
                        transform.rotation = shadow.transform.rotation;
                    }
                    else
                        transform.Rotate(0090);
                }
                MoveGhost();
            }
 
            if (Input.GetKeyDown(KeyCode.Space))
            {
                transform.position = ghost.transform.position;
                nowTurn = false;
            }
            if (!nowTurn)
            {
                PlaySound(dropSound);
                TheGameMap.StopBlock(transform);
                DestroyGhost();
                if (!TheGameMap.checkGameOver())
                    TheGameMap.makeNewBlock();
            }
        }
        else if (bombOn)
        {
 
        }
    }
 
    public void DestroyGhost()
    {
        Destroy(ghost);
    }
 
    private void Move()
    {//키 입력에 따른 이동
        if (down)
        {
            downMoveTime += TimeManager.GameTime;
            if (downMoveTime >= moveSpeed)
            {
                PlaySound(moveSound);
                bool a = TheGameMap.downTest(transform);
                if (a)
                {
                    Vector3 vec = transform.position;
                    vec.y -= 1;
                    transform.position = vec;
                }
                else
                {
                    nowTurn = false;
                }
                downMoveTime -= moveSpeed;
            }
        }
        if (nowTurn)
            if (left || right)
            {
                sideMoveTime += TimeManager.GameTime;
                if (sideMoveTime >= moveSpeed)
                {
                    PlaySound(moveSound);
                    if (left)
                    {
                        bool a = TheGameMap.sideTest(transform);
                        bool b = true;
                        if (a)
                        {
                            Vector3 vec = transform.position;
                            vec.x -= 1;
                            transform.position = vec;
                        }
                    }
                    else if (right)
                    {
                        bool a = TheGameMap.sideTest(transform, false);
                        bool b = true;
                        if (a)
                        {
                            Vector3 vec = transform.position;
                            vec.x += 1;
                            transform.position = vec;
                        }
                    }
                    sideMoveTime -= moveSpeed;
                }
            }
 
        shadow.transform.position = transform.position;
        MoveGhost();
    }
 
    private void MakeGhost()
    {
        ghost = Instantiate(ghostPrefab, transform.position, Quaternion.identity);
        MoveGhost();
    }
 
    private void MoveGhost()
    {
        ghost.transform.position = transform.position;
        ghost.transform.rotation = transform.rotation;
        int _x, _y;
        bool downGhost = true;
        while (downGhost)
        {
            foreach (Transform t in ghost.transform)
            {
                _x = Convert.ToInt32(t.transform.position.x);
                _y = Convert.ToInt32(t.transform.position.y);
                if (_y.Equals(0))
                {
                    downGhost = false;
                    break;
                }
                else if (_x < 0 || _x > 9)
                {
                    if (_x < 0)
                    {
                        Vector3 vec = ghost.transform.position;
                        vec.x += 1;
                        ghost.transform.position = vec;
                    }
                    else
                    {
                        Vector3 vec = ghost.transform.position;
                        vec.x -= 1;
                        ghost.transform.position = vec;
                    }
                }
                else if (TheGameMap.nowMap[(_y - 1* 10 + _x])
                {
                    downGhost = false;
                    break;
                }
            }
            if (downGhost)
            {
                Vector3 vec = ghost.transform.position;
                vec.y -= 1;
                ghost.transform.position = vec;
            }
        }
    }
 
    public void PlaySound(AudioClip a)
    {
        myAudio.PlayOneShot(a, 0.7f);
    }
}
 
cs


Comments