2
0

Jsm.GameObject.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace OpenVIII.Fields.Scripts
  4. {
  5. public static partial class Jsm
  6. {
  7. #region Classes
  8. public sealed class GameObject
  9. {
  10. #region Constructors
  11. public GameObject(int id, IReadOnlyList<GameScript> scripts)
  12. {
  13. Id = id;
  14. Scripts = scripts;
  15. }
  16. #endregion Constructors
  17. #region Properties
  18. public int Id { get; }
  19. public IReadOnlyList<GameScript> Scripts { get; }
  20. #endregion Properties
  21. #region Methods
  22. public void FormatType(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices executionContext)
  23. {
  24. formatterContext.GetObjectScriptNamesById(Id, out var typeName, out _);
  25. sw.AppendLine($"public sealed class {typeName}");
  26. {
  27. sw.AppendLine("{");
  28. sw.Indent++;
  29. if (Scripts.Count > 0)
  30. {
  31. FormatConstructor(typeName, sw, formatterContext, executionContext);
  32. foreach (var script in Scripts.Skip(1))
  33. {
  34. sw.AppendLine();
  35. script.FormatMethod(sw, formatterContext, executionContext);
  36. }
  37. }
  38. sw.Indent--;
  39. sw.AppendLine("}");
  40. }
  41. }
  42. public override string ToString() => $"{{{Id}, {nameof(Scripts)} {nameof(Scripts.Count)} = {Scripts.Count}}}";
  43. private void FormatConstructor(string typeName, ScriptWriter sw, IScriptFormatterContext formatterContext, IServices executionContext)
  44. {
  45. sw.AppendLine($"private readonly {nameof(IServices)} _ctx;");
  46. sw.AppendLine();
  47. sw.AppendLine($"public {typeName}({nameof(IServices)} executionContext)");
  48. {
  49. sw.AppendLine("{");
  50. sw.Indent++;
  51. sw.AppendLine("_ctx = executionContext;");
  52. Scripts[0].FormatMethodBody(sw, formatterContext, executionContext);
  53. sw.Indent--;
  54. sw.AppendLine("}");
  55. }
  56. }
  57. #endregion Methods
  58. }
  59. #endregion Classes
  60. }
  61. }