Skip to content

Instantly share code, notes, and snippets.

@pcp
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save pcp/ac9bd334a85b2334ed61 to your computer and use it in GitHub Desktop.

Select an option

Save pcp/ac9bd334a85b2334ed61 to your computer and use it in GitHub Desktop.

Revisions

  1. pcp revised this gist May 9, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Smart Codes
    Original file line number Diff line number Diff line change
    @@ -81,7 +81,7 @@
    {
    if (vspeed > 0 && !place_free(x, y + vspeed))
    {
    move_contact(270, -1);
    move_contact_all(270, -1);
    vspeed = 0;
    }
    }
  2. pcp created this gist May 8, 2015.
    283 changes: 283 additions & 0 deletions Smart Codes
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,283 @@
    /*
    From:http://gmc.yoyogames.com/?showtopic=423044
    */

    /// get_move_direction(dir)
    {
    var dir = argument0;
    dir.up = is_moving_up();
    dir.down = is_moving_down();
    dir.left = is_moving_left();
    dir.right = is_moving_right();
    return dir;
    }

    /// is_moving_up()
    {
    if (abs(direction) > 180)
    {
    return true;
    }
    return false;
    }

    /// is_moving_down()
    {
    if (abs(direction) < 180)
    {
    return true;
    }
    return false;
    }

    /// is_moving_left()
    {
    var dir = abs(direction);
    if (dir > 90 && dir < 270)
    {
    return true;
    }
    return false;
    }

    /// is_moving_right()
    {
    var dir = abs(direction);
    if (dir < 90 && dir > 270)
    {
    return true;
    }
    return false;
    }



    /// automatic_adjusting_screen(obj_player)
    /*
    Automatic adjusting screen
    This is probably one of the best "smart codes" I know. It resets your view
    so that "obj_player" is always perfectly in the middle.
    If you have a game where the player's sprite changes sizes or shapes,
    all you gotta do is run this code once after it changes sprites and it is perfect every time!
    Just make sure you have views turned on.
    */
    {
    var obj_player = argument0;
    view_hborder = view_wview/2 -obj_player.sprite_width;
    view_vborder = view_hview/2 -obj_player.sprite_height;
    }


    /// platformer_perfect_landing()
    /*
    Perfect landing (platformers)
    You may notice in your platformer that when you land, your character
    stops just above the ground, before landing, then drops the last few pixels.
    It can get very annoying at times.
    Use this code on your character when he comes in contact with the ground.
    It tells GM the exact amount of downward speed it needs to land perfectly.
    You won't notice any difference other than landing will be nice and smooth.
    */
    {
    if (vspeed > 0 && !place_free(x, y + vspeed))
    {
    move_contact(270, -1);
    vspeed = 0;
    }
    }

    /// object_orbit(orbitObject, spin)
    /*
    Rotate / orbit object
    This is a nifty little code that automatically makes the object go in circles
    around whatever object you put in the place of "obj_Player".
    It uses the objects sprite to find out how large of a circle it needs to make to go around it.
    Also, it will automatically jump into place so it doesn't matter where you create the rotating object in the room.
    It will jump into place and start rotating at the speed of the variable "spin" in this case.
    Just make sure this goes in your step event to keep it circling.
    */
    {
    var orbitObject = argument0;
    var spin = argument1;

    //spin += 10
    x = orbitObject.x + lengthdir_x(orbitObject.sprite_width * 1, spin);
    y = orbitObject.y + lengthdir_y(orbitObject.sprite_height * 1, spin);
    }

    /// degree_360_turning(facing)
    /*
    360 degree turning
    This is a very commonly used code for creating an object that can turn
    all the way around pointing toward the mouse at all times. It sets 1 subsprite (frame) for each direction.
    Works best when the sprite's first subsprite is facing right and the sprite has 36 frames and spinning counter clockwise.
    Now this code does that as well as makes the object face toward the mouse at all times.
    The 2nd line can be changed or taken out depending on where you want the object to face.
    */
    {
    var facing = argument0;
    image_single = facing / 10;
    facing = point_direction(x, y, mouse_x, mouse_y);
    }


    /// room_angling()
    /*
    Room angling
    This code is a little more advanced then you may think.
    It angles the entire room according to the object you put the code on (under step).
    Usually you'd want it on your main player which the view follows.
    This can give your game a really nice look but you must have a registered version of GM to do this.
    */
    {
    view_angle[0] =-direction + 90;
    image_angle = direction;
    }


    /// collision_push_off(other)
    /*
    Push-off Collision
    Basically, what this code does is gently 'pushes' off the object your colliding with,
    in the shortest possible pathway! So if you get 'stuck' in an object, using this will kick you out of it,
    or if you have object always going on-top of each other, using this will keep them apart...
    In short, a VERY handy code. I like to use it with while(place_meeting(x,y,other))
    */
    {
    var other = argument0;

    // in collision event with whatever object...
    var_dir = point_direction(x, y, other.x, other.y)
    x = x + lengthdir_x(-1, var_dir)
    y = y + lengthdir_y(-1, var_dir)
    }


    /// move_in_direction(dir, spd)
    /*
    Move in a direction.
    This moves the objects in a direction with a speed,
    without actually using direction and speed (it uses dir and spd ).
    This is good for pixel perfect collision checking, among other things.
    */
    {
    var dir = argument0;
    var spd = argument1;
    x += lengthdir_x(spd, dir)
    y += lengthdir_y(spd, dir)
    }

    /// object_keep_in_room(obj)
    /*
    Keep in the room.
    This keeps an object (it must have a sprite) inside the room.
    */
    {
    var obj = argument0;
    obj.x = min(max(obj.x, 0 + obj.sprite_xoffset), room_width + obj.sprite_xoffset - obj.sprite_width )
    obj.y = min(max(obj.y, 0 + obj.sprite_yoffset), room_height + obj.sprite_yoffset - obj.sprite_height )
    }


    /// number_random(from, to)
    /*
    Random number between x and y..
    Sets variable to a random number between x and y.
    You can set x and y as anything.. If it's 30 and 50, for example,
    it might set variable to 35 or 48.2, but never 56 and never 27.
    */
    {
    var from = agrument0;
    var to = agrument1;
    return floor(random(to - from)) + from;
    }

    /// isometric_depth(obj)
    /*
    The infamous isometric depth code.
    This makes things nearer to the bottom of the room be drawn in front of things above them, emulating real-life depth.
    */
    {
    var obj = argument0;
    obj.depth = -obj.y;
    }

    /// draw_grid(snapX, snapY)
    /*
    Grid Drawing
    Should be used in Draw Event. Draws a grid that fills up the whole room
    */
    {
    var snapX = agrument0;
    var snapY = agrument1;
    var i = 0;

    for (i = 0; i < room_width; i += snapX)
    {
    draw_line(i, 0, i, room_height);
    }
    for (i = 0; i < room_height; i += snapY)
    {
    draw_line(0, i, room_width, i);
    }
    }

    /// draw_background(x1, y1, y2, backgroundIndex)
    /*
    Faster drawn background
    Hey, I just figured out a much faster way of drawing a background tiled in an area.
    Use d3d_draw_floor, it should be much faster than manually drawing it using two for loops :D.
    Problem is it only works with texture friendly backgrounds (dimensions must be a power of 2).

    Remember to call this from a very deep object, or else it will draw it over other objects.
    */
    {
    var col;
    if (argument0 >= argument2) or (argument1 >= argument3)
    {
    return 0;
    }
    col = draw_get_color();
    draw_set_color(c_white);
    d3d_draw_floor(argument0, argument1, 0, argument2, argument3, 0,
    background_get_texture(argument4),
    (argument2 - argument0)/background_get_width(argument4),
    (argument3 - argument1)/background_get_height(argument4));
    draw_set_color(col);
    return 1;
    }

    /// collision_pixel_perfect(hSpeed, vSpeed)
    /*
    Pixel perfect collisions
    h is the variable to replace hspeed
    v is the variable to replace vspeed
    This will move a pixel individually on each axis, then check if there is a collision.
    Yes, I have tried converting h and v to a direction and speed type variable for even better collisions, but that didn't work...
    At all.

    NOTE: These variables are not affected by gravity, friction, etc. ...
    */
    {
    var v = argument0;
    var h = argument1;

    var vv hh;
    hh = 1;
    vv = 1;
    for(i=0;i<=max(abs(h),abs(v));i+=1)
    {
    if (i<abs(h) && hh)
    {
    x+=place_free(x+sign(h),y)*sign(h)
    h*=place_free(x+sign(h),y)
    hh=place_free(x+sign(h),y)
    }
    if (i<abs(v) && vv)
    {
    y+=place_free(x,y+sign(v))*sign(v)
    v*=place_free(x,y+sign(v))
    vv=place_free(x,y+sign(v))
    }
    }
    }