Ejemplo n.º 1
0
 public void TestEmptySprite()
 {
     var s = new Sprite();
     Assert.That(s.image == null);
     s.image = null;
     Assert.That(s.Id == 1);
     s = new Sprite();
     Assert.That(!s.IsValid());
     Assert.That(s.Id == 2);
 }
Ejemplo n.º 2
0
        public TheInput(Form parent)
        {
            this.DoubleBuffered = true;

            Parent = parent;
            Dock = DockStyle.Fill;

            try {
                imageCross = new Bitmap("../../cross.png");
                images = new List<Bitmap>();
                images.Add(new Bitmap("../../spark.png"));
                images.Add(new Bitmap("../../katya-head.png"));
                images.Add(new Bitmap("../../nastya-head.png"));
            } catch (Exception e) {
                Console.WriteLine("cannot load images:" + e.Message);
                Environment.Exit(1);
            }
            t0 = DateTime.Now;
            sprites = new List<Sprite>();
            curimage = 0;
            prototype = new Sprite(images[curimage], 0.0F);
            random = new Random();

            KeyUp += new KeyEventHandler(this.OnKeyUp);
            MouseDown += new MouseEventHandler(this.OnMouseDown);
            MouseUp += new MouseEventHandler(this.OnMouseUp);
            MouseMove += new MouseEventHandler(this.OnMouseMove);

            timer = new Timer(new Container());
            timer.Interval = 50;
            timer.Enabled = true;
            timer.Tick += new EventHandler(this.OnTick);

            Paint += new PaintEventHandler(this.OnPaint);
        }
Ejemplo n.º 3
0
 void OnMouseUp(object sender, MouseEventArgs a)
 {
     if (a.Button == MouseButtons.Left && !mouseDownLocation.IsEmpty) {
         U.show("OnMouseUp");
         var s = new Sprite(images[curimage], 0.0F);
         s.Position = new Vec(mouseDownLocation);
         Vec spd = new Vec(a.Location).Subtract(s.Position).Scale(0.4);
         var speedlen = spd.Length;
         if (speedlen > U.maxspeed) {
             spd = spd.Scale(U.maxspeed/speedlen);
         } else if (speedlen < U.minspeed) {
             // make random speed
             double alpha = Math.PI * 2.0 * random.NextDouble();
             speedlen = (random.NextDouble() * (U.maxspeed-U.minspeed) + U.minspeed);
             spd = Vec.Dir(alpha).Scale(speedlen);
         }
         s.Speed = spd;
         s.Rotation = (float)(random.NextDouble() - 0.5) * 10.0F;
         sprites.Add(s);
         mouseDownLocation = new Point();
         mouseLastLocation = mouseDownLocation;
     } else if (a.Button == MouseButtons.Right) {
         curimage += 1;
         if (curimage >= images.Count) {
             curimage = 0;
         }
         prototype = new Sprite(images[curimage], 0.0F);
     }
 }
Ejemplo n.º 4
0
 void OnKeyUp(object sender, KeyEventArgs a)
 {
     U.show("OnKeyUp");
     if (a.KeyCode == Keys.Escape) {
         U.show("Ended");
         Environment.Exit(0);
     } else if (a.KeyCode == Keys.Left) {
         curimage -= 1;
         if (curimage < 0) {
             curimage = images.Count - 1;
         }
         prototype = new Sprite(images[curimage], 0.0F);
     } else if (a.KeyCode == Keys.Right) {
         curimage += 1;
         if (curimage >= images.Count) {
             curimage = 0;
         }
         prototype = new Sprite(images[curimage], 0.0F);
     } else if (a.KeyCode == Keys.Back) {
         sprites.Clear();
     }
 }
Ejemplo n.º 5
0
 private List<Rib> applyCollision(Collision first, Rib rib, double dt)
 {
     double time = first.GetTime(rib);
     Collision second = rib.Collision;
     Rib self = first.SelfRib;
     // remove ribs from both collisions
     first.Remove(rib);
     if (self.Sprite.image == rib.Sprite.image) {
         // the collision occurs b/w the same type of objects
         // remove all ribs corresponding to this sprite
         ribs.RemoveAll(r => r.Sprite == rib.Sprite);
         ribs.RemoveAll(r => r.Sprite == self.Sprite);
         // scan through all other ribs in the collections and remove
         first.RemoveAll();
         second.RemoveAll();
         rib.SetEndTime(time);
         self.SetEndTime(time);
         // move sprites
         rib.Sprite.Update(rib);
         self.Sprite.Update(self);
         // create a new rib
         Sprite sprite = new Sprite(self.Sprite.image, self.Sprite, rib.Sprite);
         List<Rib> moreribs = new List<Rib>();
         sprite.MakeMove(time, dt, moreribs);
         return moreribs;
     } else {
         return null;
     }
 }
Ejemplo n.º 6
0
 public Sprite(Bitmap theimg, Sprite one, Sprite two)
     : this(theimg, one.mass + two.mass)
 {
     // now calculate the position, speed and rotation
     double invertmass = 1.0/mass;
     Position = one.Position.Scale(one.Mass).Add(two.Position.Scale(two.Mass)).Scale(invertmass);
     Speed = one.Speed.Scale(one.Mass).Add(two.Speed.Scale(two.mass)).Scale(invertmass);
     alpha = (Math.IEEERemainder(one.alpha,Math.PI*2) * one.AngularMass + Math.IEEERemainder(two.alpha,Math.PI*2) * two.AngularMass) / AngularMass;
     // sum up all moments of rotation
     double totalRotationMomentum =
         one.Mass * one.Position.Subtract(Position).Normal(one.Speed).Length +
         two.Mass * two.Position.Subtract(Position).Normal(two.Speed).Length +
         one.AngularMass * one.omega + two.AngularMass * two.omega;
     omega = totalRotationMomentum / AngularMass;
 }