You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
1.8 KiB

  1. const fixPosition = (obj, toPos, toRelativePos, invokingObj) => {
  2. if (toPos) {
  3. obj.x = toPos.x;
  4. obj.y = toPos.y;
  5. } else if (toRelativePos) {
  6. obj.x = invokingObj.obj.x + toRelativePos.x;
  7. obj.y = invokingObj.obj.y + toRelativePos.y;
  8. }
  9. };
  10. const sendObjToZone = async ({ obj, invokingObj, zoneName, toPos, toRelativePos }) => {
  11. const { serverId, instance: { syncer: globalSyncer, physics } } = obj;
  12. if (obj.zoneName === zoneName) {
  13. globalSyncer.flushForTarget(serverId);
  14. physics.removeObject(obj, obj.x, obj.y);
  15. if (toRelativePos) {
  16. toPos = {
  17. x: invokingObj.obj.x + toRelativePos.x,
  18. y: invokingObj.obj.y + toRelativePos.y
  19. };
  20. }
  21. obj.x = toPos.x;
  22. obj.y = toPos.y;
  23. physics.addObject(obj, obj.x, obj.y);
  24. globalSyncer.queue('teleportToPosition', {
  25. x: obj.x,
  26. y: obj.y
  27. }, [obj.serverId]);
  28. return;
  29. }
  30. obj.fireEvent('beforeRezone');
  31. //We set this before saving so that objects aren't saved ON portals
  32. obj.zoneName = zoneName;
  33. fixPosition(obj, toPos, toRelativePos, invokingObj);
  34. //Destroy, flush events and notify other objects
  35. globalSyncer.processDestroyedObject(obj);
  36. await obj.auth.doSave();
  37. //We have to do this again. This is because onCollisionEnter in portal is not blocking (even though it is async)
  38. // So physics will carry on and allow the obj to move onto the next tile (changing the position while we save above)
  39. fixPosition(obj, toPos, toRelativePos, invokingObj);
  40. const simplifiedObj = obj.getSimple(true, false, true);
  41. simplifiedObj.destroyed = false;
  42. simplifiedObj.forceDestroy = false;
  43. rezoneManager.stageRezone({
  44. simplifiedObj,
  45. targetZone: zoneName,
  46. keepPos: !!toPos
  47. });
  48. process.send({
  49. method: 'events',
  50. data: {
  51. rezoneStart: [{
  52. obj: { msg: {} },
  53. to: [serverId]
  54. }]
  55. }
  56. });
  57. };
  58. module.exports = sendObjToZone;