public List<AABB> loadAxisAlignedBoxes(TextureItem item, string property)
        {
            List<AABB> axisAlignedBoxes = new List<AABB>();
            int numberToLoad = 1;
            int numberOfLoaded = 0;
            bool loadNext = true;

            while (loadNext)
            {
                string tempProperty = property + numberToLoad.ToString();

                if (item.CustomProperties.ContainsKey(tempProperty))
                {
                    if (item.CustomProperties[tempProperty].type == typeof(Item))
                    {
                        Console.WriteLine(tempProperty + " Loaded");
                        RectangleItem rectangleItem = (RectangleItem)item.CustomProperties[tempProperty].value;
                        AABB axisAlignedBox = new AABB(rectangleItem.Position, rectangleItem.Width, rectangleItem.Height);
                        axisAlignedBoxes.Add(axisAlignedBox);
                        numberToLoad++;
                        numberOfLoaded++;
                    }
                }
                else
                {
                    loadNext = false;
                }
            }

            if (numberOfLoaded == 0)
            {
                axisAlignedBoxes = null;
                //Console.WriteLine("Did not load AxisAlignedBoxes");
            }
            return axisAlignedBoxes;
        }
Esempio n. 2
0
        public void loadLevel(Level level)
        {
            player1 = new Character();
            backgroundObjects = new List<BackgroundObject>();
            middleObjects = new List<MiddleObject>();
            resourceObjects = new List<MiddleObject>();
            collision = false;
            List<TextureItem> textureItems;
            AABB axisAlignedBox = null;
            List<AABB> axisAlignedBoxes = null;
            List<Polygon> polygons = null;
            float depthPosition = 0.0f;
            string name = null;
            bool isAnimated = false;

            RectangleItem levelCollisionBox = (RectangleItem)level.getItemByName("LevelAABB");
            axisAlignedBox = new AABB(levelCollisionBox.Position, levelCollisionBox.Width, levelCollisionBox.Height);
            this.axisAlignedBox = axisAlignedBox;

            /*
               * Load background objects
            * */
            textureItems = level.getTextureItems("ObjectType", "BackgroundObject");
            foreach (TextureItem item in textureItems)
            {
                polygons = null;
                axisAlignedBoxes = null;

                axisAlignedBoxes = ResourceManager.getInstance.loadAxisAlignedBoxes(item, "CollisionBox"); //AABB
                polygons = ResourceManager.getInstance.loadPolygons(item, "Polygon"); //Polygon
                name = ResourceManager.getInstance.loadName(item, "Name");

                if (axisAlignedBoxes != null)
                {
                    backgroundObjects.Add(new BackgroundObject(item.texture, item.Position, item.Rotation, item.Origin, item.Scale, null, axisAlignedBoxes, name));
                }
                else if (polygons != null)
                {
                    backgroundObjects.Add(new BackgroundObject(item.texture, item.Position, item.Rotation, item.Origin, item.Scale, polygons, null, name));
                }
                else
                {
                    backgroundObjects.Add(new BackgroundObject(item.texture, item.Position, item.Rotation, item.Origin, item.Scale, null, null, name));
                }
            }

            /*
             * Load resource objects
             * */
            textureItems = level.getTextureItems("ObjectType", "ResourceObject");
            foreach (TextureItem item in textureItems)
            {
                depthPosition = ResourceManager.getInstance.loadDepthPosition(item, "DepthPosition");
                name = ResourceManager.getInstance.loadName(item, "Name");

                resourceObjects.Add(new MiddleObject(item.texture, item.Position, item.Rotation, item.Origin, item.Scale, null, null, depthPosition, name));
            }

            /*
            * Load middle objects
             * */
            textureItems = level.getTextureItems("ObjectType", "MiddleObject");
            foreach (TextureItem item in textureItems)
            {
                polygons = null;
                axisAlignedBoxes = null;

                axisAlignedBoxes = ResourceManager.getInstance.loadAxisAlignedBoxes(item, "CollisionBox"); //AABB
                polygons = ResourceManager.getInstance.loadPolygons(item, "Polygon"); //Polygon
                depthPosition = ResourceManager.getInstance.loadDepthPosition(item, "DepthPosition");
                name = ResourceManager.getInstance.loadName(item, "Name");
                isAnimated = ResourceManager.getInstance.loadIsAnimated(item, "IsAnimated");

                if (!isAnimated)
                {
                    if (axisAlignedBoxes != null)
                    {
                        middleObjects.Add(new MiddleObject(item.texture, item.Position, item.Rotation, item.Origin, item.Scale, null, axisAlignedBoxes, depthPosition, name));
                    }
                    else if (polygons != null)
                    {
                        middleObjects.Add(new MiddleObject(item.texture, item.Position, item.Rotation, item.Origin, item.Scale, polygons, null, depthPosition, name));
                    }
                    else
                    {
                        middleObjects.Add(new MiddleObject(item.texture, item.Position, item.Rotation, item.Origin, item.Scale, null, null, depthPosition, name));
                    }
                }
                else
                {
                    Texture2D[] animationSprites = ResourceManager.getInstance.loadAnimationSprite(item, "AnimationPath");
                    int numberOfAnimations = ResourceManager.getInstance.loadAnimationProperty(item, "NumberOfAnimations");
                    int[] animationSpeeds = ResourceManager.getInstance.loadAnimationProperty2(item, "AnimationSpeed");
                    int[] numberOfFrames = ResourceManager.getInstance.loadAnimationProperty2(item, "NumberOfFrames");
                    int[] numberOfRows = ResourceManager.getInstance.loadAnimationProperty2(item, "NumberOfRows");

                    if (name == "Player")
                    {
                        if (axisAlignedBoxes != null && polygons != null)
                        {
                            player1 = new Character(animationSprites, item.Position, new Vector2(4, 4), item.Rotation, item.Origin,
                                item.Scale, polygons, axisAlignedBoxes, numberOfAnimations, animationSpeeds, numberOfFrames, numberOfRows, depthPosition, name);
                        }
                        else if (axisAlignedBoxes != null)
                        {
                            player1 = new Character(animationSprites, item.Position, new Vector2(4, 4), item.Rotation, item.Origin,
                                item.Scale, null, axisAlignedBoxes, numberOfAnimations, animationSpeeds, numberOfFrames, numberOfRows, depthPosition, name);
                        }
                        else if (polygons != null)
                        {
                            player1 = new Character(animationSprites, item.Position, new Vector2(4, 4), item.Rotation, item.Origin,
                                item.Scale, polygons, null, numberOfAnimations, animationSpeeds, numberOfFrames, numberOfRows, depthPosition, name);
                        }

                        player1Shadow = new Shadow(ResourceManager.getInstance.getRuntimeTextures()["Shadow"], new Vector2(item.Position.X - 11, item.Position.Y - 8), item.Rotation, item.Origin, new Vector2(0.2f, 0.2f), "Shadow");

                        /*
                        * Initializes camera and set its view
                        * IMPORTANT: The item used here have to point to the player
                         * */
                        camera = new Camera(Matrix.Identity, new Vector2(Renderer.getInstance.getScreenCenter().X - item.Position.X,
                            Renderer.getInstance.getScreenCenter().Y - item.Position.Y));

                        camera.setView(Matrix.CreateTranslation(new Vector3(camera.getPosition() - Renderer.getInstance.getScreenCenter(), 0f)) *
                            Matrix.CreateTranslation(new Vector3(Renderer.getInstance.getScreenCenter(), 0f)));

                        Console.WriteLine("Camera: " + camera.getPosition());
                        Console.WriteLine("Player: " + item.Position);
                    }
                    else
                    {
                        if (axisAlignedBoxes != null)
                        {
                            middleObjects.Add(new MiddleObject(animationSprites, item.Position, item.Rotation, item.Origin, item.Scale, null,
                                axisAlignedBoxes, numberOfAnimations, animationSpeeds, numberOfFrames, numberOfRows, depthPosition, name));
                        }
                        else if (polygons != null)
                        {
                            middleObjects.Add(new MiddleObject(animationSprites, item.Position, item.Rotation, item.Origin, item.Scale, polygons,
                                null, numberOfAnimations, animationSpeeds, numberOfFrames, numberOfRows, depthPosition, name));
                        }
                        else
                        {
                            middleObjects.Add(new MiddleObject(animationSprites, item.Position, item.Rotation, item.Origin, item.Scale, null,
                                null, numberOfAnimations, animationSpeeds, numberOfFrames, numberOfRows, depthPosition, name));
                        }
                    }
                }
            }

            foreach (MiddleObject resourceObject in resourceObjects)
            {
                Random random = new Random();

                int placeHere = random.Next(2);
                Console.WriteLine("RANDOM1: " + placeHere);

                if (placeHere == 1)
                {
                    Texture2D texture2D = null;
                    Vector2 position = resourceObject.Properties.getProperty<Vector2>("Position");
                    float rotation = resourceObject.Properties.getProperty<float>("Rotation");
                    Vector2 origin = resourceObject.Properties.getProperty<Vector2>("Origin");
                    Vector2 scale = resourceObject.Properties.getProperty<Vector2>("Scale");
                    float depthPos = resourceObject.Properties.getProperty<float>("DepthPosition");

                    int typeOfResource = random.Next(3);
                    Console.WriteLine("RANDOM2: " + typeOfResource);

                    if (typeOfResource == 0)
                    {
                        foreach (KeyValuePair<string, Texture2D> texture in ResourceManager.getInstance.getRuntimeTextures())
                        {
                            if (texture.Key == "Screw")
                            {
                                texture2D = texture.Value;
                                break;
                            }
                        }
                    }
                    else if (typeOfResource == 1)
                    {
                        foreach (KeyValuePair<string, Texture2D> texture in ResourceManager.getInstance.getRuntimeTextures())
                        {
                            if (texture.Key == "OilCan")
                            {
                                texture2D = texture.Value;
                                break;
                            }
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair<string, Texture2D> texture in ResourceManager.getInstance.getRuntimeTextures())
                        {
                            if (texture.Key == "Cement")
                            {
                                texture2D = texture.Value;
                                break;
                            }
                        }
                    }

                    middleObjects.Add(new MiddleObject(texture2D, position, rotation, origin, scale, null, null, depthPos, resourceObject.Name));
                }
            }

            AudioManager.getInstance.stopSound(new Sound("Menu Music"));
            AudioManager.getInstance.playSound(new Sound("Level 1 ingame"), AudioManager.LEVEL1_INGAME);
        }