public bool AddBounds(int currentTime, ref idBounds bounds, bool removeOriginOffset) { if ((_endTime > 0) && (currentTime > _endTime)) { return(false); } idAnim anim = GetAnimation(); if (anim == null) { return(false); } float weight = GetWeight(currentTime); if (weight == 0) { return(false); } int time = GetAnimationTime(currentTime); int animCount = anim.AnimationCount; bool addOrigin = (_allowMove == false) || (removeOriginOffset == false); idBounds b; Vector3 pos; for (int i = 0; i < animCount; i++) { if (anim.GetBounds(out b, i, time, _cycle) == true) { if (addOrigin == true) { anim.GetOrigin(out pos, i, time, _cycle); b.Translate(pos); } bounds.AddBounds(b); } } return(true); }
public int GetAnimationTime(int currentTime) { idAnim anim = GetAnimation(); if (anim != null) { if (_frame != 0) { return(idHelper.FrameToTime(_frame - 1)); } int time = 0; // most of the time we're running at the original frame rate, so avoid the int-to-float-to-int conversion if (_rate == 1.0f) { time = currentTime - _startTime + _timeOffset; } else { time = (int)(((currentTime - _startTime) * _rate) + _timeOffset); } // given enough time, we can easily wrap time around in our frame calculations, so // keep cycling animations' time within the length of the anim. int length = anim.Length; if ((_cycle < 0) && (length > 0)) { time %= length; // time will wrap after 24 days (oh no!), resulting in negative results for the %. // adding the length gives us the proper result. if (time < 0) { time += length; } } return(time); } return(0); }
private bool ParseAnimation(idLexer lexer, int defaultAnimCount) { List <idMD5Anim> md5anims = new List <idMD5Anim>(); idMD5Anim md5anim; idAnim anim; AnimationFlags flags = new AnimationFlags(); idToken token; idToken realName = lexer.ReadToken(); if (realName == null) { lexer.Warning("Unexpected end of file"); MakeDefault(); return(false); } string alias = realName.ToString(); int i; int count = _anims.Count; for (i = 0; i < count; i++) { if (_anims[i].FullName.Equals(alias, StringComparison.OrdinalIgnoreCase) == true) { break; } } if ((i < count) && (i >= defaultAnimCount)) { lexer.Warning("Duplicate anim '{0}'", realName); MakeDefault(); return(false); } if (i < defaultAnimCount) { anim = _anims[i]; } else { // create the alias associated with this animation anim = new idAnim(); _anims.Add(anim); } // random anims end with a number. find the numeric suffix of the animation. int len = alias.Length; for (i = len - 1; i > 0; i--) { if (Char.IsNumber(alias[i]) == false) { break; } } // check for zero length name, or a purely numeric name if (i <= 0) { lexer.Warning("Invalid animation name '{0}'", alias); MakeDefault(); return(false); } // remove the numeric suffix alias = alias.Substring(0, i + 1); // parse the anims from the string do { if ((token = lexer.ReadToken()) == null) { lexer.Warning("Unexpected end of file"); MakeDefault(); return(false); } // lookup the animation md5anim = idR.AnimManager.GetAnimation(token.ToString()); if (md5anim == null) { lexer.Warning("Couldn't load anim '{0}'", token); return(false); } md5anim.CheckModelHierarchy(_model); if (md5anims.Count > 0) { // make sure it's the same length as the other anims if (md5anim.Length != md5anims[0].Length) { lexer.Warning("Anim '{0}' does not match length of anim '{1}'", md5anim.Name, md5anims[0].Name); MakeDefault(); return(false); } } // add it to our list md5anims.Add(md5anim); }while(lexer.CheckTokenString(",") == true); if (md5anims.Count == 0) { lexer.Warning("No animation specified"); MakeDefault(); return(false); } anim.SetAnimation(this, realName.ToString(), alias, md5anims.ToArray()); // parse any frame commands or animflags if (lexer.CheckTokenString("{") == true) { while (true) { if ((token = lexer.ReadToken()) == null) { lexer.Warning("Unexpected end of file"); MakeDefault(); return(false); } string tokenValue = token.ToString(); if (tokenValue == "}") { break; } else if (tokenValue == "prevent_idle_override") { flags.PreventIdleOverride = true; } else if (tokenValue == "random_cycle_start") { flags.RandomCycleStart = true; } else if (tokenValue == "ai_no_turn") { flags.AINoTurn = true; } else if (tokenValue == "anim_turn") { flags.AnimationTurn = true; } else if (tokenValue == "frame") { // create a frame command int frameIndex; string err; // make sure we don't have any line breaks while reading the frame command so the error line # will be correct if ((token = lexer.ReadTokenOnLine()) == null) { lexer.Warning("Missing frame # after 'frame'"); MakeDefault(); return(false); } else if ((token.Type == TokenType.Punctuation) && (token.ToString() == "-")) { lexer.Warning("Invalid frame # after 'frame'"); MakeDefault(); return(false); } else if ((token.Type != TokenType.Number) || (token.SubType == TokenSubType.Float)) { lexer.Error("expected integer value, found '{0}'", token); } // get the frame number frameIndex = token.ToInt32(); // put the command on the specified frame of the animation if ((err = anim.AddFrameCommand(this, frameIndex, lexer, null)) != null) { lexer.Warning(err.ToString()); MakeDefault(); return(false); } } else { lexer.Warning("Unknown command '{0}'", token); MakeDefault(); return(false); } } } // set the flags anim.Flags = flags; return(true); }
private bool ParseAnimation(idLexer lexer, int defaultAnimCount) { List<idMD5Anim> md5anims = new List<idMD5Anim>(); idMD5Anim md5anim; idAnim anim; AnimationFlags flags = new AnimationFlags(); idToken token; idToken realName = lexer.ReadToken(); if(realName == null) { lexer.Warning("Unexpected end of file"); MakeDefault(); return false; } string alias = realName.ToString(); int i; int count = _anims.Count; for(i = 0; i < count; i++) { if(_anims[i].FullName.Equals(alias, StringComparison.OrdinalIgnoreCase) == true) { break; } } if((i < count) && (i >= defaultAnimCount)) { lexer.Warning("Duplicate anim '{0}'", realName); MakeDefault(); return false; } if(i < defaultAnimCount) { anim = _anims[i]; } else { // create the alias associated with this animation anim = new idAnim(); _anims.Add(anim); } // random anims end with a number. find the numeric suffix of the animation. int len = alias.Length; for(i = len - 1; i > 0; i--) { if(Char.IsNumber(alias[i]) == false) { break; } } // check for zero length name, or a purely numeric name if(i <= 0) { lexer.Warning("Invalid animation name '{0}'", alias); MakeDefault(); return false; } // remove the numeric suffix alias = alias.Substring(0, i + 1); // parse the anims from the string do { if((token = lexer.ReadToken()) == null) { lexer.Warning("Unexpected end of file"); MakeDefault(); return false; } // lookup the animation md5anim = idR.AnimManager.GetAnimation(token.ToString()); if(md5anim == null) { lexer.Warning("Couldn't load anim '{0}'", token); return false; } md5anim.CheckModelHierarchy(_model); if(md5anims.Count > 0) { // make sure it's the same length as the other anims if(md5anim.Length != md5anims[0].Length) { lexer.Warning("Anim '{0}' does not match length of anim '{1}'", md5anim.Name, md5anims[0].Name); MakeDefault(); return false; } } // add it to our list md5anims.Add(md5anim); } while(lexer.CheckTokenString(",") == true); if(md5anims.Count == 0) { lexer.Warning("No animation specified"); MakeDefault(); return false; } anim.SetAnimation(this, realName.ToString(), alias, md5anims.ToArray()); // parse any frame commands or animflags if(lexer.CheckTokenString("{") == true) { while(true) { if((token = lexer.ReadToken()) == null) { lexer.Warning("Unexpected end of file"); MakeDefault(); return false; } string tokenValue = token.ToString(); if(tokenValue == "}") { break; } else if(tokenValue == "prevent_idle_override") { flags.PreventIdleOverride = true; } else if(tokenValue == "random_cycle_start") { flags.RandomCycleStart = true; } else if(tokenValue == "ai_no_turn") { flags.AINoTurn = true; } else if(tokenValue == "anim_turn") { flags.AnimationTurn = true; } else if(tokenValue == "frame") { // create a frame command int frameIndex; string err; // make sure we don't have any line breaks while reading the frame command so the error line # will be correct if((token = lexer.ReadTokenOnLine()) == null) { lexer.Warning("Missing frame # after 'frame'"); MakeDefault(); return false; } else if((token.Type == TokenType.Punctuation) && (token.ToString() == "-")) { lexer.Warning("Invalid frame # after 'frame'"); MakeDefault(); return false; } else if((token.Type != TokenType.Number) || (token.SubType == TokenSubType.Float)) { lexer.Error("expected integer value, found '{0}'", token); } // get the frame number frameIndex = token.ToInt32(); // put the command on the specified frame of the animation if((err = anim.AddFrameCommand(this, frameIndex, lexer, null)) != null) { lexer.Warning(err.ToString()); MakeDefault(); return false; } } else { lexer.Warning("Unknown command '{0}'", token); MakeDefault(); return false; } } } // set the flags anim.Flags = flags; return true; }