public void RemoveItem(ICollectableItem itemToRemove) { if (itemToRemove == null) { throw new CollectableException("Cannot remove a null item"); } if (_items.Count == 0) { throw new CollectableException("List is empty - Cannot remove item from list"); } Type itemType = itemToRemove.CollectableType; if (itemType != this.CollectableType) { throw new CollectableException($"Invalid type {itemType}, expected type {this.CollectableType}"); } try { int count = _items.Count; _items.Remove(itemToRemove); if (count == _items.Count) { throw new CollectableException("Item was not removed from list"); } } catch (Exception ex) { throw new CollectableException("Unable to remove item", ex); } }
internal static ICollectableItem GetCollectableItemFromJson(string jsonItem, Type collectionType) { ICollectableItem item = null; try { switch (collectionType.Name) { case "BookBase": item = JsonConvert.DeserializeObject <BookItem>(jsonItem); break; case "StampBase": item = JsonConvert.DeserializeObject <StampItem>(jsonItem); break; default: throw new CollectableItemInstanceParseException($"Unable to parse Json. Unsupported collection type={collectionType.Name}"); } } catch (Exception ex) { throw new CollectableItemInstanceParseException($"Unable to parse Json into a collectable item. Type={collectionType.Name}, Json={jsonItem}", ex); } return(item); }
private ICollectionBase GetTestCollection(string collectionName, Type collectableType, int numberOfCollectables, int numberOfItemsPerCollectable = 0) { ICollectableBase collectable = null; ICollectableItem item = null; Type collectionType = collectableType; ICollectionBase testCollection = new HomeCollection(collectionName, collectionType); for (int i = 0; i < numberOfCollectables; i++) { if (collectionType == CollectableBaseFactory.BookType) { collectable = GetTestBookBase(i); } else if (collectionType == CollectableBaseFactory.StampType) { collectable = GetTestStampBase(i); } testCollection.AddToCollection(collectable); for (int j = 0; j < numberOfItemsPerCollectable; j++) { if (collectionType == CollectableBaseFactory.BookType) { item = GetTestBookItem(i); } else if (collectionType == CollectableBaseFactory.StampType) { item = GetTestStampItem(i); } collectable.AddItem(item); } } return(testCollection); }
public static void AddInventory(ICollectableItem item) { bool topInventoryFull = true; for (int i = 0; i < TopInventory.Length; ++i) { if (TopInventory[i] == null) { TopInventory[i] = item; topInventoryFull = false; break; } } if(topInventoryFull) { for (int i = 0; i < Inventory.Length; ++i) { if (Inventory[i] == null) { Inventory[i] = item; break; } } } }
public override void Initialise(IServiceLocator _sl) { base.Initialise(_sl); mBackgroundManager.BlankBackgrounds(); mBackgroundManager.ChangeBackground("Levels/Boardroom"); mBackgroundManager.ChangePosition(0, 2000); if (firstRun == true) { // Create Player // <Entity Type>("Texture", needs input?) createdEntity = mEntityManager.newEntity <Characters.Player.InfirmaryPlayer>("Characters/Player/standR", true); // X, Y coordinates mSceneManager.newEntity(createdEntity, 20, 2260); mLevelEntities.Add(createdEntity.UID, createdEntity); // Exit Door createdEntity = mEntityManager.newEntity <Door>("blank"); var asInterface = createdEntity as IDoor; asInterface.Initialize("L6", true); // X, Y coordinates mSceneManager.newEntity(createdEntity, 20, 2260); //Change coordinates mLevelEntities.Add(createdEntity.UID, createdEntity); //Collectable Object createdEntity = mEntityManager.newEntity <CollectableItem>("Objects/key"); ICollectableItem _item = createdEntity as ICollectableItem; foreach (KeyValuePair <int, IEntity> _keyPair in mEntityManager.AllEntities) { ILockedDoor _door = _keyPair.Value as ILockedDoor; if (_door != null) { _item.SetTarget(_door); break; } } // X, Y coordinates mSceneManager.newEntity(createdEntity, 700, 2360); //Change coordinates mLevelEntities.Add(createdEntity.UID, createdEntity); // Floor createdEntity = mEntityManager.newEntity <StaticObject>("floor"); mSceneManager.newEntity(createdEntity, -40, 2400); //Change coordinates mLevelEntities.Add(createdEntity.UID, createdEntity); //Background sound createdEntity = mEntityManager.newEntity <SoundEntity>("Audio/rain-and-thunder-heart-beat", 0f, true, true, 0.1f); mSceneManager.newEntity(createdEntity, 0, 0); mLevelEntities.Add(createdEntity.UID, createdEntity); //Footsteps createdEntity = mEntityManager.newEntity <SoundEntity>("Audio/footsteps", 3f, true, false, 1f); mSceneManager.newEntity(createdEntity, 0, 0); mLevelEntities.Add(createdEntity.UID, createdEntity); firstRun = false; } }
public void createcollectableitem_valid_type_returns_new_instance_with_type() { foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableItem newItem = CollectableBaseFactory.CreateCollectableItem(collectableType); Assert.AreEqual(collectableType, newItem.CollectableType); } }
public void createcollectableitem_valid_type_returns_new_instance() { foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableItem newItem = CollectableBaseFactory.CreateCollectableItem(collectableType); Assert.IsNotNull(newItem); } }
/// <summary> /// aggiunge l'item alla lista /// </summary> /// <param name="itemToAdd">Item to add.</param> public void AddItem(ICollectableItem itemToAdd) { Items.Add(itemToAdd); ///casting = ItemToAdd è nell'interfaccia e non deriva da MonoBehaviour,il compilatore non sapendolo,foziamo ad utilizzarlo come MonoBehaviour. MonoBehaviour goItem = (MonoBehaviour)itemToAdd as MonoBehaviour; goItem.transform.position = this.transform.position; goItem.transform.SetParent(this.transform); }
public void createandaddcollectableitem_valid_type_returns_new_item_with_type_set() { foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = CollectableBaseFactory.CreateCollectableBase(collectableType); ICollectableItem newItem = CollectableBaseFactory.CreateAndAddCollectableItem(collectable); Assert.AreEqual(collectableType, newItem.CollectableType); } }
public static ICollectableItem CreateAndAddCollectableItem(ICollectableBase collectable) { if (collectable == null) { throw new CollectableException("Collectable cannot be null when adding an instance of it"); } ICollectableItem newItem = CreateCollectableItem(collectable.CollectableType); collectable.AddItem(newItem); return(newItem); }
public void createandaddcollectableitem_valid_type_adds_new_item_to_collectable() { foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = CollectableBaseFactory.CreateCollectableBase(collectableType); ICollectableItem newItem = CollectableBaseFactory.CreateAndAddCollectableItem(collectable); ICollectableItem fromCollectable = collectable.ItemInstances[0]; Assert.AreEqual(newItem, fromCollectable); } }
public void AddItem(ICollectableItem itemToAdd) { if (itemToAdd == null) { throw new CollectableException("Cannot add a null item"); } Type itemType = itemToAdd.CollectableType; if (itemType != this.CollectableType) { throw new CollectableException($"Invalid type {itemType}, expected type {this.CollectableType}"); } _items.Add(itemToAdd); }
public void removeitem_from_existing_list_success() { int N = 3; foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = GetTestBase(collectableType, N); ICollectableItem mockItem = collectable.ItemInstances[N - 1]; collectable.RemoveItem(mockItem); IList <ICollectableItem> list = collectable.ItemInstances; Assert.AreEqual(N - 1, list.Count); } }
public void getcollectableitemfromjson_deserialize_bad_format_throws_exception() { string jsonItem = "invalid JSON"; foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { bool fail = false; try { ICollectableItem newItem = HomeCollectionRepository.GetCollectableItemFromJson(jsonItem, collectableType); } catch (CollectableItemInstanceParseException) { fail = true; Assert.IsTrue(fail, "Expected exception to be thrown when JSON is invalid and cannot be parsed to an object"); } } }
internal static ICollectableBase GetCollectableFromJson(string jsonCollectable, Type collectionType) { ICollectableBase newCollectable = null; try { dynamic collectable = JsonConvert.DeserializeObject(jsonCollectable); var items = collectable["ItemInstances"]; collectable["ItemInstances"] = null; // clear so we can parse it more easily switch (collectionType.Name) { case "BookBase": ValidateBookCollectableFields(collectable); newCollectable = JsonConvert.DeserializeObject <BookBase>(collectable.ToString()); break; case "StampBase": ValidateStampCollectableFields(collectable); newCollectable = JsonConvert.DeserializeObject <StampBase>(collectable.ToString()); break; default: throw new CollectableParseException($"Unable to parse Json. Unsupported collection type={collectionType.Name}"); } if (items != null) { foreach (var item in items) { // add custom try/catch?? ICollectableItem newItem = GetCollectableItemFromJson(item.ToString(), collectionType); newCollectable.AddItem(newItem); } } return(newCollectable); } catch (Exception ex) { throw new CollectableParseException($"Unable to parse Json into a collectable object. Type={collectionType.Name}, Json={jsonCollectable}", ex); } }
public void removeitem_from_existing_list_fails_when_item_is_null() { int N = 3; bool threwError = false; foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = GetTestBase(collectableType, N); ICollectableItem mockItem = null; try { collectable.RemoveItem(mockItem); } catch (CollectableException) { threwError = true; } Assert.IsTrue(threwError); } }
public static void RemoveInventory(ICollectableItem item) { for (int i = 0; i < TopInventory.Length; ++i) if (TopInventory[i] == item) TopInventory[i] = null; }
public void AddItem(ICollectableItem itemToAdd) { _collectableBase.AddItem(itemToAdd); }
public void RemoveItem(ICollectableItem itemToRemove) { _collectableBase.RemoveItem(itemToRemove); }
/// <summary> /// rimuove l'item dalla lista /// </summary> /// <param name="itemToRemove">Item to remove.</param> public void RemoveItem(ICollectableItem itemToRemove) { Items.Remove(itemToRemove); }