SimulateBattleInfo.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public enum SimulateRecordType
  5. {
  6. Spawn = 0,
  7. Run = 1,
  8. Jump = 2,
  9. Attack = 3,
  10. Behit = 4,
  11. LifeChange = 5,
  12. ManaChange = 6,
  13. RageChange = 7,
  14. }
  15. public class SimulateBattleInfo
  16. {
  17. public class SimulateRecord
  18. {
  19. int mFighterIdx;
  20. int mFrame;
  21. SimulateRecordType mType;
  22. object mParam;
  23. public SimulateRecord (int fighterIdx, int frame, SimulateRecordType type, object param)
  24. {
  25. mFighterIdx = fighterIdx;
  26. mFrame = frame;
  27. mType = type;
  28. mParam = param;
  29. }
  30. public JSONObject ToJson ()
  31. {
  32. JSONObject json = new JSONObject ();
  33. json.Add (mFighterIdx);
  34. json.Add (mFrame);
  35. json.Add ((int)mType);
  36. switch (mType) {
  37. case SimulateRecordType.Spawn:
  38. JSONObject paramJson = new JSONObject ();
  39. paramJson.Add (Vector3ToJson ((Vector3)((object[])mParam) [0]));
  40. paramJson.Add (Vector3ToJson ((Vector3)((object[])mParam) [1]));
  41. break;
  42. }
  43. return json;
  44. }
  45. }
  46. public ActorData[] mLeftActors;
  47. public ActorData[] mRightActors;
  48. public int mLeftCaptainIdx = -1;
  49. public int mRightCaptainIdx = -1;
  50. public int mRandomSeed;
  51. List<SimulateRecord> mRecords;
  52. public void Record (Fighter fighter, int frame, SimulateRecordType type, object param)
  53. {
  54. if (mRecords == null)
  55. mRecords = new List<SimulateRecord> ();
  56. int fighterIdx = GetFighterIdx (fighter);
  57. if (fighterIdx < 0)
  58. return;
  59. mRecords.Add (new SimulateRecord (fighterIdx, frame, type, param));
  60. }
  61. public int GetFighterIdx (Fighter fighter)
  62. {
  63. if (fighter.TeamSide == eTeamType.Friend) {
  64. for (int i = 0; i < mLeftActors.Length; i++)
  65. if (mLeftActors [i] == fighter.Actor)
  66. return i;
  67. } else {
  68. for (int i = 0; i < mRightActors.Length; i++)
  69. if (mRightActors [i] == fighter.Actor)
  70. return mLeftActors.Length + i;
  71. }
  72. return -1;
  73. }
  74. JSONObject RecordsToJson ()
  75. {
  76. JSONObject json = new JSONObject ();
  77. if (mRecords == null)
  78. return json;
  79. for (int i = 0; i < mRecords.Count; i++)
  80. json.Add (mRecords [i].ToJson ());
  81. return json;
  82. }
  83. static JSONObject Vector3ToJson(Vector3 v, int intPrecise = 10000)
  84. {
  85. JSONObject json = new JSONObject ();
  86. json.Add (Mathf.RoundToInt(v.x * intPrecise));
  87. json.Add (Mathf.RoundToInt(v.y * intPrecise));
  88. json.Add (Mathf.RoundToInt(v.z * intPrecise));
  89. return json;
  90. }
  91. static Vector3 Vector3FromJson(JSONObject json, int intPrecise = 10000)
  92. {
  93. float p = 1.0f / intPrecise;
  94. return new Vector3 (json[0].n * p, json[1].n * p, json[2].n * p);
  95. }
  96. }