Category Archives: Uncategorized

Yes Atoms is out, check out the details on the official GouldFish Games website


Category: Uncategorized

You have your base class of Baddie and built Goomba’s and Turtles with it and held it all in an array.
The question now is how do you save that array to XML?

Here is the Base and Super Classes I’ve made:

    public abstract class Baddie
    {
        public Vector2 Position;

        public Baddie()
        {
            Position = new Vector2();
        }


        public abstract void AI(float gameTime); 
    }


    public class Goomba : Baddie
    {
        public Goomba()
        {

        }

        public Goomba(Vector2 position)
        {
            Position = position;
        }

        public override void AI(float gameTime)
        {
            Position.X -= (gameTime * 10);
        }
    }

And here is the array and it being filled:

            Baddies = new List<Baddie>();

            Baddies.Add(new Goomba(new Vector2(100,30)));
            Baddies.Add(new Goomba(new Vector2(300, 40)));
            Baddies.Add(new Goomba(new Vector2(320, 40)));
            Baddies.Add(new Goomba(new Vector2(340, 30)));
            Baddies.Add(new Turtle(new Vector2(500, 50)));
            Baddies.Add(new Turtle(new Vector2(700, 10)));
            Baddies.Add(new Turtle(new Vector2(5000, 20)));

So to save this we need an array of Types that implement the base class Baddie, this can be done by hand or it can be done automatically via the following function:

        public static Type[] GetAllTypesForCollection<T>()
        {
            List<Type> list = new List<Type>();
            Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies();

            for (int i = 0; i < assemblies.Length; i++)
            {
                if (!assemblies[i].GlobalAssemblyCache)
                {
                    Type[] types = assemblies[i].GetTypes();

                    for (int j = 0; j < types.Length; j++)
                    {
                        if (typeof(T).IsAssignableFrom(types[j]))
                        {
                            list.Add(types[j]);
                        }
                    }
                }
            }

            return list.ToArray();
        }

So the save code looks like this:

            using (FileStream fs = new FileStream("test.xml", FileMode.Create))
            {
                XmlSerializer s = new XmlSerializer(typeof(List<Baddie>), GetAllTypesForCollection<Baddie>());

                s.Serialize(fs, Baddies);
            }

and the end XML like this:

<?xml version="1.0"?>
<ArrayOfBaddie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Baddie xsi:type="Goomba">
    <Position>
      <X>100</X>
      <Y>30</Y>
    </Position>
  </Baddie>
  <Baddie xsi:type="Goomba">
    <Position>
      <X>300</X>
      <Y>40</Y>
    </Position>
  </Baddie>
  <Baddie xsi:type="Goomba">
    <Position>
      <X>320</X>
      <Y>40</Y>
    </Position>
  </Baddie>
  <Baddie xsi:type="Goomba">
    <Position>
      <X>340</X>
      <Y>30</Y>
    </Position>
  </Baddie>
  <Baddie xsi:type="Turtle">
    <Position>
      <X>500</X>
      <Y>50</Y>
    </Position>
  </Baddie>
  <Baddie xsi:type="Turtle">
    <Position>
      <X>700</X>
      <Y>10</Y>
    </Position>
  </Baddie>
  <Baddie xsi:type="Turtle">
    <Position>
      <X>5000</X>
      <Y>20</Y>
    </Position>
  </Baddie>
</ArrayOfBaddie>

Category: Uncategorized