/// <summary> /// Initializes a new instance of the <see cref="SkeletonBodyItem"/> class. /// </summary> /// <param name="itemInfo">SkeletonBodyItemInfo to create the SkeletonBodyItem from</param> public SkeletonBodyItem(SkeletonBodyItemInfo itemInfo) { _itemInfo = itemInfo; var grhData = GrhInfo.GetData(_itemInfo.GrhIndex); if (grhData != null) { Grh = new Grh(grhData, AnimType.Loop, 0); } }
static GrhData TryGetGrhData(string categoryAndTitle) { GrhData grhData = null; try { grhData = GrhInfo.GetData(new SpriteCategorization(categoryAndTitle)); } catch (ArgumentNullException) { // Ignore invalid argument error } return(grhData); }
/// <summary> /// Sets the Grh to a new index. /// </summary> /// <param name="grhIndex">New Grh index to use.</param> /// <param name="anim">Type of animation.</param> /// <param name="currentTime">Current time.</param> public void SetGrh(GrhIndex grhIndex, AnimType anim, TickCount currentTime) { var grhData = GrhInfo.GetData(grhIndex); if (grhData == null && grhIndex != 0) { const string errmsg = "Failed to set Grh - GrhIndex `{0}` does not exist."; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, grhIndex); } return; } SetGrh(grhData, anim, currentTime); }
/// <summary> /// Creates a deep copy of the <see cref="GrhData"/>. /// </summary> /// <param name="newCategorization">Categorization for the duplicated GrhData. Must be unique.</param> /// <returns>Deep copy of the <see cref="GrhData"/> with the new categorization and its own /// unique <see cref="GrhIndex"/>.</returns> /// <exception cref="ArgumentException"><paramref name="newCategorization"/> is already in use.</exception> public GrhData Duplicate(SpriteCategorization newCategorization) { if (GrhInfo.GetData(newCategorization) != null) { throw new ArgumentException("Category already in use.", "newCategorization"); } var index = GrhInfo.NextFreeIndex(); Debug.Assert(GrhInfo.GetData(index) == null, "Slot to use is already in use! How the hell did this happen!? GrhInfo.NextFreeIndex() must be broken."); var dc = DeepCopy(newCategorization, index); GrhInfo.AddGrhData(dc); return(dc); }
StationaryGrhData[] CreateFrames(IList <GrhIndex> frameIndices) { var frames = new StationaryGrhData[frameIndices.Count]; for (var i = 0; i < frameIndices.Count; i++) { frames[i] = GrhInfo.GetData(frameIndices[i]) as StationaryGrhData; if (frames[i] == null) { const string errmsg = "Failed to load GrhData `{0}`. GrhData `{1}` needs it for frame index `{2}` (0-based), out of `{3}` frames total."; var err = string.Format(errmsg, frames[i], this, i, frameIndices.Count); throw new GrhDataException(this, err); } } return(frames); }
/// <summary> /// Gets the <see cref="GrhData"/> for a set. /// </summary> /// <param name="bodyName">The name of the body (determines the category to use).</param> /// <param name="setName">The name of the set (the sprite categorization title).</param> /// <returns>The <see cref="GrhData"/> for the given body and set, or null if not found.</returns> GrhData GetSetGrhData(string bodyName, string setName) { return(GrhInfo.GetData(_rootCategory + SpriteCategorization.Delimiter + bodyName, setName)); }
/// <summary> /// Draws the <see cref="ICharacterSprite"/>. /// </summary> /// <param name="spriteBatch">The <see cref="ISpriteBatch"/> to draw with.</param> /// <param name="position">The position to draw the sprite.</param> /// <param name="heading">The character's heading.</param> /// <param name="color">The color of the sprite.</param> public void Draw(ISpriteBatch spriteBatch, Vector2 position, Direction heading, Color color) { // If we have a body modifier being used, invalidate it if: // 1. The heading has changed. // 2. The animation has ended. // // If we don't have a body modifier being used, just ensure we have the correct Set being used. // // If we are moving, always use the walking animation. _currentHeading = heading; // If the body modifier is set, check if it needs to be unset if (_currentBodyModifier != null) { if (_bodyGrh.AnimType == AnimType.None || _bodyModifierDirection != heading) { _currentBodyModifier = null; } } // If we are moving, the body modifier is not set, or the sprite is invalid, use the non-modifier set if (Character.Velocity != Vector2.Zero || _currentBodyModifier == null || _bodyGrh.GrhData == null) { var prefix = (Character.Velocity == Vector2.Zero ? string.Empty : "Walk "); var directionSuffix = GetDirectionSetName(heading); _currentBodyModifier = null; InternalSetSet(prefix + directionSuffix); } // Ensure the sprite is valid before trying to update and draw it if (_bodyGrh.GrhData == null) { return; } // Update _bodyGrh.Update(_currentTime); position += new Vector2(Character.Size.X / 2f, Character.Size.Y - _bodyGrh.Size.Y); // Get the GrhDatas to draw, along with their draw order List <Tuple <int, GrhData, PaperDollLayerType> > grhDatas = new List <Tuple <int, GrhData, PaperDollLayerType> >(); grhDatas.Add(new Tuple <int, GrhData, PaperDollLayerType>(GetLayerOrder(PaperDollLayerType.Body, heading), _bodyGrh.GrhData, PaperDollLayerType.Body)); if (Paperdoll) { string setSuffix = !string.IsNullOrEmpty(_currentSet) ? "." + _currentSet : ""; if (_layers != null) { foreach (var layerName in _layers) { GrhData gd = GrhInfo.GetData(new SpriteCategorization("Character." + layerName + setSuffix)); if (gd == null) { continue; } PaperDollLayerType layerType = GetPaperDollLayerType(layerName); int layerOrder = GetLayerOrder(layerType, heading); grhDatas.Add(new Tuple <int, GrhData, PaperDollLayerType>(layerOrder, gd, layerType)); } } } // Sort by layer order grhDatas = grhDatas.OrderBy(x => x.Item1).ToList(); // Draw in order var drawingGrh = _bodyGrh.DeepCopy(); for (int i = 0; i < grhDatas.Count; i++) { GrhData gd = grhDatas[i].Item2; // Set frame GrhData gdFrame = gd.GetFrame((int)Math.Floor(_bodyGrh.Frame)) ?? gd.Frames.LastOrDefault(); if (gdFrame == null) { continue; } drawingGrh.SetGrh(gdFrame); // Get offset Vector2 sizeXOffset = new Vector2(drawingGrh.Size.X / -2f, 0); Vector2 layerOffset = GetPaperDollLayerOffset(grhDatas[i].Item3); // Draw drawingGrh.Draw(spriteBatch, position + layerOffset + sizeXOffset, color); } }