/// <summary> /// Load content. /// </summary> /// <param name="content">The content manager to use.</param> public void LoadContent(ContentManager content) { // Clear all sprites. _Sprites = new SpriteManager(); // Front. Sprite front = _Sprites.Add(new Sprite(_Sprites, "Front")); front.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Front[1]"); front.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Front[2]"); front.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Front[3]"); // Back. Sprite back = _Sprites.Add(new Sprite(_Sprites, "Back")); back.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Back[0]"); back.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Back[1]"); back.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Back[2]"); back.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Back[3]"); // Right. Sprite right = _Sprites.Add(new Sprite(_Sprites, "Right")); right.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Right[0]"); right.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Right[1]"); right.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Right[2]"); // Left. Sprite left = _Sprites.Add(new Sprite(_Sprites, "Left")); left.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Left[0]"); left.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Left[1]"); left.AddFrame(@"Characters\ZombieGuy\ZombieGuy1_Left[2]"); // Only make one sprite visible. back.Visibility = Visibility.Invisible; right.Visibility = Visibility.Invisible; left.Visibility = Visibility.Invisible; //Set the frame-rate. front.TimePerFrame = .1f; back.TimePerFrame = .1f; right.TimePerFrame = .1f; left.TimePerFrame = .1f; // Current sprite is facing up. _CurrentSprite = front; // Load all sprites' content. _Sprites.LoadContent(content); //Update the frames' origin. front.Frames.ForEach(item => item.Origin = new Vector2(item.Width / 2, item.Height / 2)); back.Frames.ForEach(item => item.Origin = new Vector2(item.Width / 2, item.Height / 2)); right.Frames.ForEach(item => item.Origin = new Vector2(item.Width / 2, item.Height / 2)); left.Frames.ForEach(item => item.Origin = new Vector2(item.Width / 2, item.Height / 2)); // Set the shape of the body. _Body.Shape.Width = 15; _Body.Shape.Height = 15; _Body.Shape.Depth = _Sprites[0][_Sprites[0].CurrentFrameIndex].Height / 3; // Update the sprites' position offset. front.PositionOffset = new Vector2(0, -front[front.CurrentFrameIndex].Origin.Y + (_Body.Shape.Height / 2)); back.PositionOffset = new Vector2(0, -back[back.CurrentFrameIndex].Origin.Y + (_Body.Shape.Height / 2)); right.PositionOffset = new Vector2(0, -right[right.CurrentFrameIndex].Origin.Y + (_Body.Shape.Height / 2)); left.PositionOffset = new Vector2(0, -left[left.CurrentFrameIndex].Origin.Y + (_Body.Shape.Height / 2)); }
/// <summary> /// Clone this sprite manager and all its sprites. /// </summary> /// <returns>A clone of this sprite manager.</returns> public SpriteManager Clone() { //Create the clone. SpriteManager manager = new SpriteManager(); //Clone the properties. manager.ContentManager = _ContentManager; //Clone the sprites. foreach (Sprite sprite in _Sprites) { //Create the cloned sprite. Sprite sClone = new Sprite(manager, sprite.Name); //Clone the properties. sClone.Position = sprite.Position; sClone.TimePerFrame = sprite.TimePerFrame; sClone.Scale = sprite.Scale; sClone.Depth = sprite.Depth; sClone.Rotation = sprite.Rotation; sClone.PositionOffset = sprite.PositionOffset; sClone.OrbitOffset = sprite.OrbitOffset; sClone.RotationOffset = sprite.RotationOffset; sClone.Tag = sprite.Tag; sClone.Transparence = sprite.Transparence; sClone.Visibility = sprite.Visibility; sClone.Orientation = sprite.Orientation; //Clone the frames. foreach (Frame frame in sprite.Frames) { //Create the cloned frame. Frame fClone = new Frame(frame.ColorPath, frame.Width, frame.Height); //Clone the properties. fClone.ColorPath = frame.ColorPath; fClone.Width = frame.Width; fClone.Height = frame.Height; fClone.Origin = frame.Origin; fClone.Texture = frame.Texture; //Add the cloned frame to the cloned sprite. sClone.AddFrame(fClone); } //Add the cloned sprite to the cloned manager. manager.Add(sClone); } //Make sure that all sprites have been properly activated. manager.ManageSprites(); //Return the clone. return manager; }
/// <summary> /// Load content and add a sprite. /// </summary> /// <param name="contentManager">The content manager to use.</param> /// <param name="spritePath">The path to the main sprite.</param> /// <param name="height">The height of the shape as seen on picture. This is used for collision data. /// -1 results in the use of the full height of the sprite and a depth of 1.</param> public virtual void LoadContent(ContentManager contentManager, String spritePath, float height) { //Clear all sprites. _Sprites = new SpriteManager(); //Add the color texture. Sprite sprite = _Sprites.Add(new Sprite(_Sprites, "Entity")); sprite.AddFrame(spritePath); //Load all sprites' content. _Sprites.LoadContent(contentManager); //Update the frame's origin. sprite.Frames[0].Origin = new Vector2(sprite.Frames[0].Width / 2, sprite.Frames[0].Height / 2); //Check if the normal and depth maps exist. bool n = File.Exists(Path.Combine(contentManager.RootDirectory, spritePath + "(Normal)" + ".xnb")); bool d = File.Exists(Path.Combine(contentManager.RootDirectory, spritePath + "(Depth)" + ".xnb")); //Set the normal and depth maps. _Sprites[0].Frames[0].NormalPath = n ? spritePath + "(Normal)" : @"Entities\DepthDefault"; _Sprites[0].Frames[0].DepthPath = d ? spritePath + "(Depth)" : @"Entities\DepthDefault"; // Set the shape of the body. _Body.Shape.Width = sprite.CurrentFrame.Width; _Body.Shape.Height = (height == -1) ? sprite.CurrentFrame.Height : height; _Body.Shape.Depth = (height == -1) ? 1 : sprite.CurrentFrame.Height - height; //Update the sprite's position offset. _Sprites[0].PositionOffset = new Vector2(0, -sprite.CurrentFrame.Origin.Y + (_Body.Shape.Height / 2)); }