Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

79 рядки
1.9 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. //We set this before saving so that objects aren't saved ON portals
  31. obj.zoneName = zoneName;
  32. fixPosition(obj, toPos, toRelativePos, invokingObj);
  33. //Destroy, flush events and notify other objects
  34. globalSyncer.processDestroyedObject(obj);
  35. await obj.auth.doSave();
  36. //We have to do this again. This is because onCollisionEnter in portal is not blocking (even though it is async)
  37. // So physics will carry on and allow the obj to move onto the next tile (changing the position while we save above)
  38. fixPosition(obj, toPos, toRelativePos, invokingObj);
  39. //Test code, remove later
  40. Object.entries(globalSyncer.buffer).forEach(([k, v]) => {
  41. v.forEach(e => {
  42. if (e.to.includes(serverId)) {
  43. /* eslint-disable-next-line */
  44. console.log('Found event', k, 'for rezoning object');
  45. }
  46. });
  47. });
  48. const simpleObj = obj.getSimple(true, false, true);
  49. simpleObj.destroyed = false;
  50. simpleObj.forceDestroy = false;
  51. rezoneManager.stageRezone(simpleObj, zoneName);
  52. process.send({
  53. method: 'events',
  54. data: {
  55. rezoneStart: [{
  56. obj: { msg: {} },
  57. to: [serverId]
  58. }]
  59. }
  60. });
  61. };
  62. module.exports = sendObjToZone;