void CInventory::Update() { // add an atom to the beaker when selected from the inventory int touchX = 0; int touchY = 0; if (g_Game.getGameState() == GS_Playing && s3ePointerGetState(S3E_POINTER_BUTTON_SELECT) & S3E_POINTER_STATE_PRESSED) { touchX = s3ePointerGetX(); touchY = s3ePointerGetY(); for (int i = 0; i < inventoryCount; i++) { if (atomObjs[i]->isTouched(touchX, touchY)) { if (g_Beaker.getAtomSymbol() != NULL && !strcmp(atomObjs[i]->getSymbol(), g_Beaker.getAtomSymbol())) { // Early-terminate because the same inventory item was clicked more than once & can be ignored break; } if (atomCount[i] == 1) { // hide atom to indicate zero are left atomObjs[i]->setVisible(false); // HASAN - new to display the inventory count image atomCountImages[i]->setVisible(false); } // create new CAtom object to be held in the beaker, so inventory atom can remain unchanged CAtom* newAtom = new CAtom(); newAtom->Init(atomObjs[i]->getSymbol()); CAtom* prevAtom = g_Beaker.setAtom(newAtom); if (prevAtom != NULL) { // replacing previously selected atom in the beaker, so moving previous one back into this inventory // 1 - find previous atom slot in inventory // 2 - if previously zero count, then set back to visible // 3 - increment the atom counter for (int j = 0; j < inventoryCount; j++) { if (!strcmp(prevAtom->getSymbol(), atomObjs[j]->getSymbol())) { // match found if (atomCount[j] <= 0) { atomObjs[j]->setVisible(true); atomCountImages[j]->setVisible(true); } atomCount[j]++; // This was a clone of the object in the inventory, need to delete it (happens inside the sprite manager) when it's being replaced and not shot into the environment g_Game.getSpriteManager()->removeSprite(prevAtom); break; } } } atomCount[i]--; } } } }
void CInventory::AddAtoms(char* i_strAtomSymbol, int i_nCount) { bool bMatchFound = false; int matchIndex = -1; if (inventoryCount == 0) { // HASAN - debug //s3eDebugOutputString("Adding inventory sprite to sprite manager."); // add the inventory to the sprite manager when the first atom is added g_Game.getSpriteManager()->addSprite(inventory_sprite); } if (inventoryCount > 0) { for (int i = 0; i < MAX_COUNT; i++) { if (!strcmp(atoms[i], i_strAtomSymbol)) { bMatchFound = true; matchIndex = i; break; } } } if (!bMatchFound) { // match strcpy(atoms[inventoryCount], i_strAtomSymbol); atomCount[inventoryCount] += i_nCount; CAtom* atom = new CAtom(); atom->Init(i_strAtomSymbol); // horizontal center, same as inventory graphic int posX = (Iw2DGetSurfaceWidth() / 2) - (IMAGE_SIZE_WIDTH / 2); int posY = Iw2DGetSurfaceHeight() - (IMAGE_SIZE_HEIGHT / 2); // offset inventory horizontally to fit in the container posX = posX + (10 + (64 / 2)); // NOTE: 64 = atom size, 10 = border + spacing if (inventoryCount > 0) { posX = posX + (inventoryCount * (64 + 15)); // NOTE: 64 = atom size, 15 = spacing + border + spacing } atom->setPosAngScale(posX, posY, 0, IW_GEOM_ONE); atom->setVelocity(0,0); // HASAN - new to create/show the inventory count images CIw2DImage* inventoryCountImage = Iw2DCreateImageResource("inventory_number"); CSprite* inventoryCountSprite = new CSprite(); inventoryCountSprite->setImage(inventoryCountImage, "inventory_number"); // NOTE: image width & height = 25 & font width & height = 30 inventoryCountSprite->setPosition(posX + 13 + (30 / 2), posY + (30 / 2) + 3); inventoryCountSprite->setDestSize(30, 30); // make larger than source to fit behind the text properly g_Game.getSpriteManager()->addSprite(inventoryCountSprite); atomCountImages[inventoryCount] = inventoryCountSprite; atomObjs[inventoryCount] = atom; inventoryCount++; } else { atomCount[matchIndex] += i_nCount; } }