Lessons Learned

I have dug back into my MemoryLapse code, it has been a long, long time. I noticed I was doing something very embarrassing. I was performing IO calls to the database in one of my Update functions. Sometimes you just code without really thinking about what you are doing. While this is a small game and it was not a huge performance hit, I still noticed a bit of lag. Lessons Learned I guess.

Anyway, this morning I wanted to do away with SQLite in my code and just serialize and deserialize JSON instead. So I have updated my Database class shown below:

namespace MemoryLapse
{
    public class Animal
    {
        public int Theme { getset; }
        public int LevelNumber { getset; }
        public int Unlocked { getset; }
    }
}
using System;
using System.IO;
using System.Runtime.Serialization.Json;
 
namespace MemoryLapse
{
    public class Database
    {
        public String Path { getset; }
        public String Name { getset; }
        public Animal[] Levels { getset; }
 
        public Database(String name)
        {
            Name = name;
            Path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
 
            if (!File.Exists(System.IO.Path.Combine(Path, Name)))
            {
                Levels = new Animal[Enum.GetValues(typeof(Theme)).Length * Enum.GetValues(typeof(Level)).Length];
 
                int i = 0;
                for (int t = 0; t < Enum.GetValues(typeof(Theme)).Length; t++)
                {
                    for (int l = 0; l < Enum.GetValues(typeof(Level)).Length; l++)
                    {
                        Levels[i] = new Animal();
 
                        Levels[i].Theme = t;
                        Levels[i].LevelNumber = l;
                        Levels[i].Unlocked = 0;
 
                        if (Levels[i].Theme == 0 && Levels[i].LevelNumber == 0)
                            Levels[i].Unlocked = 1;
 
                        i++;
                    }
                }
 
                WriteLevels();
            }
            else
            {
                ReadLevels();
            }
        }
 
        public void ReadLevels()
        {
            byte[] json = File.ReadAllBytes(System.IO.Path.Combine(Path, Name));
            using (MemoryStream ms = new MemoryStream(json))
            {
                DataContractJsonSerializer data = new DataContractJsonSerializer(typeof(Animal[]));
                Levels = data.ReadObject(ms) as Animal[];
            }
        }
 
        public void WriteLevels()
        {
            byte[] json = null;
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractJsonSerializer data = new DataContractJsonSerializer(typeof(Animal[]));
                data.WriteObject(ms, Levels);
                json = ms.ToArray();
            }
            File.WriteAllBytes(System.IO.Path.Combine(Path, Name), json);
        }
 
        public void LockTheme(Theme theme)
        {
            for (int i = 0; i < Levels.Length; i++)
            {
                if (Levels[i].Theme == (int)theme)
                {
                    Levels[i].Unlocked = 0;
                    return;
                }
            }
        }
 
        public void UnlockTheme(Theme theme)
        {
            for (int i = 0; i < Levels.Length; i++)
            {
                if (Levels[i].Theme == (int)theme)
                {
                    Levels[i].Unlocked = 1;
                    return;
                }
            }
        }
 
        public void LockLevel(Theme theme, int levelNumber)
        {
            for (int i = 0; i < Levels.Length; i++)
            {
                if (Levels[i].Theme == (int)theme && Levels[i].LevelNumber == levelNumber)
                {
                    Levels[i].Unlocked = 0;
                    return;
                }
            }
        }
 
        public void UnlockLevel(Theme theme, int levelNumber)
        {
            for (int i = 0; i < Levels.Length; i++)
            {
                if (Levels[i].Theme == (int)theme && Levels[i].LevelNumber == levelNumber)
                {
                    Levels[i].Unlocked = 1;
                    return;
                }
            }
        }
 
        public bool IsThemeUnlocked(Theme theme)
        {
            for (int i = 0; i < Levels.Length; i++)
                if (Levels[i].Theme == (int)theme)
                    if (Levels[i].Unlocked == 1)
                        return true;
            return false;
        }
 
        public bool IsLevelUnlocked(Theme theme, int levelNumber)
        {
            for (int i = 0; i < Levels.Length; i++)
                if (Levels[i].Theme == (int)theme && Levels[i].LevelNumber == levelNumber)
                    if (Levels[i].Unlocked == 1)
                        return true;
            return false;
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*