/// <summary> /// Function initializes the catapult instance and loads the animations from XML definition sheet /// </summary> public void Initialize() { // Define initial state of the catapult IsActive = true; AnimationRunning = false; currentState = CatapultState.Idle; stallUpdateCycles = 0; // Load multiple animations form XML definition XDocument doc = XDocument.Load("Content/Textures/Catapults/AnimationsDef.xml"); XName name = XName.Get("Definition"); var definitions = doc.Document.Descendants(name); // Loop over all definitions in XML foreach (var animationDefinition in definitions) { bool?toLoad = null; bool val; if (bool.TryParse(animationDefinition.Attribute("IsAI").Value, out val)) { toLoad = val; } // Check if the animation definition need to be loaded for current catapult if (toLoad == isLeftSide || null == toLoad) { // Get a name of the animation string animatonAlias = animationDefinition.Attribute("Alias").Value; Texture2D texture = contentManager.Load <Texture2D>(animationDefinition.Attribute("SheetName").Value); // Get the frame size (width & height) Point frameSize = new Point(); frameSize.X = int.Parse(animationDefinition.Attribute("FrameWidth").Value); frameSize.Y = int.Parse(animationDefinition.Attribute("FrameHeight").Value); // Get the frames sheet dimensions Point sheetSize = new Point(); sheetSize.X = int.Parse(animationDefinition.Attribute("SheetColumns").Value); sheetSize.Y = int.Parse(animationDefinition.Attribute("SheetRows").Value); // If definition has a "SplitFrame" - means that other animation should start here - load it if (null != animationDefinition.Attribute("SplitFrame")) { splitFrames.Add(animatonAlias, int.Parse(animationDefinition.Attribute("SplitFrame").Value)); } // Defing animation speed TimeSpan frameInterval = TimeSpan.FromSeconds((float)1 / int.Parse(animationDefinition.Attribute("Speed").Value)); Animation animation = new Animation(texture, frameSize, sheetSize); // If definition has an offset defined - means that it should be rendered relatively // to some element/other animation - load it if (null != animationDefinition.Attribute("OffsetX") && null != animationDefinition.Attribute("OffsetY")) { animation.Offset = new Vector2(int.Parse(animationDefinition.Attribute("OffsetX").Value), int.Parse(animationDefinition.Attribute("OffsetY").Value)); } animations.Add(animatonAlias, animation); } } // Load the textures idleTexture = contentManager.Load <Texture2D>(idleTextureName); activeProjectiles = new List <Projectile>(MaxActiveProjectiles); activeProjectilesCopy = new List <Projectile>(MaxActiveProjectiles); destroyedProjectiles = new List <Projectile>(MaxActiveProjectiles); normalProjectile = new Projectile(contentManager, spriteBatch, activeProjectiles, "Textures/Ammo/rock_ammo", ProjectileStartPosition, animations["Fire"].FrameSize.Y, isLeftSide, Gravity); normalProjectile.Initialize(); splitProjectile = new SplitProjectile(contentManager, spriteBatch, activeProjectiles, "Textures/Ammo/split_ammo", ProjectileStartPosition, animations["Fire"].FrameSize.Y, isLeftSide, Gravity); splitProjectile.Initialize(); crate = new SupplyCrate(contentManager, spriteBatch, "Textures/Crate/box", Position + new Vector2(animations["Fire"].FrameSize.X / 2, 0), isLeftSide); crate.Initialize(); // Initialize randomizer random = new Random(); }
/// <summary> /// Function initializes the catapult instance and loads the animations from XML definition sheet /// </summary> public void Initialize() { // Define initial state of the catapult IsActive = true; AnimationRunning = false; currentState = CatapultState.Idle; stallUpdateCycles = 0; // Load multiple animations form XML definition XDocument doc = XDocument.Load("Content/Textures/Catapults/AnimationsDef.xml"); XName name = XName.Get("Definition"); var definitions = doc.Document.Descendants(name); // Loop over all definitions in XML foreach (var animationDefinition in definitions) { bool? toLoad = null; bool val; if (bool.TryParse(animationDefinition.Attribute("IsAI").Value, out val)) toLoad = val; // Check if the animation definition need to be loaded for current catapult if (toLoad == isLeftSide || null == toLoad) { // Get a name of the animation string animatonAlias = animationDefinition.Attribute("Alias").Value; Texture2D texture = contentManager.Load<Texture2D>(animationDefinition.Attribute("SheetName").Value); // Get the frame size (width & height) Point frameSize = new Point(); frameSize.X = int.Parse(animationDefinition.Attribute("FrameWidth").Value); frameSize.Y = int.Parse(animationDefinition.Attribute("FrameHeight").Value); // Get the frames sheet dimensions Point sheetSize = new Point(); sheetSize.X = int.Parse(animationDefinition.Attribute("SheetColumns").Value); sheetSize.Y = int.Parse(animationDefinition.Attribute("SheetRows").Value); // If definition has a "SplitFrame" - means that other animation should start here - load it if (null != animationDefinition.Attribute("SplitFrame")) splitFrames.Add(animatonAlias, int.Parse(animationDefinition.Attribute("SplitFrame").Value)); // Defing animation speed TimeSpan frameInterval = TimeSpan.FromSeconds((float)1 / int.Parse(animationDefinition.Attribute("Speed").Value)); Animation animation = new Animation(texture, frameSize, sheetSize); // If definition has an offset defined - means that it should be rendered relatively // to some element/other animation - load it if (null != animationDefinition.Attribute("OffsetX") && null != animationDefinition.Attribute("OffsetY")) { animation.Offset = new Vector2(int.Parse(animationDefinition.Attribute("OffsetX").Value), int.Parse(animationDefinition.Attribute("OffsetY").Value)); } animations.Add(animatonAlias, animation); } } // Load the textures idleTexture = contentManager.Load<Texture2D>(idleTextureName); activeProjectiles = new List<Projectile>(MaxActiveProjectiles); activeProjectilesCopy = new List<Projectile>(MaxActiveProjectiles); destroyedProjectiles = new List<Projectile>(MaxActiveProjectiles); normalProjectile = new Projectile(contentManager, spriteBatch, activeProjectiles, "Textures/Ammo/rock_ammo", ProjectileStartPosition, animations["Fire"].FrameSize.Y, isLeftSide, Gravity); normalProjectile.Initialize(); splitProjectile = new SplitProjectile(contentManager, spriteBatch, activeProjectiles, "Textures/Ammo/split_ammo", ProjectileStartPosition, animations["Fire"].FrameSize.Y, isLeftSide, Gravity); splitProjectile.Initialize(); crate = new SupplyCrate(contentManager, spriteBatch, "Textures/Crate/box", Position + new Vector2(animations["Fire"].FrameSize.X / 2, 0), isLeftSide); crate.Initialize(); // Initialize randomizer random = new Random(); }