Exemple #1
0
 public override void handleCollision(CollidableObject collidedObject)
 {
     if (collidedObject.GetType() == typeof(EnemyBullet))
     {
         explosion.Play();
         exploded      = true;
         bounds.Width  = 128;
         bounds.Height = 128;
     }
 }
Exemple #2
0
 public override void handleCollision(CollidableObject collidedObject)
 {
     if (collidedObject.GetType() == typeof(EnemyBullet))
     {
         EnemyBullet bullet = (EnemyBullet)collidedObject;
         if (bullet.IsVisible)
         {
             isVisible = false;
         }
     }
 }
Exemple #3
0
        public void Move(CollidableObject data, Vector2 oldbounds, Vector2 bounds)
        {
            KeyValuePair <int, int> oldValue = new KeyValuePair <int, int>((int)oldbounds.X / CELL_SIZE_X, (int)oldbounds.Y / CELL_SIZE_Y);
            KeyValuePair <int, int> newValue = new KeyValuePair <int, int>((int)bounds.X / CELL_SIZE_X, (int)bounds.Y / CELL_SIZE_Y);

            if (!oldValue.Equals(newValue))
            {
                grid[oldValue].Remove(data);
                Add(data, bounds);
            }
        }
Exemple #4
0
 public override void handleCollision(CollidableObject collidedObject)
 {
     if (collidedObject.GetType() == typeof(PlayerBullet))
     {
         PlayerBullet bullet = (PlayerBullet)collidedObject;
         game.score += 10;
         if (bullet.IsVisible)
         {
             isvisible = false;
         }
     }
 }
Exemple #5
0
        public void Add(CollidableObject data, Vector2 bounds)
        {
            KeyValuePair <int, int> value = new KeyValuePair <int, int>(((int)bounds.X / CELL_SIZE_X), ((int)bounds.Y / CELL_SIZE_Y));

            if (!grid.ContainsKey(value))
            {
                grid.Add(value, new List <CollidableObject> {
                    data
                });
            }
            else
            {
                grid[value].Add(data);
            }
        }
 public abstract void handleCollision(CollidableObject collidableObject);
Exemple #7
0
        public void CheckCollisions()
        {
            foreach (KeyValuePair <int, int> entry in grid.Keys)
            {
                List <CollidableObject> currentCell = grid[entry];
                List <CollidableObject> leftCell;
                List <CollidableObject> rightCell;
                List <CollidableObject> topCell;
                List <CollidableObject> bottomCell;
                //left cell
                if (grid.ContainsKey(new KeyValuePair <int, int>(entry.Key - 1, entry.Value)))
                {
                    leftCell = grid[new KeyValuePair <int, int>(entry.Key - 1, entry.Value)];
                }
                else
                {
                    leftCell = new List <CollidableObject>();
                }
                //right cell
                if (grid.ContainsKey(new KeyValuePair <int, int>(entry.Key + 1, entry.Value)))
                {
                    rightCell = grid[new KeyValuePair <int, int>(entry.Key + 1, entry.Value)];
                }
                else
                {
                    rightCell = new List <CollidableObject>();
                }
                //top cell
                if (grid.ContainsKey(new KeyValuePair <int, int>(entry.Key, entry.Value - 1)))
                {
                    topCell = grid[new KeyValuePair <int, int>(entry.Key, entry.Value - 1)];
                }
                else
                {
                    topCell = new List <CollidableObject>();
                }
                //bottom cell
                if (grid.ContainsKey(new KeyValuePair <int, int>(entry.Key, entry.Value + 1)))
                {
                    bottomCell = grid[new KeyValuePair <int, int>(entry.Key, entry.Value + 1)];
                }
                else
                {
                    bottomCell = new List <CollidableObject>();
                }

                for (int i = 0; i < currentCell.Count; i++)
                {
                    CollidableObject collidedObject = currentCell[i];
                    for (int j = i + 1; j < currentCell.Count - 1; j++)
                    {
                        if (collidedObject.RectBounds().Intersects(currentCell[j].RectBounds()))
                        {
                            collidedObject.handleCollision(currentCell[j]);
                            currentCell[j].handleCollision(collidedObject);
                        }
                    }
                    {
                        foreach (CollidableObject collidableObjectleft in leftCell)
                        {
                            if (collidedObject.RectBounds().Intersects(collidableObjectleft.RectBounds()))
                            {
                                collidedObject.handleCollision(collidableObjectleft);
                                collidableObjectleft.handleCollision(collidedObject);
                            }
                        }
                        foreach (CollidableObject collidableObjectright in rightCell)
                        {
                            if (collidedObject.RectBounds().Intersects(collidableObjectright.RectBounds()))
                            {
                                collidedObject.handleCollision(collidableObjectright);
                                collidableObjectright.handleCollision(collidedObject);
                            }
                        }
                        foreach (CollidableObject collidableObjecttop in topCell)
                        {
                            if (collidedObject.RectBounds().Intersects(collidableObjecttop.RectBounds()))
                            {
                                collidedObject.handleCollision(collidableObjecttop);
                                collidableObjecttop.handleCollision(collidedObject);
                            }
                        }
                        foreach (CollidableObject collidableObjectbottom in bottomCell)
                        {
                            if (collidedObject.RectBounds().Intersects(collidableObjectbottom.RectBounds()))
                            {
                                collidedObject.handleCollision(collidableObjectbottom);
                                collidableObjectbottom.handleCollision(collidedObject);
                            }
                        }
                    }
                }
            }
        }
Exemple #8
0
 public override void handleCollision(CollidableObject collidableObject)
 {
 }