private void collisionWithBomb(BombEntity bomber) { (this.LogicalObj as BombLogical).suddenlyMeetTime(); }
private void collisionWithBomb(BombEntity bomb) { if (!bomb.IsBomberCollide(this)) return; BomberRenderer renderer = RendererObj as BomberRenderer; BomberLogical logical = LogicalObj as BomberLogical; BombRenderer obsRenderer = (bomb.RendererObj as BombRenderer); BombLogical obsLogical = (bomb.LogicalObj as BombLogical); #region Replace Position Vector2 newVelocity = Vector2.Zero; bool rightDirection = false; switch (renderer.direction) { case Shared.Constants.DIRECTION_LEFT: rightDirection = (obsLogical.Bound.X <= renderer.Position.X) ? true : false; if (rightDirection) newVelocity.X = obsLogical.Bound.X + obsLogical.Bound.Width - renderer.Position.X; break; case Shared.Constants.DIRECTION_RIGHT: rightDirection = (obsLogical.Bound.X >= renderer.Position.X) ? true : false; if (rightDirection) newVelocity.X = obsLogical.Bound.X - logical.Bound.X - logical.Bound.Width; break; case Shared.Constants.DIRECTION_UP: rightDirection = (obsLogical.Bound.Y <= renderer.Position.Y) ? true : false; if (rightDirection) newVelocity.Y = obsLogical.Bound.Y + obsLogical.Bound.Height - renderer.Position.Y; break; case Shared.Constants.DIRECTION_DOWN: rightDirection = (obsLogical.Bound.Y >= renderer.Position.Y) ? true : false; if (rightDirection) newVelocity.Y = obsLogical.Bound.Y - logical.Bound.Y - logical.Bound.Height; break; } if (!rightDirection) { Vector2 newPos = renderer.Position; switch (renderer.oldDirection) { case Shared.Constants.DIRECTION_LEFT: newPos.X = obsRenderer.Position.X + obsLogical.Bound.Width; break; case Shared.Constants.DIRECTION_RIGHT: newPos.X = obsRenderer.Position.X - logical.Bound.Width; break; case Shared.Constants.DIRECTION_UP: newPos.Y = obsRenderer.Position.Y + obsLogical.Bound.Height; break; case Shared.Constants.DIRECTION_DOWN: newPos.Y = obsRenderer.Position.Y - logical.Bound.Height; break; } renderer.Position = newPos; } #endregion #region Adjust Position Cell obsCell = Grid.Grid.getInst().GetCellAtPosition(obsRenderer.Position); if (obsCell != null) { if (renderer.direction == Shared.Constants.DIRECTION_DOWN || renderer.direction == Shared.Constants.DIRECTION_UP) { Cell cellLeft = Grid.Grid.getInst().GetCellAtLocation(new Vector2(obsCell.Location.X, obsCell.Location.Y - 1)); Cell cellRight = Grid.Grid.getInst().GetCellAtLocation(new Vector2(obsCell.Location.X, obsCell.Location.Y + 1)); if (cellLeft != null && cellLeft.Contents.Count == 0 && Math.Abs(obsRenderer.Position.X - (renderer.Position.X + LogicalObj.Bound.Width)) <= Shared.Constants.COLLISION_MIN) { newVelocity.X += obsRenderer.Position.X - (renderer.Position.X + LogicalObj.Bound.Width); } else if (cellRight != null && cellRight.Contents.Count == 0 && Math.Abs((obsRenderer.Position.X + bomb.LogicalObj.Bound.Width) - renderer.Position.X) <= Shared.Constants.COLLISION_MIN) { newVelocity.X += (obsRenderer.Position.X + bomb.LogicalObj.Bound.Width) - renderer.Position.X; } } else if (renderer.direction == Shared.Constants.DIRECTION_LEFT || renderer.direction == Shared.Constants.DIRECTION_RIGHT) { Cell cellUp = Grid.Grid.getInst().GetCellAtLocation(new Vector2(obsCell.Location.X - 1, obsCell.Location.Y)); Cell cellDown = Grid.Grid.getInst().GetCellAtLocation(new Vector2(obsCell.Location.X + 1, obsCell.Location.Y)); if (cellUp != null && cellUp.Contents.Count == 0 && Math.Abs(obsRenderer.Position.Y - (renderer.Position.Y + LogicalObj.Bound.Height)) <= Shared.Constants.COLLISION_MIN) { newVelocity.Y += obsRenderer.Position.Y - (renderer.Position.Y + LogicalObj.Bound.Height); } else if (cellDown != null && cellDown.Contents.Count == 0 && Math.Abs((obsRenderer.Position.Y + bomb.LogicalObj.Bound.Height) - renderer.Position.Y) <= Shared.Constants.COLLISION_MIN) { newVelocity.Y += (obsRenderer.Position.Y + bomb.LogicalObj.Bound.Height) - renderer.Position.Y; } } } #endregion if (newVelocity.Length() != 0) { renderer.MoveImmediately(newVelocity); renderer.updateMovement(); } renderer.refreshAccelerator(); }
private void collisionWithBomb(BombEntity bomb) { //(this.RendererObj as EnemyRenderer).randomDirection(); (this.RendererObj as EnemyRenderer).ChangeNegativeDirection((this.RendererObj as EnemyRenderer).direction); }