public Interior(string itk, Sprite interiorFor, ContentManager content, GraphicsDevice graphics) { /*interiorWasLoaded = loadState; * if (!interiorWasLoaded) * interiorId = Guid.NewGuid();*/ _graphics = graphics; _content = content; interiorTypeKey = itk; interiorMap = new List <TilePiece>(); interiorTiles = new HashSet <TilePiece>(); interiorObjects = new HashSet <Sprite>(); interiorObjectsToAdd = new HashSet <Sprite>(); interiorForObj = interiorFor; // load the interiorMap tileset _interiorMapData = JObject.Parse(File.ReadAllText(@"C:\Users\GMON\source\repos\GustoGame\GustoGame\Content\" + interiorTypeKey + "Interior.json")); // set the interior map int multX = (int)_interiorMapData["multX"]; int multY = (int)_interiorMapData["multY"]; width = multX * GameOptions.PrefferedBackBufferWidth; height = multY * GameOptions.PrefferedBackBufferHeight; cols = width / (GameOptions.tileWidth * 2); rows = height / (GameOptions.tileHeight * 2); if (interiorForObj != null) { startDrawPoint = new Vector2(interiorForObj.location.X - (width / 2), interiorForObj.location.Y - (height / 2)); } else { startDrawPoint = Vector2.Zero; } Vector2 drawPoint = startDrawPoint; int index = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { TilePiece tile = null; JObject tileDetails = _interiorMapData["data"][index.ToString()].Value <JObject>(); List <Sprite> groundObjects = null; // ground object if ((string)tileDetails["sittingObject"] != "null") { groundObjects = GetGroundObjects((string)tileDetails["sittingObject"], drawPoint, content, graphics); foreach (var groundObject in groundObjects) { groundObject.SetTileDesignRow(RandomEvents.rand.Next(0, groundObject.nRows)); //interiorGroundObjects.Add(groundObject); interiorObjects.Add(groundObject); } } // set interiorPiece piece switch (tileDetails["terrainPiece"].ToString()) { case "sd1": tile = new ShipDeckTile(index, null, groundObjects, drawPoint, "GameGusto", content, graphics, "shipDeckTile"); break; case "sd1w": tile = new ShipDeckTileWall(index, null, groundObjects, drawPoint, "GameGusto", content, graphics, "shipDeckTileWall"); break; case "si1": tile = new ShipInteriorTile(index, null, groundObjects, drawPoint, "GameGusto", content, graphics, "shipInteriorTile"); break; case "si1w": tile = new ShipInteriorTileWall(index, null, groundObjects, drawPoint, "GameGusto", content, graphics, "shipInteriorTileWall"); break; case "d1": tile = new DirtTile(index, null, groundObjects, drawPoint, "GameGusto", content, graphics, "dirtTile"); break; case "cvs1w": tile = new CanvasTileWall(index, null, groundObjects, drawPoint, "GameGusto", content, graphics, "canvasTileWall"); break; } if (tile != null) { //tile.inInteriorId = interiorId; tile.SetTileDesignRow(RandomEvents.rand.Next(0, tile.nRows)); interiorTiles.Add(tile); } interiorMap.Add(tile); index++; } } // Go through again to set all columns to correct frame for walls int index2 = 0; foreach (var tile in interiorMap) { if (tile == null) { index2++; continue; } if (tile.wallPiece) { // left if (interiorMap[index2 + 1] != null && interiorMap[index2 - 1] == null && !interiorMap[index2 + 1].wallPiece) { tile.currColumnFrame = 3; // left wall } // right if (interiorMap[index2 - 1] != null && interiorMap[index2 + 1] == null && !interiorMap[index2 - 1].wallPiece) { tile.currColumnFrame = 1; // right wall } // bottom wall if (interiorMap[index2 - cols] != null && interiorMap[index2 + cols] == null && !interiorMap[index2 - cols].wallPiece) // check above { tile.currColumnFrame = 2; // bottom wall } // top wall (looks the best) if (interiorMap[index2 + cols] != null && interiorMap[index2 - cols] == null && !interiorMap[index2 + cols].wallPiece) // check below { tile.currColumnFrame = 0; } } index2++; } }
public void Draw(SpriteBatch sb, Camera cam, RenderTarget2D interiorScene) { // Draw the tileset Vector2 minCorner = new Vector2(cam.Position.X - (GameOptions.PrefferedBackBufferWidth / 2), cam.Position.Y - (GameOptions.PrefferedBackBufferHeight / 2)); Vector2 maxCorner = new Vector2(cam.Position.X + (GameOptions.PrefferedBackBufferWidth / 2), cam.Position.Y + (GameOptions.PrefferedBackBufferHeight / 2)); // setup drawing for interior on backbuffer _graphics.SetRenderTarget(interiorScene); _graphics.Clear(Color.Black); startDrawPoint = new Vector2(interiorForObj.location.X - (width / 2), interiorForObj.location.Y - (height / 2)); Vector2 drawPoint = startDrawPoint; interiorTiles.Clear(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { TilePiece tile = interiorMap[((cols) * i) + j]; if (tile == null) { drawPoint.X += GameOptions.tileWidth * 2; continue; } tile.location = drawPoint; var loc = tile.location; tile.location += speed; tile.inInteriorId = interiorId; // TODO: need to move setting of InteriorId to constructor, but this screws up serialization interiorTiles.Add(tile); if (tile.groundObjects != null) { foreach (var groundObject in tile.groundObjects) { groundObject.location = loc; } } // draw if in viewporit if ((loc.X >= minCorner.X && loc.X <= maxCorner.X) && (loc.Y >= minCorner.Y && loc.Y <= maxCorner.Y)) { tile.Draw(sb, cam); } drawPoint.X += GameOptions.tileWidth * 2; } drawPoint.Y += GameOptions.tileHeight * 2; drawPoint.X = startDrawPoint.X; } // reset these menu trackers showCraftingMenu = false; showStorageMenu = false; craftObj = null; invStorage = null; List <Sprite> drawOrder = interiorObjects.ToList(); drawOrder.Sort((a, b) => a.GetBoundingBox().Bottom.CompareTo(b.GetBoundingBox().Bottom)); // Draw any interior objs foreach (var obj in drawOrder) { // npcs always have random loc set when entering interior, other objects are randomly set initially, unless coming from a save if ((!tilesSet && !(obj is IPlayer) && !interiorWasLoaded) || (obj is INPC && !showingInterior)) { obj.location = RandomInteriorTile().location; } // special draw for player if (obj is IPlayer) { DrawUtility.DrawPlayer(sb, cam, (PiratePlayer)obj); continue; } obj.Draw(sb, cam); if (obj is IVulnerable) { IVulnerable v = (IVulnerable)obj; v.DrawHealthBar(sb, cam); } if (obj is IInventoryItem) { InventoryItem item = (InventoryItem)obj; if (!item.inInventory) { item.DrawPickUp(sb, cam); } } if (obj is IPlaceable) { IPlaceable placeObj = (IPlaceable)obj; placeObj.DrawCanPickUp(sb, cam); } if (obj is ICraftingObject) { ICraftingObject tcraftObj = (ICraftingObject)obj; tcraftObj.DrawCanCraft(sb, cam); if (tcraftObj.GetShowMenu()) { showCraftingMenu = true; craftObj = tcraftObj; } } if (obj is IStorage) { Storage storage = (Storage)obj; storage.DrawOpenStorage(sb, cam); if (storage.storageOpen) { showStorageMenu = true; invStorage = storage; } } } tilesSet = true; showingInterior = true; }