왔다갔다 때리기
Unity/과제 2019. 4. 16. 08:46코루틴은 사용 후 항상 stop할 것
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public partial class App : MonoBehaviour { private eSceneType sceneType; private void Awake() { this.sceneType = eSceneType.None; Object.DontDestroyOnLoad(this); } // Start is called before the first frame update void Start() { this.LoadScene(eSceneType.Logo); } private void LoadScene(eSceneType sceneType) { AsyncOperation asyncOperation = null; if(this.sceneType != sceneType) { this.sceneType = sceneType; asyncOperation = SceneManager.LoadSceneAsync((int)sceneType); asyncOperation.completed += (ao) => { switch (this.sceneType) { case eSceneType.Logo: { this.LoadScene(eSceneType.Title); } break; case eSceneType.Title: { this.LoadScene(eSceneType.DataLoad); } break; case eSceneType.DataLoad: { this.LoadScene(eSceneType.InGame); } break; case eSceneType.InGame: { var inGame = GameObject.FindObjectOfType<InGame>(); inGame.Init(); } break; } }; } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System.Collections; using System.Collections.Generic; using UnityEngine; public partial class App : MonoBehaviour { public enum eSceneType { None = -1, App = 0, Logo = 1, Title = 2, DataLoad = 3, InGame = 4 } } | 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 | using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Character : MonoBehaviour { public CharacterInfo info; public Animation anim; private GameObject model; public GameObject Model { get { return this.model; } set { this.model = value; this.model.transform.SetParent(this.transform); this.anim = this.model.GetComponent<Animation>(); } } public System.Action OnAttackComplete; private Coroutine routine; //생성자 public Character(CharacterInfo info) { this.info = info; } //이동메서드 public void Move(Vector3 targetPosition, System.Action onCompleteMove) { Debug.Log("Move호출"); if(routine != null) { StopCoroutine(this.routine); } this.routine = StartCoroutine(this.MoveImpl(targetPosition, onCompleteMove)); } //이동코루틴 private IEnumerator MoveImpl(Vector3 targetPosition, System.Action onCompleteMove) { Debug.Log("moveImpl 호출"); this.anim.Play(DataManager.GetInstance().dicCharacterAnimData[this.info.id].run_anim_name); var normVector = (targetPosition - this.transform.position).normalized; var speed = 1.0f; var moveVector = normVector * speed * Time.deltaTime; while(true) { this.transform.position += moveVector; var distance = Vector3.Distance(targetPosition, this.transform.position); if(distance <= 0.5f) { break; } yield return null; } onCompleteMove(); } //공격메서드 public void Attack(Character target) { Debug.Log("Attack 호출"); if (routine != null) { StopCoroutine(this.routine); } this.routine = StartCoroutine(this.AttackImpl(target)); } //공격코루틴 private IEnumerator AttackImpl(Character target) { Debug.Log("attackImpl 호출"); var attackAnimName = DataManager.GetInstance().dicCharacterAnimData[this.info.id].attack_anim_name; this.anim.Play(attackAnimName); yield return new WaitForSeconds(0.26f); Debug.LogFormat("{0}에게 {1}의 데미지를 입혔습니다!", DataManager.GetInstance().dicCharacterData[target.info.id].name, DataManager.GetInstance().dicCharacterData[this.info.id].damage); target.GetDamage(DataManager.GetInstance().dicCharacterData[this.info.id].damage); yield return new WaitForSeconds(0.46f); this.OnAttackComplete(); } //피해메서드 public void GetDamage(int damage) { this.info.hp -= damage; if (this.info.hp <= 0) { this.gameObject.GetComponentInChildren<Animation>().Play(DataManager.GetInstance().dicCharacterAnimData[this.info.id].die_anim_name); } else { this.gameObject.GetComponentInChildren<Animation>().Play(DataManager.GetInstance().dicCharacterAnimData[this.info.id].hit_anim_name); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class DataLoad : MonoBehaviour { private AsyncOperation asyncOperation; // Start is called before the first frame update void Start() { //데이터 로드, 데이터 적재 Debug.Log("DataLoad 씬 Start()"); DataManager dataManager = DataManager.GetInstance(); dataManager.LoadDatas(); } } | 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 | using System.Collections; using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; public class DataManager { private static DataManager instance; public Dictionary<int, CharacterData> dicCharacterData; public Dictionary<int, CharacterAnimData> dicCharacterAnimData; //생성자 private DataManager() { this.dicCharacterAnimData = new Dictionary<int, CharacterAnimData>(); this.dicCharacterData = new Dictionary<int, CharacterData>(); } //인스턴스리턴 public static DataManager GetInstance() { if(DataManager.instance == null) { DataManager.instance = new DataManager(); } return DataManager.instance; } //제이슨 파일로드 public void LoadDatas() { //캐릭터데이터 var textAsset = Resources.Load<TextAsset>("Datas/character_data"); var json = textAsset.text; var arrJson = JsonConvert.DeserializeObject<CharacterData[]>(json); foreach (var data in arrJson) { this.dicCharacterData.Add(data.id, data); } //애니메이션데이터 var textAsset2 = Resources.Load<TextAsset>("Datas/character_anim_data"); var json2 = textAsset2.text; var arrJson2 = JsonConvert.DeserializeObject<CharacterAnimData[]>(json2); foreach (var data in arrJson2) { this.dicCharacterAnimData.Add(data.id, data); } Debug.LogFormat("dicCharacterData {0}개", this.dicCharacterData.Count); Debug.LogFormat("dicCharacterAnimData {0}개", this.dicCharacterAnimData.Count); } } | 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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class InGame : MonoBehaviour { private Character hero; private Character monster; private bool completeMethod; // Start is called before the first frame update void Start() { Debug.Log("InGame Start 메서드 실행"); this.hero.anim.Play(DataManager.GetInstance().dicCharacterAnimData[this.hero.info.id].idle_anim_name); this.monster.anim.Play(DataManager.GetInstance().dicCharacterAnimData[this.monster.info.id].idle_anim_name); this.hero.Move(this.monster.transform.position, () => { Debug.Log("사정거리에 도달했습니다."); this.hero.anim.Play(DataManager.GetInstance().dicCharacterAnimData[this.hero.info.id].idle_anim_name); this.hero.Attack(this.monster); }); this.hero.OnAttackComplete = () => { this.hero.anim.Play(DataManager.GetInstance().dicCharacterAnimData[this.hero.info.id].idle_anim_name); if (this.monster.info.hp > 0) { this.hero.Attack(this.monster); } else { this.hero.anim.Play(DataManager.GetInstance().dicCharacterAnimData[this.hero.info.id].idle_anim_name); } }; this.hero.transform.LookAt(new Vector3(1, 0, 1)); this.hero.Move(new Vector3(1, 0, 1), () => { Debug.Log("초기위치에 도달했습니다."); this.hero.anim.Play(DataManager.GetInstance().dicCharacterAnimData[this.hero.info.id].idle_anim_name); }); } public void Init() { Debug.Log("InGame 스크립트의 Init메서드 호출"); var data = DataManager.GetInstance(); this.hero = this.CreateCharacter(0); this.monster = this.CreateCharacter(1); this.hero.transform.position = new Vector3(1, 0, 1); this.monster.transform.position = new Vector3(5, 0, 5); this.hero.transform.LookAt(this.monster.transform); this.monster.transform.LookAt(this.hero.transform); } public Character CreateCharacter(int id) { var data = DataManager.GetInstance(); //캐릭터생성 var characterPrefab = Resources.Load<GameObject>("Prefabs/Character"); var characterGo = GameObject.Instantiate<GameObject>(characterPrefab); var modelPrefab = Resources.Load<GameObject>(data.dicCharacterData[id].prefab_name); var modelGo = GameObject.Instantiate<GameObject>(modelPrefab); var character = characterGo.AddComponent<Character>(); character.Model = modelGo; character.name = data.dicCharacterData[id].name; character.info = new CharacterInfo(id, data.dicCharacterData[id].anim_data_id, data.dicCharacterData[id].hp); character.transform.position = character.info.position; Debug.Log("캐릭터 생성됨"); return character; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Logo : MonoBehaviour { // Start is called before the first frame update void Start() { Debug.Log("Logo씬 스크립트의 Start메서드가 호출됨"); Debug.Log("Logo를 보여줍니다."); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Title : MonoBehaviour { // Start is called before the first frame update void Start() { Debug.Log("Title Scene Title Script 호출"); Debug.LogFormat("DataLoad씬 호출완료"); } } | cs |
'Unity > 과제' 카테고리의 다른 글
Isometric, 클릭시 좌표 출력하기 (0) | 2019.05.13 |
---|---|
2D Running Game 기본 시스템 만들기 (0) | 2019.05.09 |
씬 전환 및 가서 공격하기 (0) | 2019.04.16 |
0 (0) | 2019.04.11 |