Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function collisions_tile_layer() {
- // static definitions
- static _collide_identifier = "collide";
- static _get_horizontal_collisions = function(_layer_tiles) {
- if (hspd > 0) {
- return {
- top: tilemap_get_at_pixel(_layer_tiles, bbox_right + hspd, bbox_top),
- bottom: tilemap_get_at_pixel(_layer_tiles, bbox_right + hspd, bbox_bottom),
- };
- }
- else {
- return {
- top: tilemap_get_at_pixel(_layer_tiles, bbox_left + hspd, bbox_top),
- bottom: tilemap_get_at_pixel(_layer_tiles, bbox_left + hspd, bbox_bottom),
- };
- }
- };
- static _get_vertical_collisions = function(_layer_tiles) {
- if (vspd > 0) {
- return {
- left: tilemap_get_at_pixel(_layer_tiles, bbox_left, bbox_bottom + vspd),
- right: tilemap_get_at_pixel(_layer_tiles, bbox_right, bbox_bottom + vspd),
- };
- }
- else {
- return {
- left: tilemap_get_at_pixel(_layer_tiles, bbox_left, bbox_top + vspd),
- right: tilemap_get_at_pixel(_layer_tiles, bbox_right, bbox_top + vspd),
- };
- }
- };
- // get each tile layer in the room that contains the identifier substring
- var _layers_all = layer_get_all(); // <-- bake this into a var that get's calculated only once per room transition
- var _layers_collide = [];
- var _layer_name = "";
- var _layer_id = undefined;
- for (var _i = 0, _len = array_length(_layers_all); _i < _len; _i++) {
- _layer_id = _layers_all[_i];
- _layer_name = layer_get_name(_layer_id);
- if (string_contains(_layer_name, _collide_identifier)) {
- array_push(_layers_collide, _layer_id);
- }
- }
- // check for collisions with any of those tile layers
- var _layer_tiles = undefined;
- var _horizontal_collisions = undefined;
- var _horizontal_top = undefined;
- var _horizontal_bottom = undefined;
- var _vertical_collisions = undefined;
- var _vertical_left = undefined;
- var _vertical_right = undefined;
- for (var _i = 0, _len = array_length(_layers_collide); _i < _len; _i++) {
- _layer_id = _layers_collide[_i];
- _layer_tiles = layer_tilemap_get_id(_layer_id);
- // calculate horizontal collisions
- _horizontal_collisions = _get_horizontal_collisions(_layer_tiles);
- _horizontal_top = _horizontal_collisions.top;
- _horizontal_bottom = _horizontal_collisions.bottom;
- // if about to horizontally collide, ease into collision
- if (_horizontal_top != 0 || _horizontal_bottom != 0) {
- while (_horizontal_top == 0 && _horizontal_bottom == 0) {
- x += sign(hspd);
- // recalculate collisions to see if we should exit while() loop
- _horizontal_collisions = _get_horizontal_collisions(_layer_tiles);
- _horizontal_top = _horizontal_collisions.top;
- _horizontal_bottom = _horizontal_collisions.bottom;
- }
- hspd = 0;
- }
- // calculate vertical collisions
- _vertical_collisions = _get_vertical_collisions(_layer_tiles);
- _vertical_left = _vertical_collisions.left;
- _vertical_right = _vertical_collisions.right;
- // if about to vertically collide, ease into collision
- if (_vertical_left != 0 || _vertical_right != 0) {
- while (_vertical_left == 0 && _vertical_right == 0) {
- y += sign(vspd);
- // recalculate collisions to see if we should exit while() loop
- _vertical_collisions = _get_vertical_collisions(_layer_tiles);
- _vertical_left = _vertical_collisions.left;
- _vertical_right = _vertical_collisions.right;
- }
- vspd = 0;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement