Пример #1
0
        private void loadStructure(Vector2 position, string structureID)
        {
            elements = new List <Tuple <Rectangle, Vector2, string> >();
            bounds   = new List <RectangleF>();
            string    path = AppDomain.CurrentDomain.BaseDirectory + @"Content\structures\" + structureID + ".xml";
            XDocument doc  = XDocument.Load(path);                                            //open xml file

            foreach (XElement element in doc.Descendants("Structure").Descendants("Element")) //Go through each element of structure
            {
                //load rectangle and position from array
                Rectangle rect = RectangleEx.FromArray(element.Element("Rectangle").Value.Split(','));
                Vector2   pos  = VectorEx.FromArray(element.Element("Position").Value.Split(','));

                string sheetID = element.Element("spritesheet").Value;
                if (spritesheets.Count(s => s.SpritesheetID == sheetID) == 0)                //if preloaded sheets don't have necessary spritesheet
                {
                    spritesheets.Add(new Spritesheet(sheetID));
                }
                elements.Add(new Tuple <Rectangle, Vector2, string>(rect, pos, sheetID));
            }
            foreach (XElement element in doc.Descendants("Structure").Descendants("Bounds").Elements("Box"))
            {
                RectangleF boundary = RectangleF.FromArray(element.Value.Split(','));
                boundary.Location += position;
                bounds.Add(boundary);
            }
        }
Пример #2
0
        /// <summary>
        /// Loads a structure from an XML element
        /// </summary>
        /// <param name="structure">XML element to load structure from</param>
        /// <param name="sheets">Spritesheets to preload for lower memory use</param>
        /// <returns>New Structure from XML data</returns>
        public static Structure LoadFromXML(XElement structure, List <Spritesheet> sheets = null)
        {
            string  structureID = structure.Element("StructureID").Value;
            Vector2 pos         = VectorEx.FromArray(structure.Element("Position").Value.Split(','));

            return(new Structure(pos, structureID, sheets));
        }
Пример #3
0
        /// <summary>
        /// Creates new button at a certain position with certain id, text, spritesheets to use for caching, and font
        /// </summary>
        /// <param name="position">Position of button</param>
        /// <param name="buttonText">Text on button</param>
        /// <param name="buttonID">ID of button</param>
        /// <param name="sheets">Location of button</param>
        /// <param name="font">Font to use to write on button</param>
        public Button(Vector2 position, string buttonText, string buttonID, List <Spritesheet> sheets = null, SpriteFont font = null)
        {
            XDocument doc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory +
                                           @"Content\menus\buttons\" + buttonID + ".xml");

            this.buttonID = buttonID;
            ButtonText    = buttonText;
            textFont      = font;

            if (sheets == null)
            {
                spriteSheets = new List <Spritesheet>();
            }
            else
            {
                spriteSheets = new List <Spritesheet>(sheets);
            }

            Vector2 size = VectorEx.FromArray(doc.Descendants("Button").Elements("Size").First().Value.Split(','));

            buttonBounds = new Rectangle(position.ToPoint(), size.ToPoint());

            //get base image
            normalID = doc.Descendants("Button").Elements("Image").First().Value;
            if (spriteSheets.Count(sheet => sheet.SpritesheetID == normalID) == 0)
            {
                spriteSheets.Add(new Spritesheet(normalID));
            }

            //get hover image
            hoverID = doc.Descendants("Button").Elements("Hover").First().Value;
            if (spriteSheets.Count(sheet => sheet.SpritesheetID == hoverID) == 0)
            {
                spriteSheets.Add(new Spritesheet(hoverID));
            }

            //if there is no font loaded, use the default
            if (textFont == null)
            {
                textFont = RPG.ContentManager.Load <SpriteFont>(@"fonts\InterfaceFont");
            }

            lastState = Mouse.GetState();
        }
Пример #4
0
        protected void loadContainer(string menuID)
        {
            slots = new List <Button> ();
            XDocument doc          = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"Content\menus\" + menuID + ".xml");
            var       containerXML = doc.Element("Menu").Element("Container");

            this.position = VectorEx.FromArray(containerXML.Element("Position").Value.Split(','));
            this.format   = (ContainerFormat)Enum.Parse(typeof(ContainerFormat), containerXML.Elements("Format").First().Value);
            string  slotID     = containerXML.Element("Slot").Value;
            string  style      = containerXML.Element("Style").Value;
            bool    rounded    = bool.Parse(containerXML.Element("Rounded").Value);
            Vector2 borderSize = VectorEx.FromArray(containerXML.Element("BorderSize").Value.Split(','));

            this.columns = int.Parse(containerXML.Element("Columns").Value);
            this.loadMenu(menuID);
            Vector2 baseSlot = new Button(Vector2.Zero, "", slotID).Size;

            if (format == ContainerFormat.Rectangle)
            {
                for (int i = 0; i < container.MaxCapacity; i++)
                {
                    int     x           = i % columns;
                    int     y           = i / columns;
                    Vector2 pos         = position + borderSize + new Vector2(x * baseSlot.X, y * baseSlot.Y);
                    Button  slot        = new Button(pos, "", slotID);
                    int     currentSlot = i;
                    slot.On_Click += (object sender, MouseState CurrentState) => SlotClick(currentSlot, CurrentState);
                    slots.Add(slot);
                }
                background = new Tile(
                    new Rectangle(position.ToPoint(),
                                  (borderSize * 2 + new Vector2(baseSlot.X * columns, baseSlot.Y * (float)Math.Ceiling((float)container.MaxCapacity / columns))).ToPoint()),
                    style, rounded);
            }
            else if (format == ContainerFormat.Armor)
            {
            }
        }
Пример #5
0
        private void loadCharacter(Vector2 position, string characterID)
        {
            frames = new Dictionary <Enum, int[]>();
            XDocument charDoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"Content/characters/" + characterID + ".xml");

            //set speed values from xml file
            baseSpeed = double.Parse(charDoc.Descendants("Character").Elements("Speed").First().Value);
            speed     = baseSpeed;

            //set draw rectangle from xml file
            size          = VectorEx.FromArray(charDoc.Descendants("Character").Elements("Size").First().Value.Split(','));
            this.position = position;

            //set current direction from xml file
            currentDirection = (SpriteDirection)Enum.Parse(typeof(SpriteDirection), charDoc.Descendants("Character").Elements("Direction").First().Value);

            //set frame list from xml file
            foreach (XElement element in charDoc.Descendants("Character").Elements("Frame"))
            {
                Enum frameEnum;
                if (element.Element("Enum").Attribute("type").Value == "SpriteState")
                {
                    frameEnum = (SpriteState)Enum.Parse(typeof(SpriteState), element.Element("Enum").Value);
                }
                else
                {
                    frameEnum = (SpriteDirection)Enum.Parse(typeof(SpriteDirection), element.Element("Enum").Value);
                }
                List <int> frameIndexes = new List <int>();
                foreach (string index in element.Element("Value").Value.Split(','))
                {
                    frameIndexes.Add(int.Parse(index));
                }
                frames.Add(frameEnum, frameIndexes.ToArray());
            }
        }
Пример #6
0
        protected void loadMenu(string menuID)
        {
            sheets   = new List <Spritesheet>();
            elements = new List <IDrawable>();
            XDocument doc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"Content\menus\" + menuID + ".xml");

            doc.Descendants("Menu").Elements("Spritesheet").ToList().ForEach(element => sheets.Add(new Spritesheet(element.Value)));
            foreach (XElement element in doc.Descendants("Button"))
            {
                string  buttonID = element.Elements("ButtonID").First().Value;
                Vector2 position = VectorEx.FromArray(element.Elements("Position").First().Value.Split(','));
                string  text     = element.Elements("Text").First().Value;
                Button  temp     = new Button(position, text, buttonID, sheets);

                foreach (string methodString in element.Elements("Method"))
                {
                    temp.On_Click += (Button.ButtonClick)Delegate.CreateDelegate(typeof(Button.ButtonClick), this, methodString);
                }
                elements.Add(temp);
            }
            foreach (XElement element in doc.Descendants("Tiles"))
            {
            }
        }
Пример #7
0
        /// <summary>
        /// Creates list of all strucutures in area by reading from specified area id
        /// </summary>
        /// <param name="areaID">ID of area to load</param>
        /// <returns>List of drawable elements in array</returns>
        private void loadArea(string areaID)
        {
            XDocument doc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"Content\areas\" + areaID + ".xml");

            //create new lists for area
            drawElements = new List <IDrawable>();
            entities     = new List <Entity>();
            sheets       = new List <Spritesheet>();
            //create new eventboxes, replacing old ones if necessary
            if (boxes != null)
            {
                boxes.ForEach(box => box.UnloadBox());
            }
            boxes = new List <EventBox>();

            //loads main menu
            menu = new Menu(doc.Descendants("Area").Elements("Menu").First().Value);
            foreach (XElement element in doc.Descendants("Area").Descendants("Sheets").Descendants("SheetID"))
            {
                sheets.Add(new Spritesheet(element.Value));
            }

            if (doc.Descendants("Area").Elements("BGM").Count() > 0 &&
                doc.Descendants("Area").Elements("BGM").First().Element("MusicID").Value != "None")    //load bgm if is contained in xml
            {
                background = Content.Load <Song>("sound/music/" + doc.Descendants("Area").Elements("BGM").First().Element("MusicID").Value);
                if (MediaPlayer.State != MediaState.Playing || MediaPlayer.Queue.ActiveSong.Name != background.Name)
                {
                    MediaPlayer.Play(background);
                    MediaPlayer.Volume      = .3f;
                    MediaPlayer.IsRepeating = bool.Parse(doc.Descendants("Area").Elements("BGM").First().Element("Loop").Value);
                }
            }

            foreach (XElement element in doc.Descendants("Area").Descendants("Tile"))           //load tiles
            {
                drawElements.Add(Tile.LoadFromXML(element, sheets));
            }

            foreach (XElement element in doc.Descendants("Area").Descendants("Structure"))           //load structures
            {
                drawElements.Add(Structure.LoadFromXML(element, sheets));
            }

            foreach (XElement element in doc.Descendants("Area").Elements("Entity"))
            {
                EntityType      type      = (EntityType)Enum.Parse(typeof(EntityType), element.Element("Type").Value);
                SpriteDirection direction = (SpriteDirection)Enum.Parse(typeof(SpriteDirection), element.Element("Direction").Value);

                string entityID = element.Element("EntityID").Value;
                string name     = element.Element("Name").Value;

                RectangleF bounds = RectangleF.FromArray(element.Element("Bounds").Value.Split(','));

                string[] pos      = element.Element("Position").Value.Split(',');
                Vector2  position = new Vector2(float.Parse(pos[0]), float.Parse(pos[1]));

                EntitySpawnEventArgs args = new EntitySpawnEventArgs(entityID, name, type, 1, direction, position, bounds);
                Spawn(User, args);
            }

            foreach (XElement element in doc.Descendants("Area").Descendants("Player").Elements("EventBox"))           //load player event boxes
            {
                RectangleF         rect      = RectangleF.FromArray(element.Element("Box").Value.Split(','));
                EventBox.Condition condition = null;
                if (element.Elements("Condition").Count() > 0)
                {
                    string methodString = element.Element("Condition").Value;
                    condition = (EventBox.Condition)Delegate.CreateDelegate(typeof(EventBox.Condition), this, methodString);
                }
                if (element.Element("Method").Value == "OnNewAreaEnter")
                {
                    Vector2          position = VectorEx.FromArray(element.Element("Position").Value.Split(','));
                    NewAreaEventArgs args     = new NewAreaEventArgs(element.Element("AreaID").Value, position);
                    boxes.Add(new EventBox(User, rect, OnNewAreaEnter, args, condition));
                }
                else if (element.Element("Method").Value == "Spawn")
                {
                    int             maxNum    = int.Parse(element.Element("MaxNum").Value);
                    Vector2         position  = VectorEx.FromArray(element.Element("Position").Value.Split(','));
                    EntityType      type      = (EntityType)Enum.Parse(typeof(EntityType), element.Element("Type").Value);
                    SpriteDirection direction = (SpriteDirection)Enum.Parse(typeof(SpriteDirection), element.Element("Direction").Value);

                    EntitySpawnEventArgs args =
                        new EntitySpawnEventArgs(element.Element("EntityID").Value,
                                                 element.Element("Name").Value, type, maxNum, direction,
                                                 position, RectangleF.FromArray(element.Element("Bounds").Value.Split(',')));
                    boxes.Add(new EventBox(User, rect, Spawn, args, null));
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Load entity at position from loadpath
        /// </summary>
        /// <param name="position">Position to create entity at</param>
        /// <param name="loadPath">Path to load entity from</param>
        protected void LoadEntity(Vector2 position, string loadPath)
        {
            XDocument entityDoc   = XDocument.Load(loadPath);
            var       entityXML   = entityDoc.Element("Entity");
            string    characterID = entityXML.Elements("Base").First().Value;

            maxHealth     = int.Parse(entityXML.Elements("MaxHealth").First().Value);
            currentHealth = int.Parse(entityXML.Elements("CurrentHealth").First().Value);
            strength      = int.Parse(entityXML.Elements("Strength").First().Value);
            regenRate     = double.Parse(entityXML.Elements("Regen").First().Value) / 60;           //divide by 60 to make it per second
            //load inventory

            //if (entityXML.Elements ("InventorySize").Count () != 0)
            //	inventory = new StorageContainer (int.Parse (entityXML.Element ("InventorySize").Value));
            //else
            //	inventory = new StorageContainer (0);


            List <Spritesheet> defaultOverlay = new List <Spritesheet> ();           //TODO: Implement gear system

            //load offset and size from xml
            offset = VectorEx.FromArray(entityXML.Elements("Offset").First().Value.Split(','));
            size   = VectorEx.FromArray(entityXML.Elements("Size").First().Value.Split(','));

            //add spritesheets and load spritesheet lists
            foreach (XElement element in entityXML.Elements("Spritesheet"))
            {
                defaultOverlay.Add(new Spritesheet(element.Value));
            }
            foreach (XElement element in entityXML.Elements("SpriteList"))
            {
                defaultOverlay.AddRange(Spritesheet.LoadList(element.Value));
            }

            //create sprite
            sprite = new CharacterSprite(position - offset, characterID, defaultOverlay);

            //load healthbar if it has one
            if (entityXML.Elements("HealthBar").Count() > 0)
            {
                XElement healthBar = entityXML.Elements("HealthBar").First();

                //set colors front document
                string[] frontString = healthBar.Element("FrontColor").Value.Split(',');
                string[] backString  = healthBar.Element("BackColor").Value.Split(',');
                Color    front       = new Color(int.Parse(frontString [0]), int.Parse(frontString [1]), int.Parse(frontString [2]));
                Color    back        = new Color(int.Parse(backString [0]), int.Parse(backString [1]), int.Parse(backString [2]));

                //get size and set position
                int     buffer      = int.Parse(healthBar.Element("Buffer").Value);
                Vector2 barSize     = VectorEx.FromArray(healthBar.Element("Size").Value.Split(','));
                Vector2 barPosition = new Vector2(Bounds.Center.X - barSize.X / 2, Position.Y - barSize.Y - buffer);
                this.healthBar = new ProgressBar(barPosition, barSize, maxHealth, (int)currentHealth, front, back);
                healthOffset   = new Vector2((this.healthBar.Position - Bounds.Center).X, (this.healthBar.Position - Position).Y);

                On_Move          += this.healthBar.Move;
                On_HealthChanged += this.healthBar.ChangeProgress;
            }

            //TODO: replace frames if necessary [MOVE TO CHARACTER SPRITE]
            foreach (XElement element in entityXML.Elements("Frame"))
            {
                Enum frameEnum;
                if (element.Element("Enum").Attribute("type").Value == "SpriteState")
                {
                    frameEnum = (SpriteState)Enum.Parse(typeof(SpriteState), element.Element("Enum").Value);
                }
                else
                {
                    frameEnum = (SpriteDirection)Enum.Parse(typeof(SpriteDirection), element.Element("Enum").Value);
                }
                List <int> frameIndexes = new List <int> ();
                foreach (string index in element.Element("Value").Value.Split(','))
                {
                    frameIndexes.Add(int.Parse(index));
                }
                sprite.UpdateFrame(frameEnum, frameIndexes.ToArray());
            }
        }