Exemplo n.º 1
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;
            };
        }