public void LoadAnim(string unparsedJson) { JObject json = JObject.Parse(unparsedJson); JArray animations = (JArray)json["animations"]; for (int i = 0; i < animations.Count; i++) { anims.Add(new Animation()); JObject animation = (JObject)(animations[i]); anims[i].name = (string)animation["name"]; anims[i].speed = (float)animation["speed"]; anims[i].loopback = (int)animation["loop"]; anims[i].nFrames = (int)animation["frames"]; JArray collisionArray = (JArray)animation["collisionVolumes"]; for (int ca = 0; ca < collisionArray.Count; ca++) { JObject volumeContainer = (JObject)(collisionArray[ca]); JArray volumes = (JArray)volumeContainer["volumes"]; anims[i].collision.Add(new List<CollisionList>()); for (int v = 0; v < volumes.Count; v++) { JObject volume = (JObject)(volumes[v]); float centerX = (float)volume["centerX"]; float centerY = (float)volume["centerY"]; bool physical = (bool)volume["physical"]; CollisionList cl = new CollisionList(); JArray points = (JArray)volume["xpoints"]; List<float> xpoints = new List<float>(); for (int p = 0; p < points.Count; p++) { xpoints.Add((float)points[p] + centerX); } points = (JArray)volume["ypoints"]; List<float> ypoints = new List<float>(); for (int p = 0; p< points.Count; p++) { ypoints.Add((float)points[p] + centerY); } for (int p = 0; p < xpoints.Count; p++) { cl.Add(xpoints[p], ypoints[p]); } anims[i].collision[ca].Add(cl); } } } foreach (Animation anim in anims) { if (Directory.Exists("images\\anim\\" + entity.Name + "\\" + anim.name)) { for (int i = 0; i < anim.nFrames; i++) { Stream s = File.OpenRead("images\\anim\\" + entity.Name + "\\" + anim.name + "\\" + i + ".png"); Texture2D texture = Texture2D.FromStream(graphics, s); anim.frames.Add(texture); } } } }
public Entity(string unparsedJson) { JObject json = JObject.Parse(unparsedJson); name = (string)json["name"]; string textureName = (string)json["texture"]; this.textureName = textureName; texture = MenuSystem.textureBank.Textures[textureName]; int x, y, w, h; x = (int)((float)json["x"]); y = (int)((float)json["y"]); w = (int)json["width"]; h = (int)json["height"]; rect = new Rectangle(x - (w / 2), y - (h / 2), w, h); rotation = (float)json["rotation"]; JArray volumes = (JArray)json["collisionVolumes"]; for (int index = 0; index < volumes.Count; index++) { CollisionList cl = new CollisionList(); JObject collisionJson = (JObject)(volumes[index]); JArray points = (JArray)collisionJson["xpoints"]; List<float> xpoints = new List<float>(); for (int i = 0; i < points.Count; i++) { xpoints.Add((float)points[i] + x); } points = (JArray)collisionJson["ypoints"]; List<float> ypoints = new List<float>(); for (int i = 0; i < points.Count; i++) { ypoints.Add((float)points[i] + y); } for (int i = 0; i < xpoints.Count; i++) { cl.Add(xpoints[i], ypoints[i]); } cl.Physical = (bool)collisionJson["physical"]; cVolumes.Add(cl); } dynamic = (bool)json["dynamic"]; JArray tags = (JArray)json["tags"]; for (int index = 0; index < tags.Count; index++) { this.tags.Add((string)tags[index]); } }