public List <IEntity> PopulateScene(TiledMap map) { if (map == null) { throw new ArgumentNullException(nameof(map)); } bool playerFound = false; foreach (var objGroup in map.ObjectGroups) { foreach (var obj in objGroup.Objects) { if ("player".Equals(obj.Type, StringComparison.OrdinalIgnoreCase)) { if (!playerFound) { allocatedEntities.Add(new Player( physicsWorld, obj, map.GetTileRegion((int)obj.Gid))); playerFound = true; } else { throw new InvalidSceneDataException( "Cannot have more than one player.", nameof(map)); } } else if ("coin".Equals(obj.Type, StringComparison.OrdinalIgnoreCase)) { allocatedEntities.Add(new Coin( physicsWorld, obj, map.GetTileRegion((int)obj.Gid))); } else if (obj.ObjectType == TiledObjectType.Tile) { allocatedEntities.Add(new DrawableEntity( physicsWorld, obj, map.GetTileRegion((int)obj.Gid))); } else if (obj.ObjectType == TiledObjectType.Rectangle) { CreateRectangle(obj); } else { Console.WriteLine("[{0}] Discarding unsupported object of type {1}.", GetType().Name, obj.ObjectType); } } } if (!playerFound) { throw new InvalidSceneDataException("Must specify player object.", nameof(map)); } return(allocatedEntities); }
public void CheckForCollision(TiledMap map) { // Collision with bottom of map if (this.BoundingRect.Bottom > map.HeightInPixels) { this.position.Y = map.HeightInPixels - texture.Height; this.velocity.Y = 0; onGround = true; } // Collision with top of map if (this.BoundingRect.Top < 0) { this.position.Y = 0; this.velocity.Y = 0; } // Collision with top of map if (this.BoundingRect.Left < 0) { this.position.X = 0; this.velocity.X = 0; } // Collision with top of map if (this.BoundingRect.Right > map.WidthInPixels) { this.position.X = map.WidthInPixels - texture.Width; this.velocity.X = 0; } // Check for collision using collision layer TiledTileLayer layer = (TiledTileLayer)map.GetLayer("Collision"); foreach (var tile in layer.Tiles) { if ( ((tile.X * 32) > this.BoundingRect.Right) || ((tile.Y * 32) > this.BoundingRect.Bottom) || (((tile.X * 32) + 32) < this.BoundingRect.Left) || (((tile.Y * 32) + 32) < this.BoundingRect.Top) ) { continue; } var region = map.GetTileRegion(tile.Id); if (region != null && region.Texture.Name == "Tilesets/collision" && (region.X + region.Y == 35)) { this.position.Y = (tile.Y * 32) - this.BoundingRect.Height; this.velocity.Y = 0; onGround = true; } } }