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 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 void draw(Game game, GameTime gameTime) { if (game.currentWorld.player == null) { return; } int xx = 0; int yy = 0; int itemIndex = 0; Rectangle r; if (currentItem != null) { Game.drawString(currentItem.getDisplayName(false), new Vector2(8, 4), hotBarColor, Game.RENDER_DEPTH_GUI_TEXT); } for (int i = 0; i < 10; i++) { xx = x + (selectedSlot == i ? (cursorItem.isEmpty() ? 4 : 2) : 0) + inventoryMove + 4; yy = y + (i * gridSize); r = new Rectangle(xx + squareBorderSize, yy + squareBorderSize, itemSize, itemSize); Game.drawRectangle(guiTexture, new Rectangle(xx, yy, squareSize, squareSize), new Rectangle(0, 0, 20, 20), hotBarColor, Game.RENDER_DEPTH_GUI_IMAGE_BG); items[itemIndex].draw(game, r, hotBarColor, Game.RENDER_DEPTH_GUI_IMAGE_FG, Game.RENDER_DEPTH_GUI_TEXT); Game.drawString((i == 9 ? 0 : i + 1) + "", new Vector2(xx + 22, yy + 3), hotBarColor, Game.RENDER_DEPTH_GUI_TEXT); 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); r = new Rectangle(xx + squareBorderSize, yy + squareBorderSize, itemSize, itemSize); Game.drawRectangle(guiTexture, new Rectangle(xx, yy, squareSize, squareSize), new Rectangle(0, 0, 20, 20), inventoryColor, Game.RENDER_DEPTH_GUI_IMAGE_BG); items[itemIndex].draw(game, r, inventoryColor, Game.RENDER_DEPTH_GUI_IMAGE_FG, Game.RENDER_DEPTH_GUI_TEXT); itemIndex++; } } float a = inventoryFade * (mouseInCrafting ? 1 : .5f); Color color = new Color(a, a, a, a); Game.drawString(" Crafting", new Vector2(inventoryMove - (gridSize * 4), craftingAreaY - gridSize * .5f), color, Game.RENDER_DEPTH_GUI_TEXT); 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; r = new Rectangle(xx + squareBorderSize, yy + squareBorderSize, itemSize, itemSize); int abs = Math.Abs(i); a = (inventoryFade * (abs == 3 ? .1f : (abs == 2 ? .2f : (abs == 1 ? .5f : 1)))) * (mouseInCrafting ? 1 : .1f); color = new Color(a, a, a, a); Game.drawRectangle(guiTexture, new Rectangle(xx, yy, squareSize, squareSize), new Rectangle(0, 0, 20, 20), color, Game.RENDER_DEPTH_GUI_IMAGE_BG); valaidRecipes[j].result.draw(game, r, color, Game.RENDER_DEPTH_GUI_IMAGE_FG, Game.RENDER_DEPTH_GUI_TEXT); for (int k = 0; k < valaidRecipes[j].ingredients.Length; k++) { xx = x + (i * gridSize) + inventoryMove - (gridSize * 2); yy = y + 4 + ((k + 1) * gridSize) + craftingAreaY; r = new Rectangle(xx + squareBorderSize, yy + squareBorderSize, itemSize, itemSize); valaidRecipes[j].ingredients[k].draw(game, r, color, Game.RENDER_DEPTH_GUI_IMAGE_FG, Game.RENDER_DEPTH_GUI_TEXT); } } } } MouseState mouseState = Mouse.GetState(); xx = mouseState.X + 10; yy = mouseState.Y + 10; r = new Rectangle(xx + 2, yy + 2, itemSize, itemSize); if (inventoryFade > 0 && !cursorItem.isEmpty()) { cursorItem.draw(game, r, inventoryColor, Game.RENDER_DEPTH_GUI_CURSOR_IMAGE_FG, Game.RENDER_DEPTH_GUI_CURSOR_TEXT); Game.drawString(cursorItem.getDisplayName(true), new Vector2(r.X + 24, r.Y), inventoryColor, Game.RENDER_DEPTH_GUI_CURSOR_TEXT); } else if (inventoryFade > 0 && mouseItemSlot != -1 && !items[mouseItemSlot].isEmpty()) { Game.drawString(items[mouseItemSlot].getDisplayName(true), new Vector2(xx + 2, yy + 2), inventoryColor, Game.RENDER_DEPTH_GUI_CURSOR_TEXT); } else if (inventoryFade > 0 && !items[selectedSlot].isEmpty()) { items[selectedSlot].draw(game, r, (mouseTileDistanceFromPlayer <= items[selectedSlot].getItem().reach) ? Color.White : new Color(.1f, .1f, .1f, .1f), Game.RENDER_DEPTH_GUI_CURSOR_IMAGE_FG, Game.RENDER_DEPTH_GUI_TEXT); } currentItem = cursorItem.isEmpty() ? items[selectedSlot] : cursorItem; if ((mouseTileDistanceFromPlayer <= currentItem.getItem().reach)) { currentItem.getItem().drawHover(game, mouseTileX, mouseTileY, currentItem); } Game.drawRectangle(guiTexture, new Rectangle(mouseState.X, mouseState.Y, 16, 16), new Rectangle(0, 20, 16, 16), Game.cursorColor, Game.RENDER_DEPTH_GUI_CURSOR_IMAGE_BG); }