/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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
-
- namespace eradiKor.Structures
- {
- #region enum MonsterType
- /// <summary>
- /// Types de monstres, ie : Flying, Kornu, ...
- /// </summary>
- public enum MonsterType
- {
- Normal,
- Stronger,
- Faster,
- Flying,
- Ghost
- }
- #endregion
-
- #region struct MonsterSumUp
- public struct MonsterSumUp
- {
- #region Attributs
- //Attributs
-
- private Structures.MonsterType type;
- private int level;
- private int hp;
- private int speed;
- private int cost;
- private int scoremonster;
- private Structures.Color color;
- private int id;
- #endregion
-
- #region Constructeur
- //Constructeur
-
- public MonsterSumUp(Structures.MonsterType t, int lvl, int health, int spd, int money, int scoremon, Structures.Color col)
- {
- this.type = t;
- this.level = lvl;
- this.hp = health;
- this.speed = spd;
- this.cost = money;
- this.scoremonster = scoremon;
- this.color = col;
-
- //génčre lid en espérant quil sera unique ... :-°
- this.id = 0;
- Random rand = new Random();
- while (this.id == 0)
- this.id = rand.Next(int.MinValue + 1, int.MaxValue - 1);
- }
-
- #endregion
-
- #region Accesseurs
- //Accesseurs
-
- /// <summary>
- /// Lespčce du Kor.
- /// </summary>
- public Structures.MonsterType Type
- {
- get
- {
- return this.type;
- }
- }
-
- /// <summary>
- /// Niveau du Kor.
- /// </summary>
- public int Level
- {
- get
- {
- return this.level;
- }
- }
-
- /// <summary>
- /// Santé du Kor.
- /// </summary>
- public int Hp
- {
- get
- {
- return this.hp;
- }
- set
- {
- this.hp = value;
- }
- }
-
- /// <summary>
- /// La vitesse du Kor.
- /// </summary>
- public int Speed
- {
- get
- {
- return this.speed;
- }
- set
- {
- this.speed = value;
- }
- }
-
- /// <summary>
- /// Largent que donne le Kor.
- /// </summary>
- public int Cost
- {
- get
- {
- return this.cost;
- }
- }
-
- public int Scoremonster
- {
- get
- {
- return this.scoremonster;
- }
- }
-
-
- /// <summary>
- /// Couleur du Kor.
- /// </summary>
- public Structures.Color Color
- {
- get
- {
- return this.color;
- }
- }
-
- /// <summary>
- /// Identifiant unique (on lespčre) du monstre.
- /// </summary>
- public int Id
- {
- get
- {
- return this.id;
- }
- }
- #endregion
-
- #region Méthodes
- //Méthodes
-
- public void copyTo(ref Structures.MonsterSumUp destination)
- {
- destination = new Structures.MonsterSumUp(
- this.type,
- this.level,
- this.hp,
- this.speed,
- this.cost,
- this.scoremonster,
- this.color
- );
- }
- #endregion
- }
- #endregion
-
- #region class Monster
- /// <summary>
- /// Gčre les caractéristiques dun monstre.
- /// </summary>
- public class Monster
- {
- #region Attributs
- // Attributs
-
- private Structures.MonsterSumUp specs;
- private Vector2 position;
- private Point lastTarget;
- private IA.PathFinderNode lastNode;
- private int lastScaleCoef;
- private bool inflated, isAlive;
- private int pathAssign;
-
- //Attributs statiques
- private static Structures.MonsterSumUp[] catalog;
-
- #endregion
-
- #region Constructeur
- //Constructeur
-
- static Monster()
- {
- catalog = new Structures.MonsterSumUp[]
- {
- //MonsterSumUp(type, level, hp, speed, cost, score, color)
- new Structures.MonsterSumUp(Structures.MonsterType.Normal, 1, 400, 5, 5, 10, Structures.Color.White),
- new Structures.MonsterSumUp(Structures.MonsterType.Faster, 1, 5, 2, 10, 15, Structures.Color.White),
- new Structures.MonsterSumUp(Structures.MonsterType.Stronger, 1, 20, 1, 20, 20, Structures.Color.White),
- new Structures.MonsterSumUp(Structures.MonsterType.Flying, 1, 5, 2, 10, 15, Structures.Color.White),
- new Structures.MonsterSumUp(Structures.MonsterType.Ghost, 1, 5, 1, 5, 30, Structures.Color.White)
- };
- }
-
- public Monster(Structures.MonsterType t, Point pos, int pathAssign)
- {
- if (t == Structures.MonsterType.Normal)
- catalog[0].copyTo(ref specs);
- else if (t == Structures.MonsterType.Faster)
- catalog[1].copyTo(ref specs);
- else if (t == Structures.MonsterType.Stronger)
- catalog[2].copyTo(ref specs);
- else if (t == Structures.MonsterType.Flying)
- catalog[3].copyTo(ref specs);
- else if (t == Structures.MonsterType.Ghost)
- catalog[4].copyTo(ref specs);
-
- this.lastScaleCoef = 15;
- this.position = new Vector2(pos.X, pos.Y);
- this.lastTarget = new Point(0, 0);
- this.inflated = false;
- this.isAlive = true;
- this.pathAssign = pathAssign;
- }
-
-
- #endregion
-
- #region Accesseurs
- //Accesseurs
-
- /// <summary>
- /// Caractéristiques du Kor.
- /// </summary>
- public Structures.MonsterSumUp Specs
- {
- get
- {
- return this.specs;
- }
- }
-
- /// <summary>
- /// La position du Kor.
- /// </summary>
- public Point Position
- {
- get
- {
- return (new Point((int)this.position.X, (int)this.position.Y));
- }
- }
-
- public bool IsAlive
- {
- get
- {
- return isAlive;
- }
- }
-
-
- #endregion
-
- #region Méthodes
- //Méthodes
-
- /// <summary>
- /// Le Kor subie des dégats.
- /// </summary>
- /// <param name="dmg">Nombre de dommage subis.</param>
- public void hit(int dmg, Structures.Player player)
- {
- this.specs.Hp -= dmg;
- if (this.specs.Hp <= 0)
- {
- player.earned(this.specs.Cost);
- player.addscore(this.specs.Scoremonster);
- isAlive = false;
- }
- }
-
- /// <summary>
- /// Fait bouger le Kor.
- /// </summary>
- /// <param name="path">Le chemin généré par A*.</param>
- /// <param name="scaleCoef">Le coefficient de scale (ask le moteur graphique).</param>
- public void move(List<IA.PathFinderNode>[] path, int scaleCoef, Structures.Player player)
- {
- int nbPath = path.Length;
-
- //Si le chemin n'existe pas, on ne fait pas partir les monstres
- if (nbPath == 0 || path[0] == null)
- return;
-
- if (!inflated) //on exprime la position en pixel, plus en case
- {
- this.position.X = this.position.X * scaleCoef + (scaleCoef / 2);
- this.position.Y = this.position.Y * scaleCoef + (scaleCoef / 2);
- this.inflated = true;
- this.lastScaleCoef = scaleCoef;
- }
- else if (scaleCoef != lastScaleCoef) //la fenętre a été redimensionnée (bande de chieur !)
- {
- this.position.X = this.position.X * scaleCoef / lastScaleCoef;
- this.position.Y = this.position.Y * scaleCoef / lastScaleCoef;
- this.lastScaleCoef = scaleCoef;
- }
-
- //On récupčre la position vers laquelle le Kor doit se diriger. (celle aprčs lactuelle)
- bool found = false;
- bool moved = false;
- foreach (IA.PathFinderNode nod in path[this.pathAssign])
- {
- if (found)
- {
- this.lastTarget.X = nod.X;
- this.lastTarget.Y = nod.Y;
- this.lastNode = nod;
- moved = true;
- break;
- }
-
- if (nod.X == (int)(this.position.X / scaleCoef) && nod.Y == (int)(this.position.Y / scaleCoef))
- found = true;
- }
-
- if (found && !moved) //on est arrivé
- {
- player.lost();
- this.isAlive = false;
- }
-
- if (!found && !path[this.pathAssign].Contains(this.lastNode)) //le Kor sécarte du droit chemin ...
- {
- if (path[pathAssign] == null)
- this.pathAssign = 0;
-
- Vector3 minDist = new Vector3(0,0, float.MaxValue - 1);
-
- foreach (IA.PathFinderNode nod in path[this.pathAssign])
- {
- 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));
- if (dist.Z < minDist.Z)
- minDist = dist;
- }
-
- this.lastTarget.X = (int)minDist.X;
- this.lastTarget.Y = (int)minDist.Y;
- }
-
- //On le fait bouger :)
- if (this.lastTarget.X * scaleCoef + scaleCoef / 2 - this.position.X > 0)
- this.position.X += (float)(this.specs.Speed * scaleCoef / 100.0);
- else if (this.lastTarget.X * scaleCoef + scaleCoef / 2 - this.position.X < 0)
- this.position.X -= (float)(this.specs.Speed * scaleCoef / 100.0);
-
- if (this.lastTarget.Y * scaleCoef + scaleCoef / 2 - this.position.Y > 0)
- this.position.Y += (float)(this.specs.Speed * scaleCoef / 100.0);
- else if (this.lastTarget.Y * scaleCoef + scaleCoef / 2 - this.position.Y < 0)
- this.position.Y -= (float)(this.specs.Speed * scaleCoef / 100.0);
- }
- #endregion
- }
- #endregion
- }