void CMap::CheckForItem(int x, int y) { // Go through all of the items to check if we have a match for(int i = 0; i < (int)m_vItems.size(); i++) { CItem *pItem = &m_vItems[i]; // Check if the player position is the same as the current item if(x == pItem->GetIndex().X && y == pItem->GetIndex().Y) { // Create a new item and then copy the current item's data to the new item CItem newItem; memcpy(&newItem, pItem, sizeof(CItem)); // Check if this item has a special key with it g_ActionKeys.HandleKey(pItem->GetActionKey()); // If our inventory isn't full, let's add it, otherwise do nothing if(g_Player.GetInventorySize() < kMaxItems) { // Let's add the new item and then delete it from the map g_Player.AddItem(newItem); m_vItems.erase(m_vItems.begin() + i); } return; } } }
void CMap::CheckForItem(int x, int y) { // Go through all of the items to check if we have a match for(int i = 0; i < (int)m_vItems.size(); i++) { CItem *pItem = &m_vItems[i]; // Check if the player position is the same as the current item if(x == pItem->GetIndex().X && y == pItem->GetIndex().Y) { // Create a new item and then copy the current item's data to the new item CItem newItem; memcpy(&newItem, pItem, sizeof(CItem)); //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // After we pick up an item we want to check if there is something special // that needs to be done. We use this in the game for when we pick up the // treasure in Valkar's palace. The game is over at that point. // We just pass the action key into our action key object and handle it. g_ActionKeys.HandleKey(pItem->GetActionKey()); //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** //////////////////// // If our inventory isn't full, let's add it, otherwise do nothing if(g_Player.GetInventorySize() < kMaxItems) { // Let's add the new item and then delete it from the map g_Player.AddItem(newItem); m_vItems.erase(m_vItems.begin() + i); } return; } } }
void CMap::DeleteTakenItems() { // There are going to be items on the ground that we want to pick up, but // we don't want these items to still be there when we come back to that map. // To make sure they are gone when we come back, we assign an action key to // each item that we put on the map. Then, when we load a map and HandleMaps() // is called, this function is called to delete taken items. What we do is just // go through the items on the map and check to see if their action key is // already set to true (or 1 - we use numbers instead of bools for more functionality). // If the item's action key is already set, then we delete it from the map. // Whenever we pick up an item it's action key is set. When creating your maps // you need to be sure and give an item an action key, or else this won't work. // Go through backwards for all of the items and see if they were taken already for(int i = (int)m_vItems.size()-1; i >= 0 ; i--) { // Get the current item CItem *pItem = &m_vItems[i]; // Check if the item's action key was already set (meaning taken), if so, erase it if(g_ActionKeys.GetActionKey(pItem->GetActionKey()) != 0) m_vItems.erase(m_vItems.begin() + i); } }