/// <summary> /// Creates insect /// </summary> /// <param name="insect">Species</param> /// <param name="id">ID</param> /// <param name="insectSpecific">Insect characteristics</param> /// <param name="speciesSpecific">Species characteristics</param> /// <returns>New Insect</returns> private static Animal CreateInsect(InsectType insect, int id, string insectSpecific, string speciesSpecific) { switch (insect) { case InsectType.Butterfly: return(new Butterfly(id, insectSpecific, speciesSpecific)); case InsectType.Bee: return(new Bee(id, insectSpecific, speciesSpecific)); case InsectType.Ant: return(new Ant(id, insectSpecific, speciesSpecific)); default: return(null); } }
public Insect GetInsectData(InsectType insectType) { if (_createdInsects.ContainsKey(insectType)) { if (showMessages) Debug.Log("Reusing " + insectType.ToString()); return _createdInsects[insectType] as Insect; } else { if (showMessages) Debug.LogWarning("Creating " + insectType.ToString()); Insect insect = new Insect(insectType); _createdInsects.Add(insectType, insect); return insect; } }
public Insect(InsectType insectType) { this.insectType = insectType; antennae = 2; legs = 6; switch (insectType) { case InsectType.fly: size = 2; eyes = 1000; toughness = 1; maxHealth = 100; hunger = 100; canFly = true; canBurrow = false; break; case InsectType.beetle: size = 4; eyes = 2; toughness = 10; maxHealth = 500; hunger = 10; canFly = true; canBurrow = true; break; case InsectType.ant: size = 1; eyes = 2; toughness = 10; maxHealth = 100; hunger = 50; canFly = false; canBurrow = true; break; default: throw new ArgumentException(); } }
public Insect(InsectType insectType) { this.insectType = insectType; antennae = 2; legs = 6; switch (insectType) { case InsectType.fly: size = 2; eyes = 1000; toughness = 1; maxHealth = 100; hunger = 100; canFly = true; canBurrow = false; break; case InsectType.beetle: size = 4; eyes = 2; toughness = 10; maxHealth = 500; hunger = 10; canFly = true; canBurrow = true; break; case InsectType.ant: size = 1; eyes = 2; toughness = 10; maxHealth = 100; hunger = 50; canFly = false; canBurrow = true; break; default: throw new ArgumentException(); } }
public Insect GetInsectData(InsectType insectType) { if (_createdInsects.ContainsKey(insectType)) { if (showMessages) { Debug.Log("Reusing " + insectType.ToString()); } return(_createdInsects[insectType] as Insect); } else { if (showMessages) { Debug.LogWarning("Creating " + insectType.ToString()); } Insect insect = new Insect(insectType); _createdInsects.Add(insectType, insect); return(insect); } }
void Start() { // Illustrate that factory reuses already made insects InsectFlyweightFactory insectFactory = new InsectFlyweightFactory(); insectFactory.showMessages = true; Debug.LogError("Testing reuse of insects"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { insectFactory.GetInsectData((InsectType)i); } } Debug.LogError("Testing the pattern in action"); // An example of how this might work to our advantage. for (int i = 0; i < 3; i++) { Insect challenger = insectFactory.GetInsectData((InsectType)i); Debug.Log("Insect to test: " + challenger.insectType.ToString()); List <InsectType> defeatedInsect = new List <InsectType>(); // At what point can challenger be stronger than other insects at varying health. for (int j = 0; j < challenger.maxHealth; j++) { for (int k = 0; k < 3; k++) { if (k == i) // Don't attack insects of the same type. { continue; } InsectType insectType = (InsectType)k; if (defeatedInsect.Contains(insectType)) { continue; } Insect defender = insectFactory.GetInsectData(insectType); for (int m = defender.maxHealth; m >= 0; m--) { int challengerStrength = challenger.GetStrength(j); int defenderStrength = defender.GetStrength(m); if (challengerStrength > defenderStrength) { defeatedInsect.Add(insectType); Debug.LogWarning(challenger.insectType.ToString() + " at health: " + j.ToString() + " defeated " + defender.insectType.ToString() + " at full health of " + defender.maxHealth.ToString()); break; } } } } // Check if anyone wasn't defeated. for (int h = 0; h < 3; h++) { if (h == i) // Insects of the same type won't be checked since we didn't even try to attack them. { continue; } InsectType insectType = (InsectType)h; if (!defeatedInsect.Contains(insectType)) { Debug.LogError(challenger.insectType.ToString() + " could not defeat " + insectType.ToString()); } } } }