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.
 
 
 

81 lines
2.0 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. //Inform the main thread that we are rezoning. We do this because if the player
  38. // dc's before rezone is complete the player might become stuck in the main thread
  39. process.send({
  40. method: 'object',
  41. serverId: obj.serverId,
  42. obj: {
  43. rezoning: true
  44. }
  45. });
  46. //We have to do this again. This is because onCollisionEnter in portal is not blocking (even though it is async)
  47. // So physics will carry on and allow the obj to move onto the next tile (changing the position while we save above)
  48. fixPosition(obj, toPos, toRelativePos, invokingObj);
  49. const simpleObj = obj.getSimple(true, false, true);
  50. simpleObj.destroyed = false;
  51. simpleObj.forceDestroy = false;
  52. rezoneManager.stageRezone(simpleObj, zoneName);
  53. process.send({
  54. method: 'events',
  55. data: {
  56. rezoneStart: [{
  57. obj: { msg: {} },
  58. to: [serverId]
  59. }]
  60. }
  61. });
  62. };
  63. module.exports = sendObjToZone;