示例#1
0
文件: Tank.cs 项目: Jypeli-JYU/Jypeli
        /// <summary>
        /// Alustaa uuden tankin.
        /// </summary>
        public Tank(double width, double height)
            : base(width, height)
        {
            if (commonImage == null)
            {
                commonImage = Game.LoadImageFromResources("Tank.png");
            }
            if (commonShape == null)
            {
                commonShape = Shape.FromImage(commonImage);
            }
            Image = commonImage;
            Shape = commonShape;
            HitPoints.LowerLimit += Break;
            CollisionIgnorer      = new ObjectIgnorer();
            LinearDamping         = 0.99;

            cannon                = new Cannon(Width * 0.75, Height * 0.2);
            Cannon.Position       = new Vector(0, Height * 0.25);
            Cannon.TimeBetweenUse = TimeSpan.FromSeconds(0.5);
            Cannon.Ammo.Value     = 100;
            this.Add(Cannon);

            AddedToGame += AddWheels;
        }
示例#2
0
 /// <summary>
 /// Alustaa uuden rypälepommin.
 /// </summary>
 /// <param name="radius">Säde.</param>
 /// <param name="cl">Kuinka monta kertaa rypäleet hajoavat edelleen. Kuitenkin vähintään yhden kerran.</param>
 public ClusterGrenade(double radius, int cl)
     : base(radius)
 {
     Mass             = 20;
     CollisionIgnorer = new ObjectIgnorer();
     NumberOfClusters = 3;
     ClusterDirection = Angle.FromRadians(Math.PI / 2);
     ClusterArc       = Math.PI;
     clusterlevel     = cl - 1;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="TouchablePanel"/> class.
        /// </summary>
        public TouchablePanel()
        {
            elementToBody    = new Dictionary <FrameworkElement, Body>();
            shouldCreateBody = new List <FrameworkElement>();
            shouldRemoveBody = new List <FrameworkElement>();
            contactJoints    = new Dictionary <int, FixedHingeJoint>();
            elementToScale   = new Dictionary <FrameworkElement, ScaleState>();
            ignorer          = new ObjectIgnorer();

            engine            = new PhysicsEngine();
            engine.BroadPhase = new SweepAndPruneDetector();
            engine.Solver     = new SequentialImpulsesSolver();
            timer             = new PhysicsTimer(PhysicsTimerCallback, 0.01);

            Loaded += TouchablePanel_Loaded;
        }
示例#4
0
 /// <summary>
 /// Luo uuden fyysisen kappaleen.
 /// Ei yleensä tarvetta käyttää itse (tee mieluummin PhysicsObject)
 /// </summary>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="shape"></param>
 public PhysicsBody(double width, double height, Shape shape)
 {
     Shape            = shape;
     Size             = new Vector(width, height);
     CollisionIgnorer = new ObjectIgnorer();
 }
示例#5
0
        public static DisposeCallback CreateTank(DemoOpenInfo info, Vector2D position, List <Body> result)
        {
            Lifespan avatarLifespan = new Lifespan();

            IShape shape = ShapeFactory.CreateSprite(Cache <SurfacePolygons> .GetItem("tank.png"), 4, 18, 2);

            ObjectIgnorer ignorer  = new ObjectIgnorer();
            Body          tankBody = new Body(new PhysicsState(new ALVector2D(0, 0, 0)),
                                              shape,
                                              300,//new MassInfo(40, Scalar.PositiveInfinity),
                                              new Coefficients(0, 1),
                                              avatarLifespan);

            result.Add(tankBody);
            tankBody.State.Position.Linear += position;
            tankBody.ApplyPosition();

            tankBody.CollisionIgnorer = ignorer;
            BodyGraphic graphic = CreateGraphic(tankBody);

            graphic.ZOrder = 2;
            info.Scene.AddGraphic(graphic);

            Scalar            wheelSize     = 18;
            Scalar            wheelSpacing  = -9;
            Scalar            lenghtPercent = .84f;
            Matrix2x3         ident         = Matrix2x3.Identity;
            BoundingRectangle rect;

            shape.CalcBoundingRectangle(ref ident, out rect);
            Scalar          y         = (rect.Max.Y + 4);
            Body            lastWheel = null;
            BoundingPolygon polygon   = new BoundingPolygon(shape.Vertexes);

            Ray      ray2 = new Ray(new Vector2D(rect.Max.X, y), -Vector2D.YAxis);
            Scalar   y3   = y - polygon.Intersects(ray2);
            Vector2D avatarBarrelOffset = new Vector2D(rect.Max.X + 10, y3);

            CircleShape wheelShape = ShapeFactory.CreateColoredCircle(wheelSize, 30);
            Scalar      force      = 0;

            for (Scalar x = rect.Min.X + wheelSize; x < (rect.Max.X - wheelSize) * lenghtPercent; x += (wheelSize * 2 + wheelSpacing))
            {
                Ray    ray = new Ray(new Vector2D(x, y), -Vector2D.YAxis);
                Scalar y2  = y - polygon.Intersects(ray);



                Vector2D offset = new Vector2D(x, y2);

                Body wheel = new Body(
                    new PhysicsState(new ALVector2D(0, offset + position)),
                    wheelShape,
                    10,
                    new Coefficients(0, 3),//  coefficients.Duplicate(),
                    avatarLifespan);
                result.Add(wheel);

                wheel.CollisionIgnorer = ignorer;
                wheel.AngularDamping   = .9f;
                wheel.Updated         += delegate(object sender, UpdatedEventArgs e)
                {
                    wheel.State.ForceAccumulator.Angular += force;
                };
                info.Scene.AddGraphic(CreateGraphic(wheel));

                HingeJoint joint = new HingeJoint(tankBody, wheel, offset + position, avatarLifespan);
                joint.Softness = .1f;
                info.Scene.Engine.AddJoint(joint);

                if (lastWheel != null)
                {
                    AngleJoint joint2 = new AngleJoint(lastWheel, wheel, avatarLifespan);
                    info.Scene.Engine.AddJoint(joint2);
                }
                lastWheel = wheel;
            }


            CircleShape weaponShape = ShapeFactory.CreateColoredCircle(5, 8);

            //now begins the abuse of anominous delegates (BIG TIME)

            EventHandler <KeyboardEventArgs> keyDownHandler = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                case Key.LeftArrow:
                    force = -1500000;
                    break;

                case Key.RightArrow:
                    force = 1500000;
                    break;

                case Key.Space:

                    Scalar velocity = 2000;

                    Matrix2x3 toWorld       = tankBody.Matrices.ToWorld;
                    Matrix2x2 toWorldNormal = tankBody.Matrices.ToWorldNormal;

                    //  Matrix2D mat = avatarBodies[0].Matrices.ToWorld;
                    Vector2D     direction = toWorldNormal * Vector2D.XAxis;
                    PhysicsState state     = new PhysicsState();
                    state.Position.Linear = toWorld * (avatarBarrelOffset);
                    state.Velocity.Linear = velocity * direction + tankBody.State.Velocity.Linear;

                    Body weapon = new Body(state,
                                           weaponShape,
                                           5,
                                           new Coefficients(1, 1),
                                           new Lifespan(10));
                    //weapon.CollisionIgnorer = tankBody.CollisionIgnorer;

                    weapon.Collided += delegate(object sender2, CollisionEventArgs e2)
                    {
                        if (!weapon.Lifetime.IsExpired)
                        {
                            weapon.Lifetime.IsExpired = true;
                            AddParticles(info, weapon.State.Position.Linear, weapon.State.Velocity.Linear * .5f, 50);
                        }
                    };

                    //  weapon.Collided += weapon_Collided;
                    tankBody.State.Velocity.Linear -= (velocity * weapon.Mass.Mass * tankBody.Mass.MassInv) * direction;
                    info.Scene.AddGraphic(CreateGraphic(weapon));
                    break;
                }
            };
            EventHandler <KeyboardEventArgs> keyUpHandler = delegate(object sender, KeyboardEventArgs e)
            {
                switch (e.Key)
                {
                case Key.LeftArrow:
                    force = 0;
                    break;

                case Key.RightArrow:
                    force = 0;
                    break;
                }
            };

            Events.KeyboardDown += keyDownHandler;
            Events.KeyboardUp   += keyUpHandler;

            return(delegate()
            {
                Events.KeyboardDown -= keyDownHandler;
                Events.KeyboardUp -= keyUpHandler;
            });
        }