public void Update(KeyboardState kstate, GameTime gameTime, Camera camera) { // Update the storage inventory for timedItems List <int> removeExpiredItem = new List <int>(); List <InventoryItem> addTimedItem = new List <InventoryItem>(); foreach (var item in inventory) { if (item == null) { continue; } // for spoiled food if (item is ISpoiles) { item.msSpoilTime += gameTime.ElapsedGameTime.Milliseconds; Tuple <int, string> spoilDetails = ItemMappings.SpoilMappings[item.bbKey]; if (item.msSpoilTime > spoilDetails.Item1) { InventoryItem spoiled = ItemUtility.CreateInventoryItem(spoilDetails.Item2, item.teamType, regionKey, location, _content, _graphics); item.msSpoilTime = 0; if (item.amountStacked > 1) { item.amountStacked -= 1; } else { removeExpiredItem.Add(inventory.IndexOf(item)); } addTimedItem.Add(spoiled); } } } foreach (var remove in removeExpiredItem) { inventory[remove] = null; } foreach (var item in addTimedItem) { if (!AddInventoryItem(item)) { item.location = new Vector2(GetBoundingBox().Center.ToVector2().X, GetBoundingBox().Center.ToVector2().Y + 40); item.onGround = true; if (inInteriorId != Guid.Empty) // add drops to interior { BoundingBoxLocations.interiorMap[inInteriorId].interiorObjectsToAdd.Add(item); } else // add drops to world { ItemUtility.ItemsToUpdate.Add(item); } } } if (canOpenStorage && !storageOpen && kstate.IsKeyDown(Keys.O)) { storageOpen = true; } if (storageOpen) { if (kstate.IsKeyDown(Keys.Escape) || !canOpenStorage) { storageOpen = false; } } if (canPickUp) { // pick up the item if (playerNearItem != null && kstate.IsKeyDown(Keys.P)) { // TODO: might need to switch different chest types here? InventoryItem bci = new BaseChestItem(playerNearItem.teamType, regionKey, location, _content, _graphics); if (playerNearItem.AddInventoryItem(bci)) { bci.placeableVersion = this; bci.inInventory = true; bci.onGround = false; bci.stackable = false; bci.amountStacked = 1; remove = true; canPickUp = false; } } // there is a timer for how long you have to pick up item once you have the required number of hits msSinceStartPickupTimer += gameTime.ElapsedGameTime.Milliseconds; if (msSinceStartPickupTimer > msPickupTimer) { msSinceStartPickupTimer = 0; canPickUp = false; nTimesHit = 0; } } if (inWater) { currRowFrame = 1; msThisFrame += gameTime.ElapsedGameTime.Milliseconds; if (msThisFrame > msPerFrame) { currColumnFrame++; if (currColumnFrame >= nColumns) { currColumnFrame = 0; } msThisFrame = 0; } } else { currRowFrame = 0; } inWater = true; canOpenStorage = false; playerNearItem = null; }
public void Update(KeyboardState kstate, GameTime gameTime, Camera cam) { if (menuOpen) { timeRClicked += gameTime.ElapsedGameTime.Milliseconds; timeLClicked += gameTime.ElapsedGameTime.Milliseconds; cursorPos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); Rectangle cursorRect = new Rectangle((int)cursorPos.X, (int)cursorPos.Y, cursor.Width, cursor.Height); selectedIndex = -1; int i = 0; foreach (var slot in slotLocations.Values) { if (slot.Intersects(cursorRect)) { selectedIndex = i; // TODO: have this start timer for crafting (do some sort of animation) and then create item when timer is done. OR have a sound effect if (Mouse.GetState().LeftButton == ButtonState.Pressed && !(timeLClicked < 400)) // click timer { bool canCreateItem = false; if (craftableItemsChecked.Count > 0) { var item = craftableItemsChecked[selectedIndex]; // get item from menu icons if (emptySpotAvailable || (item.stackable && playerInvCanStackItem[item.bbKey])) { canCreateItem = true; } else { // check to see if the ingredients used will free up a spot foreach (KeyValuePair <string, int> ingredient in ingredientsAmountDifferences[item.bbKey]) { if (ingredient.Value <= 0) { canCreateItem = true; break; } } } InventoryItem itemCreated = null; if (canCreateItem) { // remove ingredients from player inv foreach (var itm in inventoryOfPlayer.inventory) { if (itm == null) { continue; } foreach (var ing in Mappings.ItemMappings.CraftingRecipes[craftObj.GetCraftSet()][item.bbKey]) { if (itm.bbKey.Equals(ing.Key)) { itm.amountStacked -= ing.Value; } } } // create inv item and add to players inv itemCreated = ItemUtility.CreateInventoryItem(item.bbKey, inventoryOfPlayer.teamType, inventoryOfPlayer.regionKey, item.location, _content, _graphics); craftObj.GetCraftingQueue().Enqueue(itemCreated); /*if (inventoryOfPlayer.AddInventoryItem(itemCreated)) * { * itemCreated.inInventory = true; * itemCreated.onGround = false; * }*/ timeLClicked = 0; } } } } i++; } } menuOpen = false; }
public void Update(KeyboardState kstate, GameTime gameTime, Camera camera) { timeSinceLastTurnFrame += gameTime.ElapsedGameTime.Milliseconds; timeSinceLastWalkFrame += gameTime.ElapsedGameTime.Milliseconds; // check inventory BoundingBoxLocations.treasureLocationsList.Clear(); List <int> removeSolvedOrSpoiled = new List <int>(); List <InventoryItem> addTimedItem = new List <InventoryItem>(); foreach (var item in inventory) { if (item == null) { continue; } // for spoiled food if (item is ISpoiles) { item.msSpoilTime += gameTime.ElapsedGameTime.Milliseconds; Tuple <int, string> spoilDetails = ItemMappings.SpoilMappings[item.bbKey]; if (item.msSpoilTime > spoilDetails.Item1) { InventoryItem spoiled = ItemUtility.CreateInventoryItem(spoilDetails.Item2, teamType, regionKey, location, _content, _graphics); item.msSpoilTime = 0; if (item.amountStacked > 1) { item.amountStacked -= 1; } else { removeSolvedOrSpoiled.Add(inventory.IndexOf(item)); } addTimedItem.Add(spoiled); } } // for treasure maps if (item.bbKey.Equals("treasureMapItem")) { TreasureMap map = (TreasureMap)item; if (map.solved) { removeSolvedOrSpoiled.Add(inventory.IndexOf(item)); } else { BoundingBoxLocations.treasureLocationsList.Add(map); } } } foreach (var remove in removeSolvedOrSpoiled) { inventory[remove] = null; } foreach (var item in addTimedItem) { if (!AddInventoryItem(item)) { item.location = new Vector2(GetBoundingBox().Center.ToVector2().X, GetBoundingBox().Center.ToVector2().Y + 40); item.onGround = true; if (inInteriorId != Guid.Empty) // add drops to interior { BoundingBoxLocations.interiorMap[inInteriorId].interiorObjectsToAdd.Add(item); } else // add drops to world { ItemUtility.ItemsToUpdate.Add(item); } } } if (showHealthBar) { timeShowingHealthBar += gameTime.ElapsedGameTime.Milliseconds; } if (timeShowingHealthBar > GameOptions.millisecondsToShowHealthBar) { showHealthBar = false; timeShowingHealthBar = 0; } if (colliding) { moving = false; } // update any water wake wake.EmitterLocation = location; wake.Update(currentSpeed, (swimming && moving)); colliding = false; swimming = true; if (timeSinceLastTurnFrame > millisecondsPerTurnFrame) { // toggle inventory (use turn frame speed here for convenience) if (kstate.IsKeyDown(Keys.Tab)) { if (showInventory) { showInventory = false; } else { showInventory = true; } } if (!onShip || playerInInterior != null) { moving = true; // player direction if (kstate.IsKeyDown(Keys.W)) { currRowFrame = 3; directionalFrame = 0; if (kstate.IsKeyDown(Keys.A)) { directionalFrame = 1; } else if (kstate.IsKeyDown(Keys.D)) { directionalFrame = 7; } } else if (kstate.IsKeyDown(Keys.S)) { currRowFrame = 0; directionalFrame = 4; if (kstate.IsKeyDown(Keys.A)) { directionalFrame = 3; } else if (kstate.IsKeyDown(Keys.D)) { directionalFrame = 5; } } else if (kstate.IsKeyDown(Keys.A)) { currRowFrame = 2; directionalFrame = 2; } else if (kstate.IsKeyDown(Keys.D)) { currRowFrame = 1; directionalFrame = 6; } else { moving = false; } inHand.currRowFrame = currRowFrame; } else { moving = false; } timeSinceLastTurnFrame -= millisecondsPerTurnFrame; } // combat if (!onShip || playerInInterior != null) { inHand.Update(kstate, gameTime, camera); if (playerInInterior != null) { inHand.inInteriorId = playerInInterior.interiorId; } else { inHand.inInteriorId = Guid.Empty; } } if (Mouse.GetState().LeftButton == ButtonState.Pressed && (!onShip || playerInInterior != null) && !showInventory) { inCombat = true; inHand.inCombat = true; currColumnFrame = 8; if (inHand is IRanged) { currColumnFrame = 9; // better frame for "holding" a ranged weapon //load ammo if (inHand.ammoLoaded == null) { foreach (var item in inventory) { if (item != null && item.GetType() == inHand.ammoItemType) { if (item.amountStacked > 0) { inHand.LoadAmmo(item); } else { inventory[inventory.IndexOf(item)] = null; } break; } } } } } else if (inCombat) { if (timeSinceSwordSwing > millisecondsCombatSwing) { currColumnFrame++; inHand.location = location; inHand.nextFrame = true; if (currColumnFrame == nColumns) { inCombat = false; inHand.inCombat = false; currColumnFrame = 0; } timeSinceSwordSwing = 0; } timeSinceSwordSwing += gameTime.ElapsedGameTime.Milliseconds; } inHand.location = location; inHand.SetBoundingBox(); // hop on ship if (nearShip && kstate.IsKeyDown(Keys.X) && !onShip && playerOnShip != null && timeSinceExitShipStart < 2000) { location = playerOnShip.GetBoundingBox().Center.ToVector2(); onShip = true; playerOnShip.playerAboard = true; playerOnShip.shipSail.playerAboard = true; // can't control non player ships (until taken over) if (playerOnShip.teamType != TeamType.Player) { playerInInterior = playerOnShip.shipInterior; } } // exit ship else if (kstate.IsKeyDown(Keys.X) && onShip) { timeSinceExitShipStart += gameTime.ElapsedGameTime.Milliseconds; if (timeSinceExitShipStart > 2000) { onShip = false; if (playerInInterior != null) { playerInInterior.showingInterior = false; playerInInterior.interiorObjects.Remove(this); playerInInterior = null; inInteriorId = Guid.Empty; } playerOnShip.playerAboard = false; playerOnShip.shipSail.playerAboard = false; location.X = playerOnShip.GetBoundingBox().Center.ToVector2().X - playerOnShip.GetBoundingBox().Width / 2 - 20; location.Y = playerOnShip.GetBoundingBox().Center.ToVector2().Y; playerOnShip = null; } } else { timeSinceExitShipStart = 0; } nearShip = false; // player moves with ship when controlling if (onShip && playerInInterior == null) { location.X = playerOnShip.GetBoundingBox().Center.ToVector2().X; location.Y = playerOnShip.GetBoundingBox().Center.ToVector2().Y; } if (playerOnShip != null && playerOnShip.sinking) { onShip = false; playerInInterior = null; playerOnShip = null; inInteriorId = Guid.Empty; } else if (moving && !inCombat) { // walking animation if (timeSinceLastWalkFrame > millisecondsPerWalkFrame) { currColumnFrame++; if (currColumnFrame == 7) // stop before combat frames { currColumnFrame = 0; } timeSinceLastWalkFrame = 0; } currentSpeed = new Vector2(PlayerMovementVectorMappings.PlayerDirectionVectorValues[directionalFrame].Item1, PlayerMovementVectorMappings.PlayerDirectionVectorValues[directionalFrame].Item2); // actual "regular" movement location += currentSpeed; } else { if (!inCombat) { currColumnFrame = 0; } } // burying storage int?removeChestIndex = null; if (canBury && kstate.IsKeyDown(Keys.B)) { timeSincePressedBury += gameTime.ElapsedGameTime.Milliseconds; if (timeSincePressedBury > 1000) { // remove first chest foreach (var item in inventory) { if (item == null) { continue; } if (item.placeableVersion != null && item.placeableVersion is IStorage) { removeChestIndex = inventory.IndexOf(item); break; } } // create map if (removeChestIndex != null) { Storage toBury = (Storage)inventory[(int)removeChestIndex].placeableVersion; TreasureMapItem mapToAdd = new TreasureMapItem(toBury, teamType, regionKey, location, _content, _graphics); mapToAdd.digTileLoc = buryTile.location; mapToAdd.treasureInRegion = buryTile.regionKey; mapToAdd.inInventory = false; mapToAdd.remove = false; mapToAdd.onGround = true; buryTile.currColumnFrame = 0; ItemUtility.ItemsToUpdate.Add(mapToAdd); inventory[(int)removeChestIndex] = null; } timeSincePressedBury = 0; } } buryTile = null; canBury = false; // entering/toggling interior if (kstate.IsKeyDown(Keys.I)) { msToggleInterior += gameTime.ElapsedGameTime.Milliseconds; if (msToggleInterior > 1000) { if (playerInInterior != null) { playerInInterior.showingInterior = false; playerInInterior.interiorObjects.Remove(this); inInteriorId = Guid.Empty; playerInInterior = null; location = entranceLoc; // player exits where they entered if (playerOnShip != null) { playerOnShip.playerInInterior = false; playerOnShip.shipSail.playerInInterior = false; } } else { // enter an interior if (onShip) // the interior of the ship the player is in { playerInInterior = playerOnShip.shipInterior; playerOnShip.playerInInterior = true; playerOnShip.shipSail.playerInInterior = true; inInteriorId = playerInInterior.interiorId; } else if (playerNearStructure != null) { entranceLoc = location; playerInInterior = playerNearStructure.structureInterior; inInteriorId = playerInInterior.interiorId; } } msToggleInterior = 0; } } playerNearStructure = null; }