Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
MusicRider - GameObjects.cs

GameObjects.cs

Caricato da: Totem
Scarica il programma completo

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using System.IO;
  7.  
  8. namespace MusicRider
  9. {
  10.     public class GameObject
  11.     {
  12.         public delegate void ModificationFilter(GameObject obj, GameTime gameTime);
  13.         public delegate void Proc(GameObject obj);
  14.  
  15.         public static void HarmonicXVariation(GameObject obj, GameTime gameTime)
  16.         {
  17.             float x = obj.Speed.X;
  18.             float y = obj.Speed.Y;
  19.             x = (float)Math.Cos(gameTime.TotalGameTime.TotalSeconds) * 50;
  20.             obj.Speed = new Vector2(x, y);
  21.         }
  22.  
  23.         public static void HarmonicYVariation(GameObject obj, GameTime gameTime)
  24.         {
  25.             float x = obj.Speed.X;
  26.             float y = obj.Speed.Y;
  27.             y = (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 50;
  28.             obj.Speed = new Vector2(x, y);
  29.         }
  30.  
  31.         public static void HarmonicScaleVariation(GameObject obj, GameTime gameTime)
  32.         {
  33.             obj.Scale = 0.6f + (float)(Math.Cos(gameTime.TotalGameTime.TotalSeconds / 2) + 1.0f) / 4;
  34.         }
  35.  
  36.         public static void RotationVariation(GameObject obj, GameTime gameTime)
  37.         {
  38.             obj.Rotation -= (float)(gameTime.ElapsedGameTime.TotalSeconds);
  39.         }
  40.  
  41.         public static void LinearScaleVariation(GameObject obj, GameTime gameTime)
  42.         {
  43.             obj.Scale += (float)(1 / 60);
  44.         }
  45.  
  46.         private Vector2 _position, _speed;
  47.         private Color _color;
  48.         private float _rotation;
  49.         private Texture2D _sprite;
  50.         private Boolean _enabled;
  51.         private float _scale;
  52.         private ModificationFilter _variation;
  53.         private String _tag;
  54.         private Proc _onCollision;
  55.         private double _spawnTime;
  56.  
  57.         public Vector2 Position { get { return _position; } set { _position = value; } }
  58.         public Vector2 Speed { get { return _speed; } set { _speed = value; } }
  59.         public float Rotation { get { return _rotation; } set { _rotation = value; } }
  60.         public float Scale { get { return _scale; } set { _scale = value; } }
  61.         public Color Color { get { return _color; } set { _color = value; } }
  62.         public Texture2D Sprite { get { return _sprite; } set { _sprite = value; } }
  63.         public Boolean Enabled { get { return _enabled; } set { _enabled = value; }  }
  64.         public ModificationFilter Variation { get { return _variation; } set { _variation = value; } }
  65.         public String Tag { get { return _tag; } set { _tag = value; } }
  66.         public Proc OnCollision { get { return _onCollision; } set { _onCollision = value; } }
  67.         public double SpawnTime { get { return _spawnTime; } set { _spawnTime = value; } }
  68.  
  69.         public GameObject()
  70.         {
  71.             this.Enabled = true;
  72.             this.Color = Color.White;
  73.             this.Scale = 1.0f;
  74.         }
  75.  
  76.         public void Draw(SpriteBatch batch)
  77.         {
  78.             if (this.Enabled && this.Sprite != null)
  79.                 batch.Draw(this.Sprite, new Rectangle((int)this.Position.X, (int)this.Position.Y, (int)(this.Sprite.Width * this.Scale), (int)(this.Sprite.Height * this.Scale)), null, this.Color, this.Rotation, new Vector2(this.Sprite.Width / 2, this.Sprite.Height / 2), SpriteEffects.None, 0);
  80.         }
  81.  
  82.         public void Update(GameTime gameTime)
  83.         {
  84.             if (this.Variation != null)
  85.                 this.Variation.Invoke(this, gameTime);
  86.             this.Position += new Vector2((float)(this.Speed.X * gameTime.ElapsedGameTime.TotalSeconds),
  87.                 (float)(this.Speed.Y * gameTime.ElapsedGameTime.TotalSeconds));
  88.         }
  89.  
  90.         public virtual Boolean Intersects(GameObject go)
  91.         {
  92.             return (new Rectangle((int)this.Position.X, (int)this.Position.Y, (int)(this.Sprite.Width * this.Scale), (int)(this.Sprite.Height * this.Scale))).Intersects(
  93.                 new Rectangle((int)go.Position.X, (int)go.Position.Y, (int)(go.Sprite.Width * go.Scale), (int)(go.Sprite.Height * go.Scale)));
  94.         }
  95.     }
  96.  
  97.     public class Player : GameObject
  98.     {
  99.         public override bool Intersects(GameObject go)
  100.         {
  101.             int x, y, w, h;
  102.             // comunque w = h e ovviamente cos(pi/4)=sin(pi/4), ma è più ordinato se x resta con w e cos
  103.             // e y con h e sin.
  104.             x = (int)(this.Position.X + (this.Sprite.Width / 2) * (1 + Math.Cos(MathHelper.PiOver4 * 3)));
  105.             y = (int)(this.Position.Y + (this.Sprite.Height / 2) * (1 - Math.Sign(MathHelper.PiOver4)));
  106.             w = (int)(this.Sprite.Width * Math.Cos(MathHelper.PiOver4));
  107.             h = (int)(this.Sprite.Height * Math.Sin(MathHelper.PiOver4));
  108.             return (new Rectangle(x, y, w, h)).Intersects(
  109.                 new Rectangle((int)go.Position.X, (int)go.Position.Y, (int)(go.Sprite.Width * go.Scale), (int)(go.Sprite.Height * go.Scale)));
  110.         }
  111.     }
  112.  
  113.         //class perchè non è possibile modificare membri di una struct che è valore restituito
  114.     class FloatingText
  115.     {
  116.         public Vector2 Position;
  117.         public String Text;
  118.         public Color Color;
  119.         public double SpawnTime;
  120.         public FloatingText(Vector2 pos, String txt, Color col, double time)
  121.         {
  122.             this.Position = pos;
  123.             this.Text = txt;
  124.             this.Color = col;
  125.             this.SpawnTime = time;
  126.          }
  127.     }
  128.  
  129.     [Serializable]
  130.     public class Record
  131.     {
  132.         private String _songName;
  133.         private DateTime _date;
  134.         private Int64 _score;
  135.         private Byte _difficulty;
  136.         private Int32 _numberOfNotes, _caughtNotes, _numberOfBadNotes, _caughtBadNotes;
  137.         private String _mode;
  138.  
  139.         public String SongName { get { return _songName; } set { _songName = value; } }
  140.         public DateTime Date { get { return _date; } set { _date = value; } }
  141.         public Int64 Score { get { return _score; } set { _score = value; } }
  142.         public Byte Difficulty { get { return _difficulty; } set { _difficulty = value; } }
  143.         public Int32 NumberOfNotes { get { return _numberOfNotes; } set { _numberOfNotes = value; } }
  144.         public Int32 NumberOfBadNotes { get { return _numberOfBadNotes; } set { _numberOfBadNotes = value; } }
  145.         public Int32 CaughtNotes { get { return _caughtNotes; } set { _caughtNotes = value; } }
  146.         public Int32 CaughtBadNotes { get { return _caughtBadNotes; } set { _caughtBadNotes = value; } }
  147.         public String Mode { get { return _mode; } set { _mode = value; } }
  148.  
  149.         public Record(String songName, DateTime date, Int64 score, Byte difficulty, Int32 totNotes,
  150.             Int32 numNotes, Int32 totBadNotes, Int32 numBadNotes, String mode)
  151.         {
  152.             this.SongName = songName;
  153.             this.Date = date;
  154.             this.Score = score;
  155.             this.Difficulty = difficulty;
  156.             this.NumberOfNotes = totNotes;
  157.             this.NumberOfBadNotes = totBadNotes;
  158.             this.CaughtNotes = numNotes;
  159.             this.CaughtBadNotes = numBadNotes;
  160.             this.Mode = mode;
  161.         }
  162.     }
  163.  
  164.     [Serializable]
  165.     class ScoreTable : List<Record>
  166.     {
  167.         public new void Add(Record item)
  168.         {
  169.             foreach (Record r in this)
  170.             {
  171.                 if ((r.SongName == item.SongName) && (r.Difficulty == item.Difficulty) && (r.Mode == item.Mode))
  172.                 {
  173.                     if (item.Score <= r.Score)
  174.                         return;
  175.                     r.Score = item.Score;
  176.                     r.Date = item.Date;
  177.                     r.CaughtBadNotes = item.CaughtBadNotes;
  178.                     r.CaughtNotes = item.CaughtNotes;
  179.                     r.NumberOfBadNotes = item.NumberOfBadNotes;
  180.                     r.NumberOfNotes = item.NumberOfNotes;
  181.                     return;
  182.                 }
  183.             }
  184.             base.Add(item);
  185.         }
  186.  
  187.         public static ScoreTable Load(String path)
  188.         {
  189.             System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer =
  190.                 new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  191.             try
  192.             {
  193.                 System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open);
  194.                 ScoreTable result;
  195.                 result = serializer.Deserialize(stream) as ScoreTable;
  196.                 stream.Close();
  197.                 serializer = null;
  198.                 return result;
  199.             }
  200.             catch
  201.             {
  202.             }
  203.             finally
  204.             {
  205.                 serializer = null;
  206.             }
  207.             return null;
  208.         }
  209.  
  210.         public void Save(String path)
  211.         {
  212.             System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer =
  213.                 new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  214.             System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Create);
  215.             serializer.Serialize(stream, this);
  216.             stream.Close();
  217.             serializer = null;
  218.         }
  219.  
  220.         public void GenerateHtmlTable(String path)
  221.         {
  222.             StreamWriter writer = new StreamWriter(path);
  223.             bool odd = false;
  224.             writer.WriteLine("<html>");
  225.             writer.WriteLine("<head>");
  226.             writer.WriteLine("<title>Music Rider's Highscores Table</title>");
  227.             writer.WriteLine("<link rel=\"stylesheet\" type=\"text/css\" href=\"records.css\">");
  228.             writer.WriteLine("</head>");
  229.             writer.WriteLine("<body>");
  230.             writer.WriteLine("<table id=\"main_table\">");
  231.             writer.WriteLine("<tr id=\"header\"><td>Brano</td> <td>Punteggio</td> <td>Note prese</td> <td>Note rosse prese</td> <td>Data</td> <td>Gameplay</td> <td>Modalità</td></tr>");
  232.             foreach (Record r in this)
  233.             {
  234.                 writer.WriteLine("<tr id=\"{0}\">", odd ? "odd" : "even");
  235.                 writer.WriteLine("<td>{0}</td> <td>{1}</td> <td>{2} / {3}</td> <td>{4} / {5}</td> <td>{6} (ore {7})</td> <td>{8}</td> <td>{9}</td>",
  236.                     r.SongName, r.Score, r.CaughtNotes, r.NumberOfNotes, r.CaughtBadNotes, r.NumberOfBadNotes,
  237.                     r.Date.ToShortDateString(), r.Date.ToShortTimeString(),
  238.                     MusicRiderGame.GetDifficulty(r.Difficulty), r.Mode);
  239.                 writer.WriteLine("</tr>");
  240.                 odd = !odd;
  241.             }
  242.             writer.WriteLine("</table>");
  243.             writer.WriteLine("</body></html>");
  244.             writer.Close();
  245.         }
  246.     }
  247. }