public void ApplyVelocity()
        {
            // this is now

            {
                var current = this.body;
                //var v = velocity.AngularVelocity * 10;         current.SetAngularVelocity(v);


                current.ApplyAngularImpulse(
                    velocity.AngularVelocity
                    * this.CurrentInput.forcex
                    * ApplyVelocityElapse.ElapsedMilliseconds
                    * 0.01
                    * (1 - (this.body.GetLinearVelocity().Length() / this.speed).Min(0.9) * 0.5)
                    );



                var vx = Math.Cos(current.GetAngle()) * velocity.LinearVelocityY * this.speed * this.CurrentInput.forcey
                         + Math.Cos(current.GetAngle() + Math.PI / 2) * velocity.LinearVelocityX * this.speed;
                var vy = Math.Sin(current.GetAngle()) * velocity.LinearVelocityY * this.speed * this.CurrentInput.forcey
                         + Math.Sin(current.GetAngle() + Math.PI / 2) * velocity.LinearVelocityX * this.speed;


                this.visual.currentvisual.alpha = 1.0;

                #region RemoteGameReference
                if (RemoteGameReference != null)
                {
                    if (vx == 0)
                    {
                        if (vy == 0)
                        //if (v == 0)
                        {
                            // not moving anymore in network mode
                            // far enough to be out of sync?

                            if (karmabody.GetLinearVelocity().Length() == 0)
                            {
                                if (this.KarmaInput0.All(k => k.value == 0))
                                {
                                    this.body.SetAngle(
                                        karmabody.GetAngle()
                                        );


                                    var gap = new __vec2(
                                        (float)this.karmabody.GetPosition().x - (float)this.body.GetPosition().x,
                                        (float)this.karmabody.GetPosition().y - (float)this.body.GetPosition().y
                                        );

                                    // tolerate lesser distance?
                                    if (gap.GetLength() > 0.5)
                                    {
                                        //this.body.SetPositionAndAngle(
                                        //    new b2Vec2(
                                        //        this.karmabody.GetPosition().x,
                                        //        this.karmabody.GetPosition().y
                                        //    ),
                                        //    this.karmabody.GetAngle()
                                        //);

                                        // show we are in the wrong place!
                                        this.visual.currentvisual.alpha = 0.3;


                                        // look at where we should be instead
                                        this.body.SetAngle(
                                            gap.GetRotation()
                                            );

                                        // and walk there!
                                        vx = Math.Cos(current.GetAngle()) * 0.5 * this.speed
                                             + Math.Cos(current.GetAngle() + Math.PI / 2) * 0 * this.speed;
                                        vy = Math.Sin(current.GetAngle()) * 0.5 * this.speed
                                             + Math.Sin(current.GetAngle() + Math.PI / 2) * 0 * this.speed;
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion


                current.SetLinearVelocity(
                    new b2Vec2(
                        vx, vy
                        )
                    );
            }

            // what about our karma body?
            if (this.KarmaInput0.Count > 0)
            {
                var _karma__keyDown = this.KarmaInput0.Peek();

                var _karma_velocity = new Velocity();


                ExtractVelocityFromInput(_karma__keyDown, _karma_velocity);

                var current = this.karmabody;
                //var v = _karma_velocity.AngularVelocity * 10;
                //current.SetAngularVelocity(v);

                current.ApplyAngularImpulse(
                    _karma_velocity.AngularVelocity
                    * ApplyVelocityElapse.ElapsedMilliseconds
                    * 0.01
                    * (1 - (current.GetLinearVelocity().Length() / this.speed).Min(0.9) * 0.5)
                    );


                var vx = Math.Cos(current.GetAngle()) * _karma_velocity.LinearVelocityY * this.speed
                         + Math.Cos(current.GetAngle() + Math.PI / 2) * _karma_velocity.LinearVelocityX * this.speed;
                var vy = Math.Sin(current.GetAngle()) * _karma_velocity.LinearVelocityY * this.speed
                         + Math.Sin(current.GetAngle() + Math.PI / 2) * _karma_velocity.LinearVelocityX * this.speed;


                current.SetActive(
                    _karma__keyDown.BodyIsActive
                    );

                current.SetLinearVelocity(
                    new b2Vec2(
                        vx, vy
                        )
                    );

                if (_karma__keyDown.fixup)
                {
                    var fixupmultiplier = 0.90;

                    current.SetAngle(
                        // meab me in scotty,
                        _karma__keyDown.angle + (current.GetAngle() - _karma__keyDown.angle) * fixupmultiplier

                        );



                    var gap = new __vec2(
                        (float)this.karmabody.GetPosition().x - (float)_karma__keyDown.x,
                        (float)this.karmabody.GetPosition().y - (float)_karma__keyDown.y
                        );

                    if (gap.GetLength() > 0.1)
                    {
                        current.SetLinearVelocity(
                            new b2Vec2(
                                vx - gap.x * 2.0,
                                vy - gap.y * 2.0
                                )
                            );
                    }
                }
            }
            ApplyVelocityElapse.Restart();
        }
Exemplo n.º 2
0
        public void ApplyVelocity()
        {
            {
                var current = this.body;
                //var v = this.AngularVelocity * 10;
                //current.SetAngularVelocity(v);

                current.ApplyAngularImpulse(
                    this.AngularVelocity * AngularVelocityMultiplier
                    * this.CurrentInput.forcex
                    * ApplyVelocityElapse.ElapsedMilliseconds
                    * 1.0
                    * (1 - (this.body.GetLinearVelocity().Length() / this.speed).Min(0.9) * 0.5)
                    );


                var vx = Math.Cos(current.GetAngle()) * this.LinearVelocityY * this.speed * this.CurrentInput.forcey
                         + Math.Cos(current.GetAngle() + Math.PI / 2) * this.LinearVelocityX * this.speed;
                var vy = Math.Sin(current.GetAngle()) * this.LinearVelocityY * this.speed * this.CurrentInput.forcey
                         + Math.Sin(current.GetAngle() + Math.PI / 2) * this.LinearVelocityX * this.speed;



                this.visual.currentvisual.alpha = 1.0;

                #region RemoteGameReference
                if (RemoteGameReference != null)
                {
                    if (vx == 0)
                    {
                        if (vy == 0)
                        //if (v == 0)
                        {
                            // not moving anymore in network mode
                            // far enough to be out of sync?

                            //if (karmabody.GetAngularVelocity() == 0)
                            if (karmabody.GetLinearVelocity().Length() == 0)
                            {
                                if (this.KarmaInput0.All(k => k.value == 0))
                                {
                                    var gap = new __vec2(
                                        (float)this.karmabody.GetPosition().x - (float)this.body.GetPosition().x,
                                        (float)this.karmabody.GetPosition().y - (float)this.body.GetPosition().y
                                        );

                                    // tolerate lesser distance?

                                    var CloseEnough = gap.GetLength() < 1.2;
                                    var TooFar      = gap.GetLength() > 5;

                                    if (CloseEnough)
                                    {
                                        ApplyVelocityMoveToLocation = false;
                                    }

                                    if (TooFar || ApplyVelocityMoveToLocation)
                                    {
                                        //this.body.SetPositionAndAngle(
                                        //    new b2Vec2(
                                        //        this.karmabody.GetPosition().x,
                                        //        this.karmabody.GetPosition().y
                                        //    ),
                                        //    this.karmabody.GetAngle()
                                        //);

                                        // show we are in the wrong place!
                                        this.visual.currentvisual.alpha = 0.3;


                                        // look at where we should be instead
                                        this.body.SetAngle(
                                            gap.GetRotation()
                                            );

                                        // and walk there!
                                        vx = Math.Cos(current.GetAngle()) * 0.5 * this.speed
                                             + Math.Cos(current.GetAngle() + Math.PI / 2) * 0 * this.speed;
                                        vy = Math.Sin(current.GetAngle()) * 0.5 * this.speed
                                             + Math.Sin(current.GetAngle() + Math.PI / 2) * 0 * this.speed;

                                        ApplyVelocityMoveToLocation = true;
                                    }
                                    else
                                    {
                                        var da0   = this.karmabody.GetAngle() - this.body.GetAngle();
                                        var da360 = (this.karmabody.GetAngle() + 360.DegreesToRadians()) - this.body.GetAngle();

                                        if (da0 < da360)
                                        {
                                            this.body.SetAngle(
                                                this.body.GetAngle() + da0 * 0.2
                                                );
                                        }
                                        else
                                        {
                                            this.body.SetAngle(
                                                this.body.GetAngle() + da360 * 0.2
                                                );
                                        }

                                        if (CloseEnough)
                                        {
                                        }
                                        else
                                        {
                                            this.body.SetPosition(
                                                new b2Vec2(
                                                    this.body.GetPosition().x + gap.x * 0.3,
                                                    this.body.GetPosition().y + gap.y * 0.3
                                                    )
                                                );
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion


                current.SetLinearVelocity(
                    new b2Vec2(
                        vx, vy


                        )
                    );
            }


            // what about our karma body?
            if (this.KarmaInput0.Count > 0)
            {
                var _karma__keyDown = this.KarmaInput0.Peek();

                var _karma_velocity = new Velocity();


                ExtractVelocityFromInput(_karma__keyDown, _karma_velocity);

                var current = this.karmabody;
                //var v = _karma_velocity.AngularVelocity * 10;
                //current.SetAngularVelocity(v);

                this.karmabody.ApplyAngularImpulse(
                    _karma_velocity.AngularVelocity
                    * ApplyVelocityElapse.ElapsedMilliseconds
                    * 0.5
                    * (1 - (this.karmabody.GetLinearVelocity().Length() / this.speed).Min(0.9) * 0.25)
                    );


                var vx = Math.Cos(current.GetAngle()) * _karma_velocity.LinearVelocityY * this.speed
                         + Math.Cos(current.GetAngle() + Math.PI / 2) * _karma_velocity.LinearVelocityX * this.speed;
                var vy = Math.Sin(current.GetAngle()) * _karma_velocity.LinearVelocityY * this.speed
                         + Math.Sin(current.GetAngle() + Math.PI / 2) * _karma_velocity.LinearVelocityX * this.speed;

                current.SetLinearVelocity(
                    new b2Vec2(
                        vx, vy
                        )
                    );

                if (_karma__keyDown.fixup)
                {
                    var fixupmultiplier = 0.90;
                    // like a magnet
                    current.SetPositionAndAngle(
                        new b2Vec2(
                            _karma__keyDown.x + (current.GetPosition().x - _karma__keyDown.x) * fixupmultiplier,
                            _karma__keyDown.y + (current.GetPosition().y - _karma__keyDown.y) * fixupmultiplier
                            ),
                        // meab me in scotty
                        _karma__keyDown.angle + (current.GetAngle() - _karma__keyDown.angle) * fixupmultiplier

                        );
                }
            }


            this.damagebody.SetPositionAndAngle(
                new b2Vec2(this.body.GetPosition().x, this.body.GetPosition().y), this.body.GetAngle()
                );

            ApplyVelocityElapse.Restart();
        }
Exemplo n.º 3
0
        public PhysicalBunker(StarlingGameSpriteWithBunkerTextures textures, StarlingGameSpriteWithPhysics Context, bool IsShop = false)
        {
            var textures_bunker = textures;

            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.bunker2_shadow()
                ).AttachTo(Context.Content_layer2_shadows);

            visual = new Sprite().AttachTo(Context.Content_layer3_buildings);


            visual_body = new Image(
                textures.bunker2()
                ).AttachTo(visual);

            visual_shopoverlay = new Image(
                textures.bunker2_shopoverlay()
                ).AttachTo(visual);

            visual_shopoverlay_arrow = new Image(
                textures.bunker2_shopoverlay_arrow()
                ).AttachTo(visual);


            if (IsShop)
            {
                #region hud_arrow
                var hud_arrow = new Image(
                    textures_bunker.bunker2_shopoverlay_arrow()
                    ).AttachTo(Context);


                StarlingGameSpriteWithPhysics.onframe +=
                    (ScriptCoreLib.ActionScript.flash.display.Stage stage, Starling starling) =>
                {
                    var gap = new __vec2(
                        (float)(this.body.GetPosition().x - Context.current.body.GetPosition().x),
                        (float)(this.body.GetPosition().y - Context.current.body.GetPosition().y)
                        );

                    var distance = gap.GetLength();

                    if (distance < 40)
                    {
                        visual_shopoverlay_arrow.visible = true;
                        hud_arrow.visible = false;
                        return;
                    }


                    //if (distance < 50)
                    //{
                    //    visual_shopoverlay_arrow.visible = false;
                    //    hud_arrow.visible = false;
                    //    return;
                    //}

                    visual_shopoverlay_arrow.visible = false;
                    hud_arrow.visible = true;

                    var cm = new Matrix();

                    //
                    var yy = 8 * Math.Sin(this.Context.gametime.ElapsedMilliseconds * 0.002);



                    cm.translate(-128, -128 - 64 + yy);


                    cm.scale(Context.stagescale, Context.stagescale);


                    cm.rotate(gap.GetRotation() - Context.current.body.GetAngle() + Context.current.CameraRotation);

                    cm.translate(
                        (stage.stageWidth * 0.5),
                        (stage.stageHeight * Context.internal_center_y)
                        );


                    hud_arrow.transformationMatrix = cm;
                };
                #endregion
            }

            this.IsShop = IsShop;

            #region damage_b2world
            {
                //initialize body
                var bdef = new b2BodyDef();
                bdef.angle         = 0;
                bdef.fixedRotation = true;
                this.damagebody    = Context.damage_b2world.CreateBody(bdef);

                //initialize shape
                var fixdef = new b2FixtureDef();

                var shape = new b2PolygonShape();
                fixdef.shape = shape;

                shape.SetAsBox(4.5, 4.5);

                fixdef.restitution = 0.4; //positively bouncy!



                var fix = this.damagebody.CreateFixture(fixdef);

                var fix_data = new Action <double>(
                    force =>
                {
                    if (force < 1)
                    {
                        return;
                    }

                    Context.oncollision(this, force);
                }
                    );

                fix.SetUserData(fix_data);
            }
            #endregion


            #region ground_b2world
            {
                //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(4.5, 4.5);

                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);
            }
            #endregion

            {
                //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(4.5, 4.5);
                fixdef.restitution = 0.4; //positively bouncy!



                this.karmabody.CreateFixture(fixdef);
            }
            Context.internalunits.Add(this);
        }