using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; using FlatRedBall; using FlatRedBall.Content.AnimationChain; using FlatRedBall.Gui; using FlatRedBall.Graphics.Animation; using FlatRedBall.Input; using FlatRedBall.Math.Geometry; namespace FlatRedBall_XNA_Template { /// /// This is the main type for your game /// public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteList sprites; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// protected override void Initialize() { FlatRedBallServices.InitializeFlatRedBall(this, this.graphics); // Uncomment the following line and add your Screen's fully qualified name // if using Screens in your project. If not, or if you don't know what it means, // just ignore the following line for now. // For more information on Screens see the Screens wiki article on FlatRedBall.com. //Screens.ScreenManager.Start("FlatRedBall_XNA_Template1.Screens.SplashScreen"); sprites = new SpriteList(); SpriteManager.Camera.Z = 100; // Create 800 Sprites for (int i = 0; i < 800; i++) { Sprite redBall = SpriteManager.AddSprite("redball.bmp"); SpriteManager.Camera.PositionRandomlyInView(redBall, SpriteManager.Camera.Z, SpriteManager.Camera.Z); float angle = (float)(FlatRedBallServices.Random.NextDouble() * Math.PI * 2); float magnitude = (float)FlatRedBallServices.Random.NextDouble() * 3; redBall.XVelocity = (float)Math.Cos(angle) * magnitude; redBall.YVelocity = (float)Math.Sin(angle) * magnitude; redBall.ColorOperation = FlatRedBall.Graphics.ColorOperation.Add; sprites.Add(redBall); } base.Initialize(); } /// /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { FlatRedBallServices.Update(gameTime); Screens.ScreenManager.Activity(); // Sort all Sprites on the X axis sprites.SortXInsertionAscending(0, sprites.Count); float maximumXDistance = 2; float maximumDistanceSquared = 2*2; float furthestLeft = -SpriteManager.Camera.RelativeXEdgeAt(0) + 1; float furthestRight = SpriteManager.Camera.RelativeXEdgeAt(0) - 1; float furthestTop = SpriteManager.Camera.RelativeYEdgeAt(0) - 1; float furthestBottom = -SpriteManager.Camera.RelativeXEdgeAt(0) + 1; // reset all colors outside of the collision loop for (int i = 0; i < sprites.Count; i++) { sprites[i].Green = 0; // this could be partitioned too, but we're more concerned about the // collisions below if (sprites[i].X < furthestLeft && sprites[i].XVelocity < 0) { sprites[i].X = furthestLeft; sprites[i].XVelocity *= -1; } if (sprites[i].X > furthestRight && sprites[i].XVelocity > 0) { sprites[i].X = furthestRight; sprites[i].XVelocity *= -1; } if (sprites[i].Y > furthestTop && sprites[i].YVelocity > 0) { sprites[i].Y = furthestTop; sprites[i].YVelocity *= -1; } if (sprites[i].Y < furthestBottom && sprites[i].YVelocity < 0) { sprites[i].Y = furthestBottom; sprites[i].YVelocity *= -1; } } int j; float xDistance; float yDistance; for (int i = 0; i < sprites.Count - 1; i++) { for (j = i + 1; j < sprites.Count; j++) { xDistance = sprites[j].Position.X - sprites[i].Position.X; if (xDistance < maximumXDistance) { // The two are close enough on the X to perform collision, so check the // distance between the two. yDistance = sprites[j].Position.Y - sprites[i].Position.Y; if (xDistance * xDistance + yDistance * yDistance < maximumDistanceSquared) { sprites[i].Green = 1; sprites[j].Green = 1; } } else { // the two are too far away, so move on to the next Sprite break; } } } base.Update(gameTime); } /// /// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { FlatRedBallServices.Draw(); base.Draw(gameTime); } } }