Example #1
0
        /// <summary>
        /// loads the room from a file
        /// </summary>
        /// <param name="filename">the filename with the room data</param>
        public Room Load(string filename)
        {
            CurrentFileName = filename;
            //get the json
            string json = File.ReadAllText(filename, Encoding.UTF8);
            //turn it into an internal json object
            JObject obj = JObject.Parse(json);

            //width
            Width = (int)obj.GetValue("width").ToObject(typeof(int));
            //height
            Height = (int)obj.GetValue("height").ToObject(typeof(int));
            //physics
            if (obj.ContainsKey("physics"))
            {
                JObject physicsObj = (JObject)obj.GetValue("physics").ToObject(typeof(JObject));
                float   gravityX   = (float)physicsObj.GetValue("gravityx").ToObject(typeof(float));
                float   gravityY   = (float)physicsObj.GetValue("gravityy").ToObject(typeof(float));
                Physics         = new PhysicsSim();
                Physics.Gravity = new Vector2(gravityX, gravityY);
            }
            //background
            if (obj.ContainsKey("background"))
            {
                JObject backObj        = (JObject)obj.GetValue("background").ToObject(typeof(JObject));
                string  backgroundType = (string)backObj.GetValue("type").ToObject(typeof(string));
                if (backgroundType.Equals("static"))
                {
                    string backAssetName = (string)backObj.GetValue("name").ToObject(typeof(string));
                    Background = new StaticBackground(this, backAssetName);
                }
                else if (backgroundType.Equals("parallax"))
                {
                    JArray   backAssetNames = (JArray)backObj.GetValue("names").ToObject(typeof(JArray));
                    string   targetName     = (string)backObj.GetValue("target").ToObject(typeof(string));
                    string[] names          = new string[backAssetNames.Count];
                    float[]  speeds         = new float[backAssetNames.Count];
                    for (int i = 0; i < backAssetNames.Count; i++)
                    {
                        JObject backLayer = (JObject)backAssetNames[i].ToObject(typeof(JObject));
                        names[i]  = (string)backLayer.GetValue("name").ToObject(typeof(string));
                        speeds[i] = (float)backLayer.GetValue("speed").ToObject(typeof(float));
                    }
                    Background = new ParallaxBackground(this, names, speeds, targetName);
                }
            }
            //world layers
            JArray layerArray = (JArray)obj.GetValue("layers").ToObject(typeof(JArray));

            foreach (JToken item in layerArray)
            {
                JObject layerObject = (JObject)item.ToObject(typeof(JObject));
                int     layer       = (int)layerObject.GetValue("layer").ToObject(typeof(int));
                JArray  objectArray = (JArray)layerObject.GetValue("objects").ToObject(typeof(JArray));
                foreach (JToken gameObjectToken in objectArray)
                {
                    JObject gameObjectData = (JObject)gameObjectToken.ToObject(typeof(JObject));
                    string  internalName   = (string)gameObjectData.GetValue("name").ToObject(typeof(string));
                    Type    type           = PEngine.GetTypeFromName(internalName);
                    if (type == null)
                    {
                        ConsoleManager.WriteLine("could not find object name \"" + internalName + "\"", "err");
                        continue;
                    }
                    Vector2    position   = new Vector2((int)gameObjectData.GetValue("x").ToObject(typeof(int)), (int)gameObjectData.GetValue("y").ToObject(typeof(int)));
                    GameObject gameObject = (GameObject)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position });
                    gameObject.Sprite.LayerData.Layer = layer;
                    GameObjectList.Add(gameObject);
                }
                JArray tileArray = (JArray)layerObject.GetValue("tiles").ToObject(typeof(JArray));
                foreach (JToken tileToken in tileArray)
                {
                    JObject tileData     = (JObject)tileToken.ToObject(typeof(JObject));
                    string  internalName = (string)tileData.GetValue("name").ToObject(typeof(string));
                    Type    type         = PEngine.GetTypeFromName(internalName);
                    if (type == null)
                    {
                        ConsoleManager.WriteLine("could not find tile name \"" + internalName + "\"", "err");
                        continue;
                    }
                    Vector2  position = new Vector2((int)tileData.GetValue("x").ToObject(typeof(int)), (int)tileData.GetValue("y").ToObject(typeof(int)));
                    GameTile tile     = (GameTile)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position });
                    tile.Sprite.LayerData.Layer = layer;
                    GameTileList.Add(tile);
                }
            }
            LoadAssets(Engine.Assets);
            return(this);
        }
Example #2
0
        /// <summary>
        /// loads the room from a file
        /// </summary>
        /// <param name="filename">the filename with the room data</param>
        public Room Load(string filename)
        {
            //get the json
            string json = File.ReadAllText(filename, Encoding.UTF8);
            //turn it into an internal json object
            JObject obj = JObject.Parse(json);

            //width
            Width = (int)obj.GetValue("width").ToObject(typeof(int));
            //height
            Height = (int)obj.GetValue("height").ToObject(typeof(int));
            //physics
            if (obj.ContainsKey("physics"))
            {
                JObject physicsObj = (JObject)obj.GetValue("physics").ToObject(typeof(JObject));
                float   gravityX   = (float)physicsObj.GetValue("gravityx").ToObject(typeof(float));
                float   gravityY   = (float)physicsObj.GetValue("gravityy").ToObject(typeof(float));
                Physics         = new PhysicsSim();
                Physics.Gravity = new Vector2(gravityX, gravityY);
            }
            //world layers
            JArray layerArray = (JArray)obj.GetValue("layers").ToObject(typeof(JArray));

            foreach (JToken item in layerArray)
            {
                JObject layerObject = (JObject)item.ToObject(typeof(JObject));
                int     layer       = (int)layerObject.GetValue("layer").ToObject(typeof(int));
                JArray  objectArray = (JArray)layerObject.GetValue("objects").ToObject(typeof(JArray));
                foreach (JToken gameObjectToken in objectArray)
                {
                    JObject gameObjectData = (JObject)gameObjectToken.ToObject(typeof(JObject));
                    string  internalName   = (string)gameObjectData.GetValue("name").ToObject(typeof(string));
                    Type    type           = PEngine.GetTypeFromName(internalName);
                    if (type == null)
                    {
                        ConsoleManager.WriteLine("could not find object name \"" + internalName + "\"", "err");
                        continue;
                    }
                    Vector2    position   = new Vector2((int)gameObjectData.GetValue("x").ToObject(typeof(int)), (int)gameObjectData.GetValue("y").ToObject(typeof(int)));
                    GameObject gameObject = (GameObject)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position });
                    gameObject.Sprite.LayerData.Layer = layer;
                    GameObjectList.Add(gameObject);
                }
                JArray tileArray = (JArray)layerObject.GetValue("tiles").ToObject(typeof(JArray));
                foreach (JToken tileToken in tileArray)
                {
                    JObject tileData     = (JObject)tileToken.ToObject(typeof(JObject));
                    string  internalName = (string)tileData.GetValue("name").ToObject(typeof(string));
                    Type    type         = PEngine.GetTypeFromName(internalName);
                    if (type == null)
                    {
                        ConsoleManager.WriteLine("could not find tile name \"" + internalName + "\"", "err");
                        continue;
                    }
                    Vector2  position = new Vector2((int)tileData.GetValue("x").ToObject(typeof(int)), (int)tileData.GetValue("y").ToObject(typeof(int)));
                    GameTile tile     = (GameTile)type.GetConstructor(new Type[] { typeof(Room), typeof(Vector2) }).Invoke(new object[] { this, position });
                    tile.Sprite.LayerData.Layer = layer;
                    GameTileList.Add(tile);
                }
            }
            LoadAssets(Engine.Assets);
            return(this);
        }