using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
namespace MusicRider
{
public class GameObject
{
public delegate void ModificationFilter(GameObject obj, GameTime gameTime);
public delegate void Proc(GameObject obj);
public static void HarmonicXVariation(GameObject obj, GameTime gameTime)
{
float x = obj.Speed.X;
float y = obj.Speed.Y;
x = (float)Math.Cos(gameTime.TotalGameTime.TotalSeconds) * 50;
obj.
Speed = new Vector2
(x, y
);
}
public static void HarmonicYVariation(GameObject obj, GameTime gameTime)
{
float x = obj.Speed.X;
float y = obj.Speed.Y;
y = (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 50;
obj.
Speed = new Vector2
(x, y
);
}
public static void HarmonicScaleVariation(GameObject obj, GameTime gameTime)
{
obj.Scale = 0.6f + (float)(Math.Cos(gameTime.TotalGameTime.TotalSeconds / 2) + 1.0f) / 4;
}
public static void RotationVariation(GameObject obj, GameTime gameTime)
{
obj.Rotation -= (float)(gameTime.ElapsedGameTime.TotalSeconds);
}
public static void LinearScaleVariation(GameObject obj, GameTime gameTime)
{
obj.Scale += (float)(1 / 60);
}
private Vector2 _position, _speed;
private Color _color;
private float _rotation;
private Texture2D _sprite;
private Boolean _enabled;
private float _scale;
private ModificationFilter _variation;
private String _tag;
private Proc _onCollision;
private double _spawnTime;
public Vector2 Position { get { return _position; } set { _position = value; } }
public Vector2 Speed { get { return _speed; } set { _speed = value; } }
public float Rotation { get { return _rotation; } set { _rotation = value; } }
public float Scale { get { return _scale; } set { _scale = value; } }
public Color Color { get { return _color; } set { _color = value; } }
public Texture2D Sprite { get { return _sprite; } set { _sprite = value; } }
public Boolean Enabled { get { return _enabled; } set { _enabled = value; } }
public ModificationFilter Variation { get { return _variation; } set { _variation = value; } }
public String Tag { get { return _tag; } set { _tag = value; } }
public Proc OnCollision { get { return _onCollision; } set { _onCollision = value; } }
public double SpawnTime { get { return _spawnTime; } set { _spawnTime = value; } }
public GameObject()
{
this.Enabled = true;
this.Color = Color.White;
this.Scale = 1.0f;
}
public void Draw(SpriteBatch batch)
{
if (this.Enabled && this.Sprite != null)
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
);
}
public void Update(GameTime gameTime)
{
if (this.Variation != null)
this.Variation.Invoke(this, gameTime);
this.
Position += new Vector2
((float)(this.
Speed.
X * gameTime.
ElapsedGameTime.
TotalSeconds),
(float)(this.Speed.Y * gameTime.ElapsedGameTime.TotalSeconds));
}
public virtual Boolean Intersects(GameObject go)
{
return (new Rectangle
((int)this.
Position.
X,
(int)this.
Position.
Y,
(int)(this.
Sprite.
Width * this.
Scale),
(int)(this.
Sprite.
Height * this.
Scale))).
Intersects(
new Rectangle
((int)go.
Position.
X,
(int)go.
Position.
Y,
(int)(go.
Sprite.
Width * go.
Scale),
(int)(go.
Sprite.
Height * go.
Scale)));
}
}
public class Player : GameObject
{
public override bool Intersects(GameObject go)
{
int x, y, w, h;
// comunque w = h e ovviamente cos(pi/4)=sin(pi/4), ma è più ordinato se x resta con w e cos
// e y con h e sin.
x = (int)(this.Position.X + (this.Sprite.Width / 2) * (1 + Math.Cos(MathHelper.PiOver4 * 3)));
y = (int)(this.Position.Y + (this.Sprite.Height / 2) * (1 - Math.Sign(MathHelper.PiOver4)));
w = (int)(this.Sprite.Width * Math.Cos(MathHelper.PiOver4));
h = (int)(this.Sprite.Height * Math.Sin(MathHelper.PiOver4));
return (new Rectangle
(x, y, w, h
)).
Intersects(
new Rectangle
((int)go.
Position.
X,
(int)go.
Position.
Y,
(int)(go.
Sprite.
Width * go.
Scale),
(int)(go.
Sprite.
Height * go.
Scale)));
}
}
//class perchè non è possibile modificare membri di una struct che è valore restituito
class FloatingText
{
public Vector2 Position;
public String Text;
public Color Color;
public double SpawnTime;
public FloatingText(Vector2 pos, String txt, Color col, double time)
{
this.Position = pos;
this.Text = txt;
this.Color = col;
this.SpawnTime = time;
}
}
[Serializable]
public class Record
{
private String _songName;
private DateTime _date;
private Int64 _score;
private Byte _difficulty;
private Int32 _numberOfNotes, _caughtNotes, _numberOfBadNotes, _caughtBadNotes;
private String _mode;
public String SongName { get { return _songName; } set { _songName = value; } }
public DateTime Date { get { return _date; } set { _date = value; } }
public Int64 Score { get { return _score; } set { _score = value; } }
public Byte Difficulty { get { return _difficulty; } set { _difficulty = value; } }
public Int32 NumberOfNotes { get { return _numberOfNotes; } set { _numberOfNotes = value; } }
public Int32 NumberOfBadNotes { get { return _numberOfBadNotes; } set { _numberOfBadNotes = value; } }
public Int32 CaughtNotes { get { return _caughtNotes; } set { _caughtNotes = value; } }
public Int32 CaughtBadNotes { get { return _caughtBadNotes; } set { _caughtBadNotes = value; } }
public String Mode { get { return _mode; } set { _mode = value; } }
public Record(String songName, DateTime date, Int64 score, Byte difficulty, Int32 totNotes,
Int32 numNotes, Int32 totBadNotes, Int32 numBadNotes, String mode)
{
this.SongName = songName;
this.Date = date;
this.Score = score;
this.Difficulty = difficulty;
this.NumberOfNotes = totNotes;
this.NumberOfBadNotes = totBadNotes;
this.CaughtNotes = numNotes;
this.CaughtBadNotes = numBadNotes;
this.Mode = mode;
}
}
[Serializable]
class ScoreTable : List<Record>
{
public new void Add
(Record item
)
{
foreach (Record r in this)
{
if ((r.SongName == item.SongName) && (r.Difficulty == item.Difficulty) && (r.Mode == item.Mode))
{
if (item.Score <= r.Score)
return;
r.Score = item.Score;
r.Date = item.Date;
r.CaughtBadNotes = item.CaughtBadNotes;
r.CaughtNotes = item.CaughtNotes;
r.NumberOfBadNotes = item.NumberOfBadNotes;
r.NumberOfNotes = item.NumberOfNotes;
return;
}
}
base.Add(item);
}
public static ScoreTable Load(String path)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer =
new System.Runtime.Serialization.Formatters.Binary.
BinaryFormatter();
try
{
System.IO.
FileStream stream
= new System.IO.
FileStream(path,
System.IO.
FileMode.
Open);
ScoreTable result;
result = serializer.Deserialize(stream) as ScoreTable;
stream.Close();
serializer = null;
return result;
}
catch
{
}
finally
{
serializer = null;
}
return null;
}
public void Save(String path)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer =
new System.Runtime.Serialization.Formatters.Binary.
BinaryFormatter();
System.IO.
FileStream stream
= new System.IO.
FileStream(path,
System.IO.
FileMode.
Create);
serializer.Serialize(stream, this);
stream.Close();
serializer = null;
}
public void GenerateHtmlTable(String path)
{
StreamWriter writer
= new StreamWriter
(path
);
bool odd = false;
writer.WriteLine("<html>");
writer.WriteLine("<head>");
writer.WriteLine("<title>Music Rider's Highscores Table</title>");
writer.WriteLine("<link rel=\"stylesheet\" type=\"text/css\" href=\"records.css\">");
writer.WriteLine("</head>");
writer.WriteLine("<body>");
writer.WriteLine("<table id=\"main_table\">");
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>");
foreach (Record r in this)
{
writer.WriteLine("<tr id=\"{0}\">", odd ? "odd" : "even");
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>",
r.SongName, r.Score, r.CaughtNotes, r.NumberOfNotes, r.CaughtBadNotes, r.NumberOfBadNotes,
r.Date.ToShortDateString(), r.Date.ToShortTimeString(),
MusicRiderGame.GetDifficulty(r.Difficulty), r.Mode);
writer.WriteLine("</tr>");
odd = !odd;
}
writer.WriteLine("</table>");
writer.WriteLine("</body></html>");
writer.Close();
}
}
}