Beispiel #1
0
        private void UpdateMovableObject(IObject movableObject, GameTime gameTime, KeyboardState presentKey, MoveDirection prevDirection)
        {
            Vector2 newLocation = movableObject.getLocation();
            int     speed       = movableObject.getSpeed();


            if (movableObject.getObjectType() != ObjectType.MAIN_CHARACTER)
            {
                for (int i = 0; i < fireBalls.Count; i++)
                {
                    if (fireBalls[i].fbrectangle().Intersects(movableObject.getSpace()))
                    {
                        score += 1;
                        movableObject.setVisibility(false);
                        fireBalls.RemoveAt(i);
                    }
                }
            }


            bool movedVert = false, movedHoriz = false;

            switch (prevDirection)
            {
            case MoveDirection.N:
            case MoveDirection.NW:
            case MoveDirection.NE:
                newLocation.Y -= speed;
                movedVert      = true;
                break;

            case MoveDirection.S:
            case MoveDirection.SE:
            case MoveDirection.SW:
                movedVert      = true;
                newLocation.Y += speed;
                break;

            case MoveDirection.E:
            case MoveDirection.W:
            case MoveDirection.STOP:
                break;

            default:
                throw new NotImplementedException("Unsupported direction: " + prevDirection);
            }


            switch (prevDirection)
            {
            case MoveDirection.E:
            case MoveDirection.NE:
            case MoveDirection.SE:
                movedHoriz     = true;
                newLocation.X += speed;
                break;

            case MoveDirection.W:
            case MoveDirection.NW:
            case MoveDirection.SW:
                movedHoriz     = true;
                newLocation.X -= speed;
                break;

            case MoveDirection.N:
            case MoveDirection.S:
            case MoveDirection.STOP:
                break;

            default:
                throw new NotImplementedException("Unsupported direction: " + prevDirection);
            }



            Rectangle objectSpace     = movableObject.getSpace(newLocation);
            bool      allowedLocation = isAllowedLocation(objectSpace);

            if (allowedLocation)
            {
                movableObject.setNewLocation(newLocation);
                if (movedVert)
                {
                    movableObject.AnimateVerticalMovement(gameTime);
                }
                if (movedHoriz)
                {
                    movableObject.AnimateHorizontalMovement(gameTime);
                }
            }
            else if ((movableObject.getObjectType() == ObjectType.MAIN_CHARACTER) && (getWorld().isInWall(objectSpace) != MoveDirection.NONE))
            {
                // XXX
                MoveDirection wallDir = getWorld().isInWall(objectSpace);
                switch (wallDir)
                {
                case MoveDirection.N:
                    if (prevDirection == MoveDirection.NE)
                    {
                        UpdateMovableObject(movableObject, gameTime, presentKey, MoveDirection.E);
                    }
                    else if (prevDirection == MoveDirection.NW)
                    {
                        UpdateMovableObject(movableObject, gameTime, presentKey, MoveDirection.W);
                    }
                    break;

                case MoveDirection.S:
                    if (prevDirection == MoveDirection.SE)
                    {
                        UpdateMovableObject(movableObject, gameTime, presentKey, MoveDirection.E);
                    }
                    else if (prevDirection == MoveDirection.SW)
                    {
                        UpdateMovableObject(movableObject, gameTime, presentKey, MoveDirection.W);
                    }
                    break;

                case MoveDirection.E:
                    if (prevDirection == MoveDirection.NE)
                    {
                        UpdateMovableObject(movableObject, gameTime, presentKey, MoveDirection.N);
                    }
                    else if (prevDirection == MoveDirection.SE)
                    {
                        UpdateMovableObject(movableObject, gameTime, presentKey, MoveDirection.S);
                    }

                    break;

                case MoveDirection.W:
                    if (prevDirection == MoveDirection.NW)
                    {
                        UpdateMovableObject(movableObject, gameTime, presentKey, MoveDirection.N);
                    }
                    else if (prevDirection == MoveDirection.SW)
                    {
                        UpdateMovableObject(movableObject, gameTime, presentKey, MoveDirection.S);
                    }

                    break;

                default:
                    throw new NotImplementedException("Unsupported direction for wall collision: " + prevDirection);
                }
            }

            if (movableObject.getObjectType() == ObjectType.ALIEN)
            {
                if (allowedLocation)
                {
                    if (randomUpdatesCount == 0 && Utils.isNewDirection())
                    {
                        randomUpdatesCount++;
                        movableObject.setDirection(Utils.getNewRandomDirection(prevDirection));
                    }
                }
                else
                {
                    movableObject.setDirection(Utils.getNewRandomDirection(prevDirection));
                }
            }
            if (movableObject.getObjectType() == ObjectType.MAIN_CHARACTER)
            {
                movableObject.setDirection(Utils.getGermiDirection(presentKey));
            }
        }