DateTime _lastTick = DateTime.Now; // when did we last tell the sprites that time had passed? #endregion Fields #region Constructors public SpaceControl() { DoubleBuffered = true; // keeps redraws and screen refresh from happening at the same time InitializeComponent(); // standard Windows Forms control init function Ship ship = new Ship(); // create the player's ship ship.Position = new Vector2F(100, 100); aiship = new AIShip(); aiship.Position = new Vector2F(800, 100); Planet planet = new Planet(); // create the big Planet planet.Position = new Vector2F(400, 300); float orbit = 200; // how far from the planet's center are the targets? List<Planet> targets = new List<Planet>(); for (int i = 0; i < NumTargets; ++i) { // Create a target. We're using "Planet", but we could use Sprite or a custom Target class. Planet target = new Planet(); target.ImageFileName = "Images/FlatTarget.gif"; // standard target art target.Space = this; // add the target to the simulation // Divide the targets uniformly around the planet. double angle = i * 2 * Math.PI / NumTargets; Vector2F offset = new Vector2F(Math.Cos(angle), Math.Sin(angle)); target.Position = planet.Position + offset * orbit; } // Add planet, AIShip and ship to simulation. planet.Space = this; ship.Space = this; aiship.Space = this; }
internal bool IsCollidingWith(Sprite s2) { // A Sprite colliding with itself is ignored. if (this == s2) return false; // If a sprite hits something that doesn't exists, ignore. if (s2._Image == null || this._Image == null) return false; if (this is Missile) { // Ignore collisions with ships. We are interested in missiles and planets only. if ((s2 is AIShip) || (s2 is Ship)) return false; } else return false; Vector2F offset = this.Position - s2.Position; double my_radius = Math.Sqrt(_Image.Width * _Image.Width + _Image.Height * _Image.Height)/2; double s2_radius = Math.Sqrt(s2._Image.Width * s2._Image.Width + s2._Image.Height * s2._Image.Height)/2; if (offset.Length > my_radius + s2_radius) return false; Bitmap b = new Bitmap(_Image.Width, _Image.Height); Graphics g = Graphics.FromImage(b); Matrix s2_to_world = s2.TransformLocalToWorld; Matrix world_to_local = TransformLocalToWorld; world_to_local.Invert(); Matrix s2_to_local = world_to_local; s2_to_local.Multiply(s2_to_world); g.Transform = s2_to_local; Vector2F draw_location = new Vector2F(0,0); g.DrawImage(s2._Image, draw_location); for (int x = 0; x < b.Width; ++x) for (int y = 0; y < b.Height; ++y) { Color mine = _Bitmap.GetPixel(x, y); Color theirs = b.GetPixel(x, y); if (mine.A > 0.5 && theirs.A > 0.5) return true; } return false; }
public void Paint(Graphics g) { if (null != _Image) { g.Transform = TransformLocalToWorld; // Add the image. Vector2F draw_location = new Vector2F(0, 0); g.DrawImage(_Image, draw_location); } }