firedingo

Level Generation Code

Feb 18th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. obj_Controller
  2. ---------------------------------------------------------------------------------------------------------------------------------------
  3. ///Level Creation
  4.  
  5. //Set Grid Size
  6. var gHeight = room_height div CELL_HEIGHT;
  7. var gWidth = room_width div CELL_WIDTH;
  8.  
  9. //Create the Grid
  10. grid = ds_grid_create(gWidth, gHeight);
  11.  
  12. //Fill grid with Air
  13. ds_grid_set_region(grid, 0, 0, gWidth-1, gHeight-1, AIR);
  14.  
  15. //Randomize the world
  16. randomize();
  17.  
  18. //Create a controller
  19. var cx = 0;
  20. var cy = room_height;
  21.  
  22. //Give the controller a random direction
  23. var cdir = irandom(3);
  24.  
  25. //Chance of controller changing direction
  26. var odds = 1;
  27.  
  28. //Start Creating the level
  29. repeat (650) {
  30. //Place a ground tile
  31. //if (cy > room_height - CELL_WIDTH * 10) {
  32. grid[# cx, cy] = GROUND;
  33. //}
  34. //Randomize the direction of the controller
  35. if (irandom(odds) == odds) {
  36. cdir = irandom(3);
  37. }
  38.  
  39. //Move the Controller
  40. var xdir = lengthdir_x(1, cdir*90);
  41. var ydir = lengthdir_y(1, cdir*90);
  42. cx += xdir;
  43. cy += ydir;
  44.  
  45. //Keep the Controller inside the grid
  46. cx = clamp(cx, 1, gWidth-2);
  47. cy = clamp(cy, 1, gHeight-2);
  48. }
  49.  
  50. //Time To Add The Ground
  51. for (var yy = 0; yy < gHeight; yy++) {
  52. for (var xx = 0; xx < gWidth; xx++) {
  53. if (grid[# xx, yy] == GROUND) {
  54. tile_add(tile_stonebricks, 0, 0, CELL_WIDTH, CELL_HEIGHT, xx*CELL_WIDTH, yy*CELL_HEIGHT, 0);
  55. }
  56. }
  57. }
  58.  
  59. ---------------------------------------------------------------------------------------
  60. ///Restart The Room
  61. room_restart();
Advertisement
Add Comment
Please, Sign In to add comment