public bool HitBy(IHitter hitter) { if (this.Status == BrickStatus.Dead) { return(false); } Control c = (Control)hitter; Rectangle r1 = new Rectangle(c.Left, c.Top, c.Width, c.Height); Rectangle r2 = new Rectangle(this.Left, this.Top, this.Width, this.Height); bool hit = r1.IntersectsWith(r2); if (hit && (hitter is Ball || hitter is Bullet)) { switch (this.Status) { case BrickStatus.Normal: this.Status = BrickStatus.Falling; break; case BrickStatus.Falling: this.Status = BrickStatus.Floating; break; case BrickStatus.Floating: this.Status = BrickStatus.Falling; break; } } return(hit); }
private Brick(Position pos, int height, int width, int durability, BrickStatus status, String textPath) : base(pos, new DirVector(0, 0), 0, height, width, new ComponentPhysicsEmpty(), new ComponentInputEmpty()) { texturePath = textPath; Durability = durability; Status = status; }
void IMovable.Move() { switch (this.Status) { case BrickStatus.Normal: break; case BrickStatus.Falling: this.Top += 2; if (this.Top >= this.Parent.ClientRectangle.Height) { this.Status = BrickStatus.Dead; } break; case BrickStatus.Floating: this.Top -= 2; IHittable obj = this.HitOthers(); if (obj != null && (this.Top <= 4 || obj is Brick)) { this.Status = BrickStatus.Normal; this.Top += 3; } break; case BrickStatus.Dead: break; default: break; } }
public BrickBasic(Position pos, int height, int width, BrickStatus status, int durability, string texturePath) { this.pos = pos; this.height = height; this.width = width; this.status = status; this.durability = durability; this.texturePath = texturePath; }
public void TestBuilder() { LevelBuilder levelBuilder = new LevelBuilder(); //Setup brick int x = 4; int y = 4; BrickStatus state = BrickStatus.destructible; int durability = 1; //Setup variable of level string levelName = "level1"; PersonalSounds music = new PersonalSounds("musicPath", "musicTheme"); BackgroundTexture background = new BackgroundTexture("backgroundPath", "backgroundTheme"); BallTexture ball = new BallTexture("ballPath", "ballTheme"); PaddleTexture paddle = new PaddleTexture("paddlePath", "paddleTheme"); //Create level with levelBuilder levelBuilder.brickSelected(x, y, state, durability); levelBuilder.setLevelName(levelName); levelBuilder.SetMusic(music.GetMusicPath(), music.GetMusicTheme()); levelBuilder.SetBackGround(background.GetBackgroundPath(), background.GetBackgroundTheme()); levelBuilder.SetBall(ball.GetBallPath(), ball.GetBallTheme()); levelBuilder.setPaddle(paddle.GetPaddlePath(), paddle.GetPaddleTheme()); Level level = levelBuilder.Build(); //Create level with classic constructor int height = 1, width = 1; ISet <BrickBasic> brick = new HashSet <BrickBasic>(); brick.Add(new BrickBasic(new Position(x - 1, y - 1), height, width, state, durability, BrickBasic.GetDefaultBrickTexturePath())); Level levelTest = new Level(levelName, brick, music, background, ball, paddle); //Check if all parameters are the same Assert.AreEqual(levelTest.GetLevelName(), level.GetLevelName()); Assert.AreEqual(levelTest.GetBackground().GetBackgroundPath(), level.GetBackground().GetBackgroundPath()); Assert.AreEqual(levelTest.GetBackground().GetBackgroundTheme(), level.GetBackground().GetBackgroundTheme()); Assert.AreEqual(levelTest.GetMusic().GetMusicPath(), level.GetMusic().GetMusicPath()); Assert.AreEqual(levelTest.GetMusic().GetMusicTheme(), level.GetMusic().GetMusicTheme()); Assert.AreEqual(levelTest.GetBallTexture().GetBallPath(), level.GetBallTexture().GetBallPath()); Assert.AreEqual(levelTest.GetBallTexture().GetBallTheme(), level.GetBallTexture().GetBallTheme()); Assert.AreEqual(levelTest.GetPaddleTexture().GetPaddlePath(), level.GetPaddleTexture().GetPaddlePath()); Assert.AreEqual(levelTest.GetPaddleTexture().GetPaddleTheme(), level.GetPaddleTexture().GetPaddleTheme()); //Check if levels are equals Assert.IsTrue(levelTest.EqualLevel(level)); }
/// <summary> /// Create a set of num bricks /// </summary> /// <param name="num"></param> public void AddBrick(int num) { int i; int x = 0, y = 0; int height = 1; int width = 1; BrickStatus status = BrickStatus.destructible; int durability = 3; string texturePath = "brickPath"; for (i = 0; i < num; i++) { brick.Add(new BrickBasic(new Position(x, y), height, width, status, durability, texturePath)); x++; y++; } }
void IMovable.Move() { switch (this.Status) { case BrickStatus.Normal: break; case BrickStatus.Falling: this.Top += 1; if (this.Top > this.Parent.ClientRectangle.Bottom) { this.Status = BrickStatus.Dead; } break; case BrickStatus.Floating: this.Top -= 1; if (this.Top <= 2) { this.Status = BrickStatus.Normal; } IHittable obj = this.HitOthers(); if (obj != null && obj is Brick) { this.Top += BRICK_SPACE; this.Status = BrickStatus.Normal; } break; case BrickStatus.Dead: this.Visible = false; break; } }
public Builder Status(BrickStatus status) { this.status = status; return(this); }
/*------------------------------------- * PRIVATE METHODS *-----------------------------------*/ private void solveCollision(float dt, Entity entity, Entity entity2, Velocity velocity, Position position, Sprite sprite, Position pos2, Sprite spr2, BoundingRectangle br2) { var managers = Engine.getInst().managers; ParticleManager pm = null; for (int i = 0; i < managers.Count; i++) { if (managers[i].GetType().ToString() == "Breakout.Engine.Managers.ParticleManager") { pm = (ParticleManager)managers[i]; } } BrickStatus brickStatus = entity2.getComponent <BrickStatus>(); if (brickStatus != null) { if (!brickStatus.alive) { return; } // Kollision med brick float x = position.x; float y = position.y; if (position.x < br2.x - (sprite.texture.Width * 0.5f) || position.x > br2.x + br2.width + (sprite.texture.Width * 0.5f)) { velocity.x *= -1; x = (position.x < br2.x - (sprite.texture.Width * 0.5f)) ? x - (sprite.texture.Width * 0.5f) : x + (sprite.texture.Width * 0.5f); } else { velocity.y *= -1; y = (position.y < br2.y - (sprite.texture.Height * 0.5f)) ? y - (sprite.texture.Height * 0.5f) : y + (sprite.texture.Height * 0.5f); } entity2.getComponent <BrickStatus>().alive = false; for (int j = 0; j < 100; j++) { pm.generateNewParticle(new Vector2(x, y)); } } else // Kollision med paddlee { if ((position.x < br2.x - (sprite.texture.Width * 0.5f) || position.x > br2.x + br2.width + (sprite.texture.Width * 0.5f)) && position.y > br2.y + (sprite.texture.Height * 0.5f)) { velocity.x *= -1; } else { // Pythagoras på X och Y hastigheten för att räkna ut totala hastigheten. float speedXY = (float)Math.Sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y)); // Räkna ut bollens position relativt center av paddlen, resultat är emellan -1 och +1. float posX = (position.x - pos2.x) / (spr2.texture.Width * 0.5f); // Värde mellan 0 och 1 för hur mycket påverkan det ska ha på x-hastighet. float influenceX = 0.5f; // Räkna ut nya X-hastigheten baserat på vart den träffar paddlen, // gör den även relativ till originalhastighet och tweaka efter influenceX ovan. velocity.x = speedXY * posX * influenceX; // Baserat på nya x-hastigheten, räkna ut nya y-hastigheten så den totala hastigheten blir samma // som innan. Återigen pythagoras sats. velocity.y = (float)Math.Sqrt((speedXY * speedXY) - (velocity.x * velocity.x)) * (velocity.y > 0 ? -1 : 1); // Add some bounce on paddle var velocityPaddle = entity2.getComponent <Velocity>(); velocityPaddle.y += 300.0f; //Angle angle = entity.getComponent<Angle>(); AngularVelocity av = entity.getComponent <AngularVelocity>(); // Spin ball av.velocity = 0.03f * posX; // Hit tilt effect AngularVelocity avPaddle = entity2.getComponent <AngularVelocity>(); if (position.x < pos2.x - 15.0f || position.x > pos2.x + 15.0f) { rotation = position.x < pos2.x ? -1 : 1; avPaddle.velocity = rotation < 0 ? -0.01f : 0.01f; } } } // "Clamp" if (((br2.x - (sprite.texture.Width * 0.5f) + 5) < position.x && position.x < (br2.x + br2.width + (sprite.texture.Width * 0.5f) - 5)) && ((br2.y - (sprite.texture.Height * 0.5f) + 5) < position.y && position.y < (br2.y + br2.height + (sprite.texture.Height * 0.5f) - 5))) { if (Math.Abs(position.x - (br2.x + br2.width + (sprite.texture.Width * 0.5f))) < Math.Abs(position.x - (br2.x - (sprite.texture.Width * 0.5f)))) { position.x = (br2.x + br2.width + (sprite.texture.Width * 0.5f)) + 1; } else { position.x = (br2.x - (sprite.texture.Width * 0.5f)) - 1; } } }
/// <summary> /// select a brick in a grid and put it in. /// </summary> /// <param name="x"> position x</param> /// <param name="y"> position y</param> /// <param name="state">state of brick</param> /// <param name="durability">durability of brick</param> /// <returns>value if brick was insert correctly</returns> public KeyValuePair <GameObjectEmpty, Boolean> brickSelected(double x, double y, BrickStatus state, int durability) { KeyValuePair <GameObjectEmpty, Boolean> retState = new KeyValuePair <GameObjectEmpty, bool>(new GameObjectEmpty(new Position(0, 0), 0, 0), false); foreach (GameObjectEmpty objectEmpty in builderGrid.Keys) { if (x > objectEmpty.GetPos().GetX() && x <= objectEmpty.GetPos().GetX() + objectEmpty.GetWidth() && y > objectEmpty.GetPos().GetY() && y <= objectEmpty.GetPos().GetY() + objectEmpty.GetHeight()) { KeyValuePair <int, int> brickSelected = builderGrid[objectEmpty]; if (this.gameGrid[brickSelected].Value != null) { KeyValuePair <GameObjectEmpty, BrickBasic> temp = new KeyValuePair <GameObjectEmpty, BrickBasic>(gameGrid[brickSelected].Key, null); gameGrid[brickSelected] = temp; retState = new KeyValuePair <GameObjectEmpty, bool>(objectEmpty, false); } else { string selectedTexture; if (state.Equals(BrickStatus.dropPowerup)) { selectedTexture = BrickBasic.GetDefaultPowerupTexturePath(); } else if (state.Equals(BrickStatus.notDestructible)) { selectedTexture = BrickBasic.GetTextureNotDestructible(); } else { selectedTexture = BrickBasic.GetDefaultBrickTexturePath(); } GameObjectEmpty gameObjectEmpty = gameGrid[brickSelected].Key; BrickBasic brick = new BrickBasic(new Position(gameObjectEmpty.GetPos().GetX(), gameObjectEmpty.GetPos().GetY()), this.gameGrid[brickSelected].Key.GetHeight(), this.gameGrid[brickSelected].Key.GetWidth(), state, durability, selectedTexture); KeyValuePair <GameObjectEmpty, BrickBasic> temp = new KeyValuePair <GameObjectEmpty, BrickBasic>(gameGrid[brickSelected].Key, brick); gameGrid[brickSelected] = temp; retState = new KeyValuePair <GameObjectEmpty, bool>(objectEmpty, true); } } } return(retState); }