Last active
September 22, 2016 17:24
-
-
Save MrSapps/5d8c8dc79c835bf0f524abe334bb7bd0 to your computer and use it in GitHub Desktop.
AnimStateToLua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using static CreatureEditor.AnimState; | |
namespace CreatureEditor | |
{ | |
internal class AnimStateToLua | |
{ | |
private readonly AnimStateContainer mData; | |
internal AnimStateToLua(AnimStateContainer data) | |
{ | |
if (data == null) | |
{ | |
throw new ArgumentException("data can't be null"); | |
} | |
mData = data; | |
} | |
private String ToLuaBool(bool b) | |
{ | |
if (b) | |
{ | |
return "true"; | |
} | |
return "false"; | |
} | |
internal void Save(string fileName) | |
{ | |
var sb = new StringBuilder(); | |
sb.AppendLine("local states = {}\n"); | |
foreach (var state in mData.mStates) | |
{ | |
/* TODO: Replaced by single "AnimationName" | |
state.BanFile; | |
state.BanID; | |
state.BanIndex; | |
*/ | |
sb.AppendFormat("states.{0} = {{\n", state.Name); | |
sb.AppendFormat(" NextAnimation ='{0}',\n", state.NextAnimation); | |
sb.AppendFormat(" NextAnimationFrameIndex = {0},\n", state.NextAnimationFrameIndex); | |
sb.AppendFormat(" FlipX = {0},\n", ToLuaBool(state.FlipX)); | |
sb.AppendFormat(" SnapToGrid = {0},\n", ToLuaBool(state.SnapToGrid)); | |
sb.AppendFormat(" UseVelocityX = {0},\n", ToLuaBool(state.UseVelocityX)); | |
sb.AppendFormat(" XSpeed={0},\n", state.XSpeed); | |
sb.AppendFormat(" YSpeed={0}\n", state.YSpeed); | |
sb.Append("}\n"); | |
if (state.SoundEffects != null && state.SoundEffects.Count() > 0) | |
{ | |
var byFrameIndex = new Dictionary<int, List<string>>(); | |
foreach (var soundFx in state.SoundEffects) | |
{ | |
if (!byFrameIndex.ContainsKey(soundFx.Frame)) | |
{ | |
byFrameIndex.Add(soundFx.Frame, new List<string>() { soundFx.SoundEffectID }); | |
} | |
else | |
{ | |
byFrameIndex[soundFx.Frame].Add(soundFx.SoundEffectID); | |
} | |
} | |
sb.AppendFormat("states.{0}.Sounds = {{}}\n", state.Name); | |
foreach (var s in byFrameIndex) | |
{ | |
sb.AppendFormat("states.{0}.Sounds[{1}] = {{\n", state.Name, s.Key); | |
foreach (var fx in s.Value) | |
{ | |
sb.AppendFormat(" '{0}',\n", fx); | |
} | |
sb.Append("}\n"); | |
} | |
} | |
if (state.ConditionsChecks != null && state.ConditionsChecks.Count() > 0) | |
{ | |
sb.AppendFormat("states.{0}.Conditions = {{}}\n", state.Name); | |
int index = 0; | |
foreach (var condition in state.ConditionsChecks) | |
{ | |
sb.AppendFormat("states.{0}.Conditions[{1}] = {{\n", state.Name, index++); | |
sb.AppendFormat(" AnyTime = {0},\n", ToLuaBool(condition.AnyTime)); | |
if (condition.AtFrames != null && condition.AtFrames.Count() > 0) | |
{ | |
sb.Append(" AtFrames = {\n"); | |
foreach (var f in condition.AtFrames) | |
{ | |
sb.AppendFormat(" [{0}] = {0},\n", f); | |
} | |
sb.Append(" },\n"); | |
} | |
sb.AppendFormat(" FlipX = {0},\n", ToLuaBool(condition.FlipX)); | |
sb.AppendFormat(" TargetAnimation ='{0}',\n", condition.TargetAnimation); | |
sb.AppendFormat(" TargetAnimationFrameIndex={0},\n", condition.TargetAnimationFrameIndex); | |
if (condition.Conditions != null && condition.Conditions.Count() > 0) | |
{ | |
sb.AppendFormat(" Conditions = {{\n"); | |
foreach (var c in condition.Conditions) | |
{ | |
sb.AppendFormat(" {0} = {1},\n", c.Condition, ToLuaBool(!c.IfFalse)); | |
} | |
sb.Append(" },\n"); | |
} | |
sb.Append("}\n"); | |
} | |
} | |
sb.Append("\n\n\n"); | |
} | |
MessageBox.Show(sb.ToString(), "test"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment