/// <summary> /// requests a font /// </summary> /// <param name="assetName">the name of the asset</param> /// <param name="callback">what to call back when we get the font</param> public void RequestFont(string assetName, Action <SpriteFont> callback) { ConsoleManager.WriteLine("requesting font \"" + assetName + "\""); if (fontAssets.ContainsKey(assetName)) { callback.Invoke(fontAssets[assetName]); } else { fontAssetRequests.Add(new KeyValuePair <string, Action <SpriteFont> >(assetName, callback)); } }
/// <summary> /// requests a sound /// </summary> /// <param name="assetName">the name of the asset</param> /// <param name="callback">what to call back when we get the sound</param> public void RequestSound(string assetName, Action <SoundEffect> callback) { ConsoleManager.WriteLine("requesting sound \"" + assetName + "\""); if (soundAssets.ContainsKey(assetName)) { callback.Invoke(soundAssets[assetName]); } else { soundAssetRequests.Add(new KeyValuePair <string, Action <SoundEffect> >(assetName, callback)); } }
/// <summary> /// loads a texture /// </summary> /// <param name="internalName">the name to call the asset in this manager</param> /// <param name="location">the location of the asset</param> /// <returns>the loaded texture</returns> public Texture2D LoadTexture(string internalName, string location) { Texture2D texture = Content.Load <Texture2D>(location); for (int i = textureAssetRequests.Count - 1; i >= 0; i--) { var req = textureAssetRequests[i]; if (req.Key.Equals(internalName)) { req.Value.Invoke(texture); textureAssetRequests.RemoveAt(i); } } ConsoleManager.WriteLine("loaded texture \"" + internalName + "\" from " + location, "load"); textureAssets[internalName] = texture; return(texture); }
/// <summary> /// loads a font /// </summary> /// <param name="internalName">the name to call the asset in this manager</param> /// <param name="location">the location of the asset</param> /// <returns>the loaded font</returns> public SpriteFont LoadFont(string internalName, string location) { SpriteFont font = Content.Load <SpriteFont>(location); for (int i = fontAssetRequests.Count - 1; i >= 0; i--) { var req = fontAssetRequests[i]; if (req.Key.Equals(internalName)) { req.Value.Invoke(font); fontAssetRequests.RemoveAt(i); } } ConsoleManager.WriteLine("loaded font \"" + internalName + "\" from " + location, "load"); fontAssets[internalName] = font; return(font); }
/// <summary> /// loads a sound /// </summary> /// <param name="internalName">the name to call the asset in this manager</param> /// <param name="location">the location of the asset</param> /// <returns>the loaded sound</returns> public SoundEffect LoadSound(string internalName, string location) { SoundEffect sound = Content.Load <SoundEffect>(location); for (int i = soundAssetRequests.Count - 1; i >= 0; i--) { var req = soundAssetRequests[i]; if (req.Key.Equals(internalName)) { req.Value.Invoke(sound); soundAssetRequests.RemoveAt(i); } } ConsoleManager.WriteLine("loaded sound \"" + internalName + "\" from " + location, "load"); soundAssets[internalName] = sound; return(sound); }
/// <summary> /// loads the assets listed in the json file /// </summary> /// <param name="filepath">the path to the json file</param> public void LoadAssetsFromFile(string filepath) { ConsoleManager.WriteLine("loading assets from file \"" + filepath + "\""); string[] lines = File.ReadAllLines(filepath, Encoding.UTF8); string json = ""; for (int i = 0; i < lines.Length; i++) { json += lines[i] + "\n"; } JObject obj = JObject.Parse(json); JArray assetList = (JArray)obj.GetValue("assets").ToObject(typeof(JArray)); foreach (JToken assetToken in assetList) { JObject assetObject = (JObject)assetToken.ToObject(typeof(JObject)); string internalName = (string)assetObject.GetValue("name").ToObject(typeof(string)); string typeName = (string)assetObject.GetValue("type").ToObject(typeof(string)); string path = (string)assetObject.GetValue("path").ToObject(typeof(string)); if (typeName.Equals("texture")) { LoadTexture(internalName, path); } else if (typeName.Equals("font")) { LoadFont(internalName, path); } else if (typeName.Equals("framedTexture")) { int frameCount = (int)assetObject.GetValue("frames").ToObject(typeof(int)); LoadFramedTexture(internalName, path, frameCount); } else if (typeName.Equals("sound")) { LoadSound(internalName, path); } else { ConsoleManager.WriteLine("could not find asset type \"" + typeName + "\"", "err"); } } }
/// <summary> /// loads a texture of multiple frames /// </summary> /// <param name="internalName">the name to call the asset in this manager</param> /// <param name="location">the location of the asset</param> /// <param name="frameCount">number of frames to load</param> /// <returns>a texture array with each loaded frame</returns> public Texture2D[] LoadFramedTexture(string internalName, string location, int frameCount) { Texture2D[] frames = new Texture2D[frameCount]; for (int i = 0; i < frames.Length; i++) { frames[i] = Content.Load <Texture2D>(location + i.ToString()); } for (int i = framedTextureAssetRequests.Count - 1; i >= 0; i--) { var req = framedTextureAssetRequests[i]; if (req.Key.Equals(internalName)) { req.Value.Invoke(frames); framedTextureAssetRequests.RemoveAt(i); } } ConsoleManager.WriteLine("loaded framed texture \"" + internalName + "\" from " + location, "load"); framedTextureAssets[internalName] = frames; return(frames); }
/// <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); }
/// <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); }