Boxel 3d Modding API

Adding a modding API to boxel 3d

  1. // ==UserScript==
  2. // @name Boxel 3d Modding API
  3. // @namespace http://tampermonkey.net/
  4. // @version v1.3.2
  5. // @description Adding a modding API to boxel 3d
  6. // @author Charlieee1
  7. // @match *dopplercreative.com/test/*
  8. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. var addUpdateFunction;
  13. var removeUpdateFunction;
  14. var nextFrameUpdateFunction;
  15. var addJumpFunction;
  16. var removeJumpFunction;
  17. var addGrappleFunction;
  18. var removeGrappleFunction;
  19. var addGrappleReleaseFunction;
  20. var removeGrappleReleaseFunction;
  21. var getPlayer = () => {return app.player};
  22. var getPlayerBody = () => {return app.player.body};
  23. var getPlayerSpeed = () => {return getPlayerBody().speed};
  24. var isDead = app.player.isFrozen();
  25. var canJump = () => {return app.player.allowJump};
  26. var getLevel = () => {return app.level};
  27. var getEngine = () => {return app.engine};
  28. var getWorld = () => {return app.engine.world};
  29. var getGravity = () => {return app.engine.world.gravity};
  30. var getTimer = () => {return app.timer.toString()};
  31. var getVelocity = () => {return app.player.body.velocity};
  32. var setVelocity;
  33. var setVelocityX;
  34. var setVelocityY;
  35. var addVelocity;
  36. var appElement = document.getElementById("app");
  37. var addModToList;
  38.  
  39. (function() {
  40. 'use strict';
  41.  
  42. app.engine.events.afterUpdate = [];
  43.  
  44. addUpdateFunction = function(func) {
  45. app.engine.events.afterUpdate.push(func);
  46. }
  47.  
  48. removeUpdateFunction = function(func) {
  49. app.engine.events.afterUpdate.splice(app.engine.events.afterUpdate.indexOf(func), 1);
  50. }
  51.  
  52. nextFrameUpdateFunction = function(func) {
  53. addUpdateFunction(function() {
  54. func();
  55. removeUpdateFunction(func);
  56. });
  57. };
  58.  
  59. var jumpFuncs = [];
  60. app.player.jumpOriginal = app.player.jump;
  61. app.player.jump = function() {
  62. let canJump_ = canJump();
  63. app.player.jumpOriginal();
  64. if ((app.player.mode == 'jump' || app.player.mode == 'control') && canJump_) {
  65. jumpFuncs.forEach((func) => {func();});
  66. }
  67. }
  68.  
  69. addJumpFunction = function(func) {
  70. jumpFuncs.push(func);
  71. }
  72.  
  73. removeJumpFunction = function(func) {
  74. if (func in jumpFuncs) {
  75. jumpFuncs.splice(jumpFuncs.indexOf(func), 1);
  76. }
  77. }
  78.  
  79. var grappleFuncs = [];
  80. app.player.grappleOriginal = app.player.addRope;
  81. app.player.addRope = function(pos) {
  82. app.player.grappleOriginal(pos);
  83. if (app.player.mode == 'grapple') {
  84. grappleFuncs.forEach((func) => {func(pos);});
  85. }
  86. }
  87.  
  88. addGrappleFunction = function(func) {
  89. grappleFuncs.push(func);
  90. }
  91.  
  92. removeGrappleFunction = function(func) {
  93. if (func in grappleFuncs) {
  94. grappleFuncs.splice(grappleFuncs.indexOf(func), 1);
  95. }
  96. }
  97.  
  98. var grappleReleaseFuncs = [];
  99. app.player.grappleReleaseOriginal = app.player.removeRope;
  100. app.player.removeRope = function() {
  101. app.player.grappleReleaseOriginal();
  102. if (app.player.mode == 'grapple') {
  103. grappleReleaseFuncs.forEach((func) => {func();});
  104. }
  105. }
  106.  
  107. addGrappleReleaseFunction = function(func) {
  108. grappleReleaseFuncs.push(func);
  109. }
  110.  
  111. removeGrappleReleaseFunction = function(func) {
  112. if (func in grappleReleaseFuncs) {
  113. grappleReleaseFuncs.splice(grappleReleaseFuncs.indexOf(func), 1);
  114. }
  115. }
  116.  
  117. setVelocity = function(vX, vY) {
  118. let player = getPlayerBody();
  119. Matter.Body.setVelocity(player, {x: vX, y: vY});
  120. }
  121.  
  122. setVelocityX = function(vX) {
  123. let vY = getVelocity().y;
  124. setVelocity(vX, vY);
  125. }
  126.  
  127. setVelocityY = function(vY) {
  128. let vX = getVelocity().x;
  129. setVelocity(vX, vY);
  130. }
  131.  
  132. addVelocity = function(dx, dy) {
  133. let v = getVelocity();
  134. if (dy == 0) {
  135. setVelocityX(v.x + dx);
  136. } else if (dx == 0) {
  137. setVelocityY(v.y + dy);
  138. } else {
  139. setVelocity(v.x + dx, v.y + dy);
  140. }
  141. }
  142.  
  143. // Mod list
  144. var modList = document.createElement("div");
  145. modList.style.position = "absolute";
  146. modList.style.bottom = 0;
  147. modList.style.right = 0;
  148. modList.style.paddingRight = 0;
  149. modList.style.paddingBottom = 0;
  150. modList.style.opactiy = "10%";
  151. modList.style.fontSize = "5px";
  152. appElement.appendChild(modList);
  153.  
  154. addModToList = function(text) {
  155. let newP = document.createElement("p");
  156. newP.style.margin = 0;
  157. newP.appendChild(document.createTextNode(text));
  158. modList.appendChild(newP);
  159. }
  160.  
  161. // Convention is to have the first letter of the mod name lowercase
  162. addModToList("Modding API");
  163. })();