public Pipe <T> fromJson(JsonObject jo) { /** * To use this fromJson() function, you need to know a little bit about the JsonObject you * are trying to convert. Namely, you need to know the output type of the newly loaded pipe * type. * * The generic type of this PipFactory represents the generic type of the pipe that is being * loaded. Certain types of Pipe Objects have a required output generic type. For instance, * the PipeSum pipe is a Pipe<int>, and no other generic type will work. * * Use: * 1) Create a PipeFactory<T> of the correct T (where T is the output type of the pipe to be loaded) * 2) Pass the json object into fromJson() funciton. * 3) The returned pipe will be a new object with the correct generic type casted as a Pipe<T> * 4) End user can (if they need to) cast the pipe into the more specific base class if needed */ switch (jo["Type"].AsString) { case "AbstractPipe": // Version 1 Debug.Log("Error: Attempting to create an AbstractExpireable object is not allowed."); break; case "PipeSum": { // Version 1 if (typeof(T) != typeof(int)) { Debug.Log(writeError("int")); } PipeSum result = new PipeSum(jo); return((Pipe <T>)result); } case "PipeAction": { // Version 1 if (typeof(T) != typeof(PMAction)) { Debug.Log(writeError("PMAction")); } PipeAction result = new PipeAction(jo); return((Pipe <T>)result); } default: { string detailedErrMsg = "Error: When attempting to create an Pipe<T> from Json, the provided "; detailedErrMsg += "JsonObject[\"Type\"] was unexpected/ incorrect. Bad type: " + jo["Type"]; Debug.LogError(detailedErrMsg); break; } } string errMsg = "Error, invalid Json[\"Type\"] when attempting to re-create/ load a Pipe<T>."; throw new InvalidLoadType(errMsg); }
private void testEquipablePipe() { Dictionary <string, int> m1Stats = new Dictionary <string, int> { { BattleStats.CURRENT_HEALTH, 10 }, { BattleStats.MAX_HEALTH, 10 }, { BattleStats.SPEED, 0 } }; Dictionary <string, int> m2Stats = new Dictionary <string, int>(m1Stats); PMMob m1 = new PMMob(m1Stats); PMMob m2 = new PMMob(m2Stats); Pipe <int> speedUp = new PipeSum(new ExpireNever <Pipe <int> >(), new Flagable(), 5); speedUp.addFlag(BattleStats.SPEED); IEquipable helmOfSpeed = new EquipablePipe(EquipSlots.HELM, speedUp); m1.equip(helmOfSpeed); IEquipable e = m1.peekEquipmentSlot(EquipSlots.HELM); if (e.targetSlot() == EquipSlots.HELM) { Debug.Log("Nocab test 2.1 passed"); } else { Debug.Log("Nocab test 2.1 fail"); } if (m1.getStat(BattleStats.SPEED) == 5) { Debug.Log("Nocab test 2.2 passed"); } else { Debug.Log("Nocab test 2.2 failed"); } m1.unequip(EquipSlots.HELM); if (m1.getStat(BattleStats.SPEED) == 0) { Debug.Log("Nocab test 2.3 passed"); } else { Debug.Log("Nocab test 2.3 failed: Equipable pipe did not reset value after unequip"); } }
public static IEquipable makeSword() { HashSet <IEquipable> behaviors = new HashSet <IEquipable>(); Flagable f = new Flagable(); f.addFlag(BattleStats.ATTACK); Pipe <int> attackup = new PipeSum(new ExpireNever <Pipe <int> >(), f, 2); IEquipable addAttachBehavior = new EquipablePipe(EquipSlots.MAIN_HAND, attackup); PMAction slashAction = new ActionAttack(3); IEquipable slashBehavior = new ActionEquipable(EquipSlots.MAIN_HAND, slashAction); behaviors.Add(addAttachBehavior); behaviors.Add(slashBehavior); MultiBehaviorEquipable sword = new MultiBehaviorEquipable(EquipSlots.MAIN_HAND, behaviors); return(sword); }
public void test() { // 1 make a Pipe IExpire <Pipe <int> > expire = new ExpireNever <Pipe <int> >(); IFlagable flags = new Flagable(); Pipe <int> p = new PipeSum(expire, flags, 10); // 2 Pump the pipe assert(p.pump(5) == 15, "Pump produced the wrong value during pump operation!"); // 3 Jsonify JsonObject jo = p.toJson(); assert(jo["Type"] == "PipeSum"); string directory = "TempDirectory"; string fileName = "TestFile"; // NOTE: PersistantFilePath = C:\Users\Arthur\AppData\LocalLow\DefaultCompany\StandAloneCon(plex)versation // 4 write to disk JsonOrigamist jsonOrigamist = new JsonOrigamist(directory, fileName); jsonOrigamist.add(jo); jsonOrigamist.writeToDisk(); // 4.1 Simulate power down CentralRegistry.deregister(p.getNocabName()); // 5 read from disk JsonArray ja = jsonOrigamist.readFromDisk(); JsonObject joFromDisk = ja[0]; assert(joFromDisk.ToString() == jo.ToString()); // 6 Load the new pipe Pipe <int> pFromDisk = new PipeSum(joFromDisk); // 7 pump the new pipe assert(p.pump(5) == pFromDisk.pump(5)); }
private void testMultiPipe() { Dictionary <string, int> m1Stats = new Dictionary <string, int> { { BattleStats.CURRENT_HEALTH, 10 }, { BattleStats.MAX_HEALTH, 10 }, { BattleStats.SPEED, 0 } }; Dictionary <string, int> m2Stats = new Dictionary <string, int>(m1Stats); PMMob m1 = new PMMob(m1Stats); PMMob m2 = new PMMob(m2Stats); Pipe <int> speedUp = new PipeSum(new ExpireNever <Pipe <int> >(), new Flagable(), 5); speedUp.addFlag(BattleStats.SPEED); IEquipable helmOfSpeed = new EquipablePipe(EquipSlots.HELM, speedUp); PMAction swordSlash = new ActionAttack(5); IEquipable attackSword = new ActionEquipable(EquipSlots.MAIN_HAND, swordSlash); HashSet <IEquipable> subEquips = new HashSet <IEquipable>() { helmOfSpeed, attackSword }; IEquipable multiItem = new MultiBehaviorEquipable(EquipSlots.HELM, subEquips); m1.equip(multiItem); IEquipable e = m1.peekEquipmentSlot(EquipSlots.HELM); if (e.targetSlot() == EquipSlots.HELM) { Debug.Log("Nocab test 3.1 passed"); } else { Debug.Log("Nocab test 3.1 fail"); } PMBattleController bc = new PMBattleController(m1, m2); PMAction a = m1.getNextAction(bc); a.activate(m2); if (m2.getStat(BattleStats.CURRENT_HEALTH) == 5) { Debug.Log("Nocab test 3.2 passed"); } else { Debug.Log("Nocab test 3.2 failed"); } if (m1.getStat(BattleStats.SPEED) == 5) { Debug.Log("Nocab test 3.3 passed"); } else { Debug.Log("Nocab test 3.3 failed"); } m1.unequip(EquipSlots.HELM); if (m1.getStat(BattleStats.SPEED) == 0) { Debug.Log("Nocab test 3.4 passed"); } else { Debug.Log("Nocab test 3.4 failed"); } if (m2.getStat(BattleStats.CURRENT_HEALTH) == 5) { Debug.Log("Nocab test 3.5 passed"); } else { Debug.Log("Nocab test 3.5 failed"); } }