public override void drawHover(Game game, int mouseTileX, int mouseTileY, ItemStack currentItem) { if (!Tile.getTileObject(currentItem.getData()).drawHover(game, mouseTileX, mouseTileY, currentItem)) { Tile tile = Tile.getTileObject(currentItem.getData()); bool tileGoesHere = tile.canBePlacedHere(game.currentWorld, mouseTileX, mouseTileY, World.TileDepth.tile); bool wallGoesHere = tile.canBePlacedHere(game.currentWorld, mouseTileX, mouseTileY, World.TileDepth.wall); if (tileGoesHere || wallGoesHere) { Game.drawRectangle(Tile.TileSheet, new Rectangle((mouseTileX * World.tileSizeInPixels) - game.currentWorld.viewOffset.X, (mouseTileY * World.tileSizeInPixels) - game.currentWorld.viewOffset.Y, World.tileSizeInPixels, World.tileSizeInPixels), Tile.getTileObject(currentItem.getData()).getTextureInfo(mouseTileX, mouseTileY, game.currentWorld, World.TileDepth.tile).rectangle, new Color(.5f, .5f, .5f, .5f), Game.RENDER_DEPTH_HOVER); } } }
public override ItemStack leftClick(Game game, ItemStack stack, int xTile, int yTile, int distance) { if (Tile.getTileObject(stack.getData()) != null && distance <= reach) { if (game.currentWorld.getTileIndex(xTile, yTile, World.TileDepth.tile) == Tile.TileAir.index) { if (Tile.getTileObject(stack.getData()).canBePlacedHere(game.currentWorld, xTile, yTile, World.TileDepth.tile)) { if (!collision(game.currentWorld, xTile, yTile, Tile.getTileObject(stack.getData()).isSolid(game.currentWorld, xTile, yTile))) { if (game.currentWorld.setTileWithUpdate(xTile, yTile, stack.getData(), World.TileDepth.tile)) { game.currentWorld.getTileObjectNoCheck(xTile, yTile, World.TileDepth.tile).justPlaced(game.currentWorld, xTile, yTile, World.TileDepth.tile); stack.setCount(stack.getCount() - 1); } } } } } return stack; }
public override ItemStack rightClick(Game game, ItemStack stack, int xTile, int yTile, int distance) { if (Tile.getTileObject(stack.getData()) != null && distance <= reach) { if (game.currentWorld.getTileIndex(xTile, yTile, World.TileDepth.wall) == Tile.TileAir.index) { if (Tile.getTileObject(stack.getData()).canBeWall) { if (Tile.getTileObject(stack.getData()).canBePlacedHere(game.currentWorld, xTile, yTile, World.TileDepth.wall)) { if (game.currentWorld.setTileWithUpdate(xTile, yTile, stack.getData(), World.TileDepth.wall)) { game.currentWorld.getTileObjectNoCheck(xTile, yTile, World.TileDepth.wall).justPlaced(game.currentWorld, xTile, yTile, World.TileDepth.wall); stack.setCount(stack.getCount() - 1); } } } } } return(stack); }
private bool inventoryContainsStack(int minSlot, int maxSlot, ItemStack stack) { for (int i = minSlot; i <= maxSlot; i++) { if (items[i].getItem().index == stack.getItem().index) { if (items[i].getData() == stack.getData()) { if (items[i].getCount() >= stack.getCount()) { return(true); } } } } return(false); }
public override void update(Game game, World world) { bool gravityOn = true; if (pickUpDelay <= 0) { float d = Vector2.Distance(world.player.position, position); Rectangle pr = new Rectangle((int)world.player.position.X, (int)world.player.position.Y, (int)world.player.size.X, (int)world.player.size.Y); Rectangle r = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y); foreach (Entity e in world.EntityList) { if (e is EntityItem) { EntityItem ei = ((EntityItem)e); if (e.uniqueID < uniqueID) { if (ei.stack.getItem().index == stack.getItem().index&& ei.stack.getData() == stack.getData()) { if (Vector2.Distance(ei.position, position) < (World.tileSizeInPixels)) { ei.stack.setCount(stack.setCount(stack.getCount() + ei.stack.getCount())); } } } } } if (pr.Intersects(r)) { stack = game.inventory.pickUp(stack); if (stack.isEmpty()) { if (!playedSoundEffectRecently) { SoundEffectPlayer.playSoundWithRandomPitch(SoundEffectPlayer.SoundPop); } } pickUpDelay = maxPickupDelay; } else if (d < (6 * World.tileSizeInPixels)) { position += ((world.player.position - position) / d) * 4; gravityOn = false; } } if (stack.isEmpty()) { remove(game, world); } if (!collision(world, speed.X, 0)) { position.X += speed.X; } else { speed.X = 0; } if (!collision(world, 0, speed.Y)) { position.Y += speed.Y; } else { speed.Y = 0; } if (collision(world, 0, 1)) { } else if (gravityOn) { speed.Y += gravityAcc; } pickUpDelay--; }
public override InputState update(Game game, InputState inputState) { mouseInCrafting = mouseInCraftingArea(inputState); if (game.currentWorld.player == null) { return(inputState); } #region Scrolling and Fading int xx = 0; int yy = 0; int scrollDif = inputState.getScrollWheelValue() - lastScrollValue; if (scrollDif != 0) { if (mouseInCrafting && inventoryFadingIn) { if (scrollDif > 0) { scrollDif -= 1; craftingScroll -= 1; } if (scrollDif < 0) { scrollDif += 1; craftingScroll += 1; } while (craftingScroll < 0) { craftingScroll++; } while (craftingScroll >= valaidRecipes.Count()) { craftingScroll--; } } else { if (scrollDif > 0) { scrollDif -= 1; selectedSlot -= 1; } if (scrollDif < 0) { scrollDif += 1; selectedSlot += 1; } while (selectedSlot >= 10) { selectedSlot -= 10; } while (selectedSlot < 0) { selectedSlot += 10; } hotBarFadingIn = true; } } if (hotBarFadingIn) { if (hotBarFade < 2f) { hotBarFade += .05f; } else { hotBarFadingIn = false; } } else { if (hotBarFade > .2f) { hotBarFade -= .01f; } } if (inputState.pressed(Game.KEY_INV)) { inventoryFadingIn = !inventoryFadingIn; } if (inventoryFadingIn) { if (inventoryFade < 1f) { inventoryFade += .05f; } } else { if (inventoryFade > 0f) { inventoryFade -= .05f; } else { dropItem(game, cursorItem); cursorItem = new ItemStack(Item.itemEmpty); } } inventoryMove = (int)(inventoryFade * ((gridSize * 3) + gridSize + 2)); if (hotBarFade < 0) { hotBarFade = 0; } if (inventoryFade < 0) { inventoryFade = 0; } float f = Math.Max(hotBarFade, inventoryFade); hotBarColor = new Color(f, f, f, f); inventoryColor = new Color(inventoryFade, inventoryFade, inventoryFade, inventoryFade); lastScrollValue = inputState.getScrollWheelValue(); if (inputState.pressed(Game.KEY_D1)) { selectedSlot = 0; } else if (inputState.pressed(Game.KEY_D2)) { selectedSlot = 1; } else if (inputState.pressed(Game.KEY_D3)) { selectedSlot = 2; } else if (inputState.pressed(Game.KEY_D4)) { selectedSlot = 3; } else if (inputState.pressed(Game.KEY_D5)) { selectedSlot = 4; } else if (inputState.pressed(Game.KEY_D6)) { selectedSlot = 5; } else if (inputState.pressed(Game.KEY_D7)) { selectedSlot = 6; } else if (inputState.pressed(Game.KEY_D8)) { selectedSlot = 7; } else if (inputState.pressed(Game.KEY_D9)) { selectedSlot = 8; } else if (inputState.pressed(Game.KEY_D0)) { selectedSlot = 9; } #endregion mouseItemSlot = -1; mouseCraftSlot = -1; int itemIndex = 0; for (int i = 0; i < 10; i++) { xx = x + (selectedSlot == i ? 4 : 0) + inventoryMove + 4; yy = y + (i * gridSize); if (new Rectangle(xx, yy, gridSize, gridSize).Contains(inputState.mouseX(), inputState.mouseY())) { mouseItemSlot = itemIndex; } itemIndex++; } if (inventoryFade > 0) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 4; j++) { xx = x + (j * gridSize) + inventoryMove - (gridSize * 4); yy = y + (i * gridSize); if (new Rectangle(xx, yy, gridSize, gridSize).Contains(inputState.mouseX(), inputState.mouseY())) { mouseItemSlot = itemIndex; } itemIndex++; } } for (int i = -2; i <= 2; i++) { int j = i + craftingScroll; if (j >= 0 && j < valaidRecipes.Count()) { xx = x + (i * gridSize) + inventoryMove - (gridSize * 2); yy = y + craftingAreaY; if (new Rectangle(xx, yy, gridSize, gridSize).Contains(inputState.mouseX(), inputState.mouseY())) { mouseCraftSlot = j; } } } } bool isCursor = !cursorItem.isEmpty(); ItemStack currentItem = isCursor ? cursorItem : items[selectedSlot]; if (game.currentWorld != null) { mouseTileX = (inputState.mouseX() + game.currentWorld.viewOffset.X) / World.tileSizeInPixels; mouseTileY = (inputState.mouseY() + game.currentWorld.viewOffset.Y) / World.tileSizeInPixels; mouseTileDistanceFromPlayer = (int)Vector2.Distance(new Vector2(mouseTileX * World.tileSizeInPixels, mouseTileY * World.tileSizeInPixels), game.currentWorld.player.position + (game.currentWorld.player.size / 2)); } if (mouseItemSlot == -1 && mouseCraftSlot == -1) { if (inputState.downMouse(InputState.Left)) { setCurrentItem(currentItem.getItem().leftClick(game, currentItem, mouseTileX, mouseTileY, mouseTileDistanceFromPlayer), isCursor); } if (inputState.downMouse(InputState.Right)) { setCurrentItem(currentItem.getItem().rightClick(game, currentItem, mouseTileX, mouseTileY, mouseTileDistanceFromPlayer), isCursor); } if (inputState.pressed(Game.KEY_USE)) { ItemStack useItem = currentItem; Tile tileWall = game.currentWorld.getTileObject(mouseTileX, mouseTileY, World.TileDepth.wall); Tile tileTile = game.currentWorld.getTileObject(mouseTileX, mouseTileY, World.TileDepth.tile); if (tileWall.index != Tile.TileAir.index || tileTile.index != Tile.TileAir.index) { useItem = (tileTile.index == Tile.TileAir.index ? tileWall : tileTile).use(game, currentItem, mouseTileX, mouseTileY, mouseTileDistanceFromPlayer, tileTile.index == Tile.TileAir.index?World.TileDepth.wall:World.TileDepth.tile); } if (useItem != null) { setCurrentItem(useItem); } } } currentItem.getItem().updateAfterClick(game, currentItem, mouseTileX, mouseTileY, mouseTileDistanceFromPlayer); bool leftClick = false; bool rightClick = false; int speed1Time = 40; int speed2Time = 200; int speed3Time = 400; if (inputState.downMouse(InputState.Left)) { if (leftClickTimer == 0) { leftClick = true; } else if (leftClickTimer > speed1Time) { if (leftClickTimer > speed3Time) { leftClick = true; } else if (leftClickTimer > speed2Time) { if (leftClickTimer % 2 == 0) { leftClick = true; } } else if (leftClickTimer % 10 == 0) { leftClick = true; } } leftClickTimer++; } else { leftClickTimer = 0; } if (inputState.downMouse(InputState.Right)) { if (rightClickTimer == 0) { rightClick = true; } else if (rightClickTimer > speed1Time) { if (rightClickTimer > speed3Time) { rightClick = true; } else if (rightClickTimer > speed2Time) { if (rightClickTimer % 2 == 0) { rightClick = true; } } else if (rightClickTimer % 10 == 0) { rightClick = true; } } rightClickTimer++; } else { rightClickTimer = 0; } if (inventoryFade == 0) { if (mouseItemSlot >= 0 && mouseItemSlot < 10) { hotBarFadingIn = true; if (leftClick) { selectedSlot = mouseItemSlot; } } } else if (!(mouseItemSlot == -1 && mouseCraftSlot == -1)) { if (mouseItemSlot == -1 && mouseCraftSlot == craftingScroll) { if (leftClick) { if (inputState.getKeyboardState().IsKeyDown(Game.KEY_INV_MOVE_ITEM_TO_OTHER)) { removeItemStacks(0, 49, valaidRecipes[mouseCraftSlot].ingredients); pickUp(valaidRecipes[mouseCraftSlot].result.clone()); } else { if (cursorItem.sameItem(valaidRecipes[mouseCraftSlot].result)) { if (cursorItem.getCount() + valaidRecipes[mouseCraftSlot].result.getCount() <= cursorItem.getItem().maxStack) { cursorItem.setCount(cursorItem.getCount() + valaidRecipes[mouseCraftSlot].result.getCount()); removeItemStacks(0, 49, valaidRecipes[mouseCraftSlot].ingredients); } } else if (cursorItem.isEmpty()) { cursorItem = valaidRecipes[mouseCraftSlot].result.clone(); removeItemStacks(0, 49, valaidRecipes[mouseCraftSlot].ingredients); } } updateValaidRecipes(); } } else if (mouseItemSlot == -1 && (mouseCraftSlot >= 0)) { if (leftClick) { craftingScroll = mouseCraftSlot; } } else { if (leftClick) { if (cursorItem.sameItem(items[mouseItemSlot])) { if (items[mouseItemSlot].getCount() < cursorItem.getItem().maxStack&& cursorItem.getCount() > 0) { cursorItem.setCount(items[mouseItemSlot].setCount(items[mouseItemSlot].getCount() + cursorItem.getCount())); } else { ItemStack c = cursorItem.clone(); cursorItem = items[mouseItemSlot].clone(); items[mouseItemSlot] = c.clone(); } } else { ItemStack c = cursorItem.clone(); cursorItem = items[mouseItemSlot].clone(); items[mouseItemSlot] = c.clone(); } updateValaidRecipes(); } if (rightClick) { if (cursorItem.isEmpty() && items[mouseItemSlot].isEmpty()) { } else if (cursorItem.isEmpty()) { double c = items[mouseItemSlot].getCount() / 2.0; Game.updateMessage += c; cursorItem = items[mouseItemSlot].clone(); cursorItem.setCount((int)Math.Ceiling(c)); items[mouseItemSlot].setCount((int)Math.Floor(c)); } else if (items[mouseItemSlot].isEmpty()) { items[mouseItemSlot] = cursorItem.clone(); items[mouseItemSlot].setCount(1); cursorItem.setCount(cursorItem.getCount() - 1); } else { if (cursorItem.sameItem(items[mouseItemSlot])) { if (!items[mouseItemSlot].isMax()) { items[mouseItemSlot].setCount(items[mouseItemSlot].getCount() + 1); cursorItem.setCount(cursorItem.getCount() - 1); } } else { ItemStack c = cursorItem.clone(); cursorItem = items[mouseItemSlot].clone(); items[mouseItemSlot] = c.clone(); } } updateValaidRecipes(); } } } if (inputState.pressed(Game.KEY_DROP)) { game.currentWorld.EntityAddingList.Add(new EntityItem(game.currentWorld.player.position, new ItemStack(currentItem.getItem(), 1, currentItem.getData()), 60)); currentItem.setCount(currentItem.getCount() - 1); setCurrentItem(currentItem); } tick++; inputState.eatMouse(); inputState.eatKey(Game.KEY_INV); inputState.eatKey(Game.KEY_INV_MOVE_ITEM_TO_OTHER); inputState.eatKey(Game.KEY_DROP); inputState.eatKey(Game.KEY_D0); inputState.eatKey(Game.KEY_D1); inputState.eatKey(Game.KEY_D2); inputState.eatKey(Game.KEY_D3); inputState.eatKey(Game.KEY_D4); inputState.eatKey(Game.KEY_D5); inputState.eatKey(Game.KEY_D6); inputState.eatKey(Game.KEY_D7); inputState.eatKey(Game.KEY_D8); inputState.eatKey(Game.KEY_D9); return(inputState); }
private bool inventoryContainsStack(int minSlot, int maxSlot, ItemStack stack) { for (int i = minSlot; i <= maxSlot; i++) { if (items[i].getItem().index == stack.getItem().index) { if (items[i].getData() == stack.getData()) { if (items[i].getCount() >= stack.getCount()) { return true; } } } } return false; }