Jsm.GameObject.cs 2.1 KB

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