|
C#/실습 2019. 11. 26. 17:40
코드:
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
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace _11._26_study2
{
class App
{
private delegate void Del(Monster monster, Action<Monster> action);
Action<Monster> monsterHit;
Action<Hero, Monster> attackAction;
public App()
{
var hero = new Hero(10);
var monster = new Monster(100);
//monsterHit = (monster) =>
//{
// Console.WriteLine("몬스터의 체력이 감소되었습니다. 몬스터의 현재체력: {0}", monster.hp);
// {
// }
// else
// {
// }
//};
//**********************************************
//모든 과정을 App을 통해 전달:
//어떻게 맞는지 정의
monster.GetDamageAc = (damage) =>
{
monster.GetDamage(damage);
Console. WriteLine( "몬스터가 피격당했습니다. 몬스터 현재체력: {0}", monster.hp); //얘 맞음
};
//어떻게 때리는지 정의
hero.iDamaged = (damage) =>
{
Console.WriteLine("영웅이 때렸습니다. 데미지:{0}", damage);
monster.GetDamageAc(damage); //너 맞으래
};
//얘네 서로 어떻게싸우는지 정의
attackAction = (hero, monster) =>
{
{
Thread.Sleep(300);
AttackUntilDie(hero, monster, attackAction);
}
else
{
}
};
AttackUntilDie(hero, monster, attackAction);
}
public void AttackUntilDie(Hero hero, Monster monster, Action<Hero, Monster> attackAction)
{
attackAction(hero, monster); //얘네 서로 싸운다
}
}
}
|
|
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
|
using System;
using System.Collections.Generic;
using System.Text;
namespace _11._26_study2
{
class Hero
{
public int damage;
public Action<int> iDamaged;
public Hero(int damage)
{
this.damage = damage;
}
//public void Attack(Monster monster, Action<Monster> attackComplete)
//{
// //몬스터의 체력을 감소시킨다
// attackComplete(monster);
//}
public void BlindAttack()
{
iDamaged(this.damage);
}
}
}
|
|
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
|
using System;
using System.Collections.Generic;
using System.Text;
namespace _11._26_study2
{
class Monster
{
public int hp;
public Action onDie;
public Action<int> GetDamageAc;
public Monster(int hp)
{
this.hp = hp;
}
public void MyHpIsZero()
{
if(this.hp <=0)
{
onDie(); //나 죽었소
}
}
public void GetDamage(int damage)
{
this.hp -= damage;
GetDamageAc(hp); // 나 맞았소
}
}
}
|
|
출력:
C#/실습 2019. 11. 13. 17:28
예제:
Hero Go 버튼을 누르기 전
누른 직후
사정 거리 내 도착 후
코드:
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum eCharacter
{
none,
ch_01_01,
ch_02_01,
StoneMonster
}
public class App : MonoBehaviour
{
public eCharacter eCharacterNames;
public Button btnCreateStage;
public Button btnCreateHero;
public Button btnCreateMonster;
public Button btnHeroGO;
private Dictionary<string, GameObject> dicPrefabs;
private Stage stage;
private Hero hero;
private Monster monster;
private bool isHeroGO = false;
private bool isAttack = false;
// Start is called before the first frame update
void Start()
{
int count = 0;
this.Init();
this.btnCreateStage.onClick.AddListener(() =>
{
this.CreateStage();
});
this.btnCreateHero.onClick.AddListener(() =>
{
if(count ==0)
{
this.CreateHero();
count++;
}
else
{
}
});
this.btnCreateMonster.onClick.AddListener(() =>
{
this.CreateMonster();
});
this.btnHeroGO.onClick.AddListener(() =>
{
isHeroGO = true;
});
}
public GameObject FindChild(Transform parent, string childName)
{
GameObject childGO = null;
foreach (Transform child in parent)
{
{
childGO = child.gameObject;
return childGO;
}
else
{
childGO = FindChild(child.transform, childName);
if (childGO != null)
{
return childGO;
}
continue;
}
}
return childGO;
}
public void Init()
{
dicPrefabs = new Dictionary<string, GameObject>();
var prefabMonster = Resources.Load<GameObject >( "Prefabs/StoneMonster");
}
public void CreateMonster()
{
{
return;
}
GameObject monsterGO = new GameObject("Monster");
Monster monster = monsterGO.AddComponent<Monster>();
var prefab = dicPrefabs[eCharacterNames.ToString()];
var model = Instantiate(prefab);
monster.SetModel(model);
monsterGO.transform.position = FindChild(stage.transform, "MonsterSpawnP").transform.position;
this.monster = monster;
}
public void CreateStage()
{
GameObject stageGO = new GameObject("Stage");
Stage stage = stageGO.AddComponent<Stage>();
var prefabs = dicPrefabs["Stage"];
var model = Instantiate(prefabs);
model.transform.SetParent(stageGO.transform);
this.stage = stage;
}
public void CreateHero()
{
{
return;
}
GameObject heroGO = new GameObject("Hero");
Hero hero = heroGO.AddComponent<Hero>();
var prefab = dicPrefabs[eCharacterNames.ToString()];
var model = Instantiate(prefab);
hero.SetModel(model);
heroGO.transform.position = FindChild(stage.transform, "HeroSpawnP").transform.position;
hero.attack_range = 1f;
this.hero = hero;
}
// Update is called once per frame
void Update()
{
if(isHeroGO == true)
{
this.hero.transform.position = Vector3.Lerp(this.hero.transform.position, this.monster.transform.position, 0.05f);
if ((Vector3.Distance(this.hero.transform.position, this.monster.transform.position) <= this.hero.attack_range))
{
isHeroGO = false;
isAttack = true;
}
}
if(isAttack)
{
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Stage : MonoBehaviour
{
public GameObject model;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : MonoBehaviour
{
public GameObject model;
public float attack_range;
// Start is called before the first frame update
void Start()
{
}
public void SetModel(GameObject model)
{
if (this.model != null)
{
Destroy(this.model);
}
this.model = model;
this.model.transform.SetParent(this.gameObject.transform, false);
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monster : MonoBehaviour
{
public GameObject model;
// Start is called before the first frame update
void Start()
{
}
public void SetModel(GameObject model)
{
if (this.model != null)
{
Destroy(this.model);
}
this.model = model;
this.model.transform.SetParent(this.gameObject.transform, false);
}
// Update is called once per frame
void Update()
{
}
}
|
|
C#/실습 2019. 11. 12. 20:00
예제:
코드:
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App : MonoBehaviour
{
public Animation Anim1;
public Animation Anim2;
public Button btn;
private float elapsedTime1; //경과 시간
private float elapsedTime2;
private bool isAttackAnimationPlay1 = false;
private bool isAttackAnimationPlay2 = false;
private bool isAttackAnimationPlay3 = false;
private bool isAttacked = false;
private bool isHero2Alive = true;
private Hero hero1;
private Hero hero2;
private Sword sword;
// Start is called before the first frame update
void Start()
{
//버튼을 누르면 공격 애니메이션을 실행한다
this.btn.onClick.AddListener(() =>
{
isAttackAnimationPlay1 = true;
});
//AnimationState state1 = this.Anim1["attack_sword_01"];
//foreach (AnimationState state in Anim1)
//{
// {
// }
//}
//model 불러오기
var hero1Model = Instantiate(heroPrefab);
var hero2Model = Instantiate(heroPrefab);
GameObject heroGO1 = new GameObject("Hero1");
hero1 = heroGO1.AddComponent<Hero>();
hero1Model.transform.SetParent(heroGO1.transform, false);
hero1Model.transform.localRotation = new Quaternion(0, 0, 0, 0);
GameObject heroGO2 = new GameObject("Hero2");
hero2 = heroGO2.AddComponent<Hero>();
hero2Model.transform.SetParent(heroGO2.transform, false);
hero2Model.transform.localRotation = new Quaternion(0, 0, 0, 0);
heroGO2.transform.position = new Vector3(1, 0, 0);
GameObject swordGO = new GameObject("Sword");
sword = swordGO.AddComponent<Sword>();
}
// Update is called once per frame
//매 프레임 마다 호출됨(각 프레임 경과 타임은 다르다)
void Update()
{
if (isAttackAnimationPlay1 == true)
{
this.elapsedTime1 += Time.deltaTime;
//0.2665초때 타격이라고 출력하기
if (this.elapsedTime1 >= 0.2665f && isAttacked == false)
{
isAttacked = true;
this.elapsedTime2 = 0;
Debug.LogFormat("타격1! 시간: {0}", this.elapsedTime1);
}
if (this.elapsedTime1 >= 0.733f)
{
this.isAttackAnimationPlay1 = false;
this.elapsedTime1 = 0;
this.isAttacked = false;
this.isAttackAnimationPlay2 = true;
}
}
if (isAttackAnimationPlay2 == true)
{
this.elapsedTime1 += Time.deltaTime;
//0.5333초때 타격이라고 출력하기
if (this.elapsedTime1 >= 0.5333f && isAttacked == false)
{
isAttacked = true;
this.elapsedTime2 = 0;
Debug.LogFormat("타격2! 시간: {0}", this.elapsedTime1);
}
if (this.elapsedTime1 >= 0.8f)
{
this.isAttackAnimationPlay2 = false;
this.elapsedTime1 = 0;
this.isAttacked = false;
this.isAttackAnimationPlay3 = true;
}
}
if (isAttackAnimationPlay3 == true)
{
this.elapsedTime1 += Time.deltaTime;
//0.5333초때 타격이라고 출력하기
if (this.elapsedTime1 >= 0.5333f && isAttacked == false)
{
isAttacked = true;
this.elapsedTime2 = 0;
Debug.LogFormat("타격3! 시간: {0}", this.elapsedTime1);
}
if (this.elapsedTime1 >= 0.8f)
{
this.isAttackAnimationPlay3 = false;
this.elapsedTime1 = 0;
this.isAttacked = false;
}
}
this.elapsedTime2 += Time.deltaTime;
if (isAttacked == true && isHero2Alive == true)
{
}
if( hero2.hp <=0 && isHero2Alive == true)
{
isHero2Alive = false;
}
if (this.elapsedTime2 >= 0.8f && isHero2Alive == true)
{
this.elapsedTime2 = 0;
}
{
isHero2Alive = true;
}
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : MonoBehaviour
{
public int hp;
public GameObject model;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void GetHit(int damage)
{
if(this.hp - damage <= 0)
{
this.hp = 0;
}
else
{
this.hp -= damage;
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sword : MonoBehaviour
{
public int damage;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
|
|
C#/실습 2019. 11. 11. 17:13
예제:
hero가 처음에는 무기를 갖고 있다가 monster에게 넘겨준다(코드상으로만)
코드:
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 App2 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GameObject heroGO = new GameObject("Hero");
heroGO.transform.position = new Vector3(-2, 0, 0);
Hero hero = heroGO.AddComponent<Hero>();
CharacterInfo info = new CharacterInfo() { id = 300, hp = 100 };
GameObject monsterGO = new GameObject("Monster");
Monster monster = monsterGO.AddComponent<Monster>();
GameObject heroPrefab = Resources.Load<GameObject >( "Prefabs/ch_02_01");
GameObject heroModel = Instantiate(heroPrefab);
heroModel.transform.SetParent(heroGO.transform,false);
GameObject dummyhandGOHero = FindChild(heroGO.transform,"DummyRHand");
GameObject monsterPrefab = Resources.Load<GameObject >( "Prefabs/ch_03_01");
GameObject monsterModel = Instantiate(monsterPrefab);
monsterModel.transform.SetParent(monsterGO.transform, false);
GameObject dummyhandGOMonster = FindChild(monsterGO.transform, "DummyRHand");
GameObject swordPrefab = Resources.Load<GameObject >( "Prefabs/ch_sword_01");
GameObject swordModel = Instantiate(swordPrefab);
swordModel.transform.SetParent(dummyhandGOHero.transform, false);
swordModel.transform.SetParent(dummyhandGOMonster.transform, false);
GameObject swordGO = new GameObject("Sword");
Sword sword = swordGO.AddComponent<Sword>();
sword.attack_range = 1.5f;
}
public GameObject FindChild(Transform parent, string childName)
{
GameObject childGO = null;
foreach (Transform child in parent)
{
{
childGO = child.gameObject;
return childGO;
}
else
{
childGO = FindChild(child.transform, childName);
if(childGO != null)
{
return childGO;
}
continue;
}
}
return childGO;
}
// Update is called once per frame
void Update()
{
}
}
|
|
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterInfo
{
public int id;
public int hp;
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
public CharacterInfo info;
public GameObject model;
public Sword sword;
// Start is called before the first frame update
void Start()
{
}
public void SetModel(GameObject model)
{
this.model = model;
if(oldmodel != null)
{
Destroy(oldmodel.gameObject);
}
model.transform.SetParent(this.transform, false);
}
public void SetInfo(CharacterInfo info)
{
this.info = info;
}
// Update is called once per frame
void Update()
{
}
}
|
|
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : Character
{
}
|
|
|
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monster : Character
{
}
|
|
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sword : MonoBehaviour
{
public int damage;
public float attack_range;
}
|
|
MS API 에는 특정 부모 한정 오브젝트의 이름을 찾기가 없어서 새로운 메소드를 만들어 그것을 구현했다.
C#/실습 2019. 11. 8. 16:57
예제:
코드:
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App : MonoBehaviour
{
private Dictionary<int, CharacterData> CharacterDatas;
public Ingame ingame;
// Start is called before the first frame update
void Start()
{
//json 파일 로드
string json = Resources.Load<TextAsset >( "Datas/character_data2").text;
CharacterData[] arrCharacterDatas = JsonConvert.DeserializeObject<CharacterData[]>(json);
CharacterDatas = new Dictionary<int, CharacterData>();
foreach(var data in arrCharacterDatas)
{
}
// prefab 파일 로드
GameObject prefabFiery = this.GetPrefab(this.CharacterDatas[200].prefab_name);
GameObject prefabCyclopes = this.GetPrefab(this.CharacterDatas[100].prefab_name);
//ingameparams 구조체에 담기
Ingame.IngameParams param = new Ingame.IngameParams();
param.CharacterDatas = this.CharacterDatas;
param.heroPrefab = prefabFiery;
param.monsterPrefab = prefabCyclopes;
//ingame에 전달
GameObject ingameGO = new GameObject("Ingame");
this.ingame = ingameGO.AddComponent<Ingame>();
}
public GameObject GetPrefab(string prefabName)
{
string path = string.Format("Prefabs/{0}", prefabName);
return prefab;
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ingame : MonoBehaviour
{
public class IngameParams
{
public Dictionary<int, CharacterData> CharacterDatas;
public GameObject monsterPrefab;
public GameObject heroPrefab;
}
IngameParams param;
Hero hero;
Monster monster;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//사거리 재고 공격하기
float dis = Vector3.Distance(monster.transform.position, hero.transform.position);
if (heroAttackrange >= dis)
{
}
else
{
Debug.LogFormat("대상을 공격할 수 없습니다. dis: {0} range: {1}", dis, heroAttackrange);
}
}
public void Init(IngameParams param)
{
this.param = param;
this.monster = (Monster)this.GetCharacterGO<Monster>("Monster", this.param.monsterPrefab);
CharacterInfo monsterInfo = new CharacterInfo(this.param.CharacterDatas[100].hp, this.param.CharacterDatas[100].damage, 100);
this.hero = (Hero)this.GetCharacterGO<Hero>("Hero", this.param.heroPrefab);
CharacterInfo heroInfo = new CharacterInfo(this.param.CharacterDatas[200].hp, this.param.CharacterDatas[200].damage, 200);
}
public Character GetCharacterGO<T>(string name, GameObject prefab) where T : Character
{
GameObject GO = new GameObject(name);
GameObject modelGO = Instantiate(prefab);
modelGO.transform.SetParent(GO.transform, false);
return GO.AddComponent<T>();
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
private CharacterInfo info;
public CharacterInfo Info
{
get { return this.info; }
}
// Start is called before the first frame update
void Start()
{
}
public void Init(CharacterInfo info)
{
this.info = info;
}
public void Attack(GameObject target)
{
}
// Update is called once per frame
void Update()
{
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterData
{
public int id;
public string name;
public int hp;
public int damage;
public float attack_range;
public string prefab_name;
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterInfo
{
public int id;
public int hp;
public int damage;
public CharacterInfo(int hp, int damage, int id)
{
this.hp = hp;
this.damage = damage;
this.id = id;
}
}
|
|
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : Character
{
}
|
|
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monster : Character
{
}
|
|
C#/실습 2019. 11. 7. 17:51
예제:
코드:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App : MonoBehaviour
{
Ingame ingame;
// Start is called before the first frame update
void Start()
{
GameObject ingameGO = new GameObject("Ingame");
this.ingame = ingameGO.AddComponent<Ingame>();
ingame.StartGame();
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
|
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ingame : MonoBehaviour
{
Chicken chicken;
Monster monster;
Hero hero;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
internal void StartGame()
{
GameObject heroGO = new GameObject("Hero");
GameObject monsterGO = new GameObject("Monster");
GameObject chickenGO = new GameObject("Chicken");
this.chicken = chickenGO.AddComponent<Chicken>();
this.monster = monsterGO.AddComponent<Monster>();
this.hero = heroGO.AddComponent<Hero>();
//info 받아오기
TextAsset asset = Resources.Load<TextAsset >( "Data/character_data2"); //프로젝트에 존재하는 모든 파일을 어셋이라고 한다.
CharacterData[] characterDatas = JsonConvert.DeserializeObject <CharacterData[] >( asset.text);
Dictionary<int, CharacterData> dicCharacterDatas = new Dictionary<int, CharacterData>();
foreach (var data in characterDatas)
{
}
var characterdata = dicCharacterDatas[100];
//model 가져오기
string path = string.Format("Prefabs/{0}", characterdata.prefab_name);
GameObject modelCyclopes = Instantiate(prefabCyclopes);
modelCyclopes.transform.SetParent(heroGO.transform, false);
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : MonoBehaviour
{
public CharacterInfo info;
private Vector3 Pos;
GameObject model;
public void Init(CharacterInfo info, GameObject model, Vector3 pos)
{
this.info = info;
this.Pos = pos;
this.model = model;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterData
{
public int id;
public string name;
public string prefab_name;
public int hp;
public int damage;
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterInfo
{
private int id;
public string name;
public int hp;
public int damage;
public int ID { get { return this.id; } }
public CharacterInfo(int id, string name, int hp, int damage)
{
this.id = id;
this.name = name;
this.hp = hp;
this.damage = damage;
}
}
|
|
json 파일: [ { "id": 100, "name": "Cyclopes", "prefab_name": "Boximon Cyclopes", "hp": 100, "damage": 3 }, { "id": 200, "name": "Fiery", "prefab_name": "Boximon Fiery", "hp": 95, "damage": 4 } ]
C#/실습 2019. 11. 5. 17:50
Unity:
App2는 Ingame을 관리 할 수있고 Ingame 안에서 Monster객체와 Hero 객체가 생성되면서 서로에게 데미지를 주고있다.
Monster객체는 계층 구조로 model을 갖고있으며 멤버변수로도 model을 갖고있다. (<--왜?)
코드:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App : MonoBehaviour
{
private Ingame ingame;
// Start is called before the first frame update
void Start()
{
GameObject IngameGO = new GameObject("Ingame");
this.ingame = IngameGO.AddComponent<Ingame>();
ingame.StartGame();
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ingame : MonoBehaviour
{
public Monster monster;
public Hero hero;
// Start is called before the first frame update
//void Awake()
//{
//}
void Start()
{
}
public void StartGame()
{
GameObject prefabFiery = Resources.Load<GameObject >( "Boximon Fiery"); //메모리상에 올린다.
GameObject prefabCyclopes = Resources.Load<GameObject >( "Boximon Cyclopes");
GameObject monsterGO = new GameObject("monsterGO");
GameObject heroGO = new GameObject("heroGO");
this.monster = monsterGO.AddComponent<Monster>();
this.hero = heroGO.AddComponent<Hero>();
var modelFiery = Instantiate(prefabFiery); //메모리에 올려져있는 데이터를 클론에 복제한다.
var modelCyclopes = Instantiate(prefabCyclopes);
monster.transform.position = new Vector3(0,0,0);
hero.transform.position = new Vector3(4, 0, 0);
modelFiery.transform.SetParent(monster.transform,false); //클론화된 모델 객체가 monster의 transform을 상속받는다.
modelCyclopes.transform.SetParent(hero.transform, false);
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
public int hp;
public int damage;
public GameObject model;
// Start is called before the first frame update
void Start()
{
}
public void Attack(Character target)
{
Debug.LogFormat( "{0}이(가) {1}에게 {2}만큼의 피해를 주었습니다.", this.name, target.name, this.damage);
}
public void Hit(int damage)
{
this.hp -= damage;
Debug.LogFormat("{0}이(가) 맞았습니다. 현재 체력:({1})", this.name, this.hp);
}
// Update is called once per frame
void Update()
{
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monster : Character
{
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : Character
{
}
|
|
C#/실습 2019. 10. 31. 16:22
예제:
코드:
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _10._31_step2
{
class App
{
public App()
{
LinkedList list = new LinkedList(4);
Item item1 = new Item(1, "단검");
list.PrintByIndex();
list.PrintByIndex();
list.PrintByNode();
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _10._31_step2
{
public class Item
{
public int Id { get; private set; }
public string Name { get; private set; }
public Item(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
}
|
|
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _10._31_step2
{
public class LinkedList
{
public Node[] nodes;
public Node first;
public int count = 0;
public int Count
{
get
{
return count;
}
}
public LinkedList(int capacity)
{
Console.WriteLine("LinkedList 생성자가 호출되었습니다.");
nodes = new Node[capacity];
}
public void PrintByNode()
{
Console.WriteLine("노드 순으로 출력:");
if(first != null)
{
Node node = first;
while (node != null)
{
}
}
}
public void PrintByIndex()
{
Console.WriteLine("인덱스 순으로 출력:");
foreach (Node node in nodes)
{
if (node != null)
{
}
}
}
public int CheckNumofNull()
{
{
if (nodes[i] != null)
nullCount--;
}
return nullCount;
}
private Node FindNode(Item item)
{
{
if (nodes[i].item == item)
{
return nodes[i];
}
}
return null;
}
public void Remove(Item item)
{
//1.아이템을 담고있는 해당 노드를 찾는다
var node = FindNode(item);//nodes[i]
if (node == null)
{
Console.WriteLine("해당 아이템을 찾을 수 없습니다.");
return;
}
//2.1 해당 노드가 first 인 경우 자신의 참조를 끊고 first 값을 자신의 next 값으로 바꿔준다.
//2.2 해당 노드를 next값으로 갖고있는 노드를 찾고 그걸 해당노드의 next값으로 바꿔준다.
if (node == first)
{
count--;
}
else
{
{
if (nodes[i].next == node)
{
count--;
break;
}
}
}
//3. 해당 노드를 참조하고있는 배열의 인덱스를 null로 바꾼다.
{
if (nodes[i] == node)
{
nodes[i] = null;
break;
}
}
//4. 해당 노드를 지운다. --> 해당 노드가 아무것도 참조받지 않고 참조 하지 않게한다.
}
public Node FindLastNode(Node node)
{
//배열이랑 상관이 없다.
{
return node;
}
}
public void Add(Item item)
{
if (CheckNumofNull() == 0)
{
Console.WriteLine("배열에 공간이 없습니다.");
return;
}
Node node = new Node(item);
if (first == null) // 첫번째 노드가 없을때
{
first = node;
count++;
{
if (nodes[i] == null)
{
nodes[i] = node;
Console.WriteLine("새로운 아이템이 인덱싱 되어 추가되었습니다.");
break;
}
}
}
else // 첫번째 노드가 있을때.
{
Node lastNode = FindLastNode(this.first);
count++;
{
if (nodes[i] == null)
{
nodes[i] = node;
Console.WriteLine("새로운 아이템이 인덱싱 되어 추가되었습니다.");
break;
}
}
}
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _10._31_step2
{
public class Node
{
public Node next;
public Item item;
public Node(Item item)
{
this.item = item;
}
}
}
|
|
|