public void TestCollisionWithWall() { _gameTime = NewGameTime(144); _wall = new Sprite(); _wall.Position = new Vector2(10, 100+80); _wall.Rect = new Rectangle((int)_wall.Position.X, (int)_wall.Position.Y, 100, 20); _jucko = new Character(); _jucko.Position = new Vector2(20, 100); _jucko.Rect = new Rectangle((int)_jucko.Position.X, (int)_jucko.Position.Y, 30, 90); _jucko.Update(_gameTime); _wall.Update(_gameTime); for (int updateGravity = 0; updateGravity < 50; updateGravity++) { _jucko.Update(_gameTime); Rectangle overlap = Rectangle.Intersect(_jucko.Rect, _wall.Rect); if (!overlap.IsEmpty) { _jucko.CollisionWith(_wall, overlap); } } Assert.AreEqual(90, _jucko.Position.Y); }
public void TestCollisionWithWallBounce() { //float YPosition1 = LetCharacterFall(StartPosition: new Vector2(200, 200), FallingSteps: 5, FPSCount: 50); _gameTime = NewGameTime(144); _wall = new Sprite(); _wall.Position = new Vector2(10, 100 + 80); _wall.Rect = new Rectangle((int)_wall.Position.X, (int)_wall.Position.Y, 100, 20); _wall.Color = Color.White; _wall.Update(_gameTime); _jucko = new Character(); _jucko.Position = new Vector2(20, 0); _jucko.Rect = new Rectangle((int)_jucko.Position.X, (int)_jucko.Position.Y, 30, 90); _jucko.Color = Color.Red; for (int updateGravity = 0; updateGravity < 50; updateGravity++) { _jucko.Update(_gameTime); } Rectangle overlap = Rectangle.Intersect(_jucko.Rect, _wall.Rect); if (!overlap.IsEmpty) { _jucko.CollisionWith(_wall, overlap); } for (int updateBounce = 0; updateBounce < 50; updateBounce++) { _jucko.Update(_gameTime); } Assert.AreEqual(18, _jucko.Position.Y); }
/// <summary> /// 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. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here allCharacters = new List<ICharacter>(); allSprites = new List<ISprite>(); allSpritesMoving = new List<SpriteMoving>(); allProjectiles = new List<Projectile>(); allParticles = new List<Particle>(); allBackgrounds = new List<ISprite>(); worldOffset = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); previousKeys = new Keys[0] { }; _randomizer = new Random(); InputManager.AddMouseCallback(InputManager.MouseButtons.LeftButton, shoot, InputManager.InputStates.OnInputDown); InputManager.AddMouseCallback(InputManager.MouseButtons.RightButton, explode, InputManager.InputStates.OnInputChange); InputManager.AddKeyboardCallback(Keys.R, resetPosition, InputManager.InputStates.OnInputDown); InputManager.AddKeyboardCallback(Keys.D1, checkKeys, InputManager.InputStates.OnInputDown); InputManager.AddKeyboardCallback(Keys.D2, checkKeys, InputManager.InputStates.OnInputDown); //InputManager.AddKeyboardCallback(Keys.D2, jucko.Body.Position.X++, InputManager.InputStates.OnInputDown); for (int i = 0; i < 3; i++) { allBackgrounds.Add(new Sprite()); allBackgrounds[i].Texture = "bg" + i; allBackgrounds[i].Position = new Vector2(0, worldOffset.Height - 500 - i * 150); allBackgrounds[i].Rect = new Rectangle((int)allBackgrounds[i].Position.X, (int)allBackgrounds[i].Position.Y, worldOffset.Width, 500); allBackgrounds[i].Color = new Color(_randomizer.Next(0, 255), _randomizer.Next(0, 255), _randomizer.Next(0, 255)); } jucko = new Character(); jucko.Head = CreateCharacterSprite(2 + 13, 0 + 32, 26, 34, Color.White, "head", 13, 34 - 2); jucko.Body = CreateCharacterSprite(0, 26, 30, 42, Color.White, "body", 0, 0); jucko.FeetLeft = CreateCharacterSprite(2 + 9, 63, 18, 30, Color.White, "leg", 9, 0); jucko.FeetRight = CreateCharacterSprite(10 + 9, 63, 18, 30, Color.White, "leg", 9, 0); jucko.Position = new Vector2(300, 300); jucko.Color = Color.Red; jucko.Rect = new Rectangle((int)jucko.Position.X, (int)jucko.Position.Y, 30, 93); allCharacters.Add(jucko); ISprite block = new Sprite(); allSprites.Add(CreateBlock(200, 400, 500, 20, Color.Red, "block")); allSprites.Add(CreateBlock(280, 140, 340, 20, Color.White, "block")); allSprites.Add(CreateBlock(240, 360, 20, 400, Color.Yellow, "block")); allSprites.Add(CreateBlock(240, 760, 100, 40, Color.Yellow, "block")); for (int i = 0; i < 5; i++) { allSprites.Add(CreateBlock(400 + i * 20, 380 - i * 20, 40, 20, Color.Red, "block")); //Stairs Red } allSprites.Add(CreateBlock(520, 300, 200, 20, Color.Blue, "block")); //Bar at top of stairs Rectangle border = new Rectangle(0, 0, Window.ClientBounds.Width * 2, Window.ClientBounds.Height); allSprites.Add(CreateBlock(border.X, border.Y, border.Width, 20, Color.White, "block")); //level border top allSprites.Add(CreateBlock(border.X, border.Y, 20, border.Height, Color.White, "block")); //level border left allSprites.Add(CreateBlock(border.X, border.Y + border.Height - 20, border.Width, 20, Color.White, "block")); //level border bottom allSprites.Add(CreateBlock(border.X + border.Width - 20, border.Y, 20, border.Height, Color.White, "block")); //level border right SpriteMoving movingblock = new SpriteMoving(10f, new Vector2(300, 60), new Vector2(200, 30), Color.Red, "block", new Vector2(20, 20), 100); allSpritesMoving.Add(movingblock); base.Initialize(); }
//[TestMethod] //public void TestProjectileCollision() { // _gameTime = NewGameTime(144); // _bullet = new Projectile(new Vector2(0, 0), new Vector2(100, 0), 50f, "", Color.Red, 100); // _wall = new Sprite(); // _wall.Position = new Vector2(5,0); // _wall.Rect = new Rectangle((int)_wall.Position.X, (int)_wall.Position.Y,100,20); // Rectangle overlap = new Rectangle() ; // while (overlap.IsEmpty) { // _bullet.Update(_gameTime); // overlap = Rectangle.Intersect(); // } // Assert.AreEqual(_bullet.Position.X, 200); // Assert.AreEqual(_bullet.Position.Y, 203); //} private float LetCharacterFall(Vector2 StartPosition, int FallingSteps, int FPSCount) { _jucko = new Character(); _gameTime = NewGameTime(FPSCount); _jucko.Position = StartPosition; for (int i = 0; i < FallingSteps; i++) { _jucko.Update(_gameTime); } return _jucko.Position.Y; }