SET.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Microsoft.Xna.Framework;
  2. namespace OpenVIII.Fields.Scripts.Instructions
  3. {
  4. /// <summary>
  5. /// Place this entity's model at XCoord, YCoord standing on the given walkmesh triangle. Unlike SET3, this function will place the event on the walkable terrain (the ZCoord is interpolated from the walkmesh).
  6. /// </summary>
  7. /// <see cref="http://wiki.ffrtt.ru/index.php?title=FF8/Field/Script/Opcodes/01D_SET"/>
  8. public sealed class SET : JsmInstruction
  9. {
  10. #region Fields
  11. private readonly Vector2 _pos;
  12. private readonly int _walkmeshTriangleId;
  13. #endregion Fields
  14. #region Constructors
  15. public SET(int walkmeshTriangleId, int x, int y) => (_walkmeshTriangleId, _pos.X, _pos.Y) = (walkmeshTriangleId, x, y);
  16. public SET(int walkmeshTriangleId, IStack<IJsmExpression> stack)
  17. : this(walkmeshTriangleId,
  18. y: ((IConstExpression)stack.Pop()).Int32(),
  19. x: ((IConstExpression)stack.Pop()).Int32())
  20. {
  21. }
  22. #endregion Constructors
  23. #region Methods
  24. public override void Format(ScriptWriter sw, IScriptFormatterContext formatterContext, IServices services) => sw.Format(formatterContext, services)
  25. .Property(nameof(FieldObject.Model))
  26. .Method(nameof(FieldObjectModel.SetPosition))
  27. .Argument("walkmeshTriangleId", _walkmeshTriangleId)
  28. .Argument("x", (int)_pos.X)
  29. .Argument("y", (int)_pos.Y)
  30. .Comment(nameof(SET3));
  31. public override IAwaitable TestExecute(IServices services)
  32. {
  33. var currentObject = ServiceId.Field[services].Engine.CurrentObject;
  34. currentObject.Model.SetPosition(new WalkMeshCoords(_walkmeshTriangleId, _pos));
  35. return DummyAwaitable.Instance;
  36. }
  37. public override string ToString() => $"{nameof(SET)}({nameof(_walkmeshTriangleId)}: {_walkmeshTriangleId}, {nameof(_pos)}: {_pos})";
  38. #endregion Methods
  39. }
  40. }