• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Tile Collision / New Rooms ?

L

Lithalean

Guest
Scripts:
tile_collide_at_points
Code:
///@param tile_map_id
///@param point_arrays...
var tile_map_id = argument[0];

// Found variable
var found = false;

// for the point arrays
var vector2_x = 0;
var vector2_y = 1;

// Loop through the points and check for a tile
for (var i=1; i<argument_count; i++) {
    var point = argument[i];
    found = found || tilemap_get_at_pixel(tile_map_id, point[vector2_x], point[vector2_y]);
}

// return found
return found;
move_and_contact_tiles
Code:
///@param tile_map_id
///@param tile_size
///@param velocity_array
var tile_map_id = argument0;
var tile_size = argument1;
var velocity = argument2;

// For the velocity array
var vector2_x = 0;
var vector2_y = 1;

// Move horizontally
x += velocity[vector2_x];

// Right collisions
if velocity[vector2_x] > 0 {
    var tile_right = tile_collide_at_points(tile_map_id, [bbox_right-1, bbox_top], [bbox_right-1, bbox_bottom-1]);
    if tile_right {
        x = bbox_right & ~(tile_size-1);
        x -= bbox_right-x;
        velocity[@ vector2_x] = 0;
    }
} else {
    var tile_left = tile_collide_at_points(tile_map_id, [bbox_left, bbox_top], [bbox_left, bbox_bottom-1]);
    if tile_left {
        x = bbox_left & ~(tile_size-1);
        x += tile_size+x-bbox_left;
        velocity[@ vector2_x] = 0;
    }
}

// Move vertically
y += velocity[vector2_y];

// Vertical collisions
if velocity[vector2_y] > 0 {
    var tile_bottom = tile_collide_at_points(tile_map_id, [bbox_left, bbox_bottom-1], [bbox_right-1, bbox_bottom-1]);
    if tile_bottom {
        y = bbox_bottom & ~(tile_size-1);
        y -= bbox_bottom-y;
        velocity[@ vector2_y] = 0;
    }
} else {
    var tile_top = tile_collide_at_points(tile_map_id, [bbox_left, bbox_top], [bbox_right-1, bbox_top]);
    if tile_top {
        y = bbox_top & ~(tile_size-1);
        y += tile_size+y-bbox_top;
        velocity[@ vector2_y] = 0;
    }
}

Player Events:
o_player Events: Create
Code:
/// @description Initalize
velocity_ = [0, 0];
gravity_ = 1;
jump_speed_ = 28;
max_velocity_ = [8, 32];
acceleration_ = 2.1;

// Spine
skeleton_animation_mix("IDLE", "WALK", 0.03);
skeleton_animation_mix("WALK", "IDLE", 0.03);
skeleton_animation_set("IDLE");

// Get the tilemap id
var layer_id = layer_get_id("Collisions");
collision_tile_map_id_ = layer_tilemap_get_id(layer_id);
o_player Events: Step
Code:
/// @description Movement logic
// Get the input
var x_input = (keyboard_check(vk_right) - keyboard_check(vk_left)) * acceleration_;

// Vector variables
var vector2_x = 0;
var vector2_y = 1;

// Horizontal movement
velocity_[vector2_x] = clamp(velocity_[vector2_x]+x_input, -max_velocity_[vector2_x], max_velocity_[vector2_x]);

// Friction
if x_input == 0 {
    velocity_[vector2_x] = lerp(velocity_[vector2_x], 0, .2);
}

// Gravity
velocity_[vector2_y] += gravity_;

// Move and contact tiles
move_and_contact_tiles(collision_tile_map_id_, 128, velocity_);

// Jumping
var on_ground = tile_collide_at_points(collision_tile_map_id_, [bbox_left, bbox_bottom], [bbox_right-1, bbox_bottom]);
if on_ground {
    // Jumping
    if keyboard_check_pressed(vk_up) {
        velocity_[vector2_y] = -jump_speed_;
    }
} else {
    // Control jump height
    if keyboard_check_released(vk_up) && velocity_[vector2_y] <= -(jump_speed_/3) {
        velocity_[vector2_y] = -(jump_speed_/3);
    }
}

// Spine
if keyboard_check_pressed(vk_right) || keyboard_check_pressed(vk_left) {
    moving = true;
    skeleton_animation_set("WALK");
}
if keyboard_check_released(vk_right) || keyboard_check_released(vk_left) {
    moving = false;
    skeleton_animation_set("IDLE");
}
o_player Events: Collision (o_door)
Code:
/// Go through the door
if (room_exists(other.room_)) {
    persistent = true;
    room_goto(other.room_)
    x = other.start_x;
    y = other.start_y;
}

Game Events:
o_game Events: Room Start
Code:
if instance_exists(global.player_start_position) {
    if instance_exists(o_player) {
        o_player.persistent = false;
        o_player.x = global.player_start_position.x;
        o_player.y = global.player_start_position.y;
        o_player.layer = layer_get_id("Instances");
    } else {
        var start_x = global.player_start_position.x;
        var start_y = global.player_start_position.y;
        instance_create_layer(start_x, start_y, "Instances", o_player);
    }
    global.player_start_position = noone;
}
i_game_start Creation Code
Code:
var _start_x = global.player_start_position.x;
var _start_y = global.player_start_position.y;

instance_create_layer(_start_x, _start_y, "Instances", o_player);


OK, so everything works fine, but when I go to a new room, my collision tiles don't work. If I start the game in the second room, they work fine, but if I then go into room one, they don't work again.
I've also read multiple times that making your player persistent is a bad idea. Any advice on that would be appreciated as well.
Thank you for your time.
 
Last edited by a moderator:

TheouAegis

Member
Well considering you only set the collision_tile_map_ in the player's create event, you need to make sure the player's actually being created every room. That may mean destroying the player at the end of each room.
 
Top