Exemplo n.º 1
0
        private void checkMouseCollisions()
        {
            Vector2 mousePos    = mouseCursor.Properties.getProperty <Vector2>("Position");
            Vector2 mouseOrigin = mouseCursor.Properties.getProperty <Vector2>("Origin");

            if (gameState == GameState.IN_STARTMENU || gameState == GameState.IN_INGAMEMENU)
            {
                foreach (MenuButton menuButton in menuManager.getGameMenus().ElementAt(menuManager.getCurrentIndex()).getMenuButtons())
                {
                    Vector2 menuPos    = menuButton.Properties.getProperty <Vector2>("Position");
                    Vector2 menuOrigin = menuButton.Properties.getProperty <Vector2>("Origin");

                    if (CollisionManager.intersects(mousePos - mouseOrigin, menuPos - menuOrigin, menuOrigin))
                    {
                        mouseCursor.onMouseHover(menuButton);
                    }
                    else
                    {
                        mouseCursor.onMouseNotHover(menuButton);
                    }
                }
            }
            else
            {
                //Later we could add one more foreach here that loops through the differnet huds and checks if they are open,
                //and if so loop through the huds buttons
                if (getPlayer1().getOpenQuests())
                {
                    foreach (MenuButton button in hudManager.getQuestHud().getButtons())
                    {
                        Vector2 menuPos    = button.Properties.getProperty <Vector2>("Position");
                        Vector2 menuOrigin = button.Properties.getProperty <Vector2>("Origin");

                        if (CollisionManager.intersects(mousePos - mouseOrigin, menuPos - menuOrigin, menuOrigin))
                        {
                            mouseCursor.onMouseHover(button);
                        }
                        else
                        {
                            mouseCursor.onMouseNotHover(button);
                        }
                    }
                }

                foreach (MiddleObject middleObject in middleObjects)
                {
                    Vector2 objectPos    = middleObject.Properties.getProperty <Vector2>("Position");
                    Vector2 objectOrigin = middleObject.Properties.getProperty <Vector2>("Origin");

                    if (CollisionManager.intersects(mousePos - mouseOrigin, objectPos - objectOrigin, objectOrigin))
                    {
                        mouseCursor.onMouseHover(middleObject);
                    }
                    else
                    {
                        mouseCursor.onMouseNotHover(middleObject);
                    }
                }

                foreach (BackgroundObject backgroundObject in backgroundObjects)
                {
                    Vector2 objectPos    = backgroundObject.Properties.getProperty <Vector2>("Position");
                    Vector2 objectOrigin = backgroundObject.Properties.getProperty <Vector2>("Origin");

                    if (CollisionManager.intersects(mousePos - mouseOrigin, objectPos - objectOrigin, objectOrigin))
                    {
                        mouseCursor.onMouseHover(backgroundObject);
                    }
                    else
                    {
                        mouseCursor.onMouseNotHover(backgroundObject);
                    }
                }

                if (!mouseCursor.getHoveringOverThisFrame())
                {
                    mouseCursor.onMouseNotHoverThisFrame();
                }
                mouseCursor.setHoveringOverThisFrame(false);


                //collision++;
            }
        }
Exemplo n.º 2
0
        private void checkCollisions(GameTime gameTime)
        {
            List <Polygon> player1Polygons = player1.Properties.getProperty <List <Polygon> >("Polygon");
            List <AABB>    player1AABB     = player1.Properties.getProperty <List <AABB> >("AABB");
            Vector2        player1Velocity = player1.Properties.getProperty <Vector2>("Velocity");

            //Check collision for all middle objects
            foreach (MiddleObject middleObject in middleObjects)
            {
                CollisionType collisionType = middleObject.Properties.getProperty <CollisionType>("CollisionType");

                if (collisionType == CollisionType.POLYGON)
                {
                    List <Polygon> middleObjectPolygons = middleObject.Properties.getProperty <List <Polygon> >("Polygon");

                    foreach (Polygon player1Polygon in player1Polygons)
                    {
                        foreach (Polygon middleObjectPolygon in middleObjectPolygons)
                        {
                            PolygonCollisionResult collisionResult = CollisionManager.intersects(player1Polygon, middleObjectPolygon, player1Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);
                            if (collisionResult.WillIntersect)
                            {
                                player1.onCollision(gameTime, collisionResult, player1Velocity);
                                camera.onCollision(gameTime, collisionResult, player1Velocity);
                                middleObject.onCollision();
                                menuManager.onCollision(gameTime, collisionResult, player1Velocity);
                                hudManager.onCollision(gameTime, collisionResult, player1Velocity);
                                fontManager.onCollision(gameTime, collisionResult, player1Velocity);

                                collision = true;
                                break;
                            }
                            else
                            {
                                player1.onNotCollision();
                                middleObject.onNotCollision();
                            }
                        }
                        if (collision)
                        {
                            break;
                        }
                    }
                }
                if (collision)
                {
                    collision = false;
                    break;
                }

                /*
                 * else if (collisionType == CollisionType.AABB)
                 * {
                 *  //Console.WriteLine("Type is AABB");
                 *  List<AABB> staticObjectAABB = staticObject.Properties.getProperty<List<AABB>>("AABB");
                 *
                 *  foreach (AABB player1Box in player1AABB)
                 *  {
                 *      foreach (AABB staticBox in staticObjectAABB)
                 *      {
                 *          if (CollisionManager.intersects(player1Box, staticBox))
                 *          {
                 *              Console.WriteLine("AABB Collision: " + collision);
                 *              player1.onCollision(collisionResult, gameTime);
                 *              camera.setPosition(camera.getOldPosition());
                 *              doAction(player1, staticObject);
                 *
                 *              jumpOver = true;
                 *              break;
                 *          }
                 *      }
                 *      if (jumpOver)
                 *      {
                 *          jumpOver = false;
                 *          break;
                 *      }
                 *  }
                 * }
                 * */
            }

            //Check collision for all background objects
            foreach (BackgroundObject backgroundObject in backgroundObjects)
            {
                CollisionType collisionType = backgroundObject.Properties.getProperty <CollisionType>("CollisionType");

                if (collisionType == CollisionType.POLYGON)
                {
                    List <Polygon> backgroundObjectPolygons = backgroundObject.Properties.getProperty <List <Polygon> >("Polygon");

                    foreach (Polygon player1Polygon in player1Polygons)
                    {
                        foreach (Polygon backgroundObjectPolygon in backgroundObjectPolygons)
                        {
                            PolygonCollisionResult collisionResult = CollisionManager.intersects(player1Polygon, backgroundObjectPolygon, player1Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);
                            if (collisionResult.WillIntersect)
                            {
                                backgroundObject.onCollision();

                                collision = true;
                                break;
                            }
                            else
                            {
                                backgroundObject.onNotCollision();
                            }
                        }
                        if (collision)
                        {
                            break;
                        }
                    }
                }
                if (collision)
                {
                    collision = false;
                    break;
                }
            }

            /*
             * foreach (Polygon player1Polygon in player1Polygons)
             * {
             *  if (CollisionManager.checkWallCollision(player1Polygon))
             *  {
             *      player1.onCollision();
             *      camera.setPosition(camera.getOldPosition());
             *      break;
             *  }
             * }
             * */
            /*
             * foreach (AABB player1Box in player1AABB)
             * {
             *  if (CollisionManager.checkWallCollision(player1Box))
             *  {
             *      player1.onCollision();
             *      camera.setPosition(camera.getOldPosition());
             *      break;
             *  }
             * }
             * */
        }