public static Victim CreateVictimOfClass(VictimConfig vicConfig, Vector2 position, MegaTile megaTile, Level level) { Victim returnVictim = null; switch (vicConfig.VictimClass) { case VictimClass.BASE: returnVictim = new VictimBase(megaTile, vicConfig.GroundAnim.Copy(), vicConfig.StumpAnim.Copy(), vicConfig.ExplodeAnim.Copy(), vicConfig.TornadoAnim.Copy(), vicConfig.VictimSound, position); break; case VictimClass.CAR: returnVictim = new VictimCar(megaTile, vicConfig.GroundAnim.Copy(), vicConfig.StumpAnim.Copy(), vicConfig.ExplodeAnim.Copy(), vicConfig.TornadoAnim.Copy(), vicConfig.VictimSound, position); break; case VictimClass.HOUSE: returnVictim = new VictimHouse(megaTile, vicConfig.GroundAnim.Copy(), vicConfig.StumpAnim.Copy(), vicConfig.ExplodeAnim.Copy(), vicConfig.TornadoAnim.Copy(), vicConfig.VictimSound, position); break; case VictimClass.SKYSCRAPER: returnVictim = new VictimSkyscraper(megaTile, vicConfig.GroundAnim.Copy(), vicConfig.StumpAnim.Copy(), vicConfig.ExplodeAnim.Copy(), vicConfig.TornadoAnim.Copy(), vicConfig.VictimSound, position); break; case VictimClass.BOSS: returnVictim = new VictimBoss(megaTile, vicConfig.GroundAnim.Copy(), vicConfig.StumpAnim.Copy(), vicConfig.ExplodeAnim.Copy(), vicConfig.TornadoAnim.Copy(), vicConfig.VictimSound, position); break; case VictimClass.MAN: returnVictim = new VictimMan(megaTile, vicConfig.GroundAnim.Copy(), vicConfig.StumpAnim.Copy(), vicConfig.ExplodeAnim.Copy(), vicConfig.TornadoAnim.Copy(), vicConfig.VictimSound, position); break; case VictimClass.PLANE: if (level == null) { throw new Exception("Level cannot be null for construction of a plane"); } returnVictim = new VictimPlane(megaTile, vicConfig.GroundAnim.Copy(), vicConfig.StumpAnim.Copy(), vicConfig.ExplodeAnim.Copy(), vicConfig.TornadoAnim.Copy(), vicConfig.VictimSound, level); break; case VictimClass.UFO: if (level == null) { throw new Exception("Level cannot be null for construction of a UFO"); } returnVictim = new VictimUFO(megaTile, vicConfig.GroundAnim.Copy(), vicConfig.StumpAnim.Copy(), vicConfig.ExplodeAnim.Copy(), vicConfig.TornadoAnim.Copy(), vicConfig.VictimSound, level); break; default: throw new ApplicationException("Attempted to create a victim of a class that doesn't exist!"); break; } //Set other properties for victims returnVictim.Mass= vicConfig.Mass; returnVictim.GroundHealth = vicConfig.GroundHealth; returnVictim.TornadoHealth = vicConfig.TorontoHealth; returnVictim.speed = vicConfig.VictimSpeed; Victim.CurrentVicitimID = Victim.CurrentVicitimID + 1; returnVictim.VictimID = Victim.CurrentVicitimID; return returnVictim; }
public static Victim CreateVictimOfClass(VictimConfig vicConfig, Vector2 position, MegaTile megaTile) { return CreateVictimOfClass(vicConfig, position, megaTile, null); }
//Try to randomly place a victim of a specific type on this mega tile private bool tryAddingVictim(VictimConfig vc) { rand.Next(); //Pick random location to try adding the victim int maxWidth = bounds.Left + bounds.Width - vc.GroundAnim.FrameWidth; if (maxWidth < bounds.Left) maxWidth = bounds.Left; int maxHeight = bounds.Top + bounds.Height - vc.GroundAnim.FrameHeight; if (maxHeight < bounds.Top) maxHeight = bounds.Top; Vector2 randomLoc = new Vector2(rand.Next(bounds.Left, maxWidth), rand.Next(bounds.Top, maxHeight)); //check for collisions with existing objects //If a collision is found, return failure foreach (Victim v in victims) { if(v.Bounds.Intersects(new Rectangle2D(randomLoc.X, randomLoc.Y, vc.GroundAnim.FrameWidth, vc.GroundAnim.FrameHeight))){ return false; } } //Put the victim in a random location victims.Add(VictimFactory.CreateVictimOfClass(vc, randomLoc, this)); //Check for a boss victim and notify level if one is created if (vc.VictimClass == VictimClass.BOSS) { level.NumberBossesAlive += 1; } return true; }
public static Victim CreateVictimOfClass(VictimConfig vicConfig, Level level) { return CreateVictimOfClass(vicConfig, new Vector2(0,0), null, level); }
//Read in all attributes and sub-elements for a Victim type //Reader should be positioned on a VICTIMTYPE element private static void ReadVictimType(XmlTextReader reader, ContentManager content) { string tempString; //Move to first attribute for this victim if (reader.MoveToFirstAttribute()) { //Create a new victim type VictimConfig tempVictimType = new VictimConfig(); //Loop through all attributes for Victim type do{ try { //Get the attribute name tempString = reader.Name.ToUpper(); if (tempString == "MASS") { tempVictimType.Mass = float.Parse(reader.Value); } else if(tempString == "GROUNDHEALTH"){ tempVictimType.GroundHealth = float.Parse(reader.Value); } else if(tempString == "TORNADOHEALTH"){ tempVictimType.TorontoHealth = float.Parse(reader.Value); } else if (tempString == "NAME") { tempVictimType.VictimName = reader.Value; } else if (tempString == "SOUNDEFFECT") { tempVictimType.VictimSound = reader.Value; } else if (tempString == "CLASS") { tempVictimType.VictimClass = (VictimClass)Enum.Parse(typeof(VictimClass), reader.Value.ToUpper()); } else if (tempString == "SPEED") { tempVictimType.VictimSpeed = float.Parse(reader.Value); } //Handle undefined level attributes else { throw new ApplicationException("Invalid Victim type attribute '" + tempString + "' on line " + reader.LineNumber); } } catch (Exception e) { throw new ApplicationException("Error parsing Victim type attribute on line " + reader.LineNumber + ": " + e.Message); } } while (reader.MoveToNextAttribute()); //Look for nested elements while (!(reader.NodeType == XmlNodeType.EndElement && reader.Name.ToUpper() == "VICTIMTYPE")) { reader.Read(); reader.MoveToContent(); string animTypeName; if (reader.NodeType == XmlNodeType.Element) { tempString = reader.Name.ToUpper(); //Look for valid Victim type sub-elements if (tempString == "ANIMATION") { reader.MoveToFirstAttribute(); animTypeName = reader.Name.ToUpper(); //Make sure type attribute is defined first if (animTypeName == "TYPE") { animTypeName = reader.Value.ToUpper(); } else { throw new ApplicationException("Type attribute must be provided first for animation on line " + reader.LineNumber); } //Check which type of animation this is defining if (animTypeName == "GROUND") { tempVictimType.GroundAnim = ReadAnimation(reader, content); } else if (animTypeName == "STUMP") { tempVictimType.StumpAnim = ReadAnimation(reader, content); } else if (animTypeName == "EXPLODE") { tempVictimType.ExplodeAnim = ReadAnimation(reader, content); } else if (animTypeName == "TORNADO") { tempVictimType.TornadoAnim = ReadAnimation(reader, content); } else { throw new ApplicationException("Invalid Victim Animation type '" + animTypeName + "' on line " + reader.LineNumber); } } else { throw new ApplicationException("Invalid Victim Type Sub-Element '" + tempString + "' on line " + reader.LineNumber); } } } //Add the Victim type to the list victimTypes.Add(tempVictimType); } }