Platform_JumpThrow_Rotation

Discussion in 'Tutorials' started by jonjons, Dec 5, 2018.

  1. jonjons

    jonjons Member

    Joined:
    May 10, 2017
    Posts:
    292
    GM Version: Studio 2
    Target Platform: Windows
    Download:
    https://drive.google.com/open?id=1amUC6qLRbWY5WEO7OWf2c4sJksFUXwiY

    MarketPlace: https://marketplace.yoyogames.com/assets/7923/platformjumprotation

    Links: N/A
    Description:
    Here's a small solution i wanted to share for jumping throw platforms that have a rotation value / collision.
    Ive searched a lot before doing it, i coulnt find anything related to this problem.

    The solution i found is very simple, before trying all the cos and sins and collisions checks.
    A simple place_metting(A) < place_metting(B) seems to work fine.



    Frist:
    Obj_wall is set to parent all of other platforms, and all wall/platform sprites are 32x32

    [​IMG]


    Heres the full code:

    Player:
    CreateEvent:

    Code:
    /// @description Insert description here
    
    canMove = 1; //--TOGGLE-MOVE-ON/OFF--
    walksp = 3; //--WALK-SPEED-
    hsp = 0; //--HORIZONTAL-
    vsp = 0; //--VERTICAL-
    grv = 0.2; //--GRAVITY-ACCELARATION-
    
    
    jumpSP = -7; //--JUMP-SPEED-
    
    platGo = 0; //--GOING-THROW-PLATFORMS--
    grounded = 0; //-0-pl-stand-/-2-pl-duck-/-1-pl-onAir---
    
    img_spd = 0;
    image_speed = img_spd;
    
    spr_ind = spr_plrIdle;
    sprite_index = spr_ind;
    
    depth = 0;
    Player:
    StepEvent:

    Code:
    /// @description Insert description here
    
    key_left = keyboard_check(vk_left);
    key_right = keyboard_check(vk_right);
    key_duck = keyboard_check(vk_down);
    key_jump = keyboard_check_pressed(ord("Z"));
    key_jump2 = keyboard_check_released(ord("Z"));
    key_fire= keyboard_check(ord("X"));
    
    
    
    
    //---//--GRAVITY--//----------------------------------------
    if (vsp < 14 && grounded == 1)
    {
        vsp += grv;
    }
    
    
    //---//--INPUT-LEFT-RIGHT--//-------------------------------
    if (canMove == 1)
    {
        var move = key_right - key_left;
        hsp = move * walksp;
    }
    
    //---//--PLR-JUMP--//----------------------------------------
    
    if ( key_jump && grounded == 0)
    {
        vsp = jumpSP;
    }
    
    if ( key_jump2 && vsp < 0 )
    {
        vsp/=2;
    }
    
    //-----------//-----PLAYER-MOVE-VERTICAL-----//--------------
    
    repeat(abs(vsp))
    {
        if (!place_meeting(x, y + sign(vsp), obj_wall_Parent))
        {
            y += sign(vsp);
        }
        else
        {
            vsp = 0;
            break;
        }
    }
    
    //-----------//-----PLAYER-MOVE-HORIZONTAL-----//------------
    if (canMove == 1)
    {
        repeat(abs(hsp))
        {
            // Move up slope
            if (place_meeting(x + sign(hsp), y, obj_wall_Parent) && !place_meeting(x + sign(hsp), y - 1, obj_wall_Parent))
            {    --y;    }
            // Move down slope
            if (!place_meeting(x + sign(hsp), y, obj_wall_Parent) && !place_meeting(x + sign(hsp), y + 1, obj_wall_Parent) && place_meeting(x + sign(hsp), y + 2, obj_wall_Parent))
            {    ++y;    }
    
            if (!place_meeting(x + sign(hsp), y, obj_wall_Parent))
            {
                x += sign(hsp);
            }
            else
            {
                hsp = 0;
                break;
            }
        }
    }
    
    //----------------------------------//---------//-------------
    
    //-------//--CALC-PL-VARS-TO-BE-USED-ON-PL-MODE--//--------------
    
    if ( place_meeting ( x, y+1, obj_wall_Parent) && key_duck)
    {
        image_index = 2;
        grounded = 2;  //----//-plr-duck-------------
        canMove = 0;
    }
    else if ( place_meeting ( x, y+1, obj_wall_Parent) && vsp >= 0 ) //--vsp-negative-moves-the-plr-UP
    {
        if ( hsp != 0 && ( key_right || key_left ))
        {
            image_index = 1;
        }
        else
        {
            image_index = 0;
        }
        grounded = 0;  //----//--plr-stand------------
        canMove = 1;
    }
    else
    {
        image_index = 3;
        grounded = 1;  //----//--plr-onAir----------------
        canMove = 1;
    }
    
    
    //-----//--PLR-FACING-RIGHT-LEFT--//--------------------------------
    //if ( key_right || key_left )
    //{
    //    image_xscale = key_right - key_left;
    //}
    
    if ( key_right )
    {
        image_xscale = 1;
    }
    else if ( key_left )
    {
        image_xscale = -1;
    }
    
    //------//--GOING-THORW-PLATFORMS--//--------------------------------
    if ( grounded == 2 && key_jump && platGo == 0)
    {
        platGo = 0.2;
    }
    if (platGo > 0)
    {
        platGo -= delta_time/1000000;
        platGo = clamp(platGo, 0, 1);
    }
    
    //------//--TEST--PLAYER-STUCK-INSIDE-WALL-//--------------------------------
    if (key_fire)
    {
        if (key_duck)
        {
            y += 2;
        }
        else
        {
            y -= 2;
        }
        vsp = 0;
    }
    
    
    
    //--------------//---PLATFORM-ROTATION---//-------------------
    if (instance_exists(obj_sensor))
    {
     
        var sensCHK = instance_place(x, y+10, obj_sensor);
        if (sensCHK != noone)
        {
            if ( place_meeting(x, y, sensCHK ) < place_meeting(x, y+22, sensCHK ) && platGo == 0 ) // 22
            {
                sensCHK.sensCHK = true;
                depth = -210;
            }
            else
            {
                sensCHK.sensCHK = false;
                depth = 0;
            }
        }
        else if (sensCHK == noone || platGo > 0 )
        {
            depth = 0;
            obj_sensor.sensCHK = false;
        }
    
    }
    Player:
    DrawEvent:

    Code:
    /// @description Insert description here
    // You can write your code in this editor
    
    draw_self();
    
    draw_text(x-30, y-80, "depth: " +string(depth) );
    
    var _inst = instance_place(x, y+10, obj_Platform_Rotation);
    if ( instance_exists(_inst) )
    {
        draw_text(x-50, y-100, "_inst: " +string(_inst) );
    }


    //------------------------//------//------------------


    obj_Platform_Rotation
    CreateEvent

    Code:
    /// @description Insert description here
    
    sensor = instance_create_depth(x, y, depth, obj_sensor);
    sensor.x = x;
    sensor.y = y;
    sensor.image_xscale = image_xscale;
    sensor.image_yscale = image_yscale;
    sensor.image_angle = image_angle;
    
    
    obj_Platform_Rotation
    StepEvent

    Code:
    /// @description
    // 22 defines the maximum player height, or the number to check between the two collisions
    
    if ( sensor.sensCHK == true )
    {
        sprite_index = spr_Platform_Rotation;
    }
    else
    {
        sprite_index = -1;
    }


    //------------------------//------//------------------


    obj_sensor
    CreateEvent

    Code:
    sensCHK = false;
     
    Last edited: Feb 7, 2019
    mamacato and NeZvers like this.
  2. Jack McRip

    Jack McRip Member

    Joined:
    Dec 25, 2018
    Posts:
    11
    Thank you
    :)
     
  3. jonjons

    jonjons Member

    Joined:
    May 10, 2017
    Posts:
    292
    Just updated the asset
    now the player can jump throw the platforms in the room only by interacting with the platform is on.
    This way it can achieve a chain ladder effect or street building ladder effect.

    Also fixed the previous slope problem, the player image xscale was being set to 0
    if the left and right keys were being pressed together, this would made him get stuck inside a wall.

     
    Amon likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice