Okay, thanks Jim and squidpunch :) This is my first game using C#/XNA, so at the minute all the code for my collision detection is held in game1 (which I now realise is a sloppy way of doing things..), would it still work okay using the asteroid manager in this way, or would I have to transfer the collision detection code somewhere else (in the Asteroid Manager class?). I also have mini-asteroids the branch off when a regular asteroid is shot... how should I deal with those? =/
This is my collision code, doGameLogic detects collision between the player and an asteroid, and ShotAstCollide detects collision between a bullet and an asteroid(and mini-asteroid):
| private void doGameLogic() |
| { |
| bool hasCollision = false; |
| |
| Rectangle shipRectangle = player.GetBounds(); |
| Rectangle shotRect = shot.GetShotBounds(); |
| foreach (GameComponent gc in Components) |
| { |
| if (gc is Asteroid) |
| { |
| hasCollision = ((Asteroid)gc).CheckCollision(shipRectangle); |
| if (hasCollision) |
| { |
| vibrate.AddVibration(0.8f, 0.8f, 3000); |
| audio.Explosion.Play(); |
| RemoveAllAsteroids(); |
| multiplier = 0; |
| lives -= 1; |
| |
| Start(); |
| |
| break; |
| } |
| else |
| GamePad.SetVibration(PlayerIndex.One, 0f, 0f); |
| } |
| |
| if (gc is MiniAst1) |
| { |
| hasCollision = ((MiniAst1)gc).CheckCollision(shipRectangle); |
| if (hasCollision) |
| { |
| vibrate.AddVibration(0.8f, 0.8f, 3000); |
| audio.Explosion.Play(); |
| RemoveAllAsteroids(); |
| multiplier = 0; |
| lives -= 1; |
| |
| Start(); |
| |
| break; |
| } |
| } |
| |
| if (gc is MiniAst2) |
| { |
| hasCollision = ((MiniAst2)gc).CheckCollision(shipRectangle); |
| if (hasCollision) |
| { |
| vibrate.AddVibration(0.8f, 0.8f, 3000); |
| audio.Explosion.Play(); |
| RemoveAllAsteroids(); |
| multiplier = 0; |
| lives -= 1; |
| |
| Start(); |
| |
| break; |
| } |
| } |
| |
| } |
| } |
| |
| |
| |
| private void ShotAstCollide() |
| { |
| bool hasCollision = false; |
| |
| foreach (GameComponent gc in Components) |
| { |
| if (gc is Shot) |
| { |
| Shot shot = (Shot)gc; |
| foreach (GameComponent Ast in Components) |
| { |
| |
| if (Ast is Asteroid) |
| { |
| Asteroid asteroid = (Asteroid)Ast; |
| hasCollision = asteroid.CheckCollision(shot.GetBounds()); |
| if (hasCollision) |
| { |
| if (asteroid.hit == false) |
| { |
| |
| asteroid.Split(); |
| score += 1 * multiplier; |
| multiplier++; |
| asteroid.hit = true; |
| Components.Remove(asteroid); |
| |
| Shot shotToKill = (Shot)gc; |
| Components.Remove(shotToKill); |
| shotToKill = null; |
| return; |
| } |
| } |
| } |
| if (Ast is MiniAst1) |
| { |
| MiniAst1 miniAst1 = (MiniAst1)Ast; |
| hasCollision = miniAst1.CheckCollision(shot.GetBounds()); |
| if (hasCollision) |
| { |
| if (miniAst1.hit == false) |
| { |
| score += 2 * multiplier; |
| multiplier++; |
| miniAst1.hit = true; |
| Components.Remove(miniAst1); |
| Shot shotToKill = (Shot)gc; |
| Components.Remove(shotToKill); |
| shotToKill = null; |
| return; |
| } |
| } |
| } |
| if (Ast is MiniAst2) |
| { |
| MiniAst2 miniAst2 = (MiniAst2)Ast; |
| |
| |
| hasCollision = miniAst2.CheckCollision(shot.GetBounds()); |
| if (hasCollision) |
| { |
| if (miniAst2.hit == false) |
| { |
| score += 2 * multiplier; |
| multiplier++; |
| miniAst2.hit = true; |
| Components.Remove(miniAst2); |
| Shot shotToKill = (Shot)gc; |
| Components.Remove(shotToKill); |
| shotToKill = null; |
| return; |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
|
Here is RemoveAllAsteroids:
| private void RemoveAllAsteroids() |
| { |
| for (int i = 0; i < Components.Count; i++) |
| { |
| if (Components[i] is Asteroid) |
| { |
| Components.RemoveAt(i); |
| i--; |
| } |
| |
| if (Components[i] is MiniAst1) |
| { |
| Components.RemoveAt(i); |
| i--; |
| } |
| |
| if (Components[i] is MiniAst2) |
| { |
| Components.RemoveAt(i); |
| i--; |
| } |
| } |
| } |
I already have a boolean in the Asteroid called "hit" this can do the same thing as "IsAlive" yeah?
Sorry, I'm not quite 100% on how to inegrate the asteroid manager with my current code.
Obviously I'd change everything to do with removing asteroids from the components list but could I keep the basic collision code the same?
The code I currently use to load the asteroids at the start of the game is this:
| private void Start() |
| { |
| //STARTASTCOUNT is a constant integer with a value of 10 |
| for (int i = 0; i < STARTASTCOUNT; i++) |
| { |
| Components.Add(new Asteroid(this, ref asteroidTexture)); |
| } |
| |
| //create and put player in position |
| if (player == null) |
| { |
| player = new Ship(this, ref asteroidTexture); |
| Components.Add(player); |
| } |
| player.PutinStartPosition(); |
| |
| } |
Thanks for the help so far guys :)