protected override void Open() { dispose += DemoHelper.BasicDemoSetup(DemoInfo); Scene.Engine.AddLogic(new GravityField(new Vector2D(0, 1000), new Lifespan())); SurfacePolygons surfacePolygons = Cache <SurfacePolygons> .GetItem("physicsplayGround.png"); foreach (Vector2D[] vertexes in surfacePolygons.Polygons) { Vector2D[] processed = vertexes; for (int index = 1; index < 4; index++) { processed = VertexHelper.Reduce(processed, index); } processed = VertexHelper.Subdivide(processed, 16); IShape shape = ShapeFactory.CreateColoredPolygon(processed, 10); DemoHelper.AddShape(DemoInfo, shape, Scalar.PositiveInfinity, new ALVector2D(0, surfacePolygons.Offset)).IgnoresGravity = true; } for (int x = 440; x < 480; x += 10) { for (int y = -2000; y < 0; y += 12) { Body body = DemoHelper.AddCircle(DemoInfo, 5, 7, 3, new ALVector2D(0, x + DemoHelper.NextScalar(-400, 400), y)); body.Updated += delegate(object sender, UpdatedEventArgs e) { if (body.State.Position.Linear.Y > 900) { body.State.Position.Linear.Y = -100; } }; } } for (int x = 490; x < 510; x += 10) { for (int y = -550; y < -500; y += 12) { Body body = DemoHelper.AddRectangle(DemoInfo, 10, 20, 10, new ALVector2D(0, x + DemoHelper.NextScalar(-400, 400), y)); body.Updated += delegate(object sender, UpdatedEventArgs e) { if (body.State.Position.Linear.Y > 900) { body.State.Position.Linear.Y = -100; } }; } } }
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; }; }