// nop public void SetVelocityFromInput(KeySample __keyDown) { CurrentInput = __keyDown; ExtractVelocityFromInput(__keyDown, velocity); if (velocity.LinearVelocityX == 0) { if (velocity.LinearVelocityY == 0) { if (velocity.AngularVelocity == 0) { return; } } } // we are moving. stop laying on the ground mode this.visual.LayOnTheGround = false; }
public void FeedKarma() { if (this.KarmaInput0.Count > 0) { var k = new KeySample { value = CurrentInput.value, angle = this.body.GetAngle(), BodyIsActive = this.ground_body.IsActive(), fixup = true, x = this.body.GetPosition().x, y = this.body.GetPosition().y, }; if (CurrentInput.fixup) { k.x = CurrentInput.x; k.y = CurrentInput.y; k.angle = CurrentInput.angle; } this.KarmaInput0.Enqueue(k); this.KarmaInput0.Dequeue(); } }
public void ExtractVelocityFromInput(KeySample __keyDown, Velocity value) { value.AngularVelocity = 0; value.LinearVelocityX = 0; value.LinearVelocityY = 0; if (__keyDown != null) { if (__keyDown[Keys.Up]) { // we have reasone to keep walking value.LinearVelocityY = 1; } if (__keyDown[Keys.Left]) { // we have reasone to keep walking value.AngularVelocity = -1; } if (__keyDown[Keys.Right]) { // we have reasone to keep walking value.AngularVelocity = 1; } } }
public void SetVelocityFromInput(KeySample __keyDown) { CurrentInput = __keyDown; var velocity = new Velocity(); ExtractVelocityFromInput(__keyDown, velocity); this.AngularVelocity = velocity.AngularVelocity; this.LinearVelocityX = velocity.LinearVelocityX; this.LinearVelocityY = velocity.LinearVelocityY; }
public void CreateSmoke() { // how much is this huring FPS? if (issmoke) { return; } PhysicalRocket smoke = null; if (CreateSmokeRecycleCache.Count < 8) { smoke = new PhysicalRocket(textures_rocket, Context, issmoke: true); } else { smoke = CreateSmokeRecycleCache.Dequeue(); smoke.body.SetActive(true); smoke.visual.visible = true; } smoke.smokerandom = Context.random.NextDouble() * Math.PI * 2; smoke.smoketime = Context.gametime.ElapsedMilliseconds; if (this.body.GetLinearVelocity().Length() > 0) { smoke.smokescale = 0.7 + 0.7 * Context.random.NextDouble(); CreateSmokeRecycleCache.Enqueue(smoke); } else { smoke.smokescale = 2.0; } { var up = new KeySample(); up[Keys.Up] = true; smoke.speed = 5; smoke.SetVelocityFromInput(up); } var a = this.body.GetAngle() + (175 + Context.random.Next(10)).DegreesToRadians(); smoke.SetPositionAndAngle( this.body.GetPosition().x + Math.Cos(a) * 2, this.body.GetPosition().y + Math.Sin(a) * 2, a ); smoke.ShowPositionAndAngle(); }
public StarlingGameSpriteWithTankControl() { var textures_tank = new StarlingGameSpriteWithTankTextures(new_texsprite_crop); this.onbeforefirstframe += (stage, s) => { #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 var tank1 = new PhysicalTank(textures_tank, this); current = tank1; var tank2 = new PhysicalTank(textures_tank, this); onsyncframe += delegate { tank1.SetVelocityFromInput(__keyDown); }; }; }
public void ExtractVelocityFromInput(KeySample __keyDown, Car unit4_physics) { //var rot = 0; //var dx = 0.0; //var dy = 0.0; unit4_physics.accelerate = Car.ACC_NONE; unit4_physics.steer_left = Car.STEER_NONE; unit4_physics.steer_right = Car.STEER_NONE; if (__keyDown == null) { return; } if (__keyDown[Keys.Up]) { // we have reasone to keep walking unit4_physics.accelerate = Car.ACC_ACCELERATE; //dy = 1; } if (__keyDown[Keys.Down]) { // we have reasone to keep walking // go slow backwards //dy = -0.5; unit4_physics.accelerate = Car.ACC_BRAKE; } if (__keyDown[Keys.Left]) { // we have reasone to keep walking unit4_physics.steer_left = Car.STEER_LEFT; } if (__keyDown[Keys.Right]) { // we have reasone to keep walking unit4_physics.steer_right = Car.STEER_RIGHT; } }
public StarlingGameSpriteWithCannonControl() { var textures_cannon = new StarlingGameSpriteWithCannonTextures(new_tex_crop); onbeforefirstframe += (stage, s) => { var cannon1 = new PhysicalCannon(textures_cannon, this); #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 current = cannon1; onsyncframe += delegate { cannon1.SetVelocityFromInput(__keyDown); }; }; }
public void SetVelocityFromInput(KeySample __keyDown) { CurrentInput = __keyDown; var velocity = new Velocity(); ExtractVelocityFromInput(__keyDown, velocity); this.AngularVelocity = velocity.AngularVelocity; this.LinearVelocityX = velocity.LinearVelocityX; this.LinearVelocityY = velocity.LinearVelocityY; if (this.LinearVelocityX == 0) { if (this.LinearVelocityY == 0) { if (this.AngularVelocity == 0) { return; } } } if (this.visual.Altitude == 0) { if (AutomaticTakeoff) { // slow takeoff? this.VerticalVelocity = 0.4; // reset this.AngularVelocity = 0; this.LinearVelocityX = 0; this.LinearVelocityY = 0; } } }
public PhysicalPed(StarlingGameSpriteWithPedTextures textures, StarlingGameSpriteWithPhysics Context) { this.CurrentInput = new KeySample(); this.textures = textures; this.Context = Context; for (int i = 0; i < 7; i++) { this.KarmaInput0.Enqueue( new KeySample() ); } visual = new VisualPed(textures, Context, AnimateSeed: Context.random.Next() % 3000 ); #region b2world { var bodyDef = new b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! bodyDef.linearDamping = 0; bodyDef.angularDamping = 6; //bodyDef.angle = 1.57079633; //bodyDef.fixedRotation = true; body = Context.ground_b2world.CreateBody(bodyDef); var fixDef = new Box2D.Dynamics.b2FixtureDef(); fixDef.density = 0.1; fixDef.friction = 0.0; fixDef.restitution = 0; fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(1.0); var fix = body.CreateFixture(fixDef); var fix_data = new Action <double>( zombie_forceA => { if (zombie_forceA < 1) { return; } // zombie runs against a building if (zombie_forceA > 3.6) { if (visual.WalkLikeZombie) { Console.WriteLine(new { zombie_forceA }); this.body.SetActive(false); this.damagebody.SetActive(false); this.visual.LayOnTheGround = true; this.SetVelocityFromInput(new KeySample()); } } if (oncollision != null) { oncollision(this, zombie_forceA); } } ); fix.SetUserData(fix_data); } #endregion #region groundkarma_b2world { var bodyDef = new b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! bodyDef.linearDamping = 0; bodyDef.angularDamping = 6; //bodyDef.angle = 1.57079633; //bodyDef.fixedRotation = true; karmabody = Context.groundkarma_b2world.CreateBody(bodyDef); var fixDef = new Box2D.Dynamics.b2FixtureDef(); fixDef.density = 0.1; fixDef.friction = 0.0; fixDef.restitution = 0; fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(1.0); var fix = karmabody.CreateFixture(fixDef); } #endregion #region damage_b2world { var bodyDef = new b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! bodyDef.linearDamping = 0; bodyDef.angularDamping = 6; //bodyDef.angle = 1.57079633; //bodyDef.fixedRotation = true; damagebody = Context.damage_b2world.CreateBody(bodyDef); var fixDef = new Box2D.Dynamics.b2FixtureDef(); fixDef.density = 0.1; fixDef.friction = 0.0; fixDef.restitution = 0; fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(1.0); var fix = damagebody.CreateFixture(fixDef); var fix_data = new Action <double>( jeep_forceA => { if (jeep_forceA < 0.5) { return; } if (visual.WalkLikeZombie) { this.body.SetActive(false); this.damagebody.SetActive(false); this.visual.LayOnTheGround = true; this.SetVelocityFromInput(new KeySample()); } //if (jeep_forceA < 1) // return; //if (oncollision != null) // oncollision(this, jeep_forceA); } ); fix.SetUserData(fix_data); } #endregion Context.internalunits.Add(this); }
public PhysicalHindWeaponized( StarlingGameSpriteWithHindTextures textures_hind, StarlingGameSpriteWithRocketTextures textures_rocket, StarlingGameSpriteWithPhysics __Context, Image Explosion1 = null ) : base(textures_hind, __Context) { var RocketsMax = 12; var Rockets = new Queue <PhysicalRocket>(); var Context = __Context; var rocket0 = new PhysicalRocket(textures_rocket, Context, Explosion1: Explosion1); rocket0.body.SetActive(false); rocket0.SetPositionAndAngle(-0.5, 2); var rocket1 = new PhysicalRocket(textures_rocket, Context, Explosion1: Explosion1); rocket1.body.SetActive(false); rocket1.SetPositionAndAngle(-0.5, -2); #region z fixup rocket0.visual.parent.setChildIndex( rocket0.visual, this.visual.visualnowings.parent.getChildIndex( this.visual.visualnowings ) ); #endregion #region z fixup rocket1.visual.parent.setChildIndex( rocket1.visual, this.visual.visualnowings.parent.getChildIndex( this.visual.visualnowings ) ); #endregion var hind0 = this; #region ShowPositionAndAngleForSlaves hind0.ShowPositionAndAngleForSlaves = delegate { // we are faking 3d here! var sc = 1 + hind0.visual.airzoom * hind0.visual.Altitude; if (rocket0 != null) { rocket0.body.SetActive(false); rocket0.Altitude = hind0.visual.Altitude; rocket0.SetPositionAndAngle( hind0.body.GetPosition().x + Math.Cos(hind0.body.GetAngle() - Math.PI * 0.5 - hind0.CameraRotation) * 2.2 * sc, hind0.body.GetPosition().y + Math.Sin(hind0.body.GetAngle() - Math.PI * 0.5 - hind0.CameraRotation) * 2.2 * sc, hind0.body.GetAngle() - hind0.CameraRotation ); rocket0.ShowPositionAndAngle(); } if (rocket1 != null) { rocket1.body.SetActive(false); rocket1.Altitude = hind0.visual.Altitude; rocket1.SetPositionAndAngle( hind0.body.GetPosition().x + Math.Cos(hind0.body.GetAngle() + Math.PI * 0.5 - hind0.CameraRotation) * 2.2 * sc, hind0.body.GetPosition().y + Math.Sin(hind0.body.GetAngle() + Math.PI * 0.5 - hind0.CameraRotation) * 2.2 * sc, hind0.body.GetAngle() - hind0.CameraRotation ); rocket1.ShowPositionAndAngle(); } }; #endregion #region FireRocket FireRocket = delegate { if (rocket0 != null) { var sc = 1 + hind0.visual.airzoom * hind0.visual.Altitude; rocket0.SetPositionAndAngle( hind0.body.GetPosition().x + Math.Cos(hind0.body.GetAngle() - Math.PI * 0.5 - hind0.CameraRotation) * 3.5 * sc, hind0.body.GetPosition().y + Math.Sin(hind0.body.GetAngle() - Math.PI * 0.5 - hind0.CameraRotation) * 3.5 * sc, hind0.body.GetAngle() - hind0.CameraRotation ); rocket0.ShowPositionAndAngle(); rocket0.body.SetActive(true); rocket0.CreateSmoke(); { var up = new KeySample(); up[Keys.Up] = true; rocket0.speed = 60 + this.body.GetLinearVelocity().Length(); rocket0.SetVelocityFromInput(up); } Rockets.Enqueue(rocket0); rocket0 = null; if (rocket1 == null) { if (Rockets.Count > RocketsMax) { rocket1 = Rockets.Dequeue(); rocket1.SetVelocityFromInput(new KeySample()); rocket1.visual.visible = true; } else { rocket1 = new PhysicalRocket(textures_rocket, Context, Explosion1: Explosion1); rocket1.body.SetActive(false); #region z fixup rocket1.visual.parent.setChildIndex( rocket1.visual, this.visual.visualnowings.parent.getChildIndex( this.visual.visualnowings ) ); #endregion } } } else if (rocket1 != null) { var sc = 1 + hind0.visual.airzoom * hind0.visual.Altitude; rocket1.SetPositionAndAngle( hind0.body.GetPosition().x + Math.Cos(hind0.body.GetAngle() + Math.PI * 0.5 - hind0.CameraRotation) * 3.5 * sc, hind0.body.GetPosition().y + Math.Sin(hind0.body.GetAngle() + Math.PI * 0.5 - hind0.CameraRotation) * 3.5 * sc, hind0.body.GetAngle() - hind0.CameraRotation ); rocket1.ShowPositionAndAngle(); rocket1.body.SetActive(true); rocket1.CreateSmoke(); { var up = new KeySample(); up[Keys.Up] = true; rocket1.speed = 60 + this.body.GetLinearVelocity().Length(); rocket1.SetVelocityFromInput(up); } Rockets.Enqueue(rocket1); rocket1 = null; if (rocket0 == null) { if (Rockets.Count > RocketsMax) { rocket0 = Rockets.Dequeue(); rocket0.SetVelocityFromInput(new KeySample()); rocket0.visual.visible = true; } else { rocket0 = new PhysicalRocket(textures_rocket, Context, Explosion1: Explosion1); rocket0.body.SetActive(false); #region z fixup rocket0.visual.parent.setChildIndex( rocket0.visual, this.visual.visualnowings.parent.getChildIndex( this.visual.visualnowings ) ); #endregion } } } }; #endregion }
public PhysicalCannon(StarlingGameSpriteWithCannonTextures textures, StarlingGameSpriteWithPhysics Context) { this.CurrentInput = new KeySample(); this.driverseat = new DriverSeat(); this.Context = Context; visual = new VisualCannon(textures, Context); for (int i = 0; i < 7; i++) { this.KarmaInput0.Enqueue( new KeySample() ); } { //initialize body var bdef = new b2BodyDef(); bdef.angle = 0; bdef.fixedRotation = true; this.body = Context.ground_b2world.CreateBody(bdef); //initialize shape var fixdef = new b2FixtureDef(); var shape = new b2PolygonShape(); fixdef.shape = shape; shape.SetAsBox(2, 2); fixdef.restitution = 0.4; //positively bouncy! var fix = this.body.CreateFixture(fixdef); var fix_data = new Action <double>( jeep_forceA => { if (jeep_forceA < 1) { return; } Context.oncollision(this, jeep_forceA); } ); fix.SetUserData(fix_data); } { //initialize body var bdef = new b2BodyDef(); bdef.angle = 0; bdef.fixedRotation = true; this.karmabody = Context.groundkarma_b2world.CreateBody(bdef); //initialize shape var fixdef = new b2FixtureDef(); var shape = new b2PolygonShape(); fixdef.shape = shape; shape.SetAsBox(2, 2); fixdef.restitution = 0.4; //positively bouncy! this.karmabody.CreateFixture(fixdef); } Context.internalunits.Add(this); }
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 void SetVelocityFromInput(KeySample __keyDown) { }
public static void InitializeConnection( this KeySample key, bool WriteMode = true, bool ReadMode = false, TextField text = null, Action <Action <string> > yield_PostMessage = null, Action <XElement> yield_Notify = null ) { //var that = new { About }; var nc = new NetConnection(); var connected = false; Action <string> RaiseMessage = x => { if (text != null) { //text.text = x + Environment.NewLine + text.text; text.text = x; } }; Action <string> PostMessage = message => { RaiseMessage("drop: " + message); }; if (WriteMode) { var sync = new ScriptCoreLib.ActionScript.flash.utils.Timer(1000 / 60); var syncid = 0; var was = -1; sync.timer += delegate { if (key.value == was) { if (was == 0) { return; } } syncid++; PostMessage( new XElement("sync", new XElement("KeySample", new XAttribute("value", "" + key.value), new XAttribute("forcex", "" + key.forcex), new XAttribute("forcey", "" + key.forcey), new XAttribute("syncid", "" + syncid) ) ).ToString() ); was = key.value; }; sync.start(); } //that.AtNotifyVisualizeTouch += // (x, y) => // { // XElement VisualizeTouch = new DoubleVector2 // { // X = x, // Y = y // }; // PostMessage( // new XElement("Updates", // new XElement("VisualizeTouch", VisualizeTouch) // ).ToString() // ); // }; nc.netStatus += e => { RaiseMessage("nc.netStatus: " + e.info.code); if (e.info.code == "NetGroup.Connect.Success") { connected = true; RaiseMessage("connected, looking for long range coms... (7 to 30sec delay) might need to reset android wifi, pc wifi or wifi router itself!"); return; } if (e.info.code == "NetConnection.Connect.Success") { RaiseMessage("looking for long range coms... looking for permission..."); var groupspec = new GroupSpecifier("myGroup/groupOne"); groupspec.postingEnabled = true; groupspec.ipMulticastMemberUpdatesEnabled = true; groupspec.addIPMulticastAddress("225.225.0.1:30303"); var group = new NetGroup(nc, groupspec.groupspecWithAuthorizations()); PostMessage = message => { if (connected) { RaiseMessage("write: " + message); group.post(message); } else { RaiseMessage("skip: " + message); } }; if (yield_PostMessage != null) { yield_PostMessage(PostMessage); } //if (WriteMode) //{ // PostMessage( // new XElement("KeySample", // new XAttribute("value", "" + key.value) // ).ToString() // ); //} //AtPostMessage += PostMessage; group.netStatus += g => { if (g.info.code == "NetGroup.Posting.Notify") { // Type Coercion failed: cannot convert Object@60b6cb9 to LANMulticast_Components_MySprite1__f__AnonymousType0_1_33554444. var source = (string)g.info.message; //Console.WriteLine("source: " + source); RaiseMessage("source: " + source); var xml = XElement.Parse(source); //xml.Elements().Where(k => k.Name.LocalName == "BuildRocket").Elements().WithEach( // ksource => // { // //Console.WriteLine("BuildRocket: " + ksource); // DoubleVector2 k = ksource; // that.NotifyBuildRocket(k.X, k.Y); // } //); if (yield_Notify != null) { yield_Notify(xml); } if (ReadMode) { //xml.Elements().Where(k => k.Name.LocalName == "KeySample").WithEach( xml.Elements("KeySample").WithEach( ksource => { var value = int.Parse(ksource.Attribute("value").Value); var forcex = double.Parse(ksource.Attribute("forcex").Value); var forcey = double.Parse(ksource.Attribute("forcey").Value); //RaiseMessage("value: " + value); key.value = value; key.forcex = forcex; key.forcey = forcey; //Console.WriteLine("VisualizeTouch: " + ksource); //new XElement("value", "" + k.value) //DoubleVector2 k = ksource; //that.NotifyVisualizeTouch(k.X, k.Y); } ); } return; } RaiseMessage("group.netStatus: " + g.info.code); }; return; } }; nc.connect("rtmfp:"); }
public PhysicalSilo(StarlingGameSpriteWithBunkerTextures textures, StarlingGameSpriteWithPhysics Context) { this.CurrentInput = new KeySample(); this.driverseat = new DriverSeat(); this.textures = textures; this.Context = Context; for (int i = 0; i < 7; i++) { this.KarmaInput0.Enqueue( new KeySample() ); } visualshadow = new Image( textures.silo1_shadow() ).AttachTo(Context.Content_layer2_shadows); visual = new Image( textures.silo1() ).AttachTo(Context.Content_layer3_buildings); { //initialize body var bdef = new b2BodyDef(); bdef.angle = 0; bdef.fixedRotation = true; this.body = Context.ground_b2world.CreateBody(bdef); //initialize shape var fixdef = new b2FixtureDef(); var shape = new b2CircleShape(4); fixdef.shape = shape; fixdef.restitution = 0.4; //positively bouncy! var fix = this.body.CreateFixture(fixdef); var fix_data = new Action <double>( force => { if (force < 1) { return; } Context.oncollision(this, force); } ); fix.SetUserData(fix_data); } { //initialize body var bdef = new b2BodyDef(); bdef.angle = 0; bdef.fixedRotation = true; this.karmabody = Context.groundkarma_b2world.CreateBody(bdef); //initialize shape var fixdef = new b2FixtureDef(); var shape = new b2CircleShape(4); fixdef.shape = shape; fixdef.restitution = 0.4; //positively bouncy! this.karmabody.CreateFixture(fixdef); } Context.internalunits.Add(this); }
public PhysicalHind(StarlingGameSpriteWithHindTextures textures, StarlingGameSpriteWithPhysics Context) { this.CurrentInput = new KeySample(); this.driverseat = new DriverSeat(); this.Context = Context; visual = new VisualHind(textures, Context.Content, Context.airzoom); for (int i = 0; i < 7; i++) { this.KarmaInput0.Enqueue( new KeySample() ); } #region ground_b2world ground_current { var ground_bodyDef = new b2BodyDef(); ground_bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! ground_bodyDef.linearDamping = 10.0; ground_bodyDef.angularDamping = 4; //bodyDef.angle = 1.57079633; //ground_bodyDef.fixedRotation = true; ground_body = Context.ground_b2world.CreateBody(ground_bodyDef); var ground_fixDef = new Box2D.Dynamics.b2FixtureDef(); ground_fixDef.density = 0.1; ground_fixDef.friction = 0.01; ground_fixDef.restitution = 0; var ground_fixdef_shape = new b2PolygonShape(); ground_fixDef.shape = ground_fixdef_shape; // physics unit is looking to right ground_fixdef_shape.SetAsBox(2, 0.5); var ground_fix = ground_body.CreateFixture(ground_fixDef); } #endregion #region groundkarma_body { var ground_bodyDef = new b2BodyDef(); ground_bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! ground_bodyDef.linearDamping = 10.0; ground_bodyDef.angularDamping = 4; //bodyDef.angle = 1.57079633; //ground_bodyDef.fixedRotation = true; groundkarma_body = Context.groundkarma_b2world.CreateBody(ground_bodyDef); var ground_fixDef = new Box2D.Dynamics.b2FixtureDef(); ground_fixDef.density = 0.1; ground_fixDef.friction = 0.01; ground_fixDef.restitution = 0; var ground_fixdef_shape = new b2PolygonShape(); ground_fixDef.shape = ground_fixdef_shape; // physics unit is looking to right ground_fixdef_shape.SetAsBox(2, 0.5); var ground_fix = groundkarma_body.CreateFixture(ground_fixDef); } #endregion #region air_b2world air_current { var air_bodyDef = new b2BodyDef(); air_bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! air_bodyDef.linearDamping = 10.0; air_bodyDef.angularDamping = 4; //bodyDef.angle = 1.57079633; //air_bodyDef.fixedRotation = true; air_bodyDef.active = false; air_body = Context.air_b2world.CreateBody(air_bodyDef); var air_fixDef = new Box2D.Dynamics.b2FixtureDef(); air_fixDef.density = 0.1; air_fixDef.friction = 0.01; air_fixDef.restitution = 0; var air_fixdef_shape = new b2PolygonShape(); air_fixDef.shape = air_fixdef_shape; // physics unit is looking to right air_fixdef_shape.SetAsBox(2, 0.5); var air_fix = air_body.CreateFixture(air_fixDef); } #endregion #region smoke_b2world { var bodyDef = new b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! bodyDef.linearDamping = 0; bodyDef.angularDamping = 6; //bodyDef.angle = 1.57079633; //bodyDef.fixedRotation = true; damage_body = Context.damage_b2world.CreateBody(bodyDef); //body = Context.ground_b2world.CreateBody(bodyDef); var fixDef = new Box2D.Dynamics.b2FixtureDef(); fixDef.density = 0.1; fixDef.friction = 0.0; fixDef.restitution = 0; fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(2); // var fix = damage_body.CreateFixture(fixDef); //var fix_data = new Action<double>( // jeep_forceA => // { // if (jeep_forceA < 1) // return; // if (Context.oncollision != null) // Context.oncollision(this, jeep_forceA); // } //); //fix.SetUserData(fix_data); } #endregion ApplyVelocityElapsed = Context.gametime.ElapsedMilliseconds; Context.internalunits.Add(this); }
public StarlingGameSpriteWithMap() { var textures = new StarlingGameSpriteWithTankTextures(new_texsprite_crop); var textures_map = new StarlingGameSpriteWithMapTextures(new_tex_crop); var textures_explosions = new StarlingGameSpriteWithMapExplosionsTextures(new_tex96); this.onbeforefirstframe += (stage, s) => { new Image(textures_map.hill1()).AttachTo(Content).y = -256; new Image(textures_map.hole1()).AttachTo(Content).y = -512; new Image(textures_map.grass1()).AttachTo(Content).y = -512 - 256; new Image(textures_map.road0()).AttachTo(Content).x = -256; new Image(textures_map.road0()).AttachTo(Content).x = 0; new Image(textures_map.road0()).AttachTo(Content).x = 256; new Image(textures_map.touchdown()).AttachTo(Content).y = 256; new Image(textures_map.tree0_shadow()).AttachTo(Content).y = 128 + 16; new Image(textures_map.tree0()).AttachTo(Content).y = 128; var exp = new Image(textures_explosions.explosions[0]()).AttachTo(Content); #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 var tank1 = new PhysicalTank(textures, this); current = tank1; onsyncframe += delegate { exp.texture = textures_explosions.explosions[this.syncframeid % textures_explosions.explosions.Length](); tank1.SetVelocityFromInput(__keyDown); this.Text = new { this.syncframeid, this.syncframetime }.ToString(); }; }; }
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); }; }; }
public void ExtractVelocityFromInput(KeySample __keyDown, Velocity value) { value.AngularVelocity = 0; value.LinearVelocityX = 0; value.LinearVelocityY = 0; if (__keyDown != null) { #region alt Func <Keys, Keys, bool> alt = (k1, k2) => { if (__keyDown[Keys.Alt, Keys.ControlKey]) { return(__keyDown[k2]); } return(__keyDown[k1]); }; #endregion var k = new { up = __keyDown[Keys.Up], down = __keyDown[Keys.Down], left = alt(Keys.Left, Keys.A), right = alt(Keys.Right, Keys.D), strafeleft = alt(Keys.A, Keys.Left), straferight = alt(Keys.D, Keys.Right), }; if (k.up) { // we have reasone to keep walking value.LinearVelocityY = 1; } if (k.down) { // we have reasone to keep walking // go slow backwards value.LinearVelocityY = -0.5; } if (k.left) { // we have reasone to keep walking value.AngularVelocity = -1; } if (k.right) { // we have reasone to keep walking value.AngularVelocity = 1; } if (k.strafeleft) { // we have reasone to keep walking value.LinearVelocityX = -1; } if (k.straferight) { // we have reasone to keep walking value.LinearVelocityX = 1; } } }
public void ExtractVelocityFromInput(KeySample __keyDown, Velocity value) { value.AngularVelocity = 0; value.LinearVelocityX = 0; value.LinearVelocityY = 0; if (__keyDown != null) { #region alt Func <Keys, Keys, bool> alt = (k1, k2) => { if (__keyDown[Keys.Alt]) { return(__keyDown[k2]); } return(__keyDown[k1]); }; #endregion // script: error JSC1000: ActionScript : unable to emit br.s at 'FlashHeatZeeker.UnitPedControl.Library.PhysicalPed.ExtractVelocityFromInput'#004c: invalid br opcode var k = new { up = __keyDown[Keys.Up], down = __keyDown[Keys.Down], left = alt(Keys.Left, Keys.A), right = alt(Keys.Right, Keys.D), strafeleft = alt(Keys.A, Keys.Left), straferight = alt(Keys.D, Keys.Right), }; if (k.up) { // we have reasone to keep walking value.LinearVelocityY = 1; } if (k.down) { // we have reasone to keep walking // go slow backwards value.LinearVelocityY = -0.5; } if (k.left) { // we have reasone to keep walking value.AngularVelocity = -1; } if (k.right) { // we have reasone to keep walking value.AngularVelocity = 1; } if (k.strafeleft) { // we have reasone to keep walking value.LinearVelocityX = -1; } if (k.straferight) { // we have reasone to keep walking value.LinearVelocityX = 1; } } }
public PhysicalJeep(StarlingGameSpriteWithJeepTextures textures, StarlingGameSpriteWithPhysics Context) { this.CurrentInput = new KeySample(); this.CameraRotation = Math.PI / 2; this.textures = textures; this.driverseat = new DriverSeat(); this.Context = Context; visual0 = new VisualJeep(textures, Context); for (int i = 0; i < 7; i++) { this.KarmaInput0.Enqueue( new KeySample() ); } Func <double, double, double[]> ff = (a, b) => { return(new double[] { a, b }); }; { xwheels = new[] { //top left new Wheel { b2world = Context.groundkarma_b2world, x = -1.1, y = -1.2, width = 0.4, length = 0.8, revolving = true, powered = true }, //top right new Wheel { b2world = Context.groundkarma_b2world, x = 1.1, y = -1.2, width = 0.4, length = 0.8, revolving = true, powered = true }, //back left new Wheel { b2world = Context.groundkarma_b2world, x = -1.1, y = 1.2, width = 0.4, length = 0.8, revolving = false, powered = false }, //back right new Wheel { b2world = Context.groundkarma_b2world, x = 1.1, y = 1.2, width = 0.4, length = 0.8, revolving = false, powered = false }, }; karmaunit4_physics = new Car( b2world: Context.groundkarma_b2world, width: 2, length: 4, position: ff(0, 0), angle: 180, // how fast can the jeep go? power: 120, max_speed: 120, max_steer_angle: 33, //max_steer_angle: 40, wheels: xwheels ); } { xwheels = new[] { //top left new Wheel { b2world = Context.ground_b2world, x = -1.1, y = -1.2, width = 0.4, length = 0.8, revolving = true, powered = true }, //top right new Wheel { b2world = Context.ground_b2world, x = 1.1, y = -1.2, width = 0.4, length = 0.8, revolving = true, powered = true }, //back left new Wheel { b2world = Context.ground_b2world, x = -1.1, y = 1.2, width = 0.4, length = 0.8, revolving = false, powered = false }, //back right new Wheel { b2world = Context.ground_b2world, x = 1.1, y = 1.2, width = 0.4, length = 0.8, revolving = false, powered = false }, }; unit4_physics = new Car( b2world: Context.ground_b2world, width: 2, length: 4, position: ff(0, 0), angle: 180, // how fast can the jeep go? power: 120, max_speed: 120, max_steer_angle: 33, //max_steer_angle: 40, wheels: xwheels ); var fix = unit4_physics.fix; var fix_data = new Action <double>( jeep_forceA => { if (jeep_forceA < 1) { return; } if (oncollision != null) { oncollision(this, jeep_forceA); } } ); fix.SetUserData(fix_data); xwheels[0].setAngle += a => { var cm = new Matrix(); cm.translate(-2, -2); cm.scale(2, 4); cm.rotate(a.DegreesToRadians()); cm.translate(-18, -20); visual0.tire0.transformationMatrix = cm; }; xwheels[1].setAngle += a => { var cm = new Matrix(); cm.translate(-2, -2); cm.scale(2, 4); cm.rotate(a.DegreesToRadians()); cm.translate(18, -20); visual0.tire1.transformationMatrix = cm; }; } { //initialize body var def = new b2BodyDef(); def.type = b2Body.b2_dynamicBody; def.linearDamping = 0.55; //gradually reduces velocity, makes the car reduce speed slowly if neither accelerator nor brake is pressed def.bullet = true; //dedicates more time to collision detection - car travelling at high speeds at low framerates otherwise might teleport through obstacles. def.angularDamping = 0.3; this.damagebody = Context.damage_b2world.CreateBody(def); //initialize shape var fixdef = new b2FixtureDef(); fixdef.density = 1.0; fixdef.friction = 0.3; //friction when rubbing agaisnt other shapes fixdef.restitution = 0.4; //amount of force feedback when hitting something. >0 makes the car bounce off, it's fun! var fixdef_shape = new b2PolygonShape(); fixdef.shape = fixdef_shape; fixdef_shape.SetAsBox(2 / 2, 4 / 2); var fix = damagebody.CreateFixture(fixdef); } Context.internalunits.Add(this); }
public void SetVelocityFromInput(KeySample __keyDown) { CurrentInput = __keyDown; ExtractVelocityFromInput(__keyDown, unit4_physics); }
public PhysicalTank(StarlingGameSpriteWithTankTextures textures, StarlingGameSpriteWithPhysics Context) { this.CurrentInput = new KeySample(); this.textures = textures; this.Context = Context; this.driverseat = new DriverSeat(); this.visual = new VisualTank(textures, Context); for (int i = 0; i < 7; i++) { this.KarmaInput0.Enqueue( new KeySample() ); } #region b2world { var bodyDef = new b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! bodyDef.linearDamping = 1.0; bodyDef.angularDamping = 8; //bodyDef.angle = 1.57079633; //bodyDef.fixedRotation = true; body = Context.ground_b2world.CreateBody(bodyDef); //current = body; var fixDef = new Box2D.Dynamics.b2FixtureDef(); fixDef.density = 0.1; fixDef.friction = 0.01; fixDef.restitution = 0; var fixdef_shape = new b2PolygonShape(); fixDef.shape = fixdef_shape; // physics unit is looking to right fixdef_shape.SetAsBox(3, 2); var fix = body.CreateFixture(fixDef); var fix_data = new Action <double>( jeep_forceA => { if (jeep_forceA < 1) { return; } Context.oncollision(this, jeep_forceA); } ); fix.SetUserData(fix_data); } { var bodyDef = new b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! bodyDef.linearDamping = 1.0; bodyDef.angularDamping = 8; //bodyDef.angle = 1.57079633; //bodyDef.fixedRotation = true; karmabody = Context.groundkarma_b2world.CreateBody(bodyDef); //current = body; var fixDef = new Box2D.Dynamics.b2FixtureDef(); fixDef.density = 0.1; fixDef.friction = 0.01; fixDef.restitution = 0; var fixdef_shape = new b2PolygonShape(); fixDef.shape = fixdef_shape; // physics unit is looking to right fixdef_shape.SetAsBox(3, 2); var fix = karmabody.CreateFixture(fixDef); } #endregion #region b2world { var bodyDef = new b2BodyDef(); bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; // stop moving if legs stop walking! bodyDef.linearDamping = 1.0; bodyDef.angularDamping = 8; //bodyDef.angle = 1.57079633; //bodyDef.fixedRotation = true; damagebody = Context.damage_b2world.CreateBody(bodyDef); //current = body; var fixDef = new Box2D.Dynamics.b2FixtureDef(); fixDef.density = 0.1; fixDef.friction = 0.01; fixDef.restitution = 0; var fixdef_shape = new b2PolygonShape(); fixDef.shape = fixdef_shape; // physics unit is looking to right fixdef_shape.SetAsBox(3, 2); var fix = damagebody.CreateFixture(fixDef); var fix_data = new Action <double>( jeep_forceA => { if (jeep_forceA < 1) { return; } Context.oncollision(this, jeep_forceA); } ); fix.SetUserData(fix_data); } #endregion Context.internalunits.Add(this); }
public void SetVelocityFromInput(KeySample __keyDown) { this.CurrentInput = __keyDown; ExtractVelocityFromInput(__keyDown, velocity); }
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 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 StarlingGameSpriteWithHindSync() { var textures_hind = new StarlingGameSpriteWithHindTextures(this.new_tex_crop); this.onbeforefirstframe += (stage, s) => { #region __keyDown 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 #region ego var ego = new PhysicalHind(textures_hind, this) { Identity = sessionid + ":ego" }; ego.SetPositionAndAngle( random.NextDouble() * -20 - 4, random.NextDouble() * -20 - 4, random.NextDouble() * Math.PI ); current = ego; #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_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)); }; #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 PhysicalHind(textures_hind, 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 { #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; // } //); mode_changepending = false; } } #endregion current.SetVelocityFromInput(__keyDown); __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 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 PhysicalBarrel(StarlingGameSpriteWithBunkerTextures textures, StarlingGameSpriteWithPhysics Context) { this.CurrentInput = new KeySample(); // hide in a barrel? //this.driverseat = new DriverSeat(); this.textures = textures; this.Context = Context; //for (int i = 0; i < 7; i++) //{ // this.KarmaInput0.Enqueue( // new KeySample() // ); //} visualshadow = new Image( textures.barrel1_shadow() ).AttachTo(Context.Content_layer2_shadows); visual = new Image( textures.barrel1() ).AttachTo(Context.Content_layer3_buildings); { //initialize body var bdef = new b2BodyDef(); bdef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bdef.linearDamping = 8.0; bdef.angularDamping = 8; bdef.angle = 0; this.body = Context.ground_b2world.CreateBody(bdef); //initialize shape var fixdef = new b2FixtureDef(); fixdef.density = 1; fixdef.friction = 0.01; //fixdef.restitution = 0.4; //positively bouncy! var shape = new b2PolygonShape(); fixdef.shape = shape; shape.SetAsBox(1.6, 1); var fix = this.body.CreateFixture(fixdef); var fix_data = new Action <double>( jeep_forceA => { if (jeep_forceA < 1) { return; } if (oncollision != null) { oncollision(this, jeep_forceA); } } ); fix.SetUserData(fix_data); } { //initialize body var bdef = new b2BodyDef(); bdef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bdef.linearDamping = 8.0; bdef.angularDamping = 8; bdef.angle = 0; this.karmabody = Context.groundkarma_b2world.CreateBody(bdef); //initialize shape var fixdef = new b2FixtureDef(); fixdef.density = 1; fixdef.friction = 0.01; //fixdef.restitution = 0.4; //positively bouncy! var shape = new b2PolygonShape(); fixdef.shape = shape; shape.SetAsBox(1.6, 1); this.karmabody.CreateFixture(fixdef); } Context.internalunits.Add(this); }