<바론과 애니비아 쌈박질>
C#/과제 2019. 4. 11. 01:371 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class Annivia : Champion, IMoveAble, IAttackable { public virtual void Move(Vector2 movePos) { var data = DataManager.GetInstance().dicCharacterData[this.characterInfo.id]; float dis = this.characterInfo.pos.GetDistance(this.characterInfo.pos, movePos); while (true) { if (dis > 0) { System.Threading.Thread.Sleep(500); var dir = (movePos - this.characterInfo.pos).Normalize(); this.characterInfo.pos += dir * data.speed; Console.WriteLine("{0}이(가) ({1:0.00},{2:0.00})로 이동합니다.", data.name, this.characterInfo.pos.x, this.characterInfo.pos.y); dis -= data.speed; } else { this.characterInfo.pos = movePos; Console.WriteLine("{0}이(가) ({1:0.00},{2:0.00})에 도착했습니다.", data.name, this.characterInfo.pos.x, this.characterInfo.pos.y); break; } } } //기능구현 사정거리 안에 있는 놈 때리기 public void Attack(Character target) { var thisData = DataManager.GetInstance().dicCharacterData[this.characterInfo.id]; var targetData = DataManager.GetInstance().dicCharacterData[target.characterInfo.id]; //사거리 안에있으면 때림, 사거리 안에 없으면 이동함 float dis = this.characterInfo.pos.GetDistance(this.characterInfo.pos, target.characterInfo.pos); while (true) { System.Threading.Thread.Sleep(500); if (dis > thisData.range) { var dir = (target.characterInfo.pos - this.characterInfo.pos).Normalize(); this.characterInfo.pos += dir * thisData.speed; //움직임 Console.WriteLine("{0}이(가) ({1:0.00},{2:0.00})로 이동합니다.", thisData.name, this.characterInfo.pos.x, this.characterInfo.pos.y); dis -= thisData.speed; } else { //멈춰서 때림 Console.WriteLine("{0}이(가) {1}을(를) 공격합니다.", thisData.name, targetData.name); //때리면 데미지 들어감 target.TakeDamage(thisData.damage); break; } } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class App { public App() { } public void Start() { new GameLauncher(); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public interface IAttackable { void Attack(Character target); } } | 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class Baron : Monster, IAttackable { //기능구현 사정거리 안에 있는 놈 때리기 public void Attack(Character target) { var thisData = DataManager.GetInstance().dicCharacterData[this.characterInfo.id]; var targetData = DataManager.GetInstance().dicCharacterData[target.characterInfo.id]; //사거리 안에있으면 때림, 사거리 안에 없으면 안때림, 바론은 이동모테 float dis = this.characterInfo.pos.GetDistance(this.characterInfo.pos, target.characterInfo.pos); System.Threading.Thread.Sleep(500); if (dis > thisData.range) { Console.WriteLine("{0}이(가) 사정거리:{1} 밖에 있습니다.", targetData.name, thisData.range); } else { System.Threading.Thread.Sleep(500); //사거리안에 들어오면 때림 Console.WriteLine("{0}이(가) {1}을(를) 공격합니다.", thisData.name, targetData.name); //때리면 데미지 들어감 target.TakeDamage(thisData.damage); } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class Champion : 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class Character { public CharacterInfo characterInfo; //기본 생성자 public Character() { } //인포받아 생성자 public Character(CharacterInfo characterInfo) { this.characterInfo = characterInfo; } //기능구현 데미지 받기 public virtual void TakeDamage(int damage) { var data = DataManager.GetInstance().dicCharacterData[this.characterInfo.id]; if (this.characterInfo.hp > 0) { this.characterInfo.hp -= damage; if(this.characterInfo.hp <= 0) { this.characterInfo.hp = 0; } } Console.WriteLine("{0}이(가) {1}의 피해를 입었습니다.\n남은체력:({2}/{3})", data.name, damage, this.characterInfo.hp, data.hp); } } } | cs |
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; using System.Threading.Tasks; namespace TestBattle { public class CharacterData { public int id; public int type; public string name; public int hp; public int damage; public float range; public float speed; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class CharacterInfo { public int id; public int hp; public Vector2 pos; public bool alive; } } | 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 | using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace TestBattle { public class DataManager { private static DataManager Instance; public Dictionary<int, CharacterData> dicCharacterData; public DataManager() { this.dicCharacterData = new Dictionary<int, CharacterData>(); } public static DataManager GetInstance() { if (DataManager.Instance == null) { DataManager.Instance = new DataManager(); } return DataManager.Instance; } public void DataLoad(string path) { if (File.Exists(path)) { //데이터 불러오기 var json = File.ReadAllText(path); var CharacterDatas = JsonConvert.DeserializeObject<CharacterData[]>(json); foreach (var data in CharacterDatas) { this.dicCharacterData.Add(data.id, data); } Console.WriteLine("데이터 로드 완료"); } else { Console.WriteLine("데이터 파일이 없습니다."); } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class Eggnnivia : Champion { } } | cs |
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; using System.Threading.Tasks; namespace TestBattle { public class GameInfo { public List<CharacterInfo> characterInfos; public GameInfo() { this.characterInfos = new List<CharacterInfo>(); } } } | 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class GameLauncher { //캐릭터들 전역변수 private Baron baron = null; private Annivia annivia = null; private Eggnnivia eggnnivia = null; //스위치코드 private int switchCode = 0; //유저인포 private UserInfo userInfo; //캐릭터 리스트 private List<Character> characters = new List<Character>(); public GameLauncher() { //게임 시작 전 준비 this.ReadyToStart(); //게임시작 this.GameStart(); //유저인포 저장(싱크 맞추고 저장) this.SaveUserInfo(); //런쳐 종료 } private void ReadyToStart() { Console.WriteLine("게임 데이터 준비중입니다. 잠시만 기다려주세요."); //데이터 로드 DataManager.GetInstance().DataLoad("./data/character_data.Json"); //신구 유저 확인 ExistsUserInfo("./info"); //사전 준비완료 } private void GameStart() { Console.WriteLine("게임이 시작되었습니다."); #region 캐릭터 생성 if (this.userInfo.NewbieCheck == false) { //기존유저 baron = (Baron)this.CreateCharacter<Baron>(this.userInfo.gameInfo.characterInfos[0]); annivia = (Annivia)this.CreateCharacter<Annivia>(this.userInfo.gameInfo.characterInfos[1]); eggnnivia = (Eggnnivia)this.CreateCharacter<Eggnnivia>(this.userInfo.gameInfo.characterInfos[2]); } else { //신규유저 baron = this.CreateCharacter<Baron>(0); annivia = this.CreateCharacter<Annivia>(1); eggnnivia = this.CreateCharacter<Eggnnivia>(2); } baron.characterInfo.pos = new Vector2(10, 10); this.characters.Add(baron); this.characters.Add(annivia); this.characters.Add(eggnnivia); #endregion #region 게임진행 if (annivia.characterInfo.alive == true) { switchCode = 0; } else { switchCode = 1; } while (true) { if (switchCode == 0) { this.AnniviaGameStart(); } else if (switchCode == 1) { this.EggnniviaGameStart(); } else if (switchCode==-1) { break; } } #endregion //게임종료 } //유저 정보 불러오기(신규 or 기존) private void ExistsUserInfo(string path) { if (Directory.Exists(path)) { //기존 유저 Console.WriteLine("기존 유저입니다."); //캐릭터 인포 불러오기 this.userInfo = this.LoadUserInfo("./info/user_info.json"); this.userInfo.NewbieCheck = false; } else { //신규 유저 Console.WriteLine("신규 유저입니다."); //./info폴더 만들기 Directory.CreateDirectory(path); //캐릭터 인포만들기 this.CreateUserInfo(); this.userInfo.NewbieCheck = true; } } //유저 정보 불러오기 - 1(기존 유저 인포 불러오기) private UserInfo LoadUserInfo(string path) { //./info/user_info.json불러오기 var json = File.ReadAllText(path); return JsonConvert.DeserializeObject<UserInfo>(json); } //유저 정보 불러오기 - 2(신규 유저 인포 생성) private void CreateUserInfo(UserInfo userInfo = null) { if (this.userInfo == null) { if (userInfo == null) { this.userInfo = new UserInfo(); } else { this.userInfo = userInfo; } } } //기존유저 캐릭터 인스턴스 생성(불러오기) private T CreateCharacter<T>(CharacterInfo characterInfo) where T : Character, new() { T character = new T(); character.characterInfo = characterInfo; return character; } //신규유저 캐릭터 인스턴스 생성(새로만들기) private T CreateCharacter<T>(int id) where T : Character, new() { var data = DataManager.GetInstance().dicCharacterData[id]; var info = new CharacterInfo(); info.id = data.id; info.hp = data.hp; info.pos = new Vector2(0, 0); info.alive = true; T character = new T(); character.characterInfo = info; return character; } private void SaveUserInfo()//json { //현재의 유저인포(게임인포(캐릭터인포List)))를 유저인포에 저장 var characterInfos = new List<CharacterInfo>(); foreach (var character in this.characters) { characterInfos.Add(character.characterInfo); } this.userInfo.gameInfo.characterInfos = characterInfos; //파일 쓰기 var json = JsonConvert.SerializeObject(this.userInfo); File.WriteAllText("./info/user_info.json", json, Encoding.UTF8); Console.WriteLine("저장완료"); } private void AnniviaGameStart() { while (true)//애니비아버전 { Console.WriteLine(); Console.WriteLine("============================================================================================="); Console.Write("[바론의 위치({0}, {1})]\n1.애니비아(이동)/2.애니비아(공격)/3.바론(공격)/4.정보 보기/0.종료 후 저장(숫자 입력):", baron.characterInfo.pos.x, baron.characterInfo.pos.y); var input = Console.ReadLine(); if (input == "0") { //게임 종료 Console.WriteLine("게임을 종료합니다."); switchCode = -1; break; } else if (input == "1") { Console.Write("(x, y)좌표 입력\nx좌표 : "); var x = Console.ReadLine(); Console.Write("y좌표 : "); var y = Console.ReadLine(); float resultX, resultY; if (float.TryParse(x, out resultX) && float.TryParse(y, out resultY)) { var movePos = new Vector2(resultX, resultY); annivia.Move(movePos); } else { Console.WriteLine("잘못된 입력입니다."); } } else if (input == "2") { annivia.Attack(baron); if (baron.characterInfo.hp <= 0) { Console.WriteLine("바론이 죽었습니다.10초후에 부활합니다."); for (int i = 10; i > 0; i--) { System.Threading.Thread.Sleep(1000); Console.WriteLine(i); } baron.characterInfo.hp = DataManager.GetInstance().dicCharacterData[0].hp; Console.WriteLine("{0}:크와아아아아아앙({0}이 부활했습니다.) Hp:{1}/{2}", DataManager.GetInstance().dicCharacterData[0].name, baron.characterInfo.hp, DataManager.GetInstance().dicCharacterData[0].hp); } } else if (input == "3") { baron.Attack(annivia); if (annivia.characterInfo.hp <= 0) { Console.WriteLine("애니비아가 죽었습니다."); annivia.characterInfo.alive = false; eggnnivia.characterInfo.alive = true; eggnnivia.characterInfo.pos = annivia.characterInfo.pos; annivia.characterInfo.hp = DataManager.GetInstance().dicCharacterData[1].hp; eggnnivia.characterInfo.hp = DataManager.GetInstance().dicCharacterData[2].hp; Console.WriteLine("에그니비아 : 아 정글차이"); switchCode = 1; break; } } else if(input == "4") { var data = DataManager.GetInstance().dicCharacterData[0]; Console.WriteLine("============================================================================================="); Console.WriteLine("{0}의 정보", DataManager.GetInstance().dicCharacterData[0].name); Console.WriteLine("위치 : ({0}, {1}), 체력 : ({2}/{3})\n공격력 : {4}, 사거리 : {5}, 이동속도 : {6}", baron.characterInfo.pos.x, baron.characterInfo.pos.y, baron.characterInfo.hp, data.hp, data.damage, data.range, data.speed); data = DataManager.GetInstance().dicCharacterData[1]; Console.WriteLine("============================================================================================="); Console.WriteLine("{0}의 정보", DataManager.GetInstance().dicCharacterData[1].name); Console.WriteLine("위치 : ({0}, {1}), 체력 : ({2}/{3})\n공격력 : {4}, 사거리 : {5}, 이동속도 : {6}", annivia.characterInfo.pos.x, annivia.characterInfo.pos.y, annivia.characterInfo.hp, data.hp, data.damage, data.range, data.speed); } else { Console.WriteLine("잘못된 입력입니다."); } } } private void EggnniviaGameStart() { while (true)//에그니비아버전 { Console.WriteLine(); Console.WriteLine("============================================================================================="); Console.Write("[바론의 위치({0}, {1})]\n1.애그니비아(이동)/2.애그니비아(공격)/3.바론(공격)/4.애니비아(즉시부활)/5.정보 보기/0.종료 후 저장(숫자 입력):", baron.characterInfo.pos.x, baron.characterInfo.pos.y); var input = Console.ReadLine(); if (input == "0") { //게임 종료 Console.WriteLine("게임을 종료합니다."); switchCode = -1; break; } else if (input == "1") { Console.WriteLine("{0}은(는) 이동할 수 없습니다.", DataManager.GetInstance().dicCharacterData[2].name); } else if (input == "2") { Console.WriteLine("{0}은(는) 공격할 수 없습니다.", DataManager.GetInstance().dicCharacterData[2].name); } else if (input == "3") { baron.Attack(eggnnivia); if (eggnnivia.characterInfo.hp <= 0) { Console.WriteLine("에그니비아가 죽었습니다."); eggnnivia.characterInfo.alive = false; annivia.characterInfo.alive = true; annivia.characterInfo.pos = new Vector2(0, 0); Console.WriteLine("10초후에 본진(0, 0)에서 부활합니다."); for (int i = 10; i > 0; i--) { System.Threading.Thread.Sleep(1000); Console.WriteLine(i); } Console.WriteLine("{0} : 자, 날아볼까요?", DataManager.GetInstance().dicCharacterData[1].name); annivia.characterInfo.hp = DataManager.GetInstance().dicCharacterData[1].hp; switchCode = 0; break; } } else if (input == "4") { Console.WriteLine("애니비아가 즉시 부활합니다."); eggnnivia.characterInfo.alive = false; annivia.characterInfo.alive = true; Console.WriteLine("{0} : 내 날개를 타고.", DataManager.GetInstance().dicCharacterData[1].name); annivia.characterInfo.hp = eggnnivia.characterInfo.hp; switchCode = 0; break; } else if (input == "5") { var data = DataManager.GetInstance().dicCharacterData[0]; Console.WriteLine("============================================================================================="); Console.WriteLine("{0}의 정보", DataManager.GetInstance().dicCharacterData[0].name); Console.WriteLine("위치 : ({0}, {1}), 체력 : ({2}/{3})\n공격력 : {4}, 사거리 : {5}, 이동속도 : {6}", baron.characterInfo.pos.x, baron.characterInfo.pos.y, baron.characterInfo.hp, data.hp, data.damage, data.range, data.speed); data = DataManager.GetInstance().dicCharacterData[2]; Console.WriteLine("============================================================================================="); Console.WriteLine("{0}의 정보", DataManager.GetInstance().dicCharacterData[2].name); Console.WriteLine("위치 : ({0}, {1}), 체력 : ({2}/{3})\n공격력 : {4}, 사거리 : {5}, 이동속도 : {6}", eggnnivia.characterInfo.pos.x, eggnnivia.characterInfo.pos.y, eggnnivia.characterInfo.hp, data.hp, data.damage, data.range, data.speed); } else { Console.WriteLine("잘못된 입력입니다."); } } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class Monster : Character { } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public interface IMoveAble { void Move(Vector2 movePos); } } | cs |
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; using System.Threading.Tasks; namespace TestBattle { class Program { static void Main(string[] args) { App app = new App(); app.Start(); Console.ReadKey(); } } } | cs |
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; using System.Threading.Tasks; namespace TestBattle { public class UserInfo { public GameInfo gameInfo; public bool NewbieCheck; public UserInfo() { this.gameInfo = new GameInfo(); } } } | 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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestBattle { public class Vector2 { public float x, y; public Vector2(float x, float y) { this.x = x; this.y = y; } public static Vector2 operator +(Vector2 v1, Vector2 v2) { return new Vector2(v1.x + v2.x, v1.y + v2.y); } public static Vector2 operator -(Vector2 v1, Vector2 v2) { return new Vector2(v1.x - v2.x, v1.y - v2.y); } public static Vector2 operator *(Vector2 v1, float Value) { return new Vector2(v1.x * Value, v1.y * Value); } public static Vector2 operator /(Vector2 v1, float Value) { return new Vector2(v1.x / Value, v1.y / Value); } public float Dot(Vector2 v1, Vector2 v2)//스칼라 곱 { return v1.x * v2.x + v1.y * v2.y; } public float GetDistance(Vector2 v1, Vector2 v2) { return (float)(Math.Sqrt(Math.Pow(v1.x - v2.x, 2) + Math.Pow(v1.y - v2.y, 2))); } public Vector2 Normalize() { var dis = (float)Math.Sqrt(Math.Pow(this.x, 2) + Math.Pow(this.y, 2)); return new Vector2(this.x / dis, this.y / dis); } } } | cs |
'C# > 과제' 카테고리의 다른 글
<Overload, Override>, .json없이 .dat으로 (0) | 2019.04.10 |
---|---|
<lol바론과 애니비아의 사투>미완성(에러에러에러) (0) | 2019.04.09 |
<인벤토리> 저장, 불러오기 기능 (0) | 2019.04.08 |
<IEnumerable과 IEnumerator> 내일 다시 확인하여 예제 준비할 것 (0) | 2019.04.01 |
<인벤토리 1차>중첩방식 목록보기 (0) | 2019.03.31 |