public Lava(AbstractScene scene, int width, int height, Vector2 position) : base(scene.LayerManager.LavaLayer, null, position) { AddCollisionAgainst("Hero"); int spriteSize = 32; TileGroup tg = new TileGroup(32); Texture2D tileSet = Assets.GetTexture("Lava"); Color[] data = new Color[spriteSize * spriteSize]; tileSet.GetData(0, new Rectangle(0, 0, spriteSize, spriteSize), data, 0, data.Length); for (int i = 0; i < width; i += spriteSize) { for (int j = 0; j < height; j += spriteSize) { tg.AddColorData(data, new Vector2(i, j)); } } AddComponent(new Sprite(this, tg.GetTexture(), new Rectangle(0, 0, width, height))); HorizontalFriction = 0; VerticalFriction = 0; VelocityY = 0.005f; HasGravity = false; CheckGridCollisions = false; Visible = true; Active = true; AddComponent(new BoxCollisionComponent(this, width, height)); //DEBUG_SHOW_COLLIDER = true; }
public HashSet <EntityInstance> ParseLevel(AbstractScene scene, string levelID) { HashSet <EntityInstance> entities = new HashSet <EntityInstance>(); foreach (Engine.Source.Level.Level level in world.Levels) { if (!level.Identifier.Equals(levelID)) { continue; } float scrollSpeedModifier = 0f; Array.Reverse(level.LayerInstances); foreach (LayerInstance layerInstance in level.LayerInstances) { foreach (EntityInstance entity in layerInstance.EntityInstances) { entities.Add(entity); } string layerName = layerInstance.Identifier; Layer currentLayer = null; Texture2D tileSet = null; if (layerName.StartsWith(COLLIDERS) && layerInstance.GridTiles.Length > 0) { currentLayer = scene.LayerManager.EntityLayer; tileSet = null; //continue; } else if (layerName.StartsWith(BACKGROUND) && layerInstance.GridTiles.Length > 0) { //currentLayer = RootContainer.Instance.BackgroundLayer; currentLayer = scene.LayerManager.CreateBackgroundLayer(int.Parse(layerName[layerName.Length - 1] + "")); tileSet = Assets.GetTexture(GetMonoGameContentName(layerInstance.TilesetRelPath)); tileGroup = new TileGroup(); } else if (layerName.StartsWith(PARALLAX) && layerInstance.GridTiles.Length > 0) { //currentLayer = RootContainer.Instance.BackgroundLayer; scrollSpeedModifier += 0.1f; currentLayer = scene.LayerManager.CreateParallaxLayer(int.Parse(layerName[layerName.Length - 1] + ""), scrollSpeedModifier, true); tileSet = Assets.GetTexture(GetMonoGameContentName(layerInstance.TilesetRelPath)); tileGroup = new TileGroup(); } else if (layerName.StartsWith(FOREGROUND) && layerInstance.GridTiles.Length > 0) { //currentLayer = RootContainer.Instance.BackgroundLayer; currentLayer = scene.LayerManager.CreateForegroundLayer(int.Parse(layerName[layerName.Length - 1] + "")); tileSet = Assets.GetTexture(GetMonoGameContentName(layerInstance.TilesetRelPath)); tileGroup = new TileGroup(); } if (layerInstance.Identifier.StartsWith(COLLIDERS)) { currentLayer = null; //public Dictionary<string, dynamic>[] IntGrid { get; set; } foreach (IntGridValueInstance grid in layerInstance.IntGrid) { int y = (int)Math.Floor((decimal)grid.CoordId / layerInstance.CWid); int x = (int)(grid.CoordId - y * layerInstance.CWid); StaticCollider e = new StaticCollider(scene, (new Vector2(x, y))); /*Entity visual = new Entity(scene.LayerManager.EntityLayer, null, new Vector2(x, y) * Config.GRID); * visual.AddComponent(new Sprite(visual, AssetUtil.CreateRectangle(Config.GRID, Color.White))); * visual.Visible = true; * visual.Active = true;*/ switch (grid.V) { case 0: e.AddTag("Collider"); break; case 1: e.AddTag("SlideWall"); break; case 2: //e.AddTag("Platform"); break; case 3: //e.AddTag("Ladder"); //e.BlocksMovement = false; break; case 4: e.AddTag("Platform"); e.AddBlockedDirection(Direction.WEST); break; case 5: e.AddTag("Platform"); e.AddBlockedDirection(Direction.EAST); break; case 6: e.AddTag("Platform"); e.AddBlockedDirection(Direction.NORTH); break; case 7: e.AddTag("Platform"); e.AddBlockedDirection(Direction.SOUTH); break; } } } else { foreach (TileInstance tile in layerInstance.GridTiles) { //Logger.Log("Tile: " + tile.); //if (layerInstance.Identifier.StartsWith(PARALLAX)) { currentLayer = null; continue; } long tileId = tile.T; int atlasGridBaseWidth = (int)layerInstance.GridSize; int padding = 0; int spacing = 0; int gridSize = Config.GRID; int gridTileX = (int)tileId - atlasGridBaseWidth * (int)Math.Floor((decimal)(tileId / atlasGridBaseWidth)); int pixelTileX = padding + gridTileX * (gridSize + spacing); int gridTileY = (int)Math.Floor((decimal)tileId / atlasGridBaseWidth); var pixelTileY = padding + gridTileY * (gridSize + spacing); /*Entity e = new Entity(currentLayer, null, new Vector2(tile.Px[0], tile.Px[1]), tileSet); * e.SourceRectangle = new Rectangle((int)tile.Src[0], (int)tile.Src[1], gridSize, gridSize); * e.Pivot = pivot;*/ Rectangle rect = new Rectangle((int)tile.Src[0], (int)tile.Src[1], gridSize, gridSize); Vector2 pos = new Vector2(tile.Px[0], tile.Px[1]); Color[] data = new Color[gridSize * gridSize]; //tileSet.GetData<Color>(data); tileSet.GetData(0, rect, data, 0, data.Length); if (tile.F != 0) { Texture2D flipped = AssetUtil.CreateRectangle(gridSize, Color.Black); flipped.SetData(data); if (tile.F == 1) { flipped = AssetUtil.FlipTexture(flipped, false, true); } else if (tile.F == 2) { flipped = AssetUtil.FlipTexture(flipped, true, false); } else { flipped = AssetUtil.FlipTexture(flipped, true, true); } flipped.GetData(data); } //public void GetData<T>(int level, int arraySlice, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct; tileGroup.AddColorData(data, pos); //e.Visible = false; //e.Active = false; //e.Pivot = new Vector2(gridSize / 2, gridSize / 2); } if (currentLayer != null) { Entity tile = new Entity(currentLayer, null, new Vector2(0, 0)); tile.SetSprite(tileGroup.GetTexture()); tile.GetComponent <Sprite>().DrawOffset = pivot; //tile.Pivot = pivot; } } } } return(entities); }
public Spikes(AbstractScene scene, Vector2 position, int length, Direction direction) : base(scene.LayerManager.EntityLayer, null, position) { Direction = direction; DrawPriority = 21; AddTag("Spikes"); TileGroup tg = new TileGroup(); Texture2D tileSet = Assets.GetTexture("Spikes"); Color[] data = new Color[Config.GRID * Config.GRID]; Sprite sprite = null; for (int i = 0; i < length; i += Config.GRID) { if (i == 0) { tileSet.GetData(0, new Rectangle(0, 0, Config.GRID, Config.GRID), data, 0, data.Length); } else if (i == length - Config.GRID) { data = new Color[Config.GRID * Config.GRID]; tileSet.GetData(0, new Rectangle(0, 0, Config.GRID, Config.GRID), data, 0, data.Length); } else { data = new Color[Config.GRID * Config.GRID]; tileSet.GetData(0, new Rectangle(0, 0, Config.GRID, Config.GRID), data, 0, data.Length); } for (int j = 0; j < Config.GRID; j += Config.GRID) { tg.AddColorData(data, new Vector2(i, j)); } } if (Direction == Direction.SOUTH || Direction == Direction.NORTH) { sprite = new Sprite(this, tg.GetTexture(), new Rectangle(0, 0, length, Config.GRID), flipVertical: direction == Direction.SOUTH); AddComponent(new BoxCollisionComponent(this, length, Config.GRID)); } else if (Direction == Direction.WEST || Direction == Direction.EAST) { Vector2 offset = Vector2.Zero; float rotation; if (Direction == Direction.EAST) { rotation = MathUtil.DegreesToRad(90); offset = new Vector2(Config.GRID, 0); } else { rotation = MathUtil.DegreesToRad(-90); offset = new Vector2(0, length); } sprite = new Sprite(this, tg.GetTexture(), new Rectangle(0, 0, length, Config.GRID), rotation: rotation, drawOffset: offset); AddComponent(new BoxCollisionComponent(this, Config.GRID, length)); } else { throw new Exception("Wrong spikes orientation"); } AddComponent(sprite); #if DEBUG //(GetCollisionComponent() as AbstractCollisionComponent).DEBUG_DISPLAY_COLLISION = true; //DEBUG_SHOW_PIVOT = true; #endif DrawPriority = 1; }