Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. /// @function scr_collisionMesh(object index,room width,room height,texture size)
  2. /// @param objectIndex
  3. /// @param room width
  4. /// @param room height
  5. /// @param texture size
  6. //Created by Spasman
  7. //This script will take every instance of a defined object index and merge them all into larger, less resource intensive instances
  8. //Good for games that use a bunch of instances for collision and nothing else!
  9. //NOTE: This script creates rather large sprites, so remember to use sprite_delete() if the generated collision meshes are eventually destroyed or if the room ends/changes or you will have a big memory leak!
  10.  
  11. //object index = Defines what object to merge into a collision mesh
  12. //room width = Tells the script what the room width is. Just put room_width if you don't know what to do here.
  13. //room height = Tells the script what the room height is. Just put room_height if you don't know what to do here.
  14. //texture size = Defines what you want as the maximum mesh/texture size. A size of 512 or 1024 are both good options. The bigger the size, the less instances you will have but at the cost of GPU.
  15.  
  16.  
  17. //Create variables
  18. collision_surface = -1
  19. cutX = 0
  20. cutY = 0
  21. spr = -1
  22. sprW = 0
  23. sprH = 0
  24. sprIndex = 0
  25.  
  26. //defines a variable the script needs in every existing instance of the defined object index
  27. with(argument0) {
  28. collisionMesh = false
  29. collisionID = -1
  30. }
  31.  
  32. //A big scary nested while loop that will cut the surface into the pieces and turn them into sprites/collision masks
  33. //cutX and cutY define the current position of where we're cutting up the surface. Think of it as the point of the knife when slicing cake!
  34. //Enter the first while loop to determine height.
  35. while(1) {
  36. //Break out of the FIRST while loop when the cutting position Y goes beyond the room height.
  37. if cutY >= argument2 {
  38. break;
  39. }
  40. cutX = 0
  41.  
  42. //Before we determine height, enter a SECOND while loop to determine the width
  43. while(1) {
  44. //Set the default sprite size to the defined texture size
  45. sprW = argument3
  46. sprH = argument3
  47.  
  48. //If the cutting position + defined texture size go beyond the defined room sizes, adjust to only reach the end of room.
  49. if cutX+sprW > argument1 {
  50. sprW = point_distance(cutX,0,argument1,0)
  51. }
  52. if cutY+sprH > argument2 {
  53. sprH = point_distance(0,cutY,0,argument2)
  54. }
  55.  
  56. //create a surface with the same size as the current cutting area
  57. collision_surface = surface_create(sprW,sprH)
  58.  
  59. //sets the surface target to what we just created
  60. //then clear the surface alpha of any texture errors that would mess with the collision mask
  61. surface_set_target(collision_surface);
  62. draw_clear_alpha(c_white,0)
  63.  
  64. //draw the sprite assigned to the defined object index. Use the cutting position to draw the sprites within the surface area.
  65. //Uses the variable we set before to make sure it isn't an object generated by this script
  66. with(argument0) {
  67. if collisionMesh = false {
  68. if (x-other.cutX)+sprite_get_width(sprite_index) > 0 and (x-other.cutX) < other.sprW and (y-other.cutY)+sprite_get_height(sprite_index) > 0 and y-other.cutY < other.sprH {
  69. draw_sprite(sprite_index,image_index,x-other.cutX,y-other.cutY)
  70. collisionID = other.sprIndex
  71. }
  72. }
  73. }
  74.  
  75. //Resets the surface back to the application surface
  76. surface_reset_target();
  77.  
  78. //Create the sprite and collision mask from the surface from the cutting position, with the set width and height from sprW/sprH
  79. spr = sprite_create_from_surface(collision_surface,0,0,sprW,sprH,false,false,0,0)
  80. sprite_collision_mask(spr, false, 0, 0, 0, sprW, sprH, 0, 0);
  81.  
  82. //Create an instance of the defined object index at the cutting position and assign the newly created sprite from it
  83. //Sets the collisionMesh variable to true so the script ignores it
  84. //Use something like sprite_create in the object's destroy/room end events to delete the newly made sprites to prevent memory leaks, in case collisionMesh isn't enough
  85. mb = instance_create_depth(cutX,cutY,0,argument0)
  86. mb.sprite_index = spr
  87. mb.collisionMesh = true
  88. mb.sprite_create = true
  89. mb.collisionID = sprIndex
  90.  
  91.  
  92. //Move the X coordinates of the cutting position to the next texture page. Stop yourself short w/ point_distance() if you try to go beyond the room width
  93. if cutX+argument3 > argument1 {
  94. cutX += point_distance(cutX,0,argument1,0)
  95. }
  96. else {
  97. cutX += argument3
  98. }
  99.  
  100. //Destroy the surface to free up memory
  101. surface_free(collision_surface);
  102. sprIndex += 1
  103.  
  104. //Break out of the SECOND while loop when the cutting position X goes beyond the room width.
  105. if cutX >= argument1 {
  106. break;
  107. }
  108. }
  109.  
  110. //Move the Y coordinates of the cutting position to the next texture page. Stop yourself short w/ point_distance() if you try to go beyond the room height.
  111. if cutY+argument3 > argument2 {
  112. cutY += point_distance(0,cutY,0,argument2)
  113. }
  114. else {
  115. cutY += argument3
  116. }
  117. }
  118.  
  119. //Uncomment this for debugging, the debug message triggers when an 'old' instance is not detected under a new instance, which should be impossible if the script worked right.
  120. //Feel free to replace the debug message with something else to help you debug.
  121. /*with(argument0) {
  122. if collisionMesh = true and collisionID != -1 {
  123. with(argument0) {
  124. if collisionMesh = false and collisionID = other.collisionID {
  125. if !collision_rectangle(bbox_left,bbox_top,bbox_right,bbox_bottom,other,false,true) {
  126. show_debug_message("Collision mesh error with collision ID "+string(collisionID))
  127. }
  128. }
  129. }
  130. }
  131. }*/
  132.  
  133. //Now that you have fancy collision meshes, we can go ahead and destroy all of the old instances for the defined object index
  134. //Uses collisionMesh to make sure the newly made instances aren't destroyed.
  135. with(argument0) {
  136. if collisionMesh = false {
  137. instance_destroy();
  138. }
  139. }
  140. //notify compile window
  141. show_debug_message("Completed collision mesh for object index "+object_get_name(argument0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement