[Unity] 2019-11-14 코루틴 사용
APP 2019. 11. 14. 01:37using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;
public class App : MonoBehaviour
{
public Button btnStage;
public Button btnHero;
public Button btnMonster;
public Button btnAttack;
public Dictionary<int, CharacterData> dicCharacterData;
public Dictionary<int, StageData> dicStageData;
Dictionary<int, GameObject> dicPrefab;
private Stage stage;
private Dictionary<int, Character> dicCharacter;
private Vector3 heroPos;
private float speed = 0.1f;
private Hero hero;
private Monster monster;
private Animation heroAnim;
private Animation monsterAnim;
private bool IsHeroAnimPlaying = false;
private bool IsMonsterAnimPlaying = false;
float elapsedTime;
float elapsedTime2;
int cnt;
private void Awake()
{
this.dicCharacterData = new Dictionary<int, CharacterData>();
this.dicStageData = new Dictionary<int, StageData>();
this.dicPrefab = new Dictionary<int, GameObject>();
this.dicCharacter = new Dictionary<int, Character>();
CharacterData[] arrCharacterData = JsonConvert.DeserializeObject<CharacterData[]>(Resources.Load<TextAsset>("Data/character_data").text);
foreach (CharacterData data in arrCharacterData)
{
this.dicCharacterData.Add(data.id, data);
}
StageData[] arrStageData = JsonConvert.DeserializeObject<StageData[]>(Resources.Load<TextAsset>("Data/stage_data").text);
foreach (StageData data in arrStageData)
{
this.dicStageData.Add(data.id, data);
}
LoadCharacterPrefabs();
LoadStagePrefabs();
}
public void LoadCharacterPrefabs()
{
foreach (KeyValuePair<int, CharacterData> keyValuePair in dicCharacterData)
{
this.dicPrefab.Add(keyValuePair.Key, Resources.Load<GameObject>($"Prefabs/{keyValuePair.Value.prefab_name}"));
}
}
public void LoadStagePrefabs()
{
foreach (KeyValuePair<int, StageData> keyValuePair in dicStageData)
{
this.dicPrefab.Add(keyValuePair.Key, Resources.Load<GameObject>($"Prefabs/{keyValuePair.Value.prefab_name}"));
}
}
// Start is called before the first frame update
void Start()
{
btnStage.onClick.AddListener(() => {
Debug.Log("Stage버튼");
this.CreateStage(dicStageData[1000]);
});
btnHero.onClick.AddListener(() => {
Debug.Log("Hero버튼");
this.hero = this.CreateCharacter<Hero>(dicCharacterData[100]);
this.heroAnim= this.hero.GetComponentInChildren<Animation>();
dicCharacter[100].transform.SetParent(GameUtils.SearchHierarchyForBone(this.stage.transform, "HeroInitPos"), false);
});
btnMonster.onClick.AddListener(() => {
Debug.Log("Monster버튼");
this.monster = this.CreateCharacter<Monster>(dicCharacterData[200]);
dicCharacter[200].transform.SetParent(GameUtils.SearchHierarchyForBone(this.stage.transform, "MonsterInitPos"), false);
this.monsterAnim = this.monster.GetComponentInChildren<Animation>();
});
btnAttack.onClick.AddListener(() =>
{
Debug.Log("Attack버튼");
StartCoroutine(MoveCharacter());
});
}
public void CreateStage(StageData stageData)
{
GameObject go = new GameObject(stageData.name);
Stage stage = go.AddComponent<Stage>();
GameObject model = Instantiate(dicPrefab[stageData.id]);
model.transform.SetParent(stage.transform, false);
model.transform.localPosition = Vector3.zero;
this.stage = stage;
}
public T CreateCharacter<T>(CharacterData characterData) where T : Character
{
GameObject go = new GameObject(characterData.name);
T character = go.AddComponent<T>();
GameObject model = Instantiate(dicPrefab[characterData.id]);
model.transform.SetParent(character.transform, false);
model.transform.localPosition = Vector3.zero;
dicCharacter.Add(characterData.id, character);
return character;
}
IEnumerator MoveCharacter()
{
while (true)
{
yield return new WaitForSeconds(0.02f);
//Debug.Log($"Dist:{Vector3.Distance(hero.transform.position, monster.transform.position)} atk_range:{dicCharacterData[100].atk_range}");
if(Vector3.Distance(hero.transform.position,monster.transform.position)>dicCharacterData[100].atk_range)
{
this.hero.transform.position = Vector3.MoveTowards(this.hero.transform.position, this.monster.transform.position, speed);
if(IsHeroAnimPlaying==false)
{
IsHeroAnimPlaying = true;
this.heroAnim.Play("run@loop");
}
}
else if(Vector3.Distance(hero.transform.position, monster.transform.position) <= dicCharacterData[100].atk_range)
{
if(IsMonsterAnimPlaying==true)
{
elapsedTime2 += Time.deltaTime;
}
if (cnt ==0)
{
cnt++;
this.heroAnim.Play("attack_sword_01");
elapsedTime += Time.deltaTime;
}
elapsedTime += Time.deltaTime;
if(0.256f<elapsedTime && elapsedTime<0.276f)
{
this.monsterAnim.Play("Anim_Damage");
IsMonsterAnimPlaying = true;
elapsedTime2 += Time.deltaTime;
Debug.LogFormat("맞는시간{0}",elapsedTime2);
}
if (0.447f < elapsedTime2 && elapsedTime2 < 0.477f)
{
Debug.LogFormat("초기화시간{0}", elapsedTime2);
this.monsterAnim.Play("Anim_Idle");
elapsedTime2 = 0;
Debug.LogFormat("Time2초기화되었다");
}
if (0.723f<elapsedTime && elapsedTime<0.743f)
{
IsHeroAnimPlaying = false;
elapsedTime = 0;
cnt = 0;
}
}
}
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
public void InitPos(GameObject InitPos)
{
this.transform.SetParent(InitPos.transform);
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterData
{
//int string int int float string
//id name hp damage atk_range prefab_name
public int id;
public string name;
public int hp;
public int damage;
public float atk_range;
public string prefab_name;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterInfo : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class GameUtils
{
//HOW TO USE
//var cube = this.transform.FirstChildOrDefault(x => x.name == "deeply_nested_cube");
public static Transform FirstChildOrDefault(this Transform parent, Func<Transform, bool> query)
{
if (parent.childCount == 0)
{
return null;
}
Transform result = null;
for (int i = 0; i < parent.childCount; i++)
{
var child = parent.GetChild(i);
if (query(child))
{
return child;
}
result = FirstChildOrDefault(child, query);
}
return result;
}
public static Transform SearchHierarchyForBone(Transform current, string name)
{
//sb.Append(current.name + "\n");
// check if the current bone is the bone we're looking for, if so return it
if (current.name == name)
return current;
// search through child bones for the bone we're looking for
for (int i = 0; i < current.childCount; ++i)
{
// the recursive step; repeat the search one step deeper in the hierarchy
var child = current.GetChild(i);
Transform found = SearchHierarchyForBone(child, name);
// a transform was returned by the search above that is not null,
// it must be the bone we're looking for
if (found != null)
return found;
}
// bone with name was not found
return null;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : Character
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monster : Character
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Stage : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StageData
{
//int string string
//id name prefab_name
public int id;
public string name;
public string prefab_name;
}
'APP' 카테고리의 다른 글
[Unity] 코루틴을 사용, 캐릭터의 이동과 공격, 몬스터의 피격 애니메이션 실행 (0) | 2019.11.15 |
---|---|
[Unity] Coroutine 사용해서 공격 애니메이션 실행시 문제, 공격 타이밍이 맞지 않음 (0) | 2019.11.15 |
[Unity] 코루틴으로 이동하는것 해볼것 (0) | 2019.11.13 |
[Unity] 2019-11-13 Study_002 역직렬화(캐릭터,웨폰) 인게임(캐릭터,웨폰 생성) (0) | 2019.11.13 |
[Unity] 2019-11-11 Study002 캐릭터의 이동, 애니메이션, grandchild 찾기 (0) | 2019.11.12 |