Fangames > Game Design

Post your Code Snippets

<< < (4/5) > >>

Sudnep:
These are just a bunch of scripts I use for generic effects in avoidance. :) Hopefully someone finds these useful. If you have any problems or errors message me on discord @ Sudnep#2735

The way to use these scripts is to run these scripts is to run scrCreateEvent in the object's create event and then in the step event write whatever scripts you want the object to do within it. An example can be found here: link

(click to show/hide)This is a script that contains all of the variables for each other script which aren't in order. There are also two other variables in here called number and tag. The variables number and tag are useful for defining specific sets of objects.
Script: link

(click to show/hide)This is a script that when given the proper variables will change it's current image_alpha to the new desired image_alpha at a given rate.
Script: link

(click to show/hide)This is a script to toggle on and off bouncing.
Script: link

(click to show/hide)This is a script that lets objects bounce off the edges of the screen.
Script: link

(click to show/hide)This is a script to have the object curve it's direction over time.
Script: link

(click to show/hide)This is a script to change the current direction of the object to the new direction in the shortest amount of time.
Script: link

(click to show/hide)This is a script to orbit an object around a point or a specific object. The next script is used to define variables correctly since it's a bit complicated and annoying to do so by hand each time you want to use this.
Script: link

(click to show/hide)This is a script for applying the proper values to scrOrbit
Script: link

(click to show/hide)This is a script to toggle on and off destruction off screen for an object.
Script: link

(click to show/hide)This is a script to change the image_scale of an object over time when given the proper values.
Script: link

(click to show/hide)This is a script to change the speed of an object to a new speed over time.
Script: link

(click to show/hide)This is a script to spin the image of an object based on a given value.
Script: link

(click to show/hide)This is a script to spin the image of an object based on it's speed.
Script: link

(click to show/hide)This is a script to make the image of an object point in it's direction.
Script: link

(click to show/hide)This is a script to create a trail behind the object that will scale down over time as well as lose alpha over time.
Script: link

(click to show/hide)This is the object used in scrTrail
Script: link

(click to show/hide)This is a script to toggle on and off screen wrapping.
Script: link

A zip file containing all of the script.gml files and objTrail for studio can be found here: link

bananaguy12mhc:
Thank you so much Sudnep!  :atkHappy:

klazen108:
Easy Spotlight Shader - This one's a gmz because it comes with a sprite, shader, script, and example object and room! Shows a super easy method of creating spotlights without needing a huge sprite with a cutout, crazy blend modes, or any other complicated hacks - Just a function called "draw_spotlight"!

Zapmunk:
A little script I wrote to replace gamemaker's tragic lack of a move_contact_object function. Could be a good start for anyone who wants to convert the physics to non-solid blocks like me.

(click to show/hide)
--- Code: ---xamt = argument0;
yamt = argument1;
limit = argument2;

for (var i = 0; i < limit; i += 1) {
    var col = instance_place(x+xamt, y+yamt, objBlock);
    if (col != noone) {
        width = bbox_right-bbox_left;
        height = bbox_bottom - bbox_top;
        offx = x-bbox_left;
        offy = y-bbox_top;
        if (xamt > 0) {
            x = col.bbox_left-1 - width + offx;
        } else if (xamt < 0) {
            x = col.bbox_right+1 + offx;
        } else if (yamt > 0) {
            y = col.bbox_top-1 - height + offy;
        } else if (yamt < 0) {
            y = col.bbox_bottom+1 + offy;
        }
        return col;
    } else {
        x += xamt;
        y += yamt;
    }
}
return noone;
--- End code ---

It works more or less like move_contact_solid, except:

* The direction argument is given in vector component form. This is easy because this is only (supposed) to work for the cardinal directions, and will probably fail if you try to move diagonally with this. Best used with values of -1, 0, or 1 (higher numbers will decrease the step resolution)
* It can and will move you backwards if you intersect something and attempt to move to contact away from it- if you are checking for contact downwards, and your head is clipped into a ceiling, it will teleport you so your feet are on top of the block you were previously underneath
* It operates on bounding boxes, so sloped surfaces and anything with precise collision checking is out of the question. Everything will be treated as if it is rectangular.
* It returns the id of the object you moved to contact with, or noone if you did not collide with anything
* Due to platforms being children of blocks in the major engines I'm aware of, it will almost certainly royally screw up platform physics. You may need to unparent objBlock from them, and re-create the remaining platform functionality manually.

Zapmunk:
A fragment shader for converting to and from HSV. Useful for performing color effects.
The shader supplied here replaces the default image_blend functionality with something like the sprite editor's colorize function, but it can be easily be modified to do other HSV-color-space effects. (I recommend against using it for anything that doesn't require hue though, since saturation and volume can both be manipulated directly in RGB color space)

(click to show/hide)
--- Code: ---varying vec2 v_vTexcoord;
varying vec4 v_vColour;

#define EPSILON 0.00001

vec3 toHSV(vec3 col) {
    vec3 hsv = vec3(0.0);
    hsv.z = max(max(col.r, col.g), col.b); //volume
   
    float difference = hsv.z-min(min(col.r, col.g), col.b);
    hsv.y = difference / hsv.z; //saturation
   
    //possible hue ranges
    float redmax_hue = (col.g-col.b)/difference;
    float greenmax_hue = 2.0+(col.b-col.r)/difference;
    float bluemax_hue = 4.0+(col.r-col.g)/difference;
    //hue range weights
    float redweight = step(hsv.z, col.r);
    float greenweight = (1.0-redweight)*step(hsv.z, col.g);
    float blueweight = (1.0-redweight)*(1.0-greenweight);
   
    hsv.x = mod((redweight*redmax_hue +
            greenweight*greenmax_hue +
            blueweight*bluemax_hue)
            *60.0,
            360.0); //hue
   
    //div by zero handling
    //for cases of unsaturated or pure black
    //hue is zeroed, and saturation is also zero
    //since its gray, and only volume matters,
    //and we want to avoid div-by-zero fuckery
    hsv.xy *= step(EPSILON, difference)*step(EPSILON, hsv.z);
    return hsv;
}

//this function used to be more complicated but then it turns out it was trivial
float rerange(float x, float r) {
    return (x-r)/1.0; //div by one ResidentSleeper
}
vec3 toRGB(vec3 hsv) {
    //re-normalize hue to 0-6, because color hexagon reasons
    hsv.x = mod(hsv.x, 360.0);
    hsv.x /= 60.0;
   
    //weights for the possible hue ranges
    float ranges[6];
    for (int i = 0; i < 6; i++) {
        ranges[i] = step(float(i), hsv.x)*step(hsv.x, float(i+1));
    }
   
    //manual unrolling bitch
    //actually I dont even know how I would do this in a proper loop
    //shits going everywhere all at once
    //see https://en.wikipedia.org/wiki/File:HSV-RGB-comparison.svg for an explanation
    vec3 col = vec3(0.0);
    col += vec3(ranges[0], ranges[0]*rerange(hsv.x, 0.0), 0.0);
    col += vec3(ranges[1]*(1.0-rerange(hsv.x, 1.0)), ranges[1], 0.0);
    col += vec3(0.0, ranges[2], ranges[2]*rerange(hsv.x, 2.0));
    col += vec3(0.0, ranges[3]*(1.0-rerange(hsv.x, 3.0)), ranges[3]);
    col += vec3(ranges[4]*rerange(hsv.x, 4.0), 0.0, ranges[4]);
    col += vec3(ranges[5], 0.0, ranges[5]*(1.0-rerange(hsv.x, 5.0)));
   
    //We have the hue, now we just have to squoosh it into its correct sat/vol
    //I'm sure there's a clever one-liner to do this but I haven't found it
    col.r = mix(hsv.z-hsv.y, hsv.z, col.r);
    col.g = mix(hsv.z-hsv.y, hsv.z, col.g);
    col.b = mix(hsv.z-hsv.y, hsv.z, col.b);

    //handle fully-desaturated case
    //in case of div-by-zero fuckery       
    col = mix(col, vec3(hsv.z), step(hsv.y, 0.0));
   
    return col;
}

void main()
{
    vec4 base_col = texture2D(gm_BaseTexture, v_vTexcoord);
    vec3 hsv = toHSV(base_col.rgb);
   
    //you can do HSV fuckery here
    //Hue is 0.0-360.0 (degrees)
    //Saturation/volume are 0.0-1.0
    //access h, s, and v with x, y, and z
   
    hsv.x = toHSV(v_vColour.rgb).x;
   
    //and convert back to RGB here
    gl_FragColor = vec4(toRGB(hsv), base_col.a*v_vColour.a);
}
--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version