public StarlingGameSpriteWithTestGamePad()
        {
            var textures_ped = new StarlingGameSpriteWithPedTextures(new_tex_crop, this.new_texsprite_crop);
            var textures_tank = new StarlingGameSpriteWithTankTextures(this.new_texsprite_crop);
            var textures_hind = new StarlingGameSpriteWithHindTextures(this.new_tex_crop);
            var textures_jeep = new StarlingGameSpriteWithJeepTextures(new_tex_crop);
            var textures_cannon = new StarlingGameSpriteWithCannonTextures(new_tex_crop);
            var textures_bunker = new StarlingGameSpriteWithBunkerTextures(new_tex_crop);

            disablephysicsdiagnostics = true;
            disable_movezoom_and_altitude_for_scale = true;

            internalscale = 1.7;
            internal_center_y = 0.5;

            this.onbeforefirstframe += (stage, s) =>
            {

                #region __keyDown

                stage.keyDown +=
                   e =>
                   {
                       // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                       if (e.altKey)
                           __keyDown[Keys.Alt] = true;

                       __keyDown[(Keys)e.keyCode] = true;
                   };

                stage.keyUp +=
                 e =>
                 {
                     if (!e.altKey)
                         __keyDown[Keys.Alt] = false;

                     __keyDown[(Keys)e.keyCode] = false;
                 };

                #endregion


                var tank1 = new PhysicalTank(textures_tank, this);
                tank1.SetPositionAndAngle(random.NextDouble() * 4000, 100);

                // half speed!
                //tank1.AngularVelocityMultiplier = 0.5;
                //tank1.speed = 10;

                //tank1.SetPositionAndAngle(100, 100);



                var cannon1 = new PhysicalCannon(textures_cannon, this);
                cannon1.SetPositionAndAngle(random.NextDouble() * 4000, 100);

                var physical0 = new PhysicalPed(textures_ped, this);
                physical0.SetPositionAndAngle(random.NextDouble() * 4000, 100);

                current = physical0;

                var hind0 = new PhysicalHind(textures_hind, this);
                hind0.SetPositionAndAngle(random.NextDouble() * 4000, 100);

                var jeep = new PhysicalJeep(textures_jeep, this);
                jeep.SetPositionAndAngle(random.NextDouble() * 4000, 100);

                var bunker0 = new PhysicalBunker(textures_bunker, this);
                bunker0.SetPositionAndAngle(random.NextDouble() * 4000, 100);

                var silo0 = new PhysicalSilo(textures_bunker, this);

                __switchto +=
                    type =>
                    {
                        if (type == "ped")
                            current = physical0;

                        if (type == "tank")
                            current = tank1;

                        if (type == "hind")
                            current = hind0;

                        if (type == "jeep")
                            current = jeep;

                        if (type == "cannon")
                            current = cannon1;

                        if (type == "bunker")
                            current = bunker0;

                        if (type == "silo")
                            current = silo0;
                    };

                onsyncframe += delegate
                {

                    current.SetVelocityFromInput(__keyDown);


                    while (Content_layer0_tracks.numChildren > 5)
                    {
                        Content_layer0_tracks.removeChildAt(0);
                    }

                };
            };
        }
        public StarlingGameSpriteBeta2()
        {
            var textures_beta = new_tex96(new BetaBanner());

            //var textures_map = new StarlingGameSpriteWithMapTextures(new_tex_crop);

            //var textures_ped = new StarlingGameSpriteWithPedTextures(this.new_tex_crop);
            //var textures_jeep = new StarlingGameSpriteWithJeepTextures(this.new_tex_crop);

            //var textures_hind = new StarlingGameSpriteWithHindTextures(this.new_tex_crop);
            //var textures_tank = new StarlingGameSpriteWithTankTextures(this.new_tex_crop);
            //var textures_cannon = new StarlingGameSpriteWithCannonTextures(this.new_tex_crop);
            //var textures_bunker = new StarlingGameSpriteWithBunkerTextures(this.new_tex_crop);

            this.disablephysicsdiagnostics = true;
            this.disable_enter_and_space = true;

            this.onbeforefirstframe += (stage, s) =>
             {

                 #region other
                 Func<string, RemoteGame> other = __egoid =>
                 {
                     // that other game has sent us a sync frame!

                     var already_known_other = others.FirstOrDefault(k => k.__egoid == __egoid);

                     if (already_known_other == null)
                     {
                         already_known_other = new RemoteGame
                         {
                             __egoid = __egoid,

                             // this
                             __syncframeid = this.syncframeid
                         };

                         others.Add(already_known_other);
                     }


                     return already_known_other;
                 };
                 #endregion


                 #region :ego
                 var ego = this.current as PhysicalPed;

                 ego.Identity = sessionid + ":ego";

                 ego.SetPositionAndAngle(
                     random.NextDouble() * -8,
                     random.NextDouble() * -8,
                     random.NextDouble() * Math.PI
                 );

                 current = ego;
                 #endregion


                 #region __at_sync
                 __at_sync += __egoid =>
                 {
                     // that other game has sent us a sync frame!

                     var o = other(__egoid);

                     o.__syncframeid++;
                     // move on!
                 };
                 #endregion

                 #region __at_SetVerticalVelocity
                 __at_SetVerticalVelocity +=
                     (string __sessionid, string identity, string value) =>
                     {
                         var o = other(__sessionid);

                         var u = this.units.FirstOrDefault(k => k.Identity == identity);

                         if (u == ego)
                         {
                             // discard, only we are allowed to move
                             // ourselves for now
                             return;
                         }

                         (u as PhysicalHind).With(hind1 => hind1.VerticalVelocity = double.Parse(value));

                         (u as PhysicalPed).With(
                               physical0 =>
                               {
                                   //                                  BCL needs another method, please define it.
                                   //Cannot call type without script attribute :
                                   //System.Convert for Boolean ToBoolean(Double) used at

                                   var LayOnTheGround = double.Parse(value);

                                   if (LayOnTheGround == 1)
                                       physical0.visual.LayOnTheGround = true;
                                   else
                                       physical0.visual.LayOnTheGround = false;




                               }
                           );

                     };
                 #endregion

                 #region __at_SetVelocityFromInput
                 __at_SetVelocityFromInput +=
                     (
                         string __egoid,
                         string __identity,
                         string __KeySample,
                         string __fixup_x,
                         string __fixup_y,
                         string __fixup_angle

                         ) =>
                     {
                         var o = other(__egoid);

                         #region u PhysicalPed
                         var u = this.units.FirstOrDefault(k => k.Identity == __identity);

                         if (u == null)
                             if (o.ego == null)
                             {
                                 // the only object we can be creating implicitly is
                                 // the remote ego

                                 u = new PhysicalPed(textures_ped, this)
                                 {
                                     Identity = __identity,
                                     RemoteGameReference = o
                                 };

                                 u.SetPositionAndAngle(
                                     double.Parse(__fixup_x),
                                     double.Parse(__fixup_y),
                                     double.Parse(__fixup_angle)
                                 );

                                 o.ego = u;
                             }
                         #endregion

                         if (u == ego)
                         {
                             // discard, only we are allowed to move
                             // ourselves for now
                             return;
                         }

                         // set the input!


                         u.SetVelocityFromInput(
                             new KeySample
                             {
                                 value = int.Parse(__KeySample),

                                 fixup = true,

                                 x = double.Parse(__fixup_x),
                                 y = double.Parse(__fixup_y),
                                 angle = double.Parse(__fixup_angle)

                             }
                         );

                     };
                 #endregion


                 #region __at_enterorexit
                 __at_enterorexit += (__egoid, __from, __to) =>
                 {
                     var o = other(__egoid);

                     var ufrom = this.units.FirstOrDefault(k => k.Identity == __from);
                     var uto = this.units.FirstOrDefault(k => k.Identity == __to);

                     (ufrom as PhysicalPed).With(
                         candidatedriver =>
                         {
                             if (uto != null)
                                 if (uto.driverseat != null)
                                     if (uto.driverseat.driver == null)
                                     {
                                         // and the devil enters
                                         uto.RemoteGameReference = o;

                                         candidatedriver.body.SetActive(false);

                                         uto.driverseat.driver = candidatedriver;
                                         candidatedriver.seatedvehicle = uto;
                                     }
                         }
                     );

                     (uto as PhysicalPed).With(
                         driver =>
                         {
                             if (ufrom != null)
                                 if (ufrom.driverseat != null)
                                     if (ufrom.driverseat.driver == driver)
                                     {
                                         // relinguish that vehicle. no longer posessed :)
                                         ufrom.RemoteGameReference = null;

                                         // stop the vehicle
                                         ufrom.SetVelocityFromInput(new KeySample());

                                         // get out of the lift..
                                         ufrom.driverseat.driver = null;

                                         driver.seatedvehicle = null;
                                         driver.body.SetActive(true);
                                     }
                         }
                     );

                 };
                 #endregion








                 this.units.WithEachIndex(
                     (u, i) =>
                     {
                         if (u.Identity == null)
                             u.Identity = "#" + i;
                     }
                 );





                 #region beta
                 var beta = new Image(textures_beta()).AttachTo(this);

                 onframe +=
                      delegate
                      {

                          #region beta
                          {
                              var cm = new Matrix();

                              cm.translate(
                                 stage.stageWidth - 96,
                                  0);

                              beta.transformationMatrix = cm;
                          }
                          #endregion
                      };
                 #endregion


                 bool entermode_changepending = false;
                 bool mode_changepending = false;
                 onsyncframe += delegate
                 {






                     #region entermode_changepending
                     if (!__keyDown[System.Windows.Forms.Keys.Enter])
                     {
                         // space is not down.
                         entermode_changepending = true;
                     }
                     else
                     {
                         if (entermode_changepending)
                         {
                             entermode_changepending = false;

                             // enter another vehicle?

                             var candidatedriver = current as PhysicalPed;
                             if (candidatedriver != null)
                             {
                                 var target =
                                      from candidatevehicle in units
                                      where candidatevehicle.driverseat != null

                                      // can enter if the seat is full.
                                      // unless we kick them out before ofcourse
                                      where candidatevehicle.driverseat.driver == null

                                      let distance = new __vec2(
                                          (float)(candidatedriver.body.GetPosition().x - candidatevehicle.body.GetPosition().x),
                                          (float)(candidatedriver.body.GetPosition().y - candidatevehicle.body.GetPosition().y)
                                      ).GetLength()

                                      where distance < 6

                                      orderby distance ascending
                                      select new { candidatevehicle, distance };

                                 target.FirstOrDefault().With(
                                     x =>
                                     {
                                         Console.WriteLine(new { x.distance });

                                         __raise_enterorexit(
                                              "" + this.sessionid,
                                              candidatedriver.Identity,
                                              x.candidatevehicle.Identity
                                          );

                                         //current.loc.visible = false;
                                         current.body.SetActive(false);


                                         x.candidatevehicle.driverseat.driver = candidatedriver;
                                         candidatedriver.seatedvehicle = x.candidatevehicle;

                                         current = x.candidatevehicle;

                                         if (current is PhysicalJeep)
                                         {
                                             sb.snd_jeepengine_start.play();
                                         }
                                         else if (current is PhysicalBunker)
                                         {
                                             if ((current as PhysicalBunker).visual_shopoverlay.visible)
                                             {
                                                 sb.snd_its_a_shop.play();
                                                 if (ShopEnter != null)
                                                     ShopEnter(candidatedriver);
                                             }
                                             else
                                             {
                                                 if (random.NextDouble() > 0.8)
                                                 {
                                                     sb.haarp.play();
                                                 }
                                                 else
                                                 {
                                                     if (random.NextDouble() > 0.5)
                                                     {
                                                         sb.snd_itsempty.play();
                                                     }
                                                     else
                                                     {
                                                         sb.snd_nothinghere.play();
                                                     }

                                                 }
                                             }
                                         }
                                         else
                                         {
                                             sb.snd_dooropen.play(
                                               sndTransform: new SoundTransform(
                                                  0.3
                                               )
                                            );
                                         }

                                         //if (current is PhysicalHind)
                                         //{
                                         //    nightvision_on();
                                         //}

                                         hud_update();


                                         //switchto(x.x);
                                         move_zoom = 1;

                                         // fast start
                                         //(current as PhysicalHind).With(
                                         //    hind => hind.VerticalVelocity = 1
                                         //);
                                     }
                                 );
                             }
                             else
                             {
                                 (current.driverseat.driver as PhysicalPed).With(
                                     driver =>
                                     {
                                         // get out of the lift..

                                         //nightvision_off();

                                         current.driverseat.driver = null;
                                         driver.seatedvehicle = null;
                                         current.SetVelocityFromInput(new KeySample());

                                         if (current is PhysicalBunker)
                                         {
                                             if ((current as PhysicalBunker).IsShop)
                                             {
                                                 if (ShopExit != null)
                                                     ShopExit();
                                             }
                                         }

                                         // crashland?
                                         (current as PhysicalHind).With(
                                             hind =>
                                             {
                                                 if (hind.visual.Altitude > 0)
                                                 {
                                                     hind.VerticalVelocity = -1;
                                                     sb.snd_touchdown.play();
                                                 }
                                             }

                                         );

                                         sb.snd_dooropen.play(
                                           sndTransform: new SoundTransform(
                                              0.3
                                           )
                                        );
                                         //if (current.body.GetType() != Box2D.Dynamics.b2Body.b2_dynamicBody)
                                         //{
                                         //    sb.snd_letsgo.play();
                                         //}

                                         __raise_enterorexit(
                                             "" + this.sessionid,
                                             current.Identity,
                                             driver.Identity
                                         );

                                         current = driver;
                                         driver.body.SetActive(true);
                                         driver.body.SetAngularVelocity(-11);

                                         hud_update();
                                         move_zoom = 1;
                                     }
                                 );
                             }

                         }
                     }
                     #endregion


                     #region Space
                     if (!__keyDown[System.Windows.Forms.Keys.Space])
                     {
                         // space is not down.
                         mode_changepending = true;
                     }
                     else
                     {
                         if (mode_changepending)
                         {
                             (current as PhysicalHind).With(
                                 hind1 =>
                                 {
                                     if (hind1.visual.Altitude == 0)
                                     {
                                         nightvision_on();
                                         hind1.VerticalVelocity = 1.0;


                                     }
                                     else
                                     {
                                         nightvision_off();

                                         hind1.VerticalVelocity = -0.4;

                                         sb.snd_touchdown.play();
                                     }

                                     __raise_SetVerticalVelocity(
                                        "" + this.sessionid,
                                        hind1.Identity,
                                        "" + hind1.VerticalVelocity
                                    );
                                 }
                             );

                             (current as PhysicalPed).With(
                              physical0 =>
                              {
                                  if (physical0.visual.LayOnTheGround)
                                  {
                                      physical0.visual.LayOnTheGround = false;

                                      sb.snd_letsgo.play(
                                          sndTransform: new SoundTransform(
                                              0.3 * (0.15 + 0.15 * random.NextDouble())
                                          )
                                      );
                                  }
                                  else
                                  {
                                      physical0.visual.LayOnTheGround = true;

                                      sb.snd_ped_hit.play(
                                           sndTransform: new SoundTransform(
                                               0.3 * (0.15 + 0.15 * random.NextDouble())
                                           )
                                       );
                                  }

                                  var value = 0;
                                  if (physical0.visual.LayOnTheGround)
                                      value = 1;

                                  __raise_SetVerticalVelocity(
                                      "" + this.sessionid,
                                      physical0.Identity,
                                      "" + value
                                  );
                              }
                          );




                             mode_changepending = false;



                         }
                     }
                     #endregion







                     __raise_SetVelocityFromInput(
                          "" + sessionid,
                          current.Identity,
                          "" + current.CurrentInput.value,
                          "" + current.body.GetPosition().x,
                          "" + current.body.GetPosition().y,
                          "" + current.body.GetAngle()

                      );


                     // tell others this sync frame ended for us
                     __raise_sync("" + sessionid);
                 };
             };

        }
        public StarlingGameSpriteWithPedControl()
        {
            // http://armorgames.com/play/13701/


            var textures_ped = new StarlingGameSpriteWithPedTextures(
                this.new_tex_crop,
                this.new_texsprite_crop
            );
            //this.disablephysicsdiagnostics = true;

            this.onbeforefirstframe += (stage, s) =>
            {
                var patrol1 = new PhysicalPed(textures_ped, this)
                {

                    speed = 10
                };

                var physical0 = new PhysicalPed(textures_ped, this)
                {
                    AttractZombies = true
                    //speed = 8
                };


                //physical0.visual.WalkLikeZombie = true;
                physical0.visual.StandWithVisibleGun = true;



                current = physical0;

                // 32x32 = 15FPS?
                // 24x24 35?

                stage.mouseWheel += e =>
                    {
                        if (e.delta < 0)
                        {
                            this.internalscale -= 0.05;
                        }
                        if (e.delta > 0)
                        {
                            this.internalscale += 0.05;
                        }

                    };

                #region others
                for (int ix = 0; ix < 4; ix++)
                    for (int iy = 0; iy < 4; iy++)
                    {
                        var p = new PhysicalPed(textures_ped, this);

                        p.SetPositionAndAngle(
                            32 * ix, 32 * iy, random.NextDouble()
                        );

                        if (ix == 0)
                            p.BehaveLikeZombie();
                    }
                #endregion


                #region __keyDown

                stage.keyDown +=
                   e =>
                   {
                       // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                       if (e.altKey)
                           __keyDown[Keys.Alt] = true;

                       __keyDown[(Keys)e.keyCode] = true;

                       this.Text = new { e.keyCode, Keys.A }.ToString();
                   };

                stage.keyUp +=
                 e =>
                 {
                     if (!e.altKey)
                         __keyDown[Keys.Alt] = false;

                     __keyDown[(Keys)e.keyCode] = false;
                 };

                #endregion

                bool mode_changepending = false;
                var mode_gun = false;

                var sb = new Soundboard();

                onsyncframe += delegate
                {
                    #region patrol1
                    if (syncframeid % 300 == 100)
                    {
                        patrol1.body.SetAngle(
                            45.DegreesToRadians()
                        );

                        var partol_commands = new KeySample();

                        partol_commands[Keys.Up] = true;

                        patrol1.SetVelocityFromInput(partol_commands);
                    }

                    if (syncframeid % 300 == 150)
                    {
                        var partol_commands = new KeySample();

                        //partol_commands[Keys.Left] = true;

                        patrol1.SetVelocityFromInput(partol_commands);
                    }

                    if (syncframeid % 300 == 200)
                    {
                        patrol1.body.SetAngle(
                            (180 + 45).DegreesToRadians()
                        );

                        var partol_commands = new KeySample();

                        partol_commands[Keys.Up] = true;

                        patrol1.SetVelocityFromInput(partol_commands);
                    }


                    if (syncframeid % 300 == 250)
                    {
                        var partol_commands = new KeySample();

                        //partol_commands[Keys.Left] = true;

                        patrol1.SetVelocityFromInput(partol_commands);
                    }
                    #endregion


                    current.SetVelocityFromInput(__keyDown);

                    #region simulate a weapone!
                    if (__keyDown[Keys.ControlKey])
                    {
                        mode_gun = true;
                    }
                    else
                    {
                        if (mode_gun)
                        {
                            mode_gun = false;

                            (current as PhysicalPed).With(
                                ped =>
                                {
                                    ped.visual.StandWithVisibleGunFire.Restart();
                                }
                            );

                            sb.snd_shotgun3.play();

                            #region CreateBullet
                            Action<double> CreateBullet =
                                a =>
                                {
                                    var bodyDef = new b2BodyDef();

                                    bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

                                    // stop moving if legs stop walking!
                                    bodyDef.linearDamping = 0;
                                    bodyDef.angularDamping = 0;
                                    //bodyDef.angle = 1.57079633;
                                    bodyDef.fixedRotation = true;


                                    var dx = Math.Cos(current.body.GetAngle() + current.CameraRotation + a);
                                    var dy = Math.Sin(current.body.GetAngle() + current.CameraRotation + a);

                                    var body = damage_b2world.CreateBody(bodyDef);
                                    body.SetPosition(
                                        new b2Vec2(
                                            current.body.GetPosition().x + dx * 0.3,
                                            current.body.GetPosition().y + dy * 0.3
                                        )
                                    );

                                    body.SetLinearVelocity(
                                           new b2Vec2(
                                             dx * 100,
                                            dy * 100
                                        )
                                    );

                                    var fixDef = new Box2D.Dynamics.b2FixtureDef();
                                    fixDef.density = 20;
                                    fixDef.friction = 0.0;
                                    fixDef.restitution = 0;


                                    fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(0.3);


                                    var fix = body.CreateFixture(fixDef);
                                };
                            #endregion


                            CreateBullet(-6.DegreesToRadians());
                            CreateBullet(-2.DegreesToRadians());
                            CreateBullet(2.DegreesToRadians());
                            CreateBullet(6.DegreesToRadians());

                            //body.SetPosition(
                            //    new b2Vec2(0, -100 * 16)
                            //);
                        }

                    }
                    #endregion


                    #region mode
                    if (!__keyDown[Keys.Space])
                    {
                        // space is not down.
                        mode_changepending = true;
                    }
                    else
                    {
                        if (mode_changepending)
                        {
                            if (physical0.visual.LayOnTheGround)
                                physical0.visual.LayOnTheGround = false;
                            else
                                physical0.visual.LayOnTheGround = true;

                            mode_changepending = false;



                        }
                    }
                    #endregion



                };


            };



        }
        public StarlingGameSpriteWithPedControl()
        {
            // http://armorgames.com/play/13701/


            var textures_ped = new StarlingGameSpriteWithPedTextures(
                this.new_tex_crop,
                this.new_texsprite_crop
                );

            //this.disablephysicsdiagnostics = true;

            this.onbeforefirstframe += (stage, s) =>
            {
                var patrol1 = new PhysicalPed(textures_ped, this)
                {
                    speed = 10
                };

                var physical0 = new PhysicalPed(textures_ped, this)
                {
                    AttractZombies = true
                                     //speed = 8
                };


                //physical0.visual.WalkLikeZombie = true;
                physical0.visual.StandWithVisibleGun = true;



                current = physical0;

                // 32x32 = 15FPS?
                // 24x24 35?

                stage.mouseWheel += e =>
                {
                    if (e.delta < 0)
                    {
                        this.internalscale -= 0.05;
                    }
                    if (e.delta > 0)
                    {
                        this.internalscale += 0.05;
                    }
                };

                #region others
                for (int ix = 0; ix < 4; ix++)
                {
                    for (int iy = 0; iy < 4; iy++)
                    {
                        var p = new PhysicalPed(textures_ped, this);

                        p.SetPositionAndAngle(
                            32 * ix, 32 * iy, random.NextDouble()
                            );

                        if (ix == 0)
                        {
                            p.BehaveLikeZombie();
                        }
                    }
                }
                #endregion


                #region __keyDown

                stage.keyDown +=
                    e =>
                {
                    // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                    if (e.altKey)
                    {
                        __keyDown[Keys.Alt] = true;
                    }

                    __keyDown[(Keys)e.keyCode] = true;

                    this.Text = new { e.keyCode, Keys.A }.ToString();
                };

                stage.keyUp +=
                    e =>
                {
                    if (!e.altKey)
                    {
                        __keyDown[Keys.Alt] = false;
                    }

                    __keyDown[(Keys)e.keyCode] = false;
                };

                #endregion

                bool mode_changepending = false;
                var  mode_gun           = false;

                var sb = new Soundboard();

                onsyncframe += delegate
                {
                    #region patrol1
                    if (syncframeid % 300 == 100)
                    {
                        patrol1.body.SetAngle(
                            45.DegreesToRadians()
                            );

                        var partol_commands = new KeySample();

                        partol_commands[Keys.Up] = true;

                        patrol1.SetVelocityFromInput(partol_commands);
                    }

                    if (syncframeid % 300 == 150)
                    {
                        var partol_commands = new KeySample();

                        //partol_commands[Keys.Left] = true;

                        patrol1.SetVelocityFromInput(partol_commands);
                    }

                    if (syncframeid % 300 == 200)
                    {
                        patrol1.body.SetAngle(
                            (180 + 45).DegreesToRadians()
                            );

                        var partol_commands = new KeySample();

                        partol_commands[Keys.Up] = true;

                        patrol1.SetVelocityFromInput(partol_commands);
                    }


                    if (syncframeid % 300 == 250)
                    {
                        var partol_commands = new KeySample();

                        //partol_commands[Keys.Left] = true;

                        patrol1.SetVelocityFromInput(partol_commands);
                    }
                    #endregion


                    current.SetVelocityFromInput(__keyDown);

                    #region simulate a weapone!
                    if (__keyDown[Keys.ControlKey])
                    {
                        mode_gun = true;
                    }
                    else
                    {
                        if (mode_gun)
                        {
                            mode_gun = false;

                            (current as PhysicalPed).With(
                                ped =>
                            {
                                ped.visual.StandWithVisibleGunFire.Restart();
                            }
                                );

                            sb.snd_shotgun3.play();

                            #region CreateBullet
                            Action <double> CreateBullet =
                                a =>
                            {
                                var bodyDef = new b2BodyDef();

                                bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

                                // stop moving if legs stop walking!
                                bodyDef.linearDamping  = 0;
                                bodyDef.angularDamping = 0;
                                //bodyDef.angle = 1.57079633;
                                bodyDef.fixedRotation = true;


                                var dx = Math.Cos(current.body.GetAngle() + current.CameraRotation + a);
                                var dy = Math.Sin(current.body.GetAngle() + current.CameraRotation + a);

                                var body = damage_b2world.CreateBody(bodyDef);
                                body.SetPosition(
                                    new b2Vec2(
                                        current.body.GetPosition().x + dx * 0.3,
                                        current.body.GetPosition().y + dy * 0.3
                                        )
                                    );

                                body.SetLinearVelocity(
                                    new b2Vec2(
                                        dx * 100,
                                        dy * 100
                                        )
                                    );

                                var fixDef = new Box2D.Dynamics.b2FixtureDef();
                                fixDef.density     = 20;
                                fixDef.friction    = 0.0;
                                fixDef.restitution = 0;


                                fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(0.3);


                                var fix = body.CreateFixture(fixDef);
                            };
                            #endregion


                            CreateBullet(-6.DegreesToRadians());
                            CreateBullet(-2.DegreesToRadians());
                            CreateBullet(2.DegreesToRadians());
                            CreateBullet(6.DegreesToRadians());

                            //body.SetPosition(
                            //    new b2Vec2(0, -100 * 16)
                            //);
                        }
                    }
                    #endregion


                    #region mode
                    if (!__keyDown[Keys.Space])
                    {
                        // space is not down.
                        mode_changepending = true;
                    }
                    else
                    {
                        if (mode_changepending)
                        {
                            if (physical0.visual.LayOnTheGround)
                            {
                                physical0.visual.LayOnTheGround = false;
                            }
                            else
                            {
                                physical0.visual.LayOnTheGround = true;
                            }

                            mode_changepending = false;
                        }
                    }
                    #endregion
                };
            };
        }
        public StarlingGameSpriteWithTestDriversWithAudio()
        {
            // http://www.mochigames.com/game/gunship_v838523/

            textures_ped = new StarlingGameSpriteWithPedTextures(this.new_tex_crop, this.new_texsprite_crop);


            var textures_map = new StarlingGameSpriteWithMapTextures(new_tex_crop);

            var textures_jeep = new StarlingGameSpriteWithJeepTextures(this.new_tex_crop);

            var textures_hind = new StarlingGameSpriteWithHindTextures(this.new_tex_crop);
            var textures_tank = new StarlingGameSpriteWithTankTextures(this.new_texsprite_crop);
            var textures_cannon = new StarlingGameSpriteWithCannonTextures(this.new_tex_crop);
            var textures_bunker = new StarlingGameSpriteWithBunkerTextures(this.new_tex_crop);
            var textures_rocket = new StarlingGameSpriteWithRocketTextures(this.new_tex_crop);
            var textures_explosions = new StarlingGameSpriteWithMapExplosionsTextures(new_tex96);

            this.disablephysicsdiagnostics = true;

            this.onbeforefirstframe += (stage, s) =>
            {
                var explosins = new List<ExplosionInfo>();

                s.stage.color = 0xB27D51;

                // error JSC1000: ActionScript : failure at starling.display.Stage.add_keyUp : Object reference not set to an instance of an object.

                #region F2
                stage.keyUp +=
                     e =>
                     {
                         if (e.keyCode == (uint)System.Windows.Forms.Keys.F2)
                         {
                             this.Content_layer2_shadows.visible =
                                !this.Content_layer2_shadows.visible;
                         }

                     };
                #endregion

                #region F3
                stage.keyUp +=
                     e =>
                     {
                         if (e.keyCode == (uint)System.Windows.Forms.Keys.F3)
                         {
                             this.Content_layer0_tracks.visible =
                                !this.Content_layer0_tracks.visible;
                         }

                     };
                #endregion

                #region F1
                stage.keyUp +=
                     e =>
                     {
                         if (e.keyCode == (uint)System.Windows.Forms.Keys.F1)
                         {
                             if (this.internalscale == 0.3)
                                 this.internalscale = 0.05;
                             else
                                 this.internalscale = 0.3;
                         }

                     };
                #endregion

                var hud = new Image(textures_ped.hud_look()).AttachTo(this);

                #region hill1
                for (int i = 0; i < 32; i++)
                {
                    new Image(textures_map.hill1()).AttachTo(Content_layer0_ground).With(
                        hill =>
                        {
                            hill.x = 2048.Random();
                            hill.y = 2048.Random() - 1024;
                        }
                    );

                    new Image(textures_map.hole1()).AttachTo(Content_layer0_ground).With(
                        hill =>
                        {
                            hill.x = 2048.Random();
                            hill.y = 2048.Random() - 1024;
                        }
                    );

                    new Image(textures_map.grass1()).AttachTo(Content_layer0_ground).With(
                        hill =>
                        {
                            hill.x = 2048.Random();

                            var y = -2048.Random() - 512 - 256;
                            hill.y = y;
                        }
                    );
                }
                #endregion

                for (int i = -12; i < 12; i++)
                {
                    new Image(textures_map.road0()).AttachTo(Content_layer0_ground).x = 256 * i;

                    if (i % 3 == 0)
                    {
                        var z = new PhysicalPed(textures_ped, this);

                        z.SetPositionAndAngle(16 * i, 0, random.NextDouble() * Math.PI);
                        z.BehaveLikeZombie();
                    }
                }

                var needdshop = true;

                #region other units
                for (int i = 3; i < 7; i++)
                {
                    {
                        new PhysicalCannon(textures_cannon, this).SetPositionAndAngle(
                            i * 16, -20, -random.NextDouble()
                        );

                        new PhysicalCannon(textures_cannon, this).SetPositionAndAngle(
                            i * 16, 36, random.NextDouble()
                        );
                    }


                    if (i % 3 == 0)
                    {
                        new PhysicalBunker(textures_bunker, this, IsShop: needdshop).SetPositionAndAngle(
                            i * 16, -8, random.NextDouble()
                        );
                        needdshop = false;

                        new PhysicalBunker(textures_bunker, this).SetPositionAndAngle(
                            i * 16, 24, random.NextDouble()
                        );


                        var ibunker = new PhysicalBunker(textures_bunker, this);

                        ibunker.SetPositionAndAngle(
                            i * 16, 64, random.NextDouble()
                        );

                        ibunker.visualshadow.Orphanize(); //.AttachTo(this.Content_layer10_hiddenforgoggles);
                        ibunker.visual.Orphanize().AttachTo(this.Content_layer10_hiddenforgoggles);

                    }
                    else if (i % 3 == 1)
                    {


                        new PhysicalBarrel(textures_bunker, this).SetPositionAndAngle(i * 16, -4 - 3);
                        new PhysicalBarrel(textures_bunker, this).SetPositionAndAngle(i * 16 + 2, -4);
                        new PhysicalBarrel(textures_bunker, this).SetPositionAndAngle(i * 16, -4 + 3);

                        new PhysicalWatertower(textures_bunker, this).SetPositionAndAngle(
                            i * 16, 16, random.NextDouble()
                        );

                        new PhysicalWatertower(textures_bunker, this).SetPositionAndAngle(
                            i * 16 - 4, 16 + 4, random.NextDouble()
                        );

                        new PhysicalWatertower(textures_bunker, this).SetPositionAndAngle(
                            i * 16 + 4, 16 + 4, random.NextDouble()
                        );
                    }
                    else
                    {
                        new PhysicalBarrel(textures_bunker, this).SetPositionAndAngle(i * 16, 24 - 3);
                        new PhysicalBarrel(textures_bunker, this).SetPositionAndAngle(i * 16 + 2, 24);
                        new PhysicalBarrel(textures_bunker, this).SetPositionAndAngle(i * 16, 24 + 3);

                        new PhysicalSilo(textures_bunker, this).SetPositionAndAngle(
                            i * 16, -4, random.NextDouble()
                        );
                    }

                }
                #endregion



                new PhysicalBarrel(textures_bunker, this).SetPositionAndAngle(12, 0);
                new PhysicalBarrel(textures_bunker, this).SetPositionAndAngle(12, -4);
                new PhysicalBarrel(textures_bunker, this).SetPositionAndAngle(12, -8);



                new Image(textures_map.touchdown()).AttachTo(Content_layer0_ground).MoveTo(256, -256);
                new Image(textures_map.touchdown()).AttachTo(Content_layer0_ground).y = 256;

                new PhysicalTank(textures_tank, this).SetPositionAndAngle(128 / 16, 128 * 3 / 16);

                new Image(textures_map.tree0_shadow()).AttachTo(Content).y = 128 + 16;
                new Image(textures_map.tree0()).AttachTo(Content).y = 128;

                // can I have 
                // new ped, hind, jeep, tank

                var egoped = new PhysicalPed(textures_ped, this)
                {
                    AttractZombies = true,



                };

                egoped.visual.StandWithVisibleGun = true;

                current = egoped;

                current.SetPositionAndAngle(
                    16.Random(),
                    16.Random(),

                    360.Random().DegreesToRadians()
                );

                var jeep2 = new PhysicalJeep(textures_jeep, this);

                jeep2.SetPositionAndAngle(
                    16, 16, random.NextDouble()
                );

                var jeep3 = new PhysicalJeep(textures_jeep, this);


                jeep3.visual0.shadow.Orphanize(); //.AttachTo(this.Content_layer10_hiddenforgoggles);
                jeep3.visual0.currentvisual.Orphanize().AttachTo(this.Content_layer10_hiddenforgoggles);

                jeep3.SetPositionAndAngle(
                    -16, 16, random.NextDouble()
                );



                #region tree0
                for (int i = 0; i < 128; i++)
                {

                    {
                        var x = 2048.Random();
                        var y = -2048.Random() - 512 - 256;

                        new Image(textures_map.tree0_shadow()).AttachTo(Content_layer2_shadows).MoveTo(x + 16, y + 16);
                        new Image(textures_map.tree0()).AttachTo(Content).MoveTo(x, y);
                    }

                    {
                        var x = 2048.Random();
                        var y = 2048.Random() + 512 + 128;

                        new Image(textures_map.tree0_shadow()).AttachTo(Content_layer2_shadows).MoveTo(x + 16, y + 16);
                        new Image(textures_map.tree0()).AttachTo(Content).MoveTo(x, y);
                    }
                }
                #endregion

                // 12 = 34FPS

                //new PhysicalHind(textures_hind, this)
                new PhysicalHindWeaponized(textures_hind, textures_rocket, this)
                {
                    AutomaticTakeoff = true
                }.SetPositionAndAngle((128 + 256) / 16, -128 / 16);

                #region mouseWheel
                stage.mouseWheel += e =>
                {
                    e.preventDefault();

                    if (e.delta < 0)
                    {
                        this.internalscale -= 0.05;
                    }
                    if (e.delta > 0)
                    {
                        this.internalscale += 0.05;
                    }

                };
                #endregion


                #region __keyDown

                stage.keyDown +=
                   e =>
                   {
                       e.preventDefault();

                       __keyDown.forcex = 1.0;
                       __keyDown.forcey = 1.0;

                       // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                       if (e.altKey)
                           __keyDown[System.Windows.Forms.Keys.Alt] = true;

                       __keyDown[(System.Windows.Forms.Keys)e.keyCode] = true;
                   };

                stage.keyUp +=
                 e =>
                 {
                     e.preventDefault();

                     if (!e.altKey)
                         __keyDown[System.Windows.Forms.Keys.Alt] = false;

                     __keyDown[(System.Windows.Forms.Keys)e.keyCode] = false;
                 };

                #endregion

                #region CreateExplosion
                this.CreateExplosion = (x, y) =>
                {
                    var size = 0.2 + 0.2 * random.NextDouble();

                    sb.snd_explosion_small.play(
                        sndTransform: new ScriptCoreLib.ActionScript.flash.media.SoundTransform(size)
                    );

                    var exp = new Image(textures_explosions.explosions[0]()).AttachTo(Content);
                    var cm = new Matrix();

                    cm.translate(-32, -32);
                    cm.scale(10 * size, 10 * size);
                    cm.rotate(random.NextDouble() * Math.PI);

                    cm.translate(16 * x, 16 * y);

                    exp.transformationMatrix = cm;

                    explosins.Add(
                    new ExplosionInfo { visual = exp }
                    );

                };
                #endregion


                bool nightvision_changepending = false;
                bool entermode_changepending = false;
                bool mode_changepending = false;

                onframe +=
                    delegate
                    {
                        //var v = current.body.GetLinearVelocity();

                        //Text = new { move_zoom, v.x, v.y }.ToString();

                        #region hud
                        {
                            var cm = new Matrix();

                            cm.scale(0.5, 0.5);
                            cm.translate(
                                16 + HudPadding,
                                stage.stageHeight - 64 - 24);

                            hud.transformationMatrix = cm;
                        }
                        #endregion
                    };

                // ego + local environment
                #region Soundboard
                // http://www.nasa.gov/vision/universe/features/halloween_sounds.html

                sb.loopsand_run.MasterVolume = 0;
                sb.loopsand_run.Sound.play();

                sb.loophelicopter1.MasterVolume = 0;
                sb.loophelicopter1.Sound.play();

                sb.loopjeepengine.MasterVolume = 0;
                sb.loopjeepengine.Sound.play();

                sb.loopdiesel2.MasterVolume = 0;
                sb.loopdiesel2.Sound.play();

                sb.loopcrickets.MasterVolume = 0;
                sb.loopcrickets.Sound.play();

                sb.loopstrange1.MasterVolume = 0;
                sb.loopstrange1.Sound.play();

                sb.loop_GallinagoDelicata.MasterVolume = 0;
                sb.loop_GallinagoDelicata.Sound.play();

                var jeep_forceA = 0.0;
                var ped_forceA = 0.0;
                var pedzombie_forceA = 0.0;
                var barrel_forceA = 0.0;

                var hardmetal_forceA = 0.0;

                this.oncollision +=
                    (u, force) =>
                    {
                        if (u is PhysicalTank)
                            hardmetal_forceA = hardmetal_forceA.Max(force);
                        if (u is PhysicalSilo)
                            hardmetal_forceA = hardmetal_forceA.Max(force);
                        if (u is PhysicalBunker)
                            hardmetal_forceA = hardmetal_forceA.Max(force);
                        if (u is PhysicalWatertower)
                            hardmetal_forceA = hardmetal_forceA.Max(force);
                        if (u is PhysicalCannon)
                            hardmetal_forceA = hardmetal_forceA.Max(force);
                    };

                PhysicalJeep.oncollision +=
                    (u, value) =>
                    {
                        jeep_forceA = jeep_forceA.Max(value);

                    };

                PhysicalPed.oncollision +=
                     (u, value) =>
                     {
                         if (u.visual.WalkLikeZombie)
                             pedzombie_forceA = pedzombie_forceA.Max(value);
                         else
                             ped_forceA = ped_forceA.Max(value);
                     };

                PhysicalBarrel.oncollision +=
                  (u, value) =>
                  {
                      barrel_forceA = barrel_forceA.Max(value);
                  };
                #endregion

                var nightvision_filter = new ColorMatrixFilter();
                var nightvision_filter_age = new Stopwatch();
                nightvision_filter_age.Start();
                Action nighvision_handler = null;

                bool nightvision_mode = false;

                #region hud_update
                hud_update = delegate
               {
                   if (nightvision_mode)
                   {

                       hud.texture = textures_ped.hud_look_goggles();
                       return;
                   }

                   if (current is PhysicalPed)
                   {
                       hud.texture = textures_ped.hud_look();
                   }
                   else if (current == jeep3)
                   {
                       hud.texture = textures_ped.hud_look_onlygoggles();
                   }
                   else
                   {
                       if (current.body.GetType() == Box2D.Dynamics.b2Body.b2_dynamicBody)
                       {
                           hud.texture = textures_ped.hud_look_goggles();
                       }
                       else
                       {
                           hud.texture = textures_ped.hud_look_building();
                       }
                   }
               };
                #endregion



                #region nightvision_mode
                #region nightvision_on
                nightvision_on = delegate
               {
                   if (nightvision_mode)
                       return;

                   nightvision_mode = true;
                   hud_update();
                   nightvision_filter_age.Restart();
                   this.Content_layer10_hiddenforgoggles.visible = true;

                   sb.snd_nightvision.play(
                      sndTransform: new SoundTransform(
                         0.5
                      )
                   );


                   nighvision_handler = delegate
                   {
                       // http://doc.starling-framework.org/core/starling/filters/ColorMatrixFilter.html
                       // create an inverted filter with 50% saturation and 180° hue rotation
                       nightvision_filter = new ColorMatrixFilter();
                       nightvision_filter.adjustSaturation(-1.0);
                       nightvision_filter.invert();
                       //nightvision_filter.adjustContrast(1.0);

                       var a = (nightvision_filter_age.ElapsedMilliseconds / 1100.0).Min(1);

                       nightvision_filter.adjustContrast(

                           16 - 14 * a
                       );



                       //           V:\web\FlashHeatZeeker\TestDriversWithAudio\Library\StarlingGameSpriteWithTestDriversWithAudio___c__DisplayClass29___c__DisplayClass30.as(266): col: 28 Error: Implicit coercion of a value of type __AS3__.vec:Vector.<Object> to an unrelated type __AS3__.vec:Vector.<Number>.

                       //filter3.concat(vector_14);
                       //               ^

                       //  public function concat(matrix:Vector.<Number>):void
                       // public override void concat(Vector<object> matrix);

                       // X:\jsc.svn\examples\actionscript\test\TestVectorOfNumber\TestVectorOfNumber\ApplicationSprite.cs

#if FNGHTVISION
                       nightvision_filter.concat(
                           new double[] {
                           //new object[] {
                                                    
                          0, 0,  0,  0, 0,
                          0, 1,  0,  0, 0,
                          0, 0, 0,  0, 0,
                          0,  0,  0,  1,   0

                                                    }
                       );
#endif



                       this.filter = nightvision_filter;
                       this.stage.color = 0x006E00;

                       if (a == 1)
                           nighvision_handler = null;
                   };


               };
                #endregion

                #region nightvision_off
                nightvision_off = delegate
               {
                   if (!nightvision_mode)
                       return;

                   nightvision_mode = false;
                   hud_update();

                   this.Content_layer10_hiddenforgoggles.visible = false;

                   sb.snd_SelectWeapon.play(
                      sndTransform: new SoundTransform(
                         0.3
                      )
                   );

                   nightvision_filter_age.Restart();

                   nighvision_handler = delegate
                   {
                       nightvision_filter = new ColorMatrixFilter();



                       // nightvision_filter.adjustBrightness(

                       //    1 - (nightvision_filter_age.ElapsedMilliseconds / 200.0).Min(1)
                       //);

                       var a = (nightvision_filter_age.ElapsedMilliseconds / 500.0).Min(1);

                       nightvision_filter.adjustBrightness(

                            0.5 - 0.5 * a
                        );


                       this.filter = nightvision_filter;
                       this.stage.color = 0xB27D51;

                       if (a == 1)
                           nighvision_handler = null;
                   };

               };
                #endregion


                onframe +=
                    delegate
                    {
                        if (nighvision_handler != null)
                            nighvision_handler();



                    };
                #endregion

                //(units.FirstOrDefault(k => k is PhysicalBunker) as PhysicalBunker).With(
                //    shop =>
                //    {
                //        shop.IsShop = true;
                //    }
                //);
                var mode_gun = false;

                onsyncframe +=
                    delegate
                    {

                        while (Content_layer0_tracks.numChildren > 128)
                        {
                            Content_layer0_tracks.removeChildAt(0);
                        }

                        #region textures_explosions
                        foreach (var item in explosins.ToArray())
                        {
                            item.index++;

                            if (item.index == textures_explosions.explosions.Length)
                            {
                                item.visual.Orphanize();
                                explosins.Remove(item);
                            }
                            else
                            {
                                item.visual.texture = textures_explosions.explosions[item.index]();
                            }
                        }
                        #endregion

                        #region Soundboard
                        if (barrel_forceA > 0)
                        {
                            sb.snd_woodsmash.play(
                               sndTransform: new SoundTransform(
                                   Math.Min(1.0, barrel_forceA / 30.0) * (0.15 + 0.15 * random.NextDouble())
                               )
                           );

                            barrel_forceA = 0;

                        }
                        else if (hardmetal_forceA > 0)
                        {
                            sb.snd_hardmetalsmash.play(
                               sndTransform: new SoundTransform(
                                   Math.Min(1.0, hardmetal_forceA / 30.0) * (0.2 + 0.2 * random.NextDouble())
                               )
                           );

                            hardmetal_forceA = 0;
                        }
                        else if (jeep_forceA > 0)
                        {
                            sb.snd_metalsmash.play(
                               sndTransform: new SoundTransform(
                                   Math.Min(1.0, jeep_forceA / 30.0) * (0.2 + 0.2 * random.NextDouble())
                               )
                           );

                            jeep_forceA = 0;
                        }
                        else if (ped_forceA > 0)
                        {
                            sb.snd_ped_hit.play(
                               sndTransform: new SoundTransform(
                                   Math.Min(1.0, ped_forceA / 30.0) * (0.15 + 0.15 * random.NextDouble())
                               )
                           );

                            ped_forceA = 0;
                        }
                        else if (pedzombie_forceA > 0)
                        {
                            sb.snd_Argh.play(
                               sndTransform: new SoundTransform(
                                   Math.Min(1.0, pedzombie_forceA / 30.0) * (0.15 + 0.15 * random.NextDouble())
                               )
                           );

                            pedzombie_forceA = 0;
                        }

                        if (this.syncframeid == 200)
                            sb.snd_whatsthatsound.play();

                        if (this.syncframeid == 400)
                            sb.snd_needweapon.play();

                        if (this.syncframeid == 800)
                            sb.snd_didyouhearthat.play();


                        if (this.syncframeid == 1200)
                            sb.snd_whatsthatsound.play();

                        sb.loopcrickets.MasterVolume = (1 - move_zoom) * 0.09;
                        sb.loop_GallinagoDelicata.MasterVolume = (1 - move_zoom) * 0.3;

                        sb.loopstrange1.MasterVolume = (1 - move_zoom) * 0.04;

                        sb.loophelicopter1.MasterVolume = 0.0;
                        sb.loopjeepengine.MasterVolume = 0.0;
                        sb.loopdiesel2.MasterVolume = 0.0;
                        sb.loopsand_run.MasterVolume = 0.0;


                        if (current is PhysicalPed)
                        {
                            sb.loopsand_run.MasterVolume = 0.5;
                            sb.loopsand_run.LeftVolume = 0.0 + move_zoom * 0.9;
                            sb.loopsand_run.RightVolume = 0.0 + move_zoom * 0.9;
                            sb.loopsand_run.Rate = 0.9 + move_zoom * 0.1;
                        }
                        else if (current is PhysicalHind)
                        {
                            sb.loopcrickets.MasterVolume = 0;

                            sb.loophelicopter1.MasterVolume = 0.3 + (current as PhysicalHind).visual.Altitude * 0.2;
                            sb.loophelicopter1.LeftVolume = 0.7 + move_zoom * 0.1;
                            sb.loophelicopter1.RightVolume = 0.8;
                            sb.loophelicopter1.Rate = 0.7 + (current as PhysicalHind).visual.Altitude * 0.25 + move_zoom * 0.05;
                        }
                        else if (current is PhysicalJeep)
                        {
                            sb.loopjeepengine.MasterVolume = 0.5;
                            sb.loopjeepengine.LeftVolume = 0.4 + move_zoom * 0.7;
                            sb.loopjeepengine.RightVolume = 1;
                            sb.loopjeepengine.Rate = 0.9 + move_zoom * 0.5;
                        }
                        else if (current is PhysicalTank)
                        {
                            sb.loopcrickets.MasterVolume = 0;

                            sb.loopdiesel2.MasterVolume = 0.5;
                            sb.loopdiesel2.LeftVolume = 0.3 + move_zoom * 0.7;
                            sb.loopdiesel2.RightVolume = 1;
                            sb.loopdiesel2.Rate = 0.9 + move_zoom;
                        }
                        else
                        {
                            sb.loopcrickets.MasterVolume = 0.2;
                            sb.loop_GallinagoDelicata.MasterVolume = 0.2;

                        }

                        // stereoeffect // siren
                        sb.loopstrange1.LeftVolume = 0.2 * (1 + Math.Cos(this.gametime.ElapsedMilliseconds * 0.00001)) / 2.0;
                        sb.loopstrange1.RightVolume = 0.4 * (1 + Math.Cos(this.gametime.ElapsedMilliseconds * 0.00001)) / 2.0;

                        sb.loopcrickets.LeftVolume = (1 + Math.Sin(
                            this.gametime.ElapsedMilliseconds * 0.0001
                            + this.current.CameraRotation
                            + this.current.body.GetAngle()
                            )) / 2.0;
                        sb.loopcrickets.RightVolume = (3 + Math.Cos(this.gametime.ElapsedMilliseconds * 0.001
                            + this.current.CameraRotation
                            + this.current.body.GetAngle()
                            )) / 4.0;

                        sb.loop_GallinagoDelicata.LeftVolume = sb.loopcrickets.RightVolume;
                        sb.loop_GallinagoDelicata.RightVolume = sb.loopcrickets.LeftVolume;

                        #endregion

                        if (disable_enter_and_space)
                        {
                            // implemented elsewhere
                        }
                        else
                        {
                            #region Enter
                            if (!__keyDown[System.Windows.Forms.Keys.Enter])
                            {
                                // space is not down.
                                entermode_changepending = true;
                            }
                            else
                            {
                                if (entermode_changepending)
                                {
                                    entermode_changepending = false;

                                    // enter another vehicle?

                                    var candidatedriver = current as PhysicalPed;
                                    if (candidatedriver != null)
                                    {
                                        var target =
                                             from candidatevehicle in units
                                             where candidatevehicle.driverseat != null

                                             // can enter if the seat is full.
                                             // unless we kick them out before ofcourse
                                             where candidatevehicle.driverseat.driver == null

                                             let distance = new __vec2(
                                                 (float)(candidatedriver.body.GetPosition().x - candidatevehicle.body.GetPosition().x),
                                                 (float)(candidatedriver.body.GetPosition().y - candidatevehicle.body.GetPosition().y)
                                             ).GetLength()

                                             where distance < 6

                                             orderby distance ascending
                                             select new { candidatevehicle, distance };

                                        target.FirstOrDefault().With(
                                            x =>
                                            {
                                                Console.WriteLine(new { x.distance });

                                                //current.loc.visible = false;
                                                current.body.SetActive(false);


                                                x.candidatevehicle.driverseat.driver = candidatedriver;
                                                candidatedriver.seatedvehicle = x.candidatevehicle;

                                                current = x.candidatevehicle;

                                                if (current is PhysicalJeep)
                                                {
                                                    sb.snd_jeepengine_start.play();
                                                }

                                                else if (current is PhysicalBunker)
                                                {
                                                    if ((current as PhysicalBunker).visual_shopoverlay.visible)
                                                    {
                                                        sb.snd_its_a_shop.play();

                                                    }
                                                    else
                                                    {
                                                        if (random.NextDouble() > 0.8)
                                                        {
                                                            sb.haarp.play();
                                                        }
                                                        else
                                                        {
                                                            if (random.NextDouble() > 0.5)
                                                            {
                                                                sb.snd_itsempty.play();
                                                            }
                                                            else
                                                            {
                                                                sb.snd_nothinghere.play();
                                                            }

                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    sb.snd_dooropen.play(
                                                      sndTransform: new SoundTransform(
                                                         0.3
                                                      )
                                                   );
                                                }

                                                //if (current is PhysicalHind)
                                                //{
                                                //    nightvision_on();
                                                //}

                                                hud_update();


                                                //switchto(x.x);
                                                move_zoom = 1;

                                                // fast start
                                                //(current as PhysicalHind).With(
                                                //    hind => hind.VerticalVelocity = 1
                                                //);
                                            }
                                        );
                                    }
                                    else
                                    {
                                        (current.driverseat.driver as PhysicalPed).With(
                                            driver =>
                                            {
                                                // get out of the lift..

                                                //nightvision_off();

                                                current.driverseat.driver = null;
                                                driver.seatedvehicle = null;
                                                current.SetVelocityFromInput(new KeySample());

                                                // crashland?
                                                (current as PhysicalHind).With(
                                                    hind =>
                                                    {
                                                        if (hind.visual.Altitude > 0)
                                                        {
                                                            hind.VerticalVelocity = -1;
                                                            sb.snd_touchdown.play();
                                                        }
                                                    }

                                                );

                                                sb.snd_dooropen.play(
                                                  sndTransform: new SoundTransform(
                                                     0.3
                                                  )
                                               );
                                                //if (current.body.GetType() != Box2D.Dynamics.b2Body.b2_dynamicBody)
                                                //{
                                                //    sb.snd_letsgo.play();
                                                //}

                                                current = driver;
                                                driver.body.SetActive(true);
                                                driver.body.SetAngularVelocity(-11);

                                                hud_update();
                                                move_zoom = 1;
                                            }
                                        );
                                    }

                                }
                            }
                            #endregion


                            #region Space
                            if (!__keyDown[System.Windows.Forms.Keys.Space])
                            {
                                // space is not down.
                                mode_changepending = true;
                            }
                            else
                            {
                                if (mode_changepending)
                                {
                                    (current as PhysicalHind).With(
                                        hind1 =>
                                        {
                                            if (hind1.visual.Altitude == 0)
                                            {
                                                //nightvision_on();
                                                hind1.VerticalVelocity = 1.0;
                                            }
                                            else
                                            {
                                                //nightvision_off();

                                                hind1.VerticalVelocity = -0.4;

                                                sb.snd_touchdown.play();
                                            }

                                        }
                                    );

                                    (current as PhysicalPed).With(
                                     physical0 =>
                                     {
                                         if (physical0.visual.LayOnTheGround)
                                         {
                                             physical0.visual.LayOnTheGround = false;

                                             sb.snd_letsgo.play(
                                                 sndTransform: new SoundTransform(
                                                     0.3 * (0.15 + 0.15 * random.NextDouble())
                                                 )
                                             );
                                         }
                                         else
                                         {
                                             physical0.visual.LayOnTheGround = true;

                                             sb.snd_ped_hit.play(
                                                  sndTransform: new SoundTransform(
                                                      0.3 * (0.15 + 0.15 * random.NextDouble())
                                                  )
                                              );
                                         }

                                     }
                                 );




                                    mode_changepending = false;



                                }
                            }
                            #endregion

                        }


                        #region N
                        if (!__keyDown[System.Windows.Forms.Keys.N])
                        {
                            // space is not down.
                            nightvision_changepending = true;
                        }
                        else
                        {
                            if (nightvision_changepending)
                            {
                                if (nightvision_mode)
                                    nightvision_off();
                                else
                                    nightvision_on();




                                nightvision_changepending = false;



                            }
                        }
                        #endregion



                        current.SetVelocityFromInput(__keyDown);




                        #region simulate a weapone!
                        if (__keyDown[System.Windows.Forms.Keys.ControlKey])
                            if (syncframeid % 3 == 0)
                            {
                                (this.current as PhysicalHindWeaponized).With(
                                    h =>
                                    {
                                        sb.snd_missleLaunch.play(
                                            sndTransform: new SoundTransform(0.5)
                                            );

                                        h.FireRocket();
                                    }
                                );

                            }
                        #endregion

                        #region simulate a weapone!
                        if (__keyDown[System.Windows.Forms.Keys.ControlKey])
                        {
                            mode_gun = true;
                        }
                        else
                        {
                            if (mode_gun)
                            {
                                mode_gun = false;

                                (this.current as PhysicalPed).With(
                                       h =>
                                       {


                                           h.visual.StandWithVisibleGunFire.Restart();


                                           sb.snd_shotgun3.play();

                                           #region CreateBullet
                                           Action<double> CreateBullet =
                                               a =>
                                               {
                                                   var bodyDef = new b2BodyDef();

                                                   bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

                                                   // stop moving if legs stop walking!
                                                   bodyDef.linearDamping = 0;
                                                   bodyDef.angularDamping = 0;
                                                   //bodyDef.angle = 1.57079633;
                                                   bodyDef.fixedRotation = true;


                                                   var dx = Math.Cos(current.body.GetAngle() + current.CameraRotation + a);
                                                   var dy = Math.Sin(current.body.GetAngle() + current.CameraRotation + a);

                                                   var body = damage_b2world.CreateBody(bodyDef);
                                                   body.SetPosition(
                                                       new b2Vec2(
                                                           current.body.GetPosition().x + dx * 0.3,
                                                           current.body.GetPosition().y + dy * 0.3
                                                       )
                                                   );

                                                   body.SetLinearVelocity(
                                                          new b2Vec2(
                                                            dx * 100,
                                                           dy * 100
                                                       )
                                                   );

                                                   var fixDef = new Box2D.Dynamics.b2FixtureDef();
                                                   fixDef.density = 20;
                                                   fixDef.friction = 0.0;
                                                   fixDef.restitution = 0;


                                                   fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(0.3);


                                                   var fix = body.CreateFixture(fixDef);
                                               };
                                           #endregion

                                           CreateBullet(-6.DegreesToRadians());
                                           CreateBullet(-2.DegreesToRadians());
                                           CreateBullet(2.DegreesToRadians());
                                           CreateBullet(6.DegreesToRadians());


                                       }
                                   );
                            }

                        }
                        #endregion


                    };
            };
        }
        public StarlingGameSpriteWithPedControlTimetravel()
        {
            var textures = new StarlingGameSpriteWithPedTextures(new_tex_crop, new_texsprite_crop);



            this.onbeforefirstframe += (stage, s) =>
            {


                // 1000 / 15
                // this means 15 samples per second
                var ilag = 7;

                #region man_with_lag
                var man_with_lag = new PhysicalPed(textures, this);

                //karmaman.body.SetActive(false);

                for (int i = 0; i < ilag; i++)
                {
                    man_with_lag.KarmaInput4.Enqueue(
                        new KeySample()
                    );
                }

                man_with_lag.body.SetPosition(
                     new b2Vec2(-8, 8)
                 );

                man_with_lag.karmabody.SetPosition(
                     new b2Vec2(-8, 8)
                 );
                #endregion


                #region man_with_karma
                var man_with_karma = new PhysicalPed(textures, this);

                man_with_karma.SetPositionAndAngle(-8, -8);



                #endregion



                var physical0 = new PhysicalPed(textures, this);
                current = physical0;

                // 32x32 = 15FPS?
                // 24x24 35?

                #region the others
                for (int ix = 1; ix < 4; ix++)
                    for (int iy = 1; iy < 4; iy++)
                    {
                        var p = new PhysicalPed(textures, this);

                        p.SetPositionAndAngle(
                           8 * ix, 8 * iy
                        );
                    }
                #endregion


                #region __keyDown
                var __keyDown = new KeySample();

                stage.keyDown +=
                   e =>
                   {
                       // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                       if (e.altKey)
                           __keyDown[Keys.Alt] = true;

                       __keyDown[(Keys)e.keyCode] = true;
                   };

                stage.keyUp +=
                 e =>
                 {
                     if (!e.altKey)
                         __keyDown[Keys.Alt] = false;

                     __keyDown[(Keys)e.keyCode] = false;
                 };

                #endregion

                bool mode_changepending = false;

                var man_with_karma_karmavisuals = new Queue<VisualPed>();
                var physical0_karmavisuals = new Queue<VisualPed>();
                var karmasw = new Stopwatch();
                karmasw.Start();



                //var inputfilter = new Stopwatch();
                //inputfilter.Start();

                onsyncframe += delegate
                {
                    physical0.SetVelocityFromInput(__keyDown);

                    man_with_karma.SetVelocityFromInput(__keyDown);

                    man_with_lag.KarmaInput4.Enqueue(new KeySample { value = __keyDown.value });
                    var physical2_karmastream_keydown = man_with_lag.KarmaInput4.Dequeue();

                    // this one lives in the past?
                    man_with_lag.SetVelocityFromInput(physical2_karmastream_keydown);


                    #region karmavisuals
                    if (karmasw.ElapsedMilliseconds > (1000 / 15))
                    {
                        karmasw.Restart();

                        {
                            physical0_karmavisuals.Enqueue(physical0.visual);

                            if (physical0_karmavisuals.Count > ilag)
                            {
                                physical0_karmavisuals.Dequeue().Orphanize();
                            }

                            physical0.visual.shadow.visible = false;
                            physical0.visual.currentvisual.alpha = 0.2;

                            physical0.visual = new VisualPed(textures, this, physical0.visual.AnimateSeed)
                            {
                                LayOnTheGround = physical0.visual.LayOnTheGround
                            };

                            physical0.ShowPositionAndAngle();
                        }


                        {
                            man_with_karma_karmavisuals.Enqueue(man_with_karma.visual);

                            if (man_with_karma_karmavisuals.Count > 4)
                            {
                                man_with_karma_karmavisuals.Dequeue().Orphanize();
                            }

                            man_with_karma.visual.shadow.visible = false;
                            man_with_karma.visual.currentvisual.alpha = 0.2;

                            man_with_karma.visual = new VisualPed(textures, this, man_with_karma.visual.AnimateSeed)
                            {
                                LayOnTheGround = man_with_karma.visual.LayOnTheGround
                            };

                            man_with_karma.ShowPositionAndAngle();
                        }
                    }
                    #endregion



                };

                onframe += delegate
                {
                    this.Text = new
                    {
                        da = (man_with_lag.body.GetAngle() - physical0.body.GetAngle()).RadiansToDegrees(),
                        dx = man_with_lag.body.GetPosition().x - physical0.body.GetPosition().x,
                        dy = man_with_lag.body.GetPosition().y - physical0.body.GetPosition().y
                    }.ToString();




                    #region simulate a weapone!
                    if (__keyDown[Keys.ControlKey])
                        if (frameid % 20 == 0)
                        {
                            var bodyDef = new b2BodyDef();

                            bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

                            // stop moving if legs stop walking!
                            bodyDef.linearDamping = 0;
                            bodyDef.angularDamping = 0;
                            //bodyDef.angle = 1.57079633;
                            bodyDef.fixedRotation = true;

                            var body = ground_b2world.CreateBody(bodyDef);
                            body.SetPosition(
                                new b2Vec2(
                                    current.body.GetPosition().x + 2,
                                    current.body.GetPosition().y + 2
                                )
                            );

                            body.SetLinearVelocity(
                                   new b2Vec2(
                                     100,
                                    100
                                )
                            );

                            var fixDef = new Box2D.Dynamics.b2FixtureDef();
                            fixDef.density = 0.1;
                            fixDef.friction = 0.01;
                            fixDef.restitution = 0;


                            fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(1.0);


                            var fix = body.CreateFixture(fixDef);

                            //body.SetPosition(
                            //    new b2Vec2(0, -100 * 16)
                            //);
                        }
                    #endregion


                    #region mode
                    if (__keyDown[Keys.Space])
                    {
                        // space is not down.
                        mode_changepending = true;
                    }
                    else
                    {
                        if (mode_changepending)
                        {
                            if (physical0.visual.LayOnTheGround)
                                physical0.visual.LayOnTheGround = false;
                            else
                                physical0.visual.LayOnTheGround = true;

                            mode_changepending = false;



                        }
                    }
                    #endregion



                };


            };



        }
        public StarlingGameSpriteWithHindWeaponized()
        {
            var textures_ped = new StarlingGameSpriteWithPedTextures(
                this.new_tex_crop,
                this.new_texsprite_crop
                );


            var textures_hind = new StarlingGameSpriteWithHindTextures(this.new_tex_crop);
            var textures_rocket = new StarlingGameSpriteWithRocketTextures(this.new_tex_crop);
            var textures_map = new StarlingGameSpriteWithMapTextures(new_tex_crop);
            var textures_bunker = new StarlingGameSpriteWithBunkerTextures(new_tex_crop);
            var textures_explosions = new StarlingGameSpriteWithMapExplosionsTextures(new_tex96);

            //this.internalscale = 1.0;
            this.disablephysicsdiagnostics = true;

            this.onbeforefirstframe += (stage, s) =>
            {
                stage.color = 0x75C64F;

                // hind is looking right
                var explosins = new List<ExplosionInfo>();

                for (int i = -12; i < 12; i++)
                {
                    new Image(textures_map.road0()).AttachTo(Content).x = 256 * i;

                    var bunker0 = new PhysicalBunker(textures_bunker, this);
                    bunker0.SetPositionAndAngle(-12 * i, 24);

                    var z = new PhysicalPed(textures_ped, this);

                    z.SetPositionAndAngle(16 * i, 0);
                    z.BehaveLikeZombie();

                    //var exp = new Image(textures_explosions.explosions[0]()).AttachTo(Content);
                    //explosins.Add(exp);
                    //exp.scaleX = 2.0;
                    //exp.scaleY = 2.0;

                    //exp.x = 256 * i;
                }

                #region other units
                for (int i = 3; i < 9; i++)
                {
                    var hind2 = new PhysicalHindWeaponized(textures_hind, textures_rocket, this)
                    {
                        AutomaticTakeoff = true
                    };

                    hind2.SetPositionAndAngle(
                        i * 16, 8, random.NextDouble()
                    );





                }
                #endregion


                #region CreateExplosion
                this.CreateExplosion = (x, y) =>
                {
                    var size = 0.2 + 0.2 * random.NextDouble();

                    sb.snd_explosion.play(
                        sndTransform: new ScriptCoreLib.ActionScript.flash.media.SoundTransform(size)
                    );

                    var exp = new Image(textures_explosions.explosions[0]()).AttachTo(Content);
                    var cm = new Matrix();

                    cm.translate(-32, -32);
                    cm.scale(5 * size, 5 * size);
                    cm.translate(16 * x, 16 * y);

                    exp.transformationMatrix = cm;

                    explosins.Add(
                    new ExplosionInfo { visual = exp }
                    );

                };
                #endregion


                var hind0 = new PhysicalHindWeaponized(
                    textures_hind, textures_rocket, this
                    );


                current = hind0;


                #region __keyDown

                stage.keyDown +=
                   e =>
                   {
                       e.preventDefault();
                       // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                       if (e.altKey)
                           __keyDown[System.Windows.Forms.Keys.Alt] = true;

                       __keyDown[(System.Windows.Forms.Keys)e.keyCode] = true;
                   };

                stage.keyUp +=
                 e =>
                 {
                     e.preventDefault();

                     if (!e.altKey)
                         __keyDown[System.Windows.Forms.Keys.Alt] = false;

                     __keyDown[(System.Windows.Forms.Keys)e.keyCode] = false;
                 };

                #endregion

                bool mode_changepending = false;

                var dx_stop = false;
                var dy_stop = false;
                var dx = 0.0;
                var dy = 0.0;

                //              Error: Error #3707: Property can not be set in non full screen mode.
                //at flash.display::Stage/set mouseLock()

                // http://inflagrantedelicto.memoryspiral.com/2012/07/as3-quickie-mouse-lock-and-relative-mouse-coordinates/
                stage.fullScreen +=
                    delegate
                    {
                        if (stage.displayState == StageDisplayState.NORMAL)
                            return;

                        // http://helpx.adobe.com/flash-player/release-note/fp_114_air_34_release_notes.html
                        // · Mouse Lock feature disabled after entering Full Screen Interactive mode(3174344)
                        //stage.mouseLock = true;

                    };



                stage.mouseDown +=
                    e =>
                    {
                        if (stage.displayState == StageDisplayState.NORMAL)
                        {
                            return;
                        }

                        __keyDown[Keys.ControlKey] = true;
                    };

                stage.doubleClickEnabled = true;
                stage.doubleClick += delegate
                {
                    if (stage.displayState == StageDisplayState.NORMAL)
                    {
                        stage.displayState = ScriptCoreLib.ActionScript.flash.display.StageDisplayState.FULL_SCREEN;
                        stage.displayState = ScriptCoreLib.ActionScript.flash.display.StageDisplayState.FULL_SCREEN_INTERACTIVE;
                        return;
                    }
                };

                stage.mouseUp +=
                    e =>
                    {
                        if (stage.displayState == StageDisplayState.NORMAL)
                        {
                            return;
                        }

                        __keyDown[Keys.ControlKey] = false;
                    };

                stage.mouseMove +=
                     e =>
                     {
                         if (stage.displayState == StageDisplayState.NORMAL)
                         {
                             dx = 0;
                             dy = 0;
                             return;
                         }

                         //dynamic ee = e;

                         //// ReferenceError: Erroe.r #1069: Property movementX not found on flash.events.MouseEvent and there is no default value.
                         //double movementX = ee.me.ovementX;
                         //double movementY = ee.movementY;

                         // http://www.levelxgames.com/2012/11/how-to-overlay-abode-air-sdk-over-flex-sdk/
                         //dx += e.movementX;
                         //dy += e.movementY;
                     };

                onsyncframe +=
                   delegate
                   {
                       this.Text = new { dx }.ToString();

                       foreach (var item in explosins.ToArray())
                       {
                           item.index++;

                           if (item.index == textures_explosions.explosions.Length)
                           {
                               item.visual.Orphanize();
                               explosins.Remove(item);
                           }
                           else
                           {
                               item.visual.texture = textures_explosions.explosions[item.index]();
                           }
                       }

                       #region mode
                       if (!__keyDown[System.Windows.Forms.Keys.Space])
                       {
                           // space is not down.
                           mode_changepending = true;
                       }
                       else
                       {
                           if (mode_changepending)
                           {
                               (current as PhysicalHind).With(
                                   hind1 =>
                                   {
                                       if (hind1.visual.Altitude == 0)
                                           hind1.VerticalVelocity = 1.0;
                                       else
                                           hind1.VerticalVelocity = -0.4;

                                   }
                               );






                               mode_changepending = false;



                           }
                       }
                       #endregion

                       if (dx < 0)
                       {
                           __keyDown[Keys.Left] = true;
                           __keyDown[Keys.Right] = false;
                           __keyDown.forcex = (Math.Abs(dx) / 100.0).Min(1.0);
                           dx = 0;
                           dx_stop = true;
                       }
                       else if (dx > 0)
                       {
                           __keyDown[Keys.Right] = true;
                           __keyDown[Keys.Left] = false;
                           __keyDown.forcex = (Math.Abs(dx) / 100.0).Min(1.0);
                           dx = 0;
                           dx_stop = true;
                       }
                       else
                       {
                           if (dx_stop)
                           {
                               __keyDown[Keys.Left] = false;
                               __keyDown[Keys.Right] = false;
                           }
                           dx_stop = false;

                           __keyDown.forcex = 1.0;
                       }

                       if (dy < 0)
                       {
                           __keyDown[Keys.Up] = true;
                           __keyDown[Keys.Down] = false;
                           __keyDown.forcey = (Math.Abs(dy) / 200.0).Min(1.0);
                           dy_stop = true;
                       }
                       else if (dy > 0)
                       {
                           __keyDown[Keys.Up] = false;
                           __keyDown[Keys.Down] = true;
                           __keyDown.forcey = (Math.Abs(dy) / 200.0).Min(1.0);
                           dy_stop = true;
                       }
                       else
                       {
                           if (dy_stop)
                           {
                               __keyDown[Keys.Up] = false;
                               __keyDown[Keys.Down] = false;
                           }
                           dy_stop = false;

                           __keyDown.forcey = 1.0;
                       }

                       current.SetVelocityFromInput(__keyDown);



                       #region simulate a weapone!
                       if (__keyDown[System.Windows.Forms.Keys.ControlKey])
                           if (syncframeid % 3 == 0)
                           {
                               sb.snd_missleLaunch.play();
                               hind0.FireRocket();
                           }
                       #endregion
                   };
            };

        }
Example #8
0
        public static void BehaveLikeZombie(this PhysicalPed physical0)
        {
            physical0.speed = 4;


            physical0.visual.WalkLikeZombie = true;

            //var f = new ColorMatrixFilter();
            //f.adjustSaturation(-1);

            ////f.concat(
            ////         new double[] {

            ////              0, 0,  0,  0, 0,
            ////              0, 1,  0,  0, 0,
            ////              0, 0, 0,  0, 0,
            ////              0,  0,  0,  1,   0

            ////                                        }
            ////     );
            ////f.adjustSaturation(-0.8);

            //physical0.visual.currentvisual.filter = f;

            var seed = physical0.Context.random.Next(30);

            physical0.Context.onsyncframe +=
                delegate
            {
                var frame = (seed + physical0.Context.syncframeid) % 20;


                if (physical0.visual.LayOnTheGround)
                {
                    if (frame == 0)
                    {
                        if (physical0.Context.random.NextDouble() < 0.1)
                        {
                            // review!
                            physical0.body.SetActive(true);
                            physical0.damagebody.SetActive(true);
                            physical0.visual.LayOnTheGround = false;
                        }
                    }
                    return;
                }


                Func <PhysicalPed, double, double> GetMotivation =
                    (candidateped, distance) =>
                {
                    if (candidateped.AttractZombies)
                    {
                        return(distance);
                    }


                    return(16 + distance);
                };

                var target =
                    from candidate in physical0.Context.units

                    let candidateped = candidate as PhysicalPed
                                       where candidateped != null

                                       // zombies wont attract zombies
                                       where !candidateped.visual.WalkLikeZombie

                                       let gap = new __vec2(
                        (float)(candidate.body.GetPosition().x - physical0.body.GetPosition().x),
                        (float)(candidate.body.GetPosition().y - physical0.body.GetPosition().y)
                        )

                                                 let distance = gap.GetLength()

                                                                let CloseEnoughToAttract = distance < 16
                                                                                           let PreventWanderingOff = distance > 48
                                                                                                                     where CloseEnoughToAttract || PreventWanderingOff

                                                                                                                     orderby GetMotivation(candidateped, distance) ascending

                                                                                                                     select new { candidateped, distance, gap };

                // this costs 10% Total time
                var firsttarget = target.FirstOrDefault();

                if (firsttarget != null)
                {
                    // stare at victim
                    var up = new KeySample();

                    if (firsttarget.distance > 3)
                    {
                        up[Keys.Up] = true;
                        up.forcey   = firsttarget.distance.Min(8) / 4.0;
                    }
                    else
                    {
                        // attack isntead!
                    }

                    physical0.SetVelocityFromInput(up);


                    physical0.SetPositionAndAngle(
                        physical0.body.GetPosition().x,
                        physical0.body.GetPosition().y,
                        firsttarget.gap.GetRotation()
                        );

                    return;
                }


                if (frame == 0)
                {
                    // where to?
                    if (physical0.Context.random.NextDouble() < 0.5)
                    {
                        var up = new KeySample();
                        up[Keys.Left] = true;
                        up.forcex     = physical0.Context.random.NextDouble();
                        physical0.SetVelocityFromInput(up);
                    }
                    else
                    {
                        var up = new KeySample();
                        up[Keys.Right] = true;
                        up.forcex      = physical0.Context.random.NextDouble();
                        physical0.SetVelocityFromInput(up);
                    }
                }


                if (frame == 3)
                {
                    var up = new KeySample();
                    up[Keys.Up] = true;
                    physical0.SetVelocityFromInput(up);
                }

                if (frame == 17)
                {
                    var up = new KeySample();
                    physical0.SetVelocityFromInput(up);
                }
            };
        }
        public StarlingGameSpriteWithShop()
        {
            var textures_bunker = new StarlingGameSpriteWithBunkerTextures(this.new_tex_crop);
            var textures_ped = new StarlingGameSpriteWithPedTextures(this.new_tex_crop, this.new_texsprite_crop);

            this.disablephysicsdiagnostics = true;

            this.onbeforefirstframe += (stage, s) =>
            {
                s.stage.color = 0xB27D51;
                current = new PhysicalPed(textures_ped, this)
                {

                    AttractZombies = true
                };

                current.SetPositionAndAngle(
                    16.Random(),
                    16.Random(),
                    360.Random().DegreesToRadians()
                );


                #region others
                for (int ix = 0; ix < 2; ix++)
                    for (int iy = 0; iy < 2; iy++)
                    {
                        var p = new PhysicalPed(textures_ped, this);

                        p.SetPositionAndAngle(
                            8 * ix, 8 * iy, random.NextDouble()
                        );

                        p.BehaveLikeZombie();
                    }
                #endregion

                var myshop = new PhysicalBunker(textures_bunker, this, IsShop: true);

                myshop.SetPositionAndAngle(
                          -8, -8
                      );



                #region __keyDown

                stage.keyDown +=
                   e =>
                   {
                       __keyDown.forcex = 1.0;
                       __keyDown.forcey = 1.0;

                       // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                       if (e.altKey)
                           __keyDown[System.Windows.Forms.Keys.Alt] = true;

                       __keyDown[(System.Windows.Forms.Keys)e.keyCode] = true;
                   };

                stage.keyUp +=
                 e =>
                 {
                     if (!e.altKey)
                         __keyDown[System.Windows.Forms.Keys.Alt] = false;

                     __keyDown[(System.Windows.Forms.Keys)e.keyCode] = false;
                 };

                #endregion



                bool entermode_changepending = false;


                onsyncframe +=
                    delegate
                    {
                        current.SetVelocityFromInput(__keyDown);

                        #region Enter
                        if (!__keyDown[System.Windows.Forms.Keys.Enter])
                        {
                            // space is not down.
                            entermode_changepending = true;
                        }
                        else
                        {
                            if (entermode_changepending)
                            {
                                entermode_changepending = false;

                                // enter another vehicle?

                                var candidatedriver = current as PhysicalPed;
                                if (candidatedriver != null)
                                {

                                    var target =
                                         from candidatevehicle in units
                                         where candidatevehicle.driverseat != null

                                         // can enter if the seat is full.
                                         // unless we kick them out before ofcourse
                                         where candidatevehicle.driverseat.driver == null

                                         let distance = new __vec2(
                                             (float)(candidatedriver.body.GetPosition().x - candidatevehicle.body.GetPosition().x),
                                             (float)(candidatedriver.body.GetPosition().y - candidatevehicle.body.GetPosition().y)
                                         ).GetLength()

                                         where distance < 6

                                         orderby distance ascending
                                         select new { candidatevehicle, distance };

                                    target.FirstOrDefault().With(
                                        x =>
                                        {
                                            Console.WriteLine(new { x.distance });

                                            //current.loc.visible = false;
                                            current.body.SetActive(false);


                                            x.candidatevehicle.driverseat.driver = candidatedriver;
                                            candidatedriver.seatedvehicle = x.candidatevehicle;

                                            current = x.candidatevehicle;

                                            //if (current is PhysicalJeep)
                                            //{
                                            //    sb.snd_jeepengine_start.play();
                                            //}

                                            //else 
                                            if (current is PhysicalBunker)
                                            {
                                                if ((current as PhysicalBunker).visual_shopoverlay.visible)
                                                {
                                                    sb.snd_its_a_shop.play();
                                                    if (ShopEnter != null)
                                                        ShopEnter(candidatedriver);
                                                }
                                                else
                                                {
                                                    if (random.NextDouble() > 0.5)
                                                    {
                                                        sb.snd_itsempty.play();
                                                    }
                                                    else
                                                    {
                                                        sb.snd_nothinghere.play();
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                sb.snd_dooropen.play(
                                                  sndTransform: new SoundTransform(
                                                     0.3
                                                  )
                                               );
                                            }

                                            //if (current is PhysicalHind)
                                            //{
                                            //    nightvision_on();
                                            //}

                                            //hud_update();


                                            //switchto(x.x);
                                            move_zoom = 1;

                                            // fast start
                                            //(current as PhysicalHind).With(
                                            //    hind => hind.VerticalVelocity = 1
                                            //);
                                        }
                                    );
                                }
                                else
                                {
                                    (current.driverseat.driver as PhysicalPed).With(
                                        driver =>
                                        {
                                            // get out of the lift..

                                            //nightvision_off();

                                            current.driverseat.driver = null;
                                            driver.seatedvehicle = null;
                                            current.SetVelocityFromInput(new KeySample());

                                            if (current is PhysicalBunker)
                                            {
                                                if ((current as PhysicalBunker).visual_shopoverlay.visible)
                                                {
                                                    if (ShopExit != null)
                                                        ShopExit();
                                                }
                                            }
                                            // crashland?
                                            //(current as PhysicalHind).With(
                                            //    hind =>
                                            //    {
                                            //        if (hind.visual.Altitude > 0)
                                            //        {
                                            //            hind.VerticalVelocity = -1;
                                            //            sb.snd_touchdown.play();
                                            //        }
                                            //    }

                                            //);

                                            sb.snd_dooropen.play(
                                              sndTransform: new SoundTransform(
                                                 0.3
                                              )
                                           );
                                            //if (current.body.GetType() != Box2D.Dynamics.b2Body.b2_dynamicBody)
                                            //{
                                            //    sb.snd_letsgo.play();
                                            //}

                                            current = driver;
                                            driver.body.SetActive(true);
                                            driver.body.SetAngularVelocity(-11);

                                            //hud_update();
                                            move_zoom = 1;
                                        }
                                    );
                                }

                            }
                        }
                        #endregion

                    };
            };

        }
        public StarlingGameSpriteWithTestDriversSync()
        {
            var textures_map = new StarlingGameSpriteWithMapTextures(new_tex_crop);

            var textures_ped = new StarlingGameSpriteWithPedTextures(this.new_tex_crop, this.new_texsprite_crop);
            var textures_jeep = new StarlingGameSpriteWithJeepTextures(this.new_tex_crop);
            var textures_tank = new StarlingGameSpriteWithTankTextures(new_texsprite_crop);
            var textures_hind = new StarlingGameSpriteWithHindTextures(this.new_tex_crop);
            var textures_rocket = new StarlingGameSpriteWithRocketTextures(this.new_tex_crop);

            var textures_bunker = new StarlingGameSpriteWithBunkerTextures(this.new_tex_crop);

            this.onbeforefirstframe += (stage, s) =>
            {
                #region :ego
                var ego = new PhysicalPed(textures_ped, this)
                {
                    Identity = sessionid + ":ego"
                };

                ego.SetPositionAndAngle(
                    random.NextDouble() * -8,
                    random.NextDouble() * -8,
                    random.NextDouble() * Math.PI
                );

                current = ego;
                #endregion


                #region KeySample
                var __keyDown = new KeySample();

                stage.keyDown +=
                   e =>
                   {
                       // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                       if (e.altKey)
                           __keyDown[System.Windows.Forms.Keys.Alt] = true;

                       __keyDown[(System.Windows.Forms.Keys)e.keyCode] = true;
                   };

                stage.keyUp +=
                 e =>
                 {
                     if (!e.altKey)
                         __keyDown[System.Windows.Forms.Keys.Alt] = false;

                     __keyDown[(System.Windows.Forms.Keys)e.keyCode] = false;
                 };

                #endregion

                new PhysicalBunker(textures_bunker, this) { Identity = "bunker:0" }.SetPositionAndAngle(0, -24);

                var hind0 = new PhysicalHindWeaponized(textures_hind, textures_rocket, this)
                {
                    Identity = ":1",

                    AutomaticTakeoff = true,
                    AutomaticTouchdown = true

                };

                hind0.SetPositionAndAngle(-12, -12);

                new Image(textures_map.touchdown()).AttachTo(Content).y = 256;

                new PhysicalJeep(textures_jeep, this) { Identity = ":2" }.SetPositionAndAngle(0, -12);
                new PhysicalTank(textures_tank, this) { Identity = ":3" }.SetPositionAndAngle(12, -12);





                #region other
                Func<string, RemoteGame> other = __egoid =>
                {
                    // that other game has sent us a sync frame!

                    var already_known_other = others.FirstOrDefault(k => k.__egoid == __egoid);

                    if (already_known_other == null)
                    {
                        already_known_other = new RemoteGame
                        {
                            __egoid = __egoid,

                            // this
                            __syncframeid = this.syncframeid
                        };

                        others.Add(already_known_other);
                    }


                    return already_known_other;
                };
                #endregion


                #region __at_sync
                __at_sync += __egoid =>
                {
                    // that other game has sent us a sync frame!

                    var o = other(__egoid);

                    o.__syncframeid++;
                    // move on!
                };
                #endregion

                #region __at_SetVerticalVelocity
                __at_SetVerticalVelocity +=
                    (string __sessionid, string identity, string value) =>
                    {
                        var o = other(__sessionid);

                        var u = this.units.FirstOrDefault(k => k.Identity == identity);

                        (u as PhysicalHind).With(hind1 => hind1.VerticalVelocity = double.Parse(value));

                        (u as PhysicalPed).With(
                              physical0 =>
                              {
                                  //                                  BCL needs another method, please define it.
                                  //Cannot call type without script attribute :
                                  //System.Convert for Boolean ToBoolean(Double) used at

                                  var LayOnTheGround = double.Parse(value);

                                  if (LayOnTheGround == 1)
                                      physical0.visual.LayOnTheGround = true;
                                  else
                                      physical0.visual.LayOnTheGround = false;




                              }
                          );

                    };
                #endregion

                #region __at_SetVelocityFromInput
                __at_SetVelocityFromInput +=
                    (
                        string __egoid,
                        string __identity,
                        string __KeySample,
                        string __fixup_x,
                        string __fixup_y,
                        string __fixup_angle

                        ) =>
                    {
                        var o = other(__egoid);

                        var u = this.units.FirstOrDefault(k => k.Identity == __identity);

                        if (u == null)
                            if (o.ego == null)
                            {
                                // the only object we can be creating implicitly is
                                // the remote ego

                                u = new PhysicalPed(textures_ped, this)
                                {
                                    Identity = __identity,
                                    RemoteGameReference = o
                                };

                                u.SetPositionAndAngle(
                                    double.Parse(__fixup_x),
                                    double.Parse(__fixup_y),
                                    double.Parse(__fixup_angle)
                                );

                                o.ego = u;
                            }


                        // set the input!


                        u.SetVelocityFromInput(
                            new KeySample
                            {
                                value = int.Parse(__KeySample),

                                fixup = true,

                                x = double.Parse(__fixup_x),
                                y = double.Parse(__fixup_y),
                                angle = double.Parse(__fixup_angle)

                            }
                        );

                    };
                #endregion


                #region __at_enterorexit
                __at_enterorexit += (__egoid, __from, __to) =>
                {
                    var o = other(__egoid);

                    var ufrom = this.units.FirstOrDefault(k => k.Identity == __from);
                    var uto = this.units.FirstOrDefault(k => k.Identity == __to);

                    (ufrom as PhysicalPed).With(
                        candidatedriver =>
                        {
                            if (uto != null)
                                if (uto.driverseat != null)
                                    if (uto.driverseat.driver == null)
                                    {
                                        // and the devil enters
                                        uto.RemoteGameReference = o;

                                        candidatedriver.body.SetActive(false);

                                        uto.driverseat.driver = candidatedriver;
                                        candidatedriver.seatedvehicle = uto;
                                    }
                        }
                    );

                    (uto as PhysicalPed).With(
                        driver =>
                        {
                            if (ufrom != null)
                                if (ufrom.driverseat != null)
                                    if (ufrom.driverseat.driver == driver)
                                    {
                                        // relinguish that vehicle. no longer posessed :)
                                        ufrom.RemoteGameReference = null;

                                        // stop the vehicle
                                        ufrom.SetVelocityFromInput(new KeySample());

                                        // get out of the lift..
                                        ufrom.driverseat.driver = null;

                                        driver.seatedvehicle = null;
                                        driver.body.SetActive(true);
                                    }
                        }
                    );

                };
                #endregion



                bool entermode_changepending = false;
                bool mode_changepending = false;

                onsyncframe += delegate
                {

                    #region mode
                    if (!__keyDown[System.Windows.Forms.Keys.Space])
                    {
                        // space is not down.
                        mode_changepending = true;
                    }
                    else
                    {
                        if (mode_changepending)
                        {
                            (current as PhysicalHind).With(
                                hind1 =>
                                {
                                    if (hind1.visual.Altitude == 0)
                                        hind1.VerticalVelocity = 1.0;
                                    else
                                        hind1.VerticalVelocity = -0.4;

                                    __raise_SetVerticalVelocity(
                                        "" + this.sessionid,
                                        hind1.Identity,
                                        "" + hind1.VerticalVelocity
                                    );
                                }
                            );

                            (current as PhysicalPed).With(
                                 physical0 =>
                                 {
                                     if (physical0.visual.LayOnTheGround)
                                         physical0.visual.LayOnTheGround = false;
                                     else
                                         physical0.visual.LayOnTheGround = true;


                                     //                                     BCL needs another method, please define it.
                                     //Cannot call type without script attribute :
                                     //System.Convert for Double ToDouble(Boolean) used at

                                     var value = 0;
                                     if (physical0.visual.LayOnTheGround)
                                         value = 1;

                                     __raise_SetVerticalVelocity(
                                         "" + this.sessionid,
                                         physical0.Identity,
                                         "" + value
                                     );

                                 }
                             );




                            mode_changepending = false;



                        }
                    }
                    #endregion



                    #region entermode_changepending
                    if (!__keyDown[System.Windows.Forms.Keys.Enter])
                    {
                        // space is not down.
                        entermode_changepending = true;
                    }
                    else
                    {
                        if (entermode_changepending)
                        {
                            entermode_changepending = false;

                            // enter another vehicle?

                            var candidatedriver = current as PhysicalPed;
                            if (candidatedriver != null)
                            {
                                var target =
                                     from candidatevehicle in units
                                     where candidatevehicle.driverseat != null

                                     // can enter if the seat is full.
                                     // unless we kick them out before ofcourse
                                     where candidatevehicle.driverseat.driver == null

                                     let distance = new __vec2(
                                         (float)(candidatedriver.body.GetPosition().x - candidatevehicle.body.GetPosition().x),
                                         (float)(candidatedriver.body.GetPosition().y - candidatevehicle.body.GetPosition().y)
                                     ).GetLength()

                                     where distance < 6

                                     orderby distance ascending
                                     select new { candidatevehicle, distance };

                                target.FirstOrDefault().With(
                                    x =>
                                    {
                                        Console.WriteLine(new { x.distance });

                                        __raise_enterorexit(
                                           "" + this.sessionid,
                                           candidatedriver.Identity,
                                           x.candidatevehicle.Identity
                                       );

                                        //current.loc.visible = false;
                                        current.body.SetActive(false);

                                        x.candidatevehicle.driverseat.driver = candidatedriver;
                                        candidatedriver.seatedvehicle = x.candidatevehicle;

                                        move_zoom = 1;
                                        current = x.candidatevehicle;





                                        //if (current.body.GetType() == Box2D.Dynamics.b2Body.b2_dynamicBody)
                                        //{
                                        //    hud.texture = textures_ped.hud_look_goggles();
                                        //}
                                        //else
                                        //{
                                        //    hud.texture = textures_ped.hud_look_building();
                                        //}

                                        //switchto(x.x);

                                        // fast start
                                        //(current as PhysicalHind).With(
                                        //    hind => hind.VerticalVelocity = 1
                                        //);
                                    }
                                );
                            }
                            else
                            {
                                (current.driverseat.driver as PhysicalPed).With(
                                    driver =>
                                    {
                                        // stop the vehicle
                                        current.SetVelocityFromInput(new KeySample());

                                        // get out of the lift..
                                        current.driverseat.driver = null;

                                        driver.seatedvehicle = null;
                                        driver.body.SetActive(true);


                                        // crashland?
                                        //(current as PhysicalHind).With(
                                        //    hind => hind.VerticalVelocity = -1
                                        //);

                                        __raise_enterorexit(
                                             "" + this.sessionid,
                                             current.Identity,
                                             driver.Identity
                                         );

                                        current = driver;
                                        //hud.texture = textures_ped.hud_look();
                                        move_zoom = 1;
                                    }
                                );
                            }

                        }
                    }
                    #endregion



                    current.SetVelocityFromInput(__keyDown);

                    #region simulate a weapone!
                    if (__keyDown[System.Windows.Forms.Keys.ControlKey])
                        if (syncframeid % 3 == 0)
                        {
                            (this.current as PhysicalHindWeaponized).With(
                                h =>
                                {
                                    //sb.snd_missleLaunch.play();

                                    h.FireRocket();
                                }
                            );

                        }
                    #endregion

                    __raise_SetVelocityFromInput(
                         "" + sessionid,
                         current.Identity,
                         "" + current.CurrentInput.value,
                         "" + current.body.GetPosition().x,
                         "" + current.body.GetPosition().y,
                         "" + current.body.GetAngle()
                     );


                    // tell others this sync frame ended for us
                    __raise_sync("" + sessionid);
                };


            };
        }
        public StarlingGameSpriteWithTestDrivers()
        {
            var textures_ped = new StarlingGameSpriteWithPedTextures(this.new_tex_crop, this.new_texsprite_crop);
            // ??
            //<Reference Include="FlashHeatZeeker.UnitHindControl">
            //  <HintPath>..\packages\FlashHeatZeeker.UnitHindControl.1.0.0.0\lib\FlashHeatZeeker.UnitHindControl.dll</HintPath>
            //</Reference>
            // "X:\jsc.svn\examples\actionscript\svg\FlashHeatZeeker\packages\FlashHeatZeeker.UnitHindControl.1.0.0.0\lib\FlashHeatZeeker.UnitHindControl.dll"
            // "X:\jsc.svn\examples\actionscript\svg\FlashHeatZeeker\packages\FlashHeatZeeker.UnitHind.1.0.0.0\lib\FlashHeatZeeker.UnitHind.dll"
            var textures_hind = new StarlingGameSpriteWithHindTextures(this.new_tex_crop);
            var textures_jeep = new StarlingGameSpriteWithJeepTextures(this.new_tex_crop);
            var textures_tank = new StarlingGameSpriteWithTankTextures(this.new_texsprite_crop);
            var textures_cannon = new StarlingGameSpriteWithCannonTextures(this.new_tex_crop);
            var textures_bunker = new StarlingGameSpriteWithBunkerTextures(this.new_tex_crop);
            var textures_map = new StarlingGameSpriteWithMapTextures(new_tex_crop);
            var textures_rocket = new StarlingGameSpriteWithRocketTextures(this.new_tex_crop);

            this.disablephysicsdiagnostics = true;



            // ????
            this.onbeforefirstframe += (stage, s) =>
            {
                var hud = new Image(textures_ped.hud_look()).AttachTo(this);


                for (int i = 0; i < 32; i++)
                {
                    new Image(textures_map.hill1()).AttachTo(Content).With(
                        hill =>
                        {
                            hill.x = 2048.Random();
                            hill.y = 2048.Random();
                        }
                    );

                    new Image(textures_map.hole1()).AttachTo(Content).With(
                        hill =>
                        {
                            hill.x = 2048.Random();
                            hill.y = 2048.Random();
                        }
                    );

                    new Image(textures_map.grass1()).AttachTo(Content).With(
                        hill =>
                        {
                            hill.x = 2048.Random();

                            var y = -2048.Random() - 512 - 256;
                            hill.y = y;
                        }
                    );
                }

                for (int i = 0; i < 128; i++)
                {


                    var x = 2048.Random();
                    var y = -2048.Random() - 512 - 256;

                    new Image(textures_map.tree0_shadow()).AttachTo(Content).MoveTo(x + 16, y + 16);
                    new Image(textures_map.tree0()).AttachTo(Content).MoveTo(x, y);
                }
                for (int i = -12; i < 12; i++)
                {
                    new Image(textures_map.road0()).AttachTo(Content).x = 256 * i;
                }

                new Image(textures_map.touchdown()).AttachTo(Content).MoveTo(256, -256);
                new Image(textures_map.touchdown()).AttachTo(Content).y = 256;

                new PhysicalHindWeaponized(textures_hind, textures_rocket, this) { AutomaticTakeoff = true }.SetPositionAndAngle((128 + 256) / 16, -128 * 1.5 / 16);
                new PhysicalTank(textures_tank, this).SetPositionAndAngle(128 / 16, 128 * 3 / 16);

                new Image(textures_map.tree0_shadow()).AttachTo(Content).y = 128 + 16;
                new Image(textures_map.tree0()).AttachTo(Content).y = 128;

                // can I have 
                // new ped, hind, jeep, tank
                current = new PhysicalPed(textures_ped, this);


                // 12 = 34FPS
                #region other units
                for (int i = 3; i < 9; i++)
                {
                    var cannon2 = new PhysicalCannon(textures_cannon, this);

                    cannon2.SetPositionAndAngle(
                        i * 16, -32, random.NextDouble()
                    );



                    if (i % 3 == 0)
                    {
                        new PhysicalBunker(textures_bunker, this).SetPositionAndAngle(
                            i * 16, -16, random.NextDouble()
                        );
                    }
                    else if (i % 3 == 1)
                    {
                        new PhysicalWatertower(textures_bunker, this).SetPositionAndAngle(
                            i * 16, -16, random.NextDouble()
                        );
                    }
                    else
                    {
                        new PhysicalSilo(textures_bunker, this).SetPositionAndAngle(
                            i * 16, -16, random.NextDouble()
                        );
                    }

                    var hind2 = new PhysicalHindWeaponized(textures_hind, textures_rocket, this)
                    {
                        AutomaticTakeoff = true
                    };

                    hind2.SetPositionAndAngle(
                        i * 16, 8, random.NextDouble()
                    );


                    var jeep2 = new PhysicalJeep(textures_jeep, this);


                    jeep2.SetPositionAndAngle(
                        i * 16, 16, random.NextDouble()
                    );



                    var tank2 = new PhysicalTank(textures_tank, this);

                    tank2.SetPositionAndAngle(
                        i * 16, 24, random.NextDouble()
                    );


                    var ped2 = new PhysicalPed(textures_ped, this);

                    ped2.SetPositionAndAngle(
                        i * 16, 32, random.NextDouble()
                    );


                }
                #endregion




                #region __keyDown

                stage.keyDown +=
                   e =>
                   {
                       // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                       if (e.altKey)
                           __keyDown[System.Windows.Forms.Keys.Alt] = true;

                       __keyDown[(System.Windows.Forms.Keys)e.keyCode] = true;
                   };

                stage.keyUp +=
                 e =>
                 {
                     if (!e.altKey)
                         __keyDown[System.Windows.Forms.Keys.Alt] = false;

                     __keyDown[(System.Windows.Forms.Keys)e.keyCode] = false;
                 };

                #endregion

                bool entermode_changepending = false;
                bool mode_changepending = false;

                onframe +=
                    delegate
                    {
                        #region hud
                        {
                            var cm = new Matrix();

                            cm.scale(0.5, 0.5);
                            cm.translate(16, stage.stageHeight - 64 - 24);

                            hud.transformationMatrix = cm;
                        }
                        #endregion
                    };

                onsyncframe +=
                    delegate
                    {


                        #region entermode_changepending
                        if (!__keyDown[System.Windows.Forms.Keys.Enter])
                        {
                            // space is not down.
                            entermode_changepending = true;
                        }
                        else
                        {
                            if (entermode_changepending)
                            {
                                entermode_changepending = false;

                                // enter another vehicle?

                                var candidatedriver = current as PhysicalPed;
                                if (candidatedriver != null)
                                {
                                    var target =
                                         from candidatevehicle in units
                                         where candidatevehicle.driverseat != null

                                         // can enter if the seat is full.
                                         // unless we kick them out before ofcourse
                                         where candidatevehicle.driverseat.driver == null

                                         let distance = new __vec2(
                                             (float)(candidatedriver.body.GetPosition().x - candidatevehicle.body.GetPosition().x),
                                             (float)(candidatedriver.body.GetPosition().y - candidatevehicle.body.GetPosition().y)
                                         ).GetLength()

                                         where distance < 6

                                         orderby distance ascending
                                         select new { candidatevehicle, distance };

                                    target.FirstOrDefault().With(
                                        x =>
                                        {
                                            Console.WriteLine(new { x.distance });

                                            //current.loc.visible = false;
                                            current.body.SetActive(false);


                                            x.candidatevehicle.driverseat.driver = candidatedriver;
                                            candidatedriver.seatedvehicle = x.candidatevehicle;

                                            current = x.candidatevehicle;

                                            if (current.body.GetType() == Box2D.Dynamics.b2Body.b2_dynamicBody)
                                            {
                                                hud.texture = textures_ped.hud_look_goggles();
                                            }
                                            else
                                            {
                                                hud.texture = textures_ped.hud_look_building();
                                            }

                                            //switchto(x.x);
                                        }
                                    );
                                }
                                else
                                {
                                    (current.driverseat.driver as PhysicalPed).With(
                                        driver =>
                                        {
                                            current.driverseat.driver = null;
                                            driver.seatedvehicle = null;
                                            current.SetVelocityFromInput(new KeySample());

                                            // crashland?
                                            (current as PhysicalHind).With(
                                                hind => hind.VerticalVelocity = -1
                                            );


                                            current = driver;
                                            current.body.SetActive(true);
                                            hud.texture = textures_ped.hud_look();
                                        }
                                    );
                                }

                            }
                        }
                        #endregion



                        #region mode
                        if (!__keyDown[System.Windows.Forms.Keys.Space])
                        {
                            // space is not down.
                            mode_changepending = true;
                        }
                        else
                        {
                            if (mode_changepending)
                            {
                                (current as PhysicalHind).With(
                                    hind1 =>
                                    {
                                        if (hind1.visual.Altitude == 0)
                                            hind1.VerticalVelocity = 1.0;
                                        else
                                            hind1.VerticalVelocity = -0.4;

                                    }
                                );

                                (current as PhysicalPed).With(
                                 physical0 =>
                                 {
                                     if (physical0.visual.LayOnTheGround)
                                         physical0.visual.LayOnTheGround = false;
                                     else
                                         physical0.visual.LayOnTheGround = true;

                                 }
                             );




                                mode_changepending = false;



                            }
                        }
                        #endregion


                        current.SetVelocityFromInput(__keyDown);




                        #region simulate a weapone!
                        if (__keyDown[System.Windows.Forms.Keys.ControlKey])
                            if (syncframeid % 3 == 0)
                            {
                                (this.current as PhysicalHindWeaponized).With(
                                    h =>
                                    {
                                        //sb.snd_missleLaunch.play();

                                        h.FireRocket();
                                    }
                                );

                            }
                        #endregion
                    };
            };
        }
        public StarlingGameSpriteWithPedSync()
        {
            var textures_ped = new StarlingGameSpriteWithPedTextures(new_tex_crop, new_texsprite_crop);


            this.onbeforefirstframe += (stage, s) =>
            {
                #region :ego
                var ego = new PhysicalPed(textures_ped, this)
                {
                    Identity = sessionid + ":ego"
                };

                ego.SetPositionAndAngle(
                    random.NextDouble() * -8,
                    random.NextDouble() * -8,
                    random.NextDouble() * Math.PI
                );

                current = ego;
                #endregion

                // 32x32 = 15FPS?
                // 24x24 35?

                #region others
                for (int ix = 2; ix < 4; ix++)
                    for (int iy = 2; iy < 4; iy++)
                    {
                        var p = new PhysicalPed(textures_ped, this);

                        p.SetPositionAndAngle(
                            8 * ix, 8 * iy
                        );


                    }
                #endregion


                #region KeySample
                var __keyDown = new KeySample();

                stage.keyDown +=
                   e =>
                   {
                       // http://circlecube.com/2008/08/actionscript-key-listener-tutorial/
                       if (e.altKey)
                           __keyDown[Keys.Alt] = true;

                       __keyDown[(Keys)e.keyCode] = true;
                   };

                stage.keyUp +=
                 e =>
                 {
                     if (!e.altKey)
                         __keyDown[Keys.Alt] = false;

                     __keyDown[(Keys)e.keyCode] = false;
                 };

                #endregion

                #region other
                Func<string, RemoteGame> other = __egoid =>
                {
                    // that other game has sent us a sync frame!

                    var already_known_other = others.FirstOrDefault(k => k.__egoid == __egoid);

                    if (already_known_other == null)
                    {
                        already_known_other = new RemoteGame
                        {
                            __egoid = __egoid,

                            // this
                            __syncframeid = this.syncframeid
                        };

                        others.Add(already_known_other);
                    }


                    return already_known_other;
                };
                #endregion


                #region __at_sync
                __at_sync += __egoid =>
                {
                    // that other game has sent us a sync frame!

                    var o = other(__egoid);

                    o.__syncframeid++;
                    // move on!
                };
                #endregion


                #region __at_SetVelocityFromInput
                __at_SetVelocityFromInput +=
                    (
                        string __egoid,
                        string __identity,
                        string __KeySample,
                        string __fixup_x,
                        string __fixup_y,
                        string __fixup_angle

                        ) =>
                    {
                        var o = other(__egoid);



                        if (o.ego == null)
                        {
                            o.ego = new PhysicalPed(textures_ped, this)
                            {
                                Identity = __identity,
                                RemoteGameReference = o
                            };

                            o.ego.SetPositionAndAngle(
                                double.Parse(__fixup_x),
                                double.Parse(__fixup_y),
                                double.Parse(__fixup_angle)
                            );
                        }


                        // set the input!


                        o.ego.SetVelocityFromInput(
                            new KeySample
                            {
                                value = int.Parse(__KeySample),

                                fixup = true,

                                x = double.Parse(__fixup_x),
                                y = double.Parse(__fixup_y),
                                angle = double.Parse(__fixup_angle)

                            }
                        );

                    };
                #endregion


                bool mode_changepending = false;
                onsyncframe += delegate
                {
                    // sync me!

                    // tell others in the session about our game
                    // a beacon


                    #region simulate a weapone!
                    if (__keyDown[Keys.ControlKey])
                        if (frameid % 20 == 0)
                        {
                            var bodyDef = new b2BodyDef();

                            bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

                            // stop moving if legs stop walking!
                            bodyDef.linearDamping = 0;
                            bodyDef.angularDamping = 0;
                            //bodyDef.angle = 1.57079633;
                            bodyDef.fixedRotation = true;

                            var body = ground_b2world.CreateBody(bodyDef);
                            body.SetPosition(
                                new b2Vec2(
                                    current.body.GetPosition().x + 2,
                                    current.body.GetPosition().y + 2
                                )
                            );

                            body.SetLinearVelocity(
                                   new b2Vec2(
                                     100,
                                    100
                                )
                            );

                            var fixDef = new Box2D.Dynamics.b2FixtureDef();
                            fixDef.density = 0.1;
                            fixDef.friction = 0.01;
                            fixDef.restitution = 0;


                            fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(1.0);


                            var fix = body.CreateFixture(fixDef);

                            //body.SetPosition(
                            //    new b2Vec2(0, -100 * 16)
                            //);
                        }
                    #endregion


                    #region mode
                    if (!__keyDown[Keys.Space])
                    {
                        // space is not down.
                        mode_changepending = true;
                    }
                    else
                    {
                        if (mode_changepending)
                        {
                            if (ego.visual.LayOnTheGround)
                                ego.visual.LayOnTheGround = false;
                            else
                                ego.visual.LayOnTheGround = true;

                            mode_changepending = false;



                        }
                    }
                    #endregion



                    ego.SetVelocityFromInput(__keyDown);

                    __raise_SetVelocityFromInput(
                        "" + sessionid,
                        ego.Identity,
                        "" + ego.CurrentInput.value,
                        "" + ego.body.GetPosition().x,
                        "" + ego.body.GetPosition().y,
                        "" + ego.body.GetAngle()

                    );


                    // tell others this sync frame ended for us
                    __raise_sync("" + sessionid);

                };





            };



        }