VioletaBabel
47. 네모로직+지뢰찾기 본문
심심해서 해야할 건 안하고 즉흥으로 만들어 봄
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 | //Settings.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using Newtonsoft.Json.Linq; using System.IO; public class Settings : Singleton<Settings> { private bool showSettings = false; // 세팅 버튼 누를 시 BGM,Sound 화면이 떠있는 지 여부 private Image bgmImage = null; private Image soundImage = null; private DirectoryInfo di = null; private FileInfo _finfo = null; private AudioSource thisAudio = null; public AudioClip clickSound = null; [System.Serializable] public struct _Buttons { public GameObject settingsChildren; public GameObject bgmButton; public GameObject soundButton; } [Header("-Buttons")] public _Buttons Buttons = new _Buttons(); [System.Serializable] public class _Sprites { public Sprite[] soundSprite; [HideInInspector] public bool soundOn = true; public Sprite[] BGMSprite; [HideInInspector] public bool bgmOn = true; } [Space(10)] [Header("-Sprites")] public _Sprites Sprites = new _Sprites(); [System.Serializable] public class _SFX { public AudioClip clickSound = null; } public _SFX SFX = new _SFX(); private void Start() { thisAudio = GetComponent<AudioSource>(); bgmImage = Buttons.bgmButton.GetComponent<Image>(); soundImage = Buttons.soundButton.GetComponent<Image>(); InitSetting(); } private void InitSetting() { di = new DirectoryInfo(Application.persistentDataPath + "/Json/System"); if (!di.Exists) di.Create(); _finfo = new FileInfo(Application.persistentDataPath + "/Json/System/Option.Json"); if (!_finfo.Exists) SaveOptionJson(); string s = System.IO.File.ReadAllText(Application.persistentDataPath + "/Json/System/Option.Json"); JObject rt = JObject.Parse(s); JToken oj = (rt["Option"])[0]; Sprites.bgmOn = oj["bgmOn"].Value<bool>(); Sprites.soundOn = oj["soundOn"].Value<bool>(); SetBGMSprite(); SetSoundSprite(); } private void SaveOptionJson() { JObject root = new JObject(); JArray arr = new JArray(); JObject obj = new JObject(); obj.Add("bgmOn", Sprites.bgmOn); obj.Add("soundOn", Sprites.soundOn); arr.Add(obj); root.Add("Option", arr); System.IO.File.WriteAllText(_finfo.FullName, root.ToString()); } public void ToggleSettings() { if (Sprites.soundOn) thisAudio.PlayOneShot(SFX.clickSound); showSettings = !showSettings; if (showSettings) Buttons.settingsChildren.SetActive(true); else Buttons.settingsChildren.SetActive(false); } public void ToggleBGMButton() { if (Sprites.soundOn) thisAudio.PlayOneShot(SFX.clickSound); Sprites.bgmOn = !Sprites.bgmOn; SetBGMSprite(); SaveOptionJson(); } private void SetBGMSprite() { if (Sprites.bgmOn) bgmImage.sprite = Sprites.BGMSprite[1]; else bgmImage.sprite = Sprites.BGMSprite[0]; } public void ToggleSoundButton() { Sprites.soundOn = !Sprites.soundOn; SetSoundSprite(); SaveOptionJson(); if (Sprites.soundOn) thisAudio.PlayOneShot(SFX.clickSound); } private void SetSoundSprite() { if (Sprites.soundOn) soundImage.sprite = Sprites.soundSprite[1]; else soundImage.sprite = Sprites.soundSprite[0]; } public void StartGame() { if (Sprites.soundOn) thisAudio.PlayOneShot(SFX.clickSound); SceneManager.LoadScene("SelectScene"); } } | 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 | //GameManager.cs using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.SceneManagement; public class GameManager : Singleton<GameManager> { private bool soundOn = false; private AudioSource thisAudio = null; private DirectoryInfo di = null; private FileInfo _finfo = null; [System.Serializable] public class _SFX { public AudioClip clickSound = null; } public _SFX SFX = new _SFX(); private void Start() { thisAudio = GetComponent<AudioSource>(); string s = System.IO.File.ReadAllText(Application.persistentDataPath + "/Json/System/Option.Json"); JObject rt = JObject.Parse(s); JToken oj = (rt["Option"])[0]; soundOn = oj["soundOn"].Value<bool>(); } public void BackStartScreen() { if (soundOn) thisAudio.PlayOneShot(SFX.clickSound); SceneManager.LoadScene("StartScreen"); } public void GoGameScene(int blockNum) { if (soundOn) thisAudio.PlayOneShot(SFX.clickSound); di = new DirectoryInfo(Application.persistentDataPath + "/Json/System"); if (!di.Exists) di.Create(); _finfo = new FileInfo(Application.persistentDataPath + "/Json/System/GameData.Json"); if (!_finfo.Exists) { JObject root = new JObject(); JArray arr = new JArray(); JObject obj = new JObject(); obj.Add("blockNum", blockNum); arr.Add(obj); root.Add("GameData", arr); System.IO.File.WriteAllText(_finfo.FullName, root.ToString()); } SceneManager.LoadScene("GameScene"); } } | 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 | //MakeMap.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class MakeMap : Singleton<MakeMap> { public bool isGameover = false, isClear = false; [System.Serializable] public class _GameMap { public GameObject map10x10_block, map10x10_upside, map10x10_leftside; public GameObject blockPrefab, upsidePrefab, leftsidePrefab; public GameObject[,] myblock = new GameObject[10, 10]; public GameObject[] upSide = new GameObject[10]; public GameObject[] leftSide = new GameObject[10]; public SpriteRenderer[,] myBlockRenderer = new SpriteRenderer[10, 10]; //맵 오브젝트를 찍음 public void MakeGameMap() { for (int i = 0; i < 10; ++i) { for (int j = 0; j < 10; ++j) { myblock[i, j] = Instantiate(blockPrefab, new Vector3(-2.12f + 0.7f * j, 2.15f - 0.71f * i, 0), Quaternion.identity); myblock[i, j].transform.localScale = new Vector3(0.25f, 0.25f, 1); myblock[i, j].name = "Block" + i.ToString() + j.ToString(); myblock[i, j].transform.SetParent(map10x10_block.transform); BlockData myBlockData = myblock[i, j].GetComponent<BlockData>(); myBlockData.i = i; myBlockData.j = j; myBlockRenderer[i, j] = myblock[i, j].GetComponent<SpriteRenderer>(); } upSide[i] = Instantiate(upsidePrefab, new Vector3(1.0f + i * 0.7f, 0, 0), Quaternion.identity); upSide[i].name = i.ToString(); upSide[i].transform.SetParent(map10x10_upside.transform); leftSide[i] = Instantiate(leftsidePrefab, new Vector3(1, 0 - i * 0.71f, 0), Quaternion.identity); leftSide[i].name = i.ToString(); leftSide[i].transform.SetParent(map10x10_leftside.transform); } } } [Header("-GameMapObject")] public _GameMap GameMap = new _GameMap(); [System.Serializable] public class _GameData { public GameObject GameOverObj = null; public GameObject GameClearObj = null; public GameObject flagSprite = null; public GameObject[,] flagMap = new GameObject[10, 10]; public GameObject[] numSprite = new GameObject[11]; public bool[,] clickMap = new bool[10, 10]; public int mineNum; public int notMineNum = 100; public bool[,] mineMap = new bool[10, 10]; //지뢰 위치 지정 public void MakeGameData() { System.Array.Clear(mineMap, 0, mineMap.Length); mineNum = Random.Range(20, 60); for (int x = mineNum, i, j; --x > 0;) { i = Random.Range(0, 10); j = Random.Range(0, 10); if (mineMap[i, j]) ++x; else { mineMap[i, j] = true; --notMineNum; } } } //upside, leftside에 숫자 추가 public void MakeNum() { float leftNowY, leftNowX, upNowY, upNowX; int leftcount, upcount; for (int i = 0; i < 10; ++i) { leftNowY = 2.15f - 0.71f * (float)i; leftNowX = -4.5f; leftcount = 0; upNowY = 4.56f; upNowX = -2.15f + 0.7f * (float)i; upcount = 0; for (int j = 0; j < 10; ++j) { if (mineMap[i, j]) { if (leftcount > 0) { Instantiate(numSprite[leftcount], new Vector3(leftNowX, leftNowY, 0), Quaternion.identity, MakeMap.instance.GameMap.leftSide[i].transform); leftNowX += 0.425f; leftcount = 0; } else if (j.Equals(9) && leftNowX < -4.4f) Instantiate(numSprite[0], new Vector3(leftNowX, leftNowY, 0), Quaternion.identity, MakeMap.instance.GameMap.leftSide[i].transform); } else { ++leftcount; if (j.Equals(9)) Instantiate(numSprite[leftcount], new Vector3(leftNowX, leftNowY, 0), Quaternion.identity, MakeMap.instance.GameMap.leftSide[i].transform); } if (mineMap[j, i]) { if (upcount > 0) { Instantiate(numSprite[upcount], new Vector3(upNowX, upNowY, 0), Quaternion.identity, MakeMap.instance.GameMap.upSide[i].transform); upNowY -= 0.425f; upcount = 0; } else if (j.Equals(9) && upNowY > 4.4f) Instantiate(numSprite[0], new Vector3(upNowX, upNowY, 0), Quaternion.identity, MakeMap.instance.GameMap.upSide[i].transform); } else { ++upcount; if (j.Equals(9)) Instantiate(numSprite[upcount], new Vector3(upNowX, upNowY, 0), Quaternion.identity, MakeMap.instance.GameMap.upSide[i].transform); } } } } } [Header("-GameMapData")] [Space(10)] public _GameData GameData = new _GameData(); void Start() { GameMap.MakeGameMap(); GameData.MakeGameData(); GameData.MakeNum(); ////디버그용이니 나중에 삭제 //for (int i = 0; i < 10; ++i) // for (int j = 0; j < 10; ++j) // { // if (GameData.mineMap[i, j]) // { // GameMap.myBlockRenderer[i, j].color = Color.green; // } // } ////디버그용이니 나중에 삭제 // 여기까지 } public void BackSelectScreen() { isGameover = false; SceneManager.LoadScene("SelectScene"); } public bool CheckClickBlock(int i, int j, bool isShovel) { if (!isGameover && !isClear) { if (isShovel) { if (GameData.mineMap[i, j]) { GameMap.myBlockRenderer[i, j].color = Color.red; //게임오버 코드 isGameover = true; GameData.GameOverObj.SetActive(true); Invoke("BackSelectScreen", 10.0f); } else { GameMap.myBlockRenderer[i, j].color = Color.grey; //숫자를 찍는다 int mineCount = 0; for (int ii = i - 1; ii < i + 2; ++ii) { for (int jj = j - 1; jj < j + 2; ++jj) { if (ii < 0 || jj < 0 || ii > 9 || jj > 9) continue; if (GameData.mineMap[ii, jj]) ++mineCount; } } GameObject a = Instantiate(GameData.numSprite[mineCount], GameMap.myblock[i, j].transform.position, Quaternion.identity, GameMap.myblock[i, j].transform); a.transform.localScale *= 4; GameData.clickMap[i, j] = true; } if (GameData.flagMap[i, j] != null) { Destroy(GameData.flagMap[i, j]); GameData.flagMap[i, j] = null; } if (--(GameData.notMineNum) == 0) { //클리어코드 isClear = true; GameData.GameClearObj.SetActive(true); Invoke("BackSelectScreen", 10.0f); } return false; } else if(!(GameData.clickMap[i,j])) { if (GameData.flagMap[i, j] == null) { GameObject a = Instantiate(GameData.flagSprite, GameMap.myblock[i, j].transform.position, Quaternion.identity, GameMap.myblock[i, j].transform); a.transform.localScale *= 4; GameData.flagMap[i, j] = a; } else { Destroy(GameData.flagMap[i, j]); GameData.flagMap[i, j] = null; } return true; } } return false; } } | 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 | //BlockData.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class BlockData : MonoBehaviour { public int i, j; public bool clicked = false; public bool rightclick = true; //private void OnMouseDown() //{ // if (!clicked) // { // clicked = true; // if (MakeMap.instance.CheckClickBlock(i, j, true)) // clicked = false; // } //} //private void OnMouseOver() //{ // if(Input.GetMouseButtonDown(1)) // { // MakeMap.instance.CheckClickBlock(i, j, false); // } //} } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //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 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 | //Button.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.IO; using Newtonsoft.Json.Linq; public class Button : Singleton<Button> { public bool soundOn = true; private DirectoryInfo di = null; private FileInfo _finfo = null; AudioSource thisAudio = null; [System.Serializable] public class _ButtonData { public int cursorX = 0; public int cursorY = 0; public GameObject cursorObj = null; } [Header("-ButtonData")] public _ButtonData ButtonData = new _ButtonData(); [System.Serializable] public class _SFX { public AudioClip clickSound = null; } public _SFX SFX = new _SFX(); private void Start() { thisAudio = GetComponent<AudioSource>(); string s = System.IO.File.ReadAllText(Application.persistentDataPath + "/Json/System/Option.Json"); JObject rt = JObject.Parse(s); JToken oj = (rt["Option"])[0]; soundOn = oj["soundOn"].Value<bool>(); } public void ClickShovelButton() { if (soundOn) thisAudio.PlayOneShot(SFX.clickSound); MakeMap.instance.CheckClickBlock(ButtonData.cursorY, ButtonData.cursorX, true); } public void ClickFlagButton() { if (soundOn) thisAudio.PlayOneShot(SFX.clickSound); MakeMap.instance.CheckClickBlock(ButtonData.cursorY, ButtonData.cursorX, false); } public void ClickUpButton() { if (ButtonData.cursorY > 0) --(ButtonData.cursorY); RepositionCursor(); } public void ClickDownButton() { if (ButtonData.cursorY < 9) ++(ButtonData.cursorY); RepositionCursor(); } public void ClickLeftButton() { if (ButtonData.cursorX > 0) --(ButtonData.cursorX); RepositionCursor(); } public void ClickRightButton() { if (ButtonData.cursorX < 9) ++(ButtonData.cursorX); RepositionCursor(); } private void RepositionCursor() { if (soundOn) thisAudio.PlayOneShot(SFX.clickSound); if (!MakeMap.instance.isGameover) ButtonData.cursorObj.transform.position = new Vector3(-2.12f + (0.7f * (float)ButtonData.cursorX) , 2.15f - (0.71f * (float)ButtonData.cursorY), 0); } public void BackSelectScreen() { if (soundOn) thisAudio.PlayOneShot(SFX.clickSound); SceneManager.LoadScene("SelectScene"); } } | 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 | //AudioManager.cs using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; public class AudioManager : MonoBehaviour { private bool bgmOn = true, soundOn = true, first = true; AudioSource thisAudio; // Use this for initialization void Awake() { if (GameObject.FindGameObjectsWithTag("AudioManager").Length >= 2) Destroy(gameObject); else { DontDestroyOnLoad(transform.gameObject); thisAudio = GetComponent<AudioSource>(); } } private void Update() { if(first) { string s = System.IO.File.ReadAllText(Application.persistentDataPath + "/Json/System/Option.Json"); JObject rt = JObject.Parse(s); JToken oj = (rt["Option"])[0]; bgmOn = oj["bgmOn"].Value<bool>(); soundOn = oj["soundOn"].Value<bool>(); thisAudio.loop = true; } if (bgmOn && thisAudio.volume < 0.9f) thisAudio.volume = 1; else if (!bgmOn && thisAudio.volume > 0.1f) thisAudio.volume = 0; if(bgmOn && !thisAudio.isPlaying) thisAudio.Play(); } } | cs |
'BCA > 4. Unity' 카테고리의 다른 글
65. A* (유니티) (0) | 2018.08.13 |
---|---|
54. Shader (0) | 2018.07.16 |
40. 옵션창 (0) | 2018.06.11 |
39. 미니맵 (0) | 2018.06.07 |
38. 유니티에 채팅용 서버 기본 연결 (0) | 2018.06.05 |
Comments