public void Draw(TiledMapLayer layer, Matrix?viewMatrix = null, Matrix?projectionMatrix = null, Effect effect = null, float depth = 0.0f) { var viewMatrix1 = viewMatrix ?? Matrix.Identity; var projectionMatrix1 = projectionMatrix ?? Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, -1); Draw(layer, ref viewMatrix1, ref projectionMatrix1, effect, depth); }
protected override void Initialize() { base.Initialize(); this.groundLayer = this.tiledMap.TileLayers["Ground"]; this.mouseOverTileEntity = this.CreateMouseOverEntity(this.overTileColor, 17); this.Owner.AddChild(this.mouseOverTileEntity); }
void WriteLayer(ContentWriter output, TiledMapLayer layer) { output.Write(layer.Name); output.Write(layer.ID); output.Write(layer.Visible); output.Write(layer.Opacity); output.Write(layer.Offset); output.WriteObject(layer.Properties); }
void ReadLayer(ContentReader input, TiledMapLayer layer) { layer.Name = input.ReadString(); layer.ID = input.ReadInt32(); layer.Visible = input.ReadBoolean(); layer.Opacity = input.ReadSingle(); layer.Offset = input.ReadVector2(); layer.Properties = input.ReadObject <Dictionary <string, string> >(); }
static void ParseBaseLayer(XmlNode layerXml, TiledMapLayer layer) { layer.ID = int.Parse(layerXml.Attributes["id"].Value); layer.Name = layerXml.Attributes["name"].Value; layer.Opacity = XmlHelper.GetXmlFloatSafe(layerXml, "opacity"); layer.Visible = XmlHelper.GetXmlBoolSafe(layerXml, "visible"); layer.Offset = new Vector2( XmlHelper.GetXmlFloatSafe(layerXml, "offsetx"), XmlHelper.GetXmlFloatSafe(layerXml, "offsety") ); layer.Properties = XmlHelper.GetProperties(layerXml); }
private void Draw(TiledMapLayer layer, Vector2 parentOffset, ref Matrix viewMatrix, ref Matrix projectionMatrix, Effect effect, float depth) { var offset = parentOffset + layer.Offset; if (layer is TiledMapGroupLayer groupLayer) { foreach (var subLayer in groupLayer.Layers) { Draw(subLayer, offset, ref viewMatrix, ref projectionMatrix, effect, depth); } } else { _worldMatrix.Translation = new Vector3(offset, depth); var effect1 = effect ?? _defaultEffect; var tiledMapEffect = effect1 as ITiledMapEffect; if (tiledMapEffect == null) { return; } // model-to-world transform tiledMapEffect.World = _worldMatrix; tiledMapEffect.View = viewMatrix; tiledMapEffect.Projection = projectionMatrix; foreach (var layerModel in _mapModel.LayersOfLayerModels[layer]) { // desired alpha tiledMapEffect.Alpha = layer.Opacity; // desired texture tiledMapEffect.Texture = layerModel.Texture; // bind the vertex and index buffer _graphicsDevice.SetVertexBuffer(layerModel.VertexBuffer); _graphicsDevice.Indices = layerModel.IndexBuffer; // for each pass in our effect foreach (var pass in effect1.CurrentTechnique.Passes) { // apply the pass, effectively choosing which vertex shader and fragment (pixel) shader to use pass.Apply(); // draw the geometry from the vertex buffer / index buffer _graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, layerModel.TriangleCount); } } } }
private IEnumerable <TiledMapLayerModel> CreateLayerModels(TiledMap map, TiledMapLayer layer) { switch (layer) { case TiledMapTileLayer tileLayer: return(CreateTileLayerModels(map, tileLayer)); case TiledMapImageLayer imageLayer: return(CreateImageLayerModels(imageLayer)); default: return(new List <TiledMapLayerModel>()); } }
private void BuildLayer(TiledMap map, TiledMapLayer layer, Dictionary <TiledMapLayer, TiledMapLayerModel[]> dictionary) { if (layer is TiledMapGroupLayer groupLayer) { foreach (var subLayer in groupLayer.Layers) { BuildLayer(map, subLayer, dictionary); } } else { dictionary.Add(layer, CreateLayerModels(map, layer).ToArray()); } }
public static void DrawLayer(SpriteBatch spriteBatch, string LayerName) { spriteBatch.Begin(transformMatrix: Camera.camera.GetViewMatrix(), samplerState: SamplerState.PointClamp); try { TiledMapLayer layer = currentLevel.Map.GetLayer(LayerName); mapRenderer.Draw(layer, Camera.camera.GetViewMatrix()); } catch (Exception) { //if no layer exists nothing will happen } spriteBatch.End(); }
public void Draw(TiledMapLayer layer, ref Matrix viewMatrix, ref Matrix projectionMatrix, Effect effect = null, float depth = 0.0f) { if (!layer.IsVisible) { return; } if (layer is TiledMapObjectLayer) { return; } _worldMatrix.Translation = new Vector3(layer.OffsetX, layer.OffsetY, depth); var effect1 = effect ?? _defaultEffect; var tiledMapEffect = effect1 as ITiledMapEffect; if (tiledMapEffect == null) { return; } foreach (var model in layer.Models) { // model-to-world transform tiledMapEffect.World = _worldMatrix; tiledMapEffect.View = viewMatrix; tiledMapEffect.Projection = projectionMatrix; // desired alpha tiledMapEffect.Alpha = layer.Opacity; // desired texture tiledMapEffect.Texture = model.Texture; // bind the vertex and index buffer GraphicsDevice.SetVertexBuffer(model.VertexBuffer); GraphicsDevice.Indices = model.IndexBuffer; // for each pass in our effect foreach (var pass in effect1.CurrentTechnique.Passes) { // apply the pass, effectively choosing which vertex shader and fragment (pixel) shader to use pass.Apply(); // draw the geometry from the vertex buffer / index buffer GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, model.TrianglesCount); } } }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // Add your initialization logic here //this.IsMouseVisible = true; Window.Title = "Just Combat"; base.Initialize(); _gameMap = GameContent.GameMap; TiledMapLayer objectLayer = _gameMap.GetLayer("ObjectLayer"); GameMapRenderer = new TiledMapRenderer(GraphicsDevice, _gameMap); }
protected override void Initialize() { base.Initialize(); // Generate the Adjacency Matrix based on the TiledMapLayer this.groundTiledLayer = this.tiledMap.TileLayers["Ground"]; this.pathFinder.AdjacencyMatrix = this.GetAdjacencyMatrixFromTiledMapLayer(this.groundTiledLayer, this.weightsByTileId); // TODO: NOT NECESSARY this.pathFindingAlgorithm.AdjacencyMatrix = this.pathFinder.AdjacencyMatrix; this.currentTile = this.groundTiledLayer.GetLayerTileByMapCoordinates(2, 3); // Move to starting tile this.SetTransformPosition(this.currentTile.LocalPosition); }
/// <summary> /// Goes through the current level file and loads all of the collision objects, so that they can be returned and added to the list of active GameObjects. Note that all collision object must be on a layer /// named "collision" that is set to be an object layer, and not a normal tile layer (normal tiles don't have a position member). /// </summary> /// <returns></returns> public List <GameObject> getCollisionObjects() { List <GameObject> collisionObjects = new List <GameObject>(); TiledMapObjectLayer objectLayer = currentMap.GetLayer <TiledMapObjectLayer>("collision"); //Get the collision layer to start with TiledMapLayer debugLayer = currentMap.GetLayer <TiledMapLayer>("collision"); Console.WriteLine(debugLayer.ToString()); GameObject tempObject; //Make an object reference to hold objects we'll construct later, and then add properties to try { foreach (TiledMapPolygonObject tile in objectLayer.Objects) //Go through all of the tiles in the map { tempObject = new GameObject(); tempObject.transform.SetPosition(tile.Position); //Set the collision object's position //Get the vertices of this collision object Vector2[] vertices; vertices = new Vector2[tile.Points.Count()]; //Set the vertices to be the same count as that of the tile object's for (int i = 0; i < tile.Points.Count(); i++) { vertices[i] = new Vector2(tile.Points[i].X, tile.Points[i].Y); //The Points are already the displacement from an origin, so this will match the Hitbox constructor later } Hitbox hitbox = new Hitbox(vertices, tempObject); //Create a hitbox for this tile tempObject.AddComponent(hitbox); //Give the tile the hitbox tempObject.AddComponent(new CollisionComponent(null, hitbox)); //Add collision to the tile with the given hitbox and no physics component collisionObjects.Add(tempObject); //Now that the tile object has collision and a hitbox mask, add it to the list of GameObjects that we want to return } } catch (Exception e) //In case the collision layer is null { Console.WriteLine("Error encountered in getCollisionObjects(): " + e.Message); } //DEBUG Let's make sure that it's correctly adding the tile objects Console.WriteLine("Number of collision objects = " + collisionObjects.Count); return(collisionObjects); }
public void Draw(TiledMapLayer layer, ref Matrix viewMatrix, ref Matrix projectionMatrix, Effect effect = null, float depth = 0.0f) { if (_mapModel == null) { return; } if (!layer.IsVisible) { return; } if (layer is TiledMapObjectLayer) { return; } Draw(layer, Vector2.Zero, ref viewMatrix, ref projectionMatrix, effect, depth); }
private AdjacencyMatrix <LayerTile> GetAdjacencyMatrixFromTiledMapLayer(TiledMapLayer tiledMapLayer, IDictionary <int, int> weights) { var result = new AdjacencyMatrix <LayerTile>(); foreach (var tile in tiledMapLayer.Tiles) { if (weights.ContainsKey(tile.Id)) { foreach (var neighbour in tiledMapLayer.GetNeighboursFromTile(tile)) { if (neighbour != null && weights.ContainsKey(neighbour.Id)) { result.AddAdjacent(tile, neighbour, weights[neighbour.Id]); } } } } return(result); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { //Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // load player playerSprite = Content.Load <Texture2D>("Imgs/player"); player = new Player(200f, playerSprite.Width, playerSprite.Height, playerSprite); // import tmx map map = Content.Load <TiledMap>("maps/terrain"); // grab bottom, middle and top layers bottomLayer = map.GetLayer <TiledMapLayer>("bottomLayer"); middleLayer = map.GetLayer <TiledMapLayer>("middleLayer"); topLayer = map.GetLayer <TiledMapLayer>("topLayer"); overLapLayer = map.GetLayer <TiledMapLayer>("overLapLayer"); mapRenderer = new TiledMapRenderer(GraphicsDevice); mapRenderer.LoadMap(map); // collision variables objectLayer = map.GetLayer <TiledMapObjectLayer>("collision"); }
/** * <summary> * Loads the map from the TiledMap into the arrays * </summary> */ public void SetMap() { networkedScene = (Owner.Scene as NetworkedScene); IEnumerator <string> keys = tiledMap.TileLayers.Keys.GetEnumerator(); if (keys.MoveNext()) { string layer = keys.Current; occupied = new bool[width, height]; mobiles = new WorldObject[width, height]; objects = new WorldObject[width, height]; if (layer != null) { mapLayer = tiledMap.TileLayers[layer]; foreach (LayerTile tile in mapLayer.Tiles) { if (InBounds(tile.X, tile.Y)) { occupied[tile.X, tile.Y] = false; mobiles[tile.X, tile.Y] = null; objects[tile.X, tile.Y] = null; } } } } Color back = RenderManager.ActiveCamera2D.BackgroundColor; //Hide non-playable map if (height != tiledMap.Height) { Entity quad = new Entity() .AddComponent(new Transform2D() { Position = new Vector2(width * tiledMap.TileWidth, 0), Scale = new Vector2((tiledMap.Width - width), tiledMap.Height) }) .AddComponent(new Sprite(WaveContent.Assets.Blank_png) { TintColor = back }) .AddComponent(new SpriteRenderer()); EntityManager.Add(quad); } if (width != tiledMap.Width) { Entity quad = new Entity() .AddComponent(new Transform2D() { Position = new Vector2(0, height * tiledMap.TileHeight), Scale = new Vector2(tiledMap.Width, (tiledMap.Height - height)) }) .AddComponent(new Sprite(WaveContent.Assets.Blank_png) { TintColor = back }) .AddComponent(new SpriteRenderer()); EntityManager.Add(quad); } }