/tags/Premiere_soutenance/eradiKor/eradiKor/Structures/Monster.cs

http://xe8tmw7c.googlecode.com/ · C# · 377 lines · 272 code · 49 blank · 56 comment · 43 complexity · c8f56b061f0bc8b4a614001b17b45c49 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. namespace eradiKor.Structures
  7. {
  8. #region enum MonsterType
  9. /// <summary>
  10. /// Types de monstres, ie : Flying, Kornu, ...
  11. /// </summary>
  12. public enum MonsterType
  13. {
  14. Normal,
  15. Stronger,
  16. Faster,
  17. Flying,
  18. Ghost
  19. }
  20. #endregion
  21. #region struct MonsterSumUp
  22. public struct MonsterSumUp
  23. {
  24. #region Attributs
  25. //Attributs
  26. private Structures.MonsterType type;
  27. private int level;
  28. private int hp;
  29. private int speed;
  30. private int cost;
  31. private int scoremonster;
  32. private Structures.Color color;
  33. private int id;
  34. #endregion
  35. #region Constructeur
  36. //Constructeur
  37. public MonsterSumUp(Structures.MonsterType t, int lvl, int health, int spd, int money, int scoremon, Structures.Color col)
  38. {
  39. this.type = t;
  40. this.level = lvl;
  41. this.hp = health;
  42. this.speed = spd;
  43. this.cost = money;
  44. this.scoremonster = scoremon;
  45. this.color = col;
  46. //génčre l’id en espérant qu’il sera unique ... :-°
  47. this.id = 0;
  48. Random rand = new Random();
  49. while (this.id == 0)
  50. this.id = rand.Next(int.MinValue + 1, int.MaxValue - 1);
  51. }
  52. #endregion
  53. #region Accesseurs
  54. //Accesseurs
  55. /// <summary>
  56. /// L’espčce du Kor.
  57. /// </summary>
  58. public Structures.MonsterType Type
  59. {
  60. get
  61. {
  62. return this.type;
  63. }
  64. }
  65. /// <summary>
  66. /// Niveau du Kor.
  67. /// </summary>
  68. public int Level
  69. {
  70. get
  71. {
  72. return this.level;
  73. }
  74. }
  75. /// <summary>
  76. /// Santé du Kor.
  77. /// </summary>
  78. public int Hp
  79. {
  80. get
  81. {
  82. return this.hp;
  83. }
  84. set
  85. {
  86. this.hp = value;
  87. }
  88. }
  89. /// <summary>
  90. /// La vitesse du Kor.
  91. /// </summary>
  92. public int Speed
  93. {
  94. get
  95. {
  96. return this.speed;
  97. }
  98. set
  99. {
  100. this.speed = value;
  101. }
  102. }
  103. /// <summary>
  104. /// L’argent que donne le Kor.
  105. /// </summary>
  106. public int Cost
  107. {
  108. get
  109. {
  110. return this.cost;
  111. }
  112. }
  113. public int Scoremonster
  114. {
  115. get
  116. {
  117. return this.scoremonster;
  118. }
  119. }
  120. /// <summary>
  121. /// Couleur du Kor.
  122. /// </summary>
  123. public Structures.Color Color
  124. {
  125. get
  126. {
  127. return this.color;
  128. }
  129. }
  130. /// <summary>
  131. /// Identifiant unique (on l’espčre) du monstre.
  132. /// </summary>
  133. public int Id
  134. {
  135. get
  136. {
  137. return this.id;
  138. }
  139. }
  140. #endregion
  141. #region Méthodes
  142. //Méthodes
  143. public void copyTo(ref Structures.MonsterSumUp destination)
  144. {
  145. destination = new Structures.MonsterSumUp(
  146. this.type,
  147. this.level,
  148. this.hp,
  149. this.speed,
  150. this.cost,
  151. this.scoremonster,
  152. this.color
  153. );
  154. }
  155. #endregion
  156. }
  157. #endregion
  158. #region class Monster
  159. /// <summary>
  160. /// Gčre les caractéristiques d’un monstre.
  161. /// </summary>
  162. public class Monster
  163. {
  164. #region Attributs
  165. // Attributs
  166. private Structures.MonsterSumUp specs;
  167. private Vector2 position;
  168. private Point lastTarget;
  169. private IA.PathFinderNode lastNode;
  170. private int lastScaleCoef;
  171. private bool inflated, isAlive;
  172. private int pathAssign;
  173. //Attributs statiques
  174. private static Structures.MonsterSumUp[] catalog;
  175. #endregion
  176. #region Constructeur
  177. //Constructeur
  178. static Monster()
  179. {
  180. catalog = new Structures.MonsterSumUp[]
  181. {
  182. //MonsterSumUp(type, level, hp, speed, cost, score, color)
  183. new Structures.MonsterSumUp(Structures.MonsterType.Normal, 1, 400, 5, 5, 10, Structures.Color.White),
  184. new Structures.MonsterSumUp(Structures.MonsterType.Faster, 1, 5, 2, 10, 15, Structures.Color.White),
  185. new Structures.MonsterSumUp(Structures.MonsterType.Stronger, 1, 20, 1, 20, 20, Structures.Color.White),
  186. new Structures.MonsterSumUp(Structures.MonsterType.Flying, 1, 5, 2, 10, 15, Structures.Color.White),
  187. new Structures.MonsterSumUp(Structures.MonsterType.Ghost, 1, 5, 1, 5, 30, Structures.Color.White)
  188. };
  189. }
  190. public Monster(Structures.MonsterType t, Point pos, int pathAssign)
  191. {
  192. if (t == Structures.MonsterType.Normal)
  193. catalog[0].copyTo(ref specs);
  194. else if (t == Structures.MonsterType.Faster)
  195. catalog[1].copyTo(ref specs);
  196. else if (t == Structures.MonsterType.Stronger)
  197. catalog[2].copyTo(ref specs);
  198. else if (t == Structures.MonsterType.Flying)
  199. catalog[3].copyTo(ref specs);
  200. else if (t == Structures.MonsterType.Ghost)
  201. catalog[4].copyTo(ref specs);
  202. this.lastScaleCoef = 15;
  203. this.position = new Vector2(pos.X, pos.Y);
  204. this.lastTarget = new Point(0, 0);
  205. this.inflated = false;
  206. this.isAlive = true;
  207. this.pathAssign = pathAssign;
  208. }
  209. #endregion
  210. #region Accesseurs
  211. //Accesseurs
  212. /// <summary>
  213. /// Caractéristiques du Kor.
  214. /// </summary>
  215. public Structures.MonsterSumUp Specs
  216. {
  217. get
  218. {
  219. return this.specs;
  220. }
  221. }
  222. /// <summary>
  223. /// La position du Kor.
  224. /// </summary>
  225. public Point Position
  226. {
  227. get
  228. {
  229. return (new Point((int)this.position.X, (int)this.position.Y));
  230. }
  231. }
  232. public bool IsAlive
  233. {
  234. get
  235. {
  236. return isAlive;
  237. }
  238. }
  239. #endregion
  240. #region Méthodes
  241. //Méthodes
  242. /// <summary>
  243. /// Le Kor subie des dégats.
  244. /// </summary>
  245. /// <param name="dmg">Nombre de dommage subis.</param>
  246. public void hit(int dmg, Structures.Player player)
  247. {
  248. this.specs.Hp -= dmg;
  249. if (this.specs.Hp <= 0)
  250. {
  251. player.earned(this.specs.Cost);
  252. player.addscore(this.specs.Scoremonster);
  253. isAlive = false;
  254. }
  255. }
  256. /// <summary>
  257. /// Fait bouger le Kor.
  258. /// </summary>
  259. /// <param name="path">Le chemin généré par A*.</param>
  260. /// <param name="scaleCoef">Le coefficient de scale (ask le moteur graphique).</param>
  261. public void move(List<IA.PathFinderNode>[] path, int scaleCoef, Structures.Player player)
  262. {
  263. int nbPath = path.Length;
  264. //Si le chemin n'existe pas, on ne fait pas partir les monstres
  265. if (nbPath == 0 || path[0] == null)
  266. return;
  267. if (!inflated) //on exprime la position en pixel, plus en case
  268. {
  269. this.position.X = this.position.X * scaleCoef + (scaleCoef / 2);
  270. this.position.Y = this.position.Y * scaleCoef + (scaleCoef / 2);
  271. this.inflated = true;
  272. this.lastScaleCoef = scaleCoef;
  273. }
  274. else if (scaleCoef != lastScaleCoef) //la fenętre a été redimensionnée (bande de chieur !)
  275. {
  276. this.position.X = this.position.X * scaleCoef / lastScaleCoef;
  277. this.position.Y = this.position.Y * scaleCoef / lastScaleCoef;
  278. this.lastScaleCoef = scaleCoef;
  279. }
  280. //On récupčre la position vers laquelle le Kor doit se diriger. (celle aprčs l’actuelle)
  281. bool found = false;
  282. bool moved = false;
  283. foreach (IA.PathFinderNode nod in path[this.pathAssign])
  284. {
  285. if (found)
  286. {
  287. this.lastTarget.X = nod.X;
  288. this.lastTarget.Y = nod.Y;
  289. this.lastNode = nod;
  290. moved = true;
  291. break;
  292. }
  293. if (nod.X == (int)(this.position.X / scaleCoef) && nod.Y == (int)(this.position.Y / scaleCoef))
  294. found = true;
  295. }
  296. if (found && !moved) //on est arrivé
  297. {
  298. player.lost();
  299. this.isAlive = false;
  300. }
  301. if (!found && !path[this.pathAssign].Contains(this.lastNode)) //le Kor s’écarte du droit chemin ...
  302. {
  303. if (path[pathAssign] == null)
  304. this.pathAssign = 0;
  305. Vector3 minDist = new Vector3(0,0, float.MaxValue - 1);
  306. foreach (IA.PathFinderNode nod in path[this.pathAssign])
  307. {
  308. Vector3 dist = new Vector3(nod.X, nod.Y, (nod.X * scaleCoef + scaleCoef / 2 - this.position.X) * (nod.X * scaleCoef + scaleCoef / 2 - this.position.X) + (nod.Y * scaleCoef + scaleCoef / 2 - this.position.Y) * (nod.Y * scaleCoef + scaleCoef / 2 - this.position.Y));
  309. if (dist.Z < minDist.Z)
  310. minDist = dist;
  311. }
  312. this.lastTarget.X = (int)minDist.X;
  313. this.lastTarget.Y = (int)minDist.Y;
  314. }
  315. //On le fait bouger :)
  316. if (this.lastTarget.X * scaleCoef + scaleCoef / 2 - this.position.X > 0)
  317. this.position.X += (float)(this.specs.Speed * scaleCoef / 100.0);
  318. else if (this.lastTarget.X * scaleCoef + scaleCoef / 2 - this.position.X < 0)
  319. this.position.X -= (float)(this.specs.Speed * scaleCoef / 100.0);
  320. if (this.lastTarget.Y * scaleCoef + scaleCoef / 2 - this.position.Y > 0)
  321. this.position.Y += (float)(this.specs.Speed * scaleCoef / 100.0);
  322. else if (this.lastTarget.Y * scaleCoef + scaleCoef / 2 - this.position.Y < 0)
  323. this.position.Y -= (float)(this.specs.Speed * scaleCoef / 100.0);
  324. }
  325. #endregion
  326. }
  327. #endregion
  328. }