public void Load(ContentManager cm) { _map = cm.Load<Map>("Maps\\Level1"); // Figure out map rows/cols TileSheet ts = _map.GetTileSheet("Front"); Vector2 tileSize = new Vector2(ts.TileWidth, ts.TileHeight); int mapSizeX = (int) (_map.DisplayWidth / tileSize.X); int mapSizeY = (int) (_map.DisplayHeight / tileSize.Y); TileArray tiles = _map.GetLayer("StaticCollidable").Tiles; // Get all static objects for (int row = 0; row < mapSizeY; ++row) { for (int col = 0; col < mapSizeX; ++col) { if (tiles[col, row] == null) continue; Tile tile = tiles[col, row]; // Calculate origin-based position Vector2 oPos = new Vector2(col, row); oPos.X = oPos.X * tileSize.X; oPos.Y = oPos.Y * tileSize.Y; // Create MapTile MapTile mTile = new MapTile(_world, tileSize, oPos); _tiles.Add(mTile); } } }
public List<GameObject> GetGameObjects(Game game, Map map) { _layer = map.GetLayer(_layerName); if (_layer != null) { return CreateGameObjects(ExtractGameObjectPositions(_layer), game); } else { return new List<GameObject>(); } }
public Character(Texture2D texture, int currentFrame, int spriteWidth, int spriteHeight, Map map) { this.spriteTexture = texture; this.currentFrame = currentFrame; this.spriteWidth = spriteWidth; this.spriteHeight = spriteHeight; this._map = map; if (!(_map == null)) { Console.WriteLine("_map != null"); } this.collision = map.GetLayer("Colision"); //this.collision = _map.Layers[3]; m_vecLeafPositions = new Vector2[20]; m_vecLeafVelocities = new Vector2[m_vecLeafPositions.Length]; }
/// <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); Spritesheet = Content.Load<Texture2D>(@"SpriteSheet"); map = Content.Load<Map>("pacmanmap"); menuMusic = Content.Load<Song>("introMusic"); map.LoadTileSheets(xnaDisplayDevice); MediaPlayer.Play(menuMusic); pizzaguy = new PizzaGuy(new Vector2(32, 32), Spritesheet, new Rectangle(300, 300, 32, 32), Vector2.Zero, map.GetLayer("untitled layer")); ghost = new Ghost(new Vector2(32, 32*10), Spritesheet, new Rectangle(500, 300, 32, 32), Vector2.Zero, map.GetLayer("untitled layer"), pizzaguy); // TODO: use this.Content to load your game content here }
protected override void OnNavigatedTo(NavigationEventArgs e) { // Set the sharing mode of the graphics device to turn on XNA rendering SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true); // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice); golfball = new Sprite(contentManager.Load<Texture2D>("golfball")); golfball.X = ballScreenX; golfball.Y = 128; int w = SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width; int h = SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height; // Create xTile mandatory objects camera = new xTile.Dimensions.Rectangle(new xTile.Dimensions.Size(w, h)); mapDisplayDevice = new XnaDisplayDevice(contentManager, SharedGraphicsDeviceManager.Current.GraphicsDevice); // Load xTile tilemap from content, some hacking required to load file without processor+importer from contentlib // Map file must have; build action set to "none", and copy to output dir // set to "always" to be able open this way. Also "Rebuild solution" must // be invoked in Visual Studio when resource is initially added. System.IO.Stream stream = TitleContainer.OpenStream("Content\\Map02.tbin"); // Seems that only tbin type of maps are supported by libs map = FormatManager.Instance.BinaryFormat.Load(stream); fixTileLocationsBug(map); map.LoadTileSheets(mapDisplayDevice); // Setup Box2D physics physics = new Physics(); // Add golf ball to Box2D world physics.addBallSprite(golfball); // Add the ground and water hit area rectables to Box2D world // Each tile is 16x16 pixels TileArray groundTiles = map.GetLayer("HitGround").Tiles; TileArray waterTiles = map.GetLayer("HitWater").Tiles; for (int y = 0; y < 48; y++) { for (int x = 0; x < 800; x++) { Tile tgroup = groundTiles[x, y]; Tile twater = waterTiles[x, y]; if (tgroup != null) { physics.addRect(x * 16, y * 16, 16, 16, Physics.TYPE_GROUND); } if (twater != null) { physics.addRect(x * 16, y * 16, 16, 16, Physics.TYPE_WATER); } } } // Start the timer timer.Start(); base.OnNavigatedTo(e); }
/// <summary> /// Returns a list of all layers meant for collisionbox making /// </summary> /// <param name="map">The map containing the layers we want to make collisionBoxes from</param> /// <returns>A list of layers for collisionbox making</returns> List<Layer> GetLayers(Map map) { List<Layer> layers = new List<Layer>(); string layerName = "collision"; Layer collision = map.GetLayer(layerName); if (collision != null) { layers.Add(collision); } int ending = 1; while (collision != null) { string endInsert = ending.ToString(); string name = layerName.Insert(layerName.Length, endInsert); collision = map.GetLayer(name); if (collision != null) { layers.Add(collision); } ++ending; } return layers; }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); camera = new xTile.Dimensions.Rectangle(new Size(charScreenX, 309)); //System.IO.Stream stream = TitleContainer.OpenStream("Content\\Maps\\Map02.tbin"); //map = FormatManager.Instance.BinaryFormat.Load(stream); //FixTileLocationsBug(map); map = Content.Load<Map>("Maps\\Map02"); //LoadMap("Map01"); /*map = Content.Load<Map>("Maps\\Map01"); map.Layers[3].AfterDraw += OnBeforeLayerDraw; map.Layers[4].Visible = false;*/ _character = new Sprite(contentManager.Load<Texture2D>("golfball")); _character.X = charScreenX; _character.Y = 128; physics = new Physics(); physics.addCharacterSprite(_character); TileArray groundTiles = map.GetLayer("HitGround").Tiles; TileArray waterTiles = map.GetLayer("HitWater").Tiles; for (int y = 0; y < 48; y++) { for (int x = 0; x < 800; x++) { Tile tgroup = groundTiles[x, y]; Tile twater = waterTiles[x, y]; if (tgroup != null) { physics.addRect(x * 16, y * 16, 16, 16, Physics.TYPE_GROUND); } if (twater != null) { physics.addRect(x * 16, y * 16, 16, 16, Physics.TYPE_WATER); } } } physics.flickCharacter(139, 309); //_player = new Character(Content.Load<Texture2D>("Sprite_Sheet"), 1, 32, 48, this.map); //_player.Position = new Point(139, 309);//new Vector2(138, 308); base.LoadContent(); }
private Dictionary<string, Vector2> GetPlayerPositions(Map map) { Dictionary<string, Vector2> playerPositions = new Dictionary<string, Vector2>(); if (map != null) { Layer playerLayer = map.GetLayer("playerPosition"); if (playerLayer != null) { TileArray tileArray = playerLayer.Tiles; Size tileSize = playerLayer.TileSize; Size amntOfTiles = playerLayer.LayerSize; for (int x = 0; x < amntOfTiles.Width; x++) { for (int y = 0; y < amntOfTiles.Height; y++) { Location tileLocation = new Location(x, y); Tile thisTile = tileArray[tileLocation]; if (thisTile != null) { string playerName = thisTile.Properties["player"].ToString(); Vector2 vertex = new Vector2(x * tileSize.Width, y * tileSize.Height); //vertex.Y -= 75; //Vector2 vertex = new Vector2(ConvertUnits.ToSimUnits(x * tileSize.Width), ConvertUnits.ToSimUnits(y * tileSize.Height)); playerPositions.Add(playerName, vertex); } } } } else { Debug.WriteLine("playerPosition layer is null"); } } else { Debug.WriteLine("map is null"); } return playerPositions; }
/// <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); map = Content.Load<Map>("Maps\\simpleTileMap"); // tIDE map load //foreach (Layer l in map.Layers) //{ // if (l.Properties["CollisionLayer"]) // { // //Add to farser physics // //l. // } //} int fooProp = map.Layers[0].Properties["foo"]; Layer col = map.GetLayer("CollisionLayer"); //for(int i = 0; i < col.Tiles;) var viewport = this.GraphicsDevice.Viewport; Texture2D playerTexture = Content.Load<Texture2D>("player");//todo: add player texture player = new Sprite(playerTexture); world = new World(Vector2.Zero);//0 gravity world playerBody = BodyFactory.CreateBody(world); CircleShape playerShape = new CircleShape(ConvertUnits.ToSimUnits(playerTexture.Bounds.Width / 2), 5f); Fixture playerFixture = playerBody.CreateFixture(playerShape); playerBody.BodyType = BodyType.Dynamic; playerBody.Position = ConvertUnits.ToSimUnits(new Vector2(450, 0)); //playerBody.Restitution = 0.8f; accelerometerInput = new Vector2(); if (accelerometer == null) { accelerometer = new Accelerometer(); accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20); accelerometer.CurrentValueChanged += (s, e) => { accelerometerInput.X = e.SensorReading.Acceleration.X; accelerometerInput.Y = -e.SensorReading.Acceleration.Y; }; accelerometer.Start(); } }
/// <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); // TODO: use this.Content to load your game content here map = Content.Load<Map>("Maps\\Map01"); mSpriteTexture = Content.Load<Texture2D>("golfball"); MyTexture = Content.Load<Texture2D>("blank"); world = new World(new Vector2(0, 15.0f)); RenderHelper = new RenderXNAHelper(world); RenderHelper.AppendFlags(DebugViewFlags.TexturedShape); RenderHelper.RemoveFlags(DebugViewFlags.Shape); RenderHelper.DefaultShapeColor = Color.White; RenderHelper.SleepingShapeColor = Color.LightGray; RenderHelper.LoadContent(GraphicsDevice, Content); BoxBody = BodyFactory.CreateBody(world); FixtureFactory.CreateRectangle(ConvertUnits.ToSimUnits(50), ConvertUnits.ToSimUnits(50), 10, Vector2.Zero, BoxBody); foreach (Fixture fixture in BoxBody.FixtureList) { fixture.Restitution = 1.0f; fixture.Friction = 0.5f; } BoxBody.BodyType = BodyType.Dynamic; BoxBody.Position = ConvertUnits.ToSimUnits(new Vector2(400, 25)); //Create Floor //Fixture floorFixture = FixtureFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(800), ConvertUnits.ToSimUnits(10), 10); //floorFixture.Restitution = 0.5f; //Bounceability //floorFixture.Friction = 0.5f; //Friction //FloorBody = floorFixture.Body; //Get Body from Fixture //FloorBody.IsStatic = true; //FloorBody.Position = ConvertUnits.ToSimUnits(new Vector2(0, 400)); Layer layer = map.GetLayer("HitGround"); TileArray groundTiles = layer.Tiles; for (int x = 0; x < 800; x++) { for (int y = 0; y < 48; y++) { Tile tile = groundTiles[x, y]; if (tile != null) { Fixture smallBox = FixtureFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(16), ConvertUnits.ToSimUnits(16), 10); smallBox.Restitution = 0.5f; //Bounceability smallBox.Friction = 0.5f; //Friction var smallBoxBody = smallBox.Body; //Get Body from Fixture smallBoxBody.IsStatic = true; smallBoxBody.Position = ConvertUnits.ToSimUnits(new Vector2(x * 16, y * 16)); MapBodies.Add(smallBoxBody); } } } Camera = new Camera2D(GraphicsDevice); Camera.TargetPosition = screenCenter; //Camera.TrackingBody = BoxBody; }