unity 지정된 몬스터 공격 및 대상 삭제
Unity/과제2019. 4. 24. 02:11
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; public class UIIngame : UIBase { public Material HitMaterial; private Material originMaterial; public void AddFx(string fxName, Vector3 fxPosition) { //fxName으로 fx 로드 var loadFx = Resources.Load<GameObject>(fxName); //로드된 fx로 프리팹 인스턴스화 var fxGo = GameObject.Instantiate<GameObject>(loadFx); //인스턴스화 된 프리팹을 fxpoint의 위치에 설정 fxGo.transform.position = fxPosition; var particle = fxGo.GetComponent<ParticleSystem>(); StartCoroutine(this.PlayFx(particle)); } private IEnumerator PlayFx(ParticleSystem fx) { fx.Play(); yield return new WaitForSeconds(fx.main.duration); Destroy(fx.gameObject); // fx 삭제 } public IEnumerator ChangeMaterial(Character init) { this.originMaterial = init.GetComponentInChildren<SkinnedMeshRenderer>().material; yield return new WaitForSeconds(0.1f); init.GetComponentInChildren<SkinnedMeshRenderer>().material = this.HitMaterial; yield return new WaitForSeconds(0.1f); init.GetComponentInChildren<SkinnedMeshRenderer>().material = this.originMaterial; } } | 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 | using System.Collections; using System.Collections.Generic; using UnityEngine; namespace yangjaejoon.scenes { public class InGame : MonoBehaviour { public UIIngame uiInGame; private Character hero; private Character stoneMonster; private Character bull; private StageBase currentStage; public void Awake() { AssetManager.GetInstance().LoadDatas("Prefabs"); DataManager.GetInstance().LoadCharacterData("Data/character"); DataManager.GetInstance().LoadCharacterAnimData("Data/character_anim"); DataManager.GetInstance().LoadStageData("Data/stage_data"); var stageDatas = DataManager.GetInstance().GetStageData(0); this.CreateStage<Stage_01>(stageDatas.id); //맵생성 this.hero = CreateCharacter(0); //영웅생성 this.stoneMonster = CreateCharacter(2); //스톤몬스터생성 this.bull = CreateCharacter(3); //불몬스터생성 this.hero.SetInitPosition(this.currentStage.position[0].position); this.stoneMonster.SetInitPosition(this.currentStage.position[1].position); this.bull.SetInitPosition(this.currentStage.position[2].position); } public void CreateWorld(int id) { var stageData = DataManager.GetInstance().dicStageData[id]; var stagePrefab = AssetManager.GetInstance().GetPrefab(stageData.prefab_name); var stageGo = Instantiate(stagePrefab); stageGo.transform.position = Vector3.zero; } public void Init(Camera uiCamera) { uiInGame.Init(uiCamera); this.uiInGame.FadeIn(); this.uiInGame.onFadeInCompleted = () => { Debug.Log("FadeIn완료"); this.hero.OnAttackRangeInMonster = () => { this.hero.Attack(stoneMonster); uiInGame.AddFx("Prefabs/UI/SwordHitRed", hero.transform.Find("DamagePoint").position); StartCoroutine( uiInGame.ChangeMaterial(stoneMonster)); this.hero.OnAttackStart = () => { if (this.stoneMonster.info.hp > 0) { this.hero.Attack(this.stoneMonster); uiInGame.AddFx("Prefabs/UI/SwordHitRed", hero.transform.Find("DamagePoint").position); StartCoroutine(uiInGame.ChangeMaterial(stoneMonster)); } else if (this.stoneMonster.info.hp <= 0) { this.stoneMonster.OnDieCompleted = () => { this.stoneMonster.Die(); this.stoneMonster = null; this.hero.Move(bull); this.hero.OnAttackRangeInMonster = () => { this.hero.Attack(bull); uiInGame.AddFx("Prefabs/UI/SwordHitRed", hero.transform.Find("DamagePoint").position); StartCoroutine(uiInGame.ChangeMaterial(bull)); this.hero.OnAttackStart = () => { if (this.bull.info.hp > 0) { this.hero.Attack(this.bull); uiInGame.AddFx("Prefabs/UI/SwordHitRed", hero.transform.Find("DamagePoint").position); StartCoroutine(uiInGame.ChangeMaterial(bull)); } else if (this.bull.info.hp <= 0) { this.bull.OnDieCompleted = () => { this.bull.Die(); this.bull = null; StartCoroutine(this.hero.PortalMove(this.currentStage.position[3].position)); }; } }; }; }; } }; }; this.hero.Move(stoneMonster); }; } public void CreateStage<T>(int id) where T : StageBase { var stageData = DataManager.GetInstance().GetStageData(id); var stagePrefab = AssetManager.GetInstance().dicLoadedAssets[stageData.prefab_name]; var stageGo = Instantiate(stagePrefab); stageGo.transform.position = Vector3.zero; this.currentStage = stageGo.GetComponent<T>(); } public Character CreateCharacter(int id) { var data = DataManager.GetInstance().GetCharacterData(id); var asset = AssetManager.GetInstance().dicLoadedAssets[data.prefab_name]; var characterGo = GameObject.Instantiate<GameObject>(asset); var modelPrefab = AssetManager.GetInstance().dicLoadedAssets[data.prefab_name]; var character = characterGo.AddComponent<Character>(); character.name = data.name; character.info = new yangjaejoon.CharacterInfo(id, data.hp, Vector3.zero); return character; } } } | 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 | using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using CharacterInfo = yangjaejoon.CharacterInfo; public class Character : MonoBehaviour { public CharacterInfo info; private GameObject model; private Character target; private Animation anim; private Coroutine routine; public Action OnAttackStart; public Action OnAttackRangeInMonster; public Action OnDieCompleted; private int damage = 10; private int speed = 1; public void Awake() { this.anim = this.gameObject.GetComponent<Animation>(); } public virtual void Init(CharacterInfo info, GameObject model) { this.info = info; this.model = model; this.model.transform.SetParent(this.transform); this.model.transform.localPosition = Vector3.zero; } public virtual void SetInitPosition(Vector3 initPosition) { this.transform.position = initPosition; } public IEnumerator PortalMove(Vector3 position) { this.transform.LookAt(position); var data = DataManager.GetInstance(); var characterId = DataManager.GetInstance().GetCharacterData(this.info.Id); var characterData = data.dicCharacterData[characterId.id]; this.anim.Play(data.dicCharacterAnimData[characterId.anim_id].run_name); while (true) { var dir = (position - this.transform.position).normalized; var distance = Vector3.Distance(position, this.transform.position); this.transform.position += dir * this.speed * Time.deltaTime; if (distance <= 0.3f) { this.anim.Play(data.dicCharacterAnimData[characterId.anim_id].idle_name); Debug.Log("이동완료"); break; } yield return null; } } public void Move(Character target) { if (routine != null) { StopCoroutine(this.routine); } this.routine = StartCoroutine(this.MoveImpl(target)); } private IEnumerator MoveImpl(Character target) { this.transform.LookAt(target.transform); var data = DataManager.GetInstance(); var characterId = DataManager.GetInstance().GetCharacterData(this.info.Id); var characterData = data.dicCharacterData[characterId.id]; this.anim.Play(data.dicCharacterAnimData[characterId.anim_id].run_name); while (true) { var dir = (target.transform.position - this.transform.position).normalized; var distance = Vector3.Distance(target.transform.position, this.transform.position); this.transform.position += dir * this.speed * Time.deltaTime; if (characterData.attack_range > distance) { this.OnAttackRangeInMonster(); break; } if (distance <= 0.3f) { this.anim.Play(data.dicCharacterAnimData[characterId.anim_id].idle_name); Debug.Log("이동완료"); } yield return null; } } public void Attack(Character target) { if (routine != null) { StopCoroutine(this.routine); } this.routine = StartCoroutine(this.AttackImpl(target)); } private IEnumerator AttackImpl(Character target) { var data = DataManager.GetInstance().GetCharacterData(this.info.Id); var animName = DataManager.GetInstance().dicCharacterAnimData[data.anim_id]; var length = this.anim[animName.attack_name].length; var clip = this.anim[animName.attack_name].clip; var totalFrames = length * clip.frameRate; var attackFrame = 9; if (target.info.hp > 0) { this.anim.Play(animName.attack_name); var attackTime = attackFrame * length / totalFrames; yield return new WaitForSeconds(attackTime); target.transform.LookAt(this.transform.position); target.TakeDamage(this.damage); this.anim.Play(animName.idle_name); yield return new WaitForSeconds(length - attackTime); Debug.Log("공격완료"); } this.OnAttackStart(); } public void TakeDamage(int damage) { if (routine != null) { StopCoroutine(this.routine); } this.routine = StartCoroutine(this.TakeDamageImpl(damage)); } public virtual IEnumerator TakeDamageImpl(int damage) { var data = DataManager.GetInstance().GetCharacterData(this.info.Id); var animName = DataManager.GetInstance().dicCharacterAnimData[data.anim_id]; Debug.Log("공격"); this.info.hp -= damage; yield return null; if (this.info.hp <= 0) { this.info.hp = 0; this.anim.Play(animName.die_name); yield return new WaitForSeconds(anim[animName.die_name].length); this.OnDieCompleted(); Debug.LogFormat("{0}몬스터가 죽었습니다", data.name); } else { this.anim.Play(animName.hit_name); yield return new WaitForSeconds(anim[animName.hit_name].length); this.anim.Play(animName.idle_name); } } public void Die() { Debug.Log("몬스터삭제"); GameObject.Destroy(this.gameObject); } } | cs |
앞으로 해야 할 것 : 대상 공격 시 thumb 활성화 및 타격 받을 시 thumb의 hpbar 모션
대상이 타격받을 때 반격
영웅이 죽었을 때 재시작
이펙트 사용시 인스턴스화 시키고 삭제시키는 방법 말고 objectpool을 이용할 것
'Unity > 과제' 카테고리의 다른 글
타이틀씬까지 로드 (0) | 2019.04.18 |
---|---|
FadIn,FadOut 코드 (0) | 2019.04.17 |
[Unity]코루틴 사용하여 캐릭터 이동 및 공격모션 (0) | 2019.04.17 |
유니티 게임시작화면관리하기 (0) | 2019.04.16 |
유니티 캐릭터생성 및 위치지정 (0) | 2019.04.15 |