Advertisement
gentoogames

Tile Layer Collisions : Gentoo

Jul 13th, 2022
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function collisions_tile_layer() {
  2.     // static definitions
  3.     static _collide_identifier = "collide";
  4.     static _get_horizontal_collisions = function(_layer_tiles) {
  5.         if (hspd > 0) {
  6.             return {
  7.                 top:    tilemap_get_at_pixel(_layer_tiles, bbox_right + hspd, bbox_top),
  8.                 bottom: tilemap_get_at_pixel(_layer_tiles, bbox_right + hspd, bbox_bottom),
  9.             };
  10.         }
  11.         else {
  12.             return {
  13.                 top:    tilemap_get_at_pixel(_layer_tiles, bbox_left + hspd, bbox_top),
  14.                 bottom: tilemap_get_at_pixel(_layer_tiles, bbox_left + hspd, bbox_bottom),
  15.             };
  16.         }
  17.     };
  18.     static _get_vertical_collisions   = function(_layer_tiles) {
  19.         if (vspd > 0) {
  20.             return {
  21.                 left:   tilemap_get_at_pixel(_layer_tiles, bbox_left, bbox_bottom + vspd),
  22.                 right:  tilemap_get_at_pixel(_layer_tiles, bbox_right, bbox_bottom + vspd),
  23.             };
  24.         }
  25.         else {
  26.             return {
  27.                 left:   tilemap_get_at_pixel(_layer_tiles, bbox_left, bbox_top + vspd),
  28.                 right:  tilemap_get_at_pixel(_layer_tiles, bbox_right, bbox_top + vspd),
  29.             };
  30.         }
  31.     };
  32.    
  33.     // get each tile layer in the room that contains the identifier substring
  34.     var _layers_all     = layer_get_all(); // <-- bake this into a var that get's calculated only once per room transition
  35.     var _layers_collide = [];
  36.     var _layer_name     = "";
  37.     var _layer_id       = undefined;
  38.    
  39.     for (var _i = 0, _len = array_length(_layers_all); _i < _len; _i++) {
  40.         _layer_id   = _layers_all[_i];
  41.         _layer_name =  layer_get_name(_layer_id);
  42.        
  43.         if (string_contains(_layer_name, _collide_identifier)) {
  44.             array_push(_layers_collide, _layer_id);
  45.         }
  46.     }
  47.    
  48.     // check for collisions with any of those tile layers
  49.     var _layer_tiles           = undefined;
  50.     var _horizontal_collisions = undefined;
  51.     var _horizontal_top        = undefined;
  52.     var _horizontal_bottom     = undefined;
  53.     var _vertical_collisions   = undefined;
  54.     var _vertical_left         = undefined;
  55.     var _vertical_right        = undefined;
  56.        
  57.     for (var _i = 0, _len = array_length(_layers_collide); _i < _len; _i++) {
  58.         _layer_id    = _layers_collide[_i];
  59.         _layer_tiles =  layer_tilemap_get_id(_layer_id);
  60.        
  61.         // calculate horizontal collisions
  62.         _horizontal_collisions = _get_horizontal_collisions(_layer_tiles);
  63.         _horizontal_top        = _horizontal_collisions.top;
  64.         _horizontal_bottom     = _horizontal_collisions.bottom;    
  65.            
  66.         // if about to horizontally collide, ease into collision
  67.         if (_horizontal_top != 0 || _horizontal_bottom != 0) {
  68.             while (_horizontal_top == 0 && _horizontal_bottom == 0) {
  69.                 x += sign(hspd);
  70.                
  71.                 // recalculate collisions to see if we should exit while() loop
  72.                 _horizontal_collisions = _get_horizontal_collisions(_layer_tiles);
  73.                 _horizontal_top        = _horizontal_collisions.top;
  74.                 _horizontal_bottom     = _horizontal_collisions.bottom;    
  75.             }
  76.             hspd = 0;
  77.         }
  78.        
  79.         // calculate vertical collisions
  80.         _vertical_collisions = _get_vertical_collisions(_layer_tiles);
  81.         _vertical_left       = _vertical_collisions.left;
  82.         _vertical_right      = _vertical_collisions.right;     
  83.            
  84.         // if about to vertically collide, ease into collision
  85.         if (_vertical_left != 0 || _vertical_right != 0) {
  86.             while (_vertical_left == 0 && _vertical_right == 0) {
  87.                 y += sign(vspd);
  88.                
  89.                 // recalculate collisions to see if we should exit while() loop
  90.                 _vertical_collisions = _get_vertical_collisions(_layer_tiles);
  91.                 _vertical_left       = _vertical_collisions.left;
  92.                 _vertical_right      = _vertical_collisions.right;     
  93.             }
  94.             vspd = 0;
  95.         }
  96.     }
  97. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement