コード例 #1
0
ファイル: Demo.cs プロジェクト: bsvercl/physics2d
        void AddExplosion()
        {
            System.Drawing.Point p = SdlDotNet.Input.Mouse.MousePosition;

            ExplosionLogic logic = new ExplosionLogic(
                new Vector2D(p.X, p.Y),
                Vector2D.Zero,
                20000, 0.1f, 1000,
                new Lifespan(1));
            engine.AddLogic(logic);
        }
コード例 #2
0
ファイル: DemoHelper.cs プロジェクト: bsvercl/physics2d
        public static DisposeCallback BasicDemoSetup(DemoOpenInfo info)
        {
            DisposeCallback dispose = null;
            IShape bombShape = ShapeFactory.CreateSprite(Cache<SurfacePolygons>.GetItem("rocket.png"), 2, 16, 3);
            dispose += DemoHelper.RegisterBombLaunching(info, bombShape, 120);
            dispose += DemoHelper.RegisterMousePicking(info);

            dispose += DemoHelper.RegisterBodyStreamSpawning(info,
                new Body(new PhysicsState(), ParticleShape.Default, 1, Coefficients.Duplicate(), new Lifespan(.5f)), 2, 120, 1000, Key.B);
            dispose += DemoHelper.RegisterMaintainSpawning(info, SdlDotNet.Input.Key.N,
                delegate(Vector2D position)
                {
                    ExplosionLogic result = new ExplosionLogic(position, Vector2D.Zero, 9000, .1f, 600, new Lifespan(.25f));
                    info.Scene.Engine.AddLogic(result);
                    return result;
                });

            List<RaySegment> segments = new List<RaySegment>();

            for (Scalar angle = 0; angle < MathHelper.PiOver2; angle += .05f)
            {
                RaySegment seg = new RaySegment();
                seg.Length = 500;
                seg.RayInstance = new Ray(Vector2D.Zero, Vector2D.FromLengthAndAngle(1, angle));
                segments.Add(seg);
            }

            IShape rayShape = ShapeFactory.CreateRays(segments.ToArray());
            dispose += DemoHelper.RegisterMaintainSpawning(info, SdlDotNet.Input.Key.M,
                delegate(Vector2D position)
                {
                    Body lazer = new Body(new PhysicsState(), rayShape, 1, new Coefficients(1, 1), new Lifespan());
                    lazer.State.Position.Linear = position;
                    lazer.State.Velocity.Angular = .91f;
                    lazer.IgnoresGravity = true;
                    lazer.ApplyPosition();
                    info.Scene.AddGraphic(CreateGraphic(lazer));
                    return lazer;
                });





            return dispose;
        }
コード例 #3
0
        private void DoGuns(Body body, Body enemy, Key fire)
        {
            //this method requires a deep understanding of delegates and events. ENJOY :P
            Scalar projectileSpeed = 500;
            Scalar projectileRadius = 3;
            Scalar projectileMass = 3;

            BoundingRectangle rect;
            Matrix2x3 ident = Matrix2x3.Identity;
            body.Shape.CalcBoundingRectangle(ref ident, out rect);


            DateTime lastFire = DateTime.Now;
            Scalar distance = rect.Max.X + projectileRadius * 2;
            EventHandler<KeyboardEventArgs> keyDown = delegate(object sender, KeyboardEventArgs e)
            {
                DateTime now = DateTime.Now;
                if (e.Key == fire &&
                    !body.Lifetime.IsExpired &&
                    now.Subtract(lastFire).TotalSeconds > .3)
                {
                    lastFire = now;
                    Vector2D normal = body.Matrices.ToWorldNormal * Vector2D.XAxis;
                    Vector2D position = new Vector2D(body.Matrices.ToWorld.m02, body.Matrices.ToWorld.m12) + (normal * distance);
                    Vector2D offset = normal.LeftHandNormal * 3 * projectileRadius;
                    Body projectile1 = DemoHelper.AddCircle(DemoInfo, projectileRadius, 8, projectileMass, new ALVector2D(0, position + offset));
                    Body projectile2 = DemoHelper.AddCircle(DemoInfo, projectileRadius, 8, projectileMass, new ALVector2D(0, position - offset));
                    projectile1.Lifetime.MaxAge = 5;
                    projectile2.Lifetime.MaxAge = 5;
                    Vector2D velocity = normal * projectileSpeed;
                    body.State.Velocity.Linear -= (projectileMass * 2 * velocity) * body.Mass.MassInv;
                    projectile1.State.Velocity.Linear = velocity + body.State.Velocity.Linear;
                    projectile2.State.Velocity.Linear = velocity + body.State.Velocity.Linear;
                    EventHandler<CollisionEventArgs> collided = delegate(object sender1, CollisionEventArgs e1)
                    {
                        Body projectile = (Body)sender1;
                        projectile.Lifetime.IsExpired = true;
                        bool isHit = e1.Other == enemy || e1.Other == body;

                        List<Body> particles = DemoHelper.AddParticles(
                            DemoInfo,
                            projectile.State.Position.Linear,
                            projectile.State.Velocity.Linear,
                            (isHit) ? (200) : (10));
                        if (isHit && !e1.Other.Lifetime.IsExpired)
                        {
                            ExplosionLogic explosionLogic = new ExplosionLogic(
                                e1.Other.State.Position.Linear, 
                                e1.Other.State.Velocity.Linear, 
                                9000, .1f, e1.Other.Mass.Mass, 
                                new Lifespan(.5f));
                            Scene.Engine.AddLogic(explosionLogic);
                            e1.Other.Lifetime.IsExpired = true;
                            particles[0].Removed += delegate(object sender2, RemovedEventArgs e2)
                            {
                                if (particles[0].Lifetime.IsExpired)
                                {
                                    e1.Other.State.Velocity = ALVector2D.Zero;
                                    e1.Other.State.Position = new ALVector2D(DemoHelper.NextScalar(-600, 900), DemoHelper.NextScalar(-600, 900), DemoHelper.NextScalar(-600, 900));
                                    e1.Other.ApplyPosition();
                                    e1.Other.Lifetime.IsExpired = false;
                                    Scene.AddGraphic((BodyGraphic)e1.Other.Tag);
                                }
                            };
                        }
                        else if (e1.Other.Shape is CircleShape && 
                            !e1.Other.IgnoresCollisionResponse)
                        {
                            e1.Other.Lifetime.IsExpired = true;
                        }
                    };
                    projectile1.Collided += collided;
                    projectile2.Collided += collided;
                }
            };

            Events.KeyboardDown += keyDown;
            dispose += delegate()
            {
                Events.KeyboardDown -= keyDown;
            };
        }