protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); blank = new Texture2D(GraphicsDevice, 1, 1); blank.SetData(new[] { Color.White }); // load the map map = Content.Load<Map>("Map"); // find the "Box" and "Point" objects we made in the level MapObjectLayer objects = map.GetLayer("Objects") as MapObjectLayer; point = objects.GetObject("Point"); box = objects.GetObject("Box"); // attempt to read the box color from the box object properties try { boxColor.R = (byte)box.Properties["R"]; boxColor.G = (byte)box.Properties["G"]; boxColor.B = (byte)box.Properties["B"]; boxColor.A = 255; } catch { // on failure, default to yellow boxColor = Color.Yellow; } // find one tile from our tile layer TileLayer tileLayer = map.GetLayer("Tiles") as TileLayer; tile = tileLayer.Tiles[0, 0]; }
/// <summary> /// Adds a MapObject to the layer. /// </summary> /// <param name="mapObject">The MapObject to add.</param> public void AddObject(MapObject mapObject) { // avoid adding the object to the layer twice if (objects.Contains(mapObject)) return; namedObjects.Add(mapObject.Name, mapObject); objects.Add(mapObject); }
/// <summary> /// Removes an object from the layer. /// </summary> /// <param name="mapObject">The object to remove.</param> /// <returns>True if the object was found and removed, false otherwise.</returns> public bool RemoveObject(MapObject mapObject) { return RemoveObject(mapObject.Name); }
public void CreateLight(MapObject lightObject) { Vector2 position = new Vector2(lightObject.Bounds.Center.X, lightObject.Bounds.Center.Y); Property lightAngle; if(lightObject.Properties.TryGetValue("LightAngle", out lightAngle) == false) { lightAngle = new Property("LightAngle", "3.14"); } float lightAngleValue = float.Parse(lightAngle.RawValue); Property lightColorString; if(lightObject.Properties.TryGetValue("LightColor", out lightColorString) == false) { throw new Exception("Failed to retrieve light color from " + lightObject.Name + " in map " + mapFilePath); } // use reflection to get a Color from a string PropertyInfo colorProperty = typeof(Color).GetProperty(lightColorString.RawValue); Color lightColor = (Color)colorProperty.GetValue(null, null); Property lightFov; if(lightObject.Properties.TryGetValue("LightFov", out lightFov) == false) { lightFov = new Property("LightFov", "6.28"); } float lightFovValue = float.Parse(lightFov.RawValue); Property lightMotion; if(lightObject.Properties.TryGetValue("LightMotion", out lightMotion) == false) { throw new Exception("Failed to retrieve light motion from " + lightObject.Name + " in map " + mapFilePath); } Property lightRange; if(lightObject.Properties.TryGetValue("LightRange", out lightRange) == false) { throw new Exception("Failed to retrieve light range from " + lightObject.Name + " in map " + mapFilePath); } float lightRangeValue = float.Parse(lightRange.RawValue); Property lightType; if(lightObject.Properties.TryGetValue("LightType", out lightType) == false) { throw new Exception("Failed to retrieve light type from " + lightObject.Name + " in map " + mapFilePath); } EffectLight effectLight; switch(lightType.RawValue) { case "Gravity": // get gravity value Property gravityValueProperty; if(lightObject.Properties.TryGetValue("GravityValue", out gravityValueProperty) == false) { throw new Exception("Failed to retrieve gravity value from " + lightObject.Name + " in map " + mapFilePath); } float gravityValue = float.Parse(gravityValueProperty.RawValue); effectLight = new GravityLight(Engine, gravityValue); break; case "Null": effectLight = new NullLight(Engine, Lights); break; case "Velocity": // get velocity value Property velocityValueProperty; if(lightObject.Properties.TryGetValue("VelocityValue", out velocityValueProperty) == false) { throw new Exception("Failed to retrieve velocity value from " + lightObject.Name + " in map " + mapFilePath); } float velocityValue = float.Parse(velocityValueProperty.RawValue); effectLight = new VelocityLight(Engine, velocityValue); break; default: throw new Exception("Failed to instantiate light with type of " + lightObject.Name + " in map " + mapFilePath); } effectLight.Angle = lightAngleValue; effectLight.Color = lightColor; effectLight.Fov = lightFovValue; effectLight.Intensity = 1.0f; effectLight.Position = position; effectLight.Range = lightRangeValue; effectLight.Activate(lightMotion.RawValue); Lights.Add(effectLight); }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); blank = new Texture2D(GraphicsDevice, 1, 1); blank.SetData(new[] { Color.White }); // load the map. the map for this demo is 999x999 or 998,001 tiles to showcase // the efficient Draw(SpriteBatch, Rectangle) method. map = Content.Load<Map>("Map"); // find the "Box" and "Point" objects we made in the level MapObjectLayer objects = map.GetLayer("Objects") as MapObjectLayer; point = objects.GetObject("Point"); box = objects.GetObject("Box"); // attempt to read the box color from the box object properties try { boxColor.R = (byte)box.Properties["R"]; boxColor.G = (byte)box.Properties["G"]; boxColor.B = (byte)box.Properties["B"]; boxColor.A = 255; } catch { // on failure, default to yellow boxColor = Color.Yellow; } }
public void CreateEnemy(MapObject enemyObject) { Vector2 enemyPosition = new Vector2(enemyObject.Bounds.X, enemyObject.Bounds.Y); switch (enemyObject.Type) { case "PlayerEnemy": PlayerEnemy pe = new PlayerEnemy(Engine, new Vector2(enemyObject.Bounds.X, enemyObject.Bounds.Y)); Enemies.Add(pe); break; case "RollingCircleEnemy": RollingCircleEnemy rce = new RollingCircleEnemy(Engine, enemyPosition); Enemies.Add(rce); break; case "RotatingBoxEnemy": FlyingBoxEnemy rbe = new FlyingBoxEnemy(Engine, enemyPosition); Enemies.Add(rbe); break; default: throw new Exception("Failed to instantiate enemy with type of " + enemyObject.Type + " in map " + mapFilePath); } }