Advertisement
Limekys

LimekysUsefulFunctions

Mar 28th, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Game Maker 13.65 KB | Source Code | 0 0
  1. //Useful functions by Limekys (This script has MIT Licence)
  2. #macro LIMEKYS_USEFUL_FUNCTIONS_VERSION "2023.03.11"
  3.  
  4. #macro DT global.dt_steady
  5.  
  6. function Approach(_value, _dest, _amount) {
  7.     return (_value + clamp(_dest-_value, -_amount, _amount));
  8. }
  9.  
  10. function ApproachDelta(_value, _dest, _amount) {
  11.     return (_value + clamp(_dest-_value, -_amount*DT, _amount*DT));
  12. }
  13.  
  14. ///@arg {Real} value
  15. ///@arg {Real} destination
  16. ///@arg {Real} smoothness
  17. ///@arg {Real} threshold
  18. ///@returns {Real}
  19. function SmoothApproach(value, destination, smoothness, threshold = 0.01) {
  20.     var _difference = destination - value;
  21.     if (abs(_difference) < threshold) return destination;
  22.     return lerp(value, destination, 1/smoothness);
  23. }
  24.  
  25. ///@arg {Real} value
  26. ///@arg {Real} destination
  27. ///@arg {Real} smoothness
  28. ///@arg {Real} threshold
  29. ///@returns {Real}
  30. function SmoothApproachDelta(value, destination, smoothness, threshold = 0.01) {
  31.     var _difference = destination - value;
  32.     if (abs(_difference) < threshold) return destination;
  33.     return lerp(value, destination, 1/smoothness*DT*60); // 1/_smoothness*DT*60 // 1 - power(1 / _smoothness, DT)
  34. }
  35.  
  36. ///@func Chance(value)
  37. function Chance(value) {
  38.     return value > random(1);
  39. }
  40.  
  41. ///@func Wave(value1, value2, duration, offset)
  42. ///@desc Returns a value that will wave back and forth between [from-to] over [duration] seconds based on current time
  43. /// Examples:
  44. /// image_angle = Wave(-45,45,1,0)  -> rock back and forth 90 degrees in a second
  45. /// x = Wave(-10,10,0.25,0)         -> move left and right quickly
  46. /// Or here is a fun one! Make an object be all squishy!! ^u^
  47. /// image_xscale = Wave(0.5, 2.0, 1.0, 0.0)
  48. /// image_yscale = Wave(2.0, 0.5, 1.0, 0.0)
  49. function Wave(value1, value2, duration, offset) {
  50.     var _a = (value2 - value1) * 0.5;
  51.     return value1 + _a + sin((((current_time * 0.001) + duration * offset) / duration) * (pi*2)) * _a;
  52. }
  53.  
  54. function DrawSetText(_color = undefined, _font = undefined, _haling = undefined, _valing = undefined, _alpha = undefined) {
  55.     if _color != undefined draw_set_colour(_color);
  56.     if _font != undefined draw_set_font(_font);
  57.     if _haling != undefined draw_set_halign(_haling);
  58.     if _valing != undefined draw_set_valign(_valing);
  59.     if _alpha != undefined draw_set_alpha(_alpha);
  60. }
  61.  
  62. ///@desc DrawTextOutline(x,y,str,outwidth,outcol,outfidelity)
  63. ///Created by Andrew McCluskey http://nalgames.com/
  64. ///x,y: Coordinates to draw
  65. ///str: String to draw
  66. ///outwidth: Width of outline in pixels
  67. ///outcol: Colour of outline (main text draws with regular set colour)
  68. ///outfidelity: Fidelity of outline (recommended: 4 for small, 8 for medium, 16 for larger. Watch your performance!)
  69. function DrawTextOutline(_x, _y, _string, _outwidth, _outcolor, _outfidelity) {
  70.     var _dto_dcol = draw_get_color();
  71.     draw_set_color(_outcolor);
  72.     for(var dto_i = 45; dto_i < 405; dto_i += 360/_outfidelity) {
  73.         draw_text(_x + lengthdir_x(_outwidth, dto_i), _y + lengthdir_y(_outwidth, dto_i), _string);
  74.     }
  75.     draw_set_color(_dto_dcol);
  76.     draw_text(_x,_y,_string);
  77. }
  78.  
  79. /// @description DrawTextOutlineTransformed(x,y,str,outwidth,outcol,outfidelity)
  80. ///Created by Andrew McCluskey http://nalgames.com/
  81. ///Edited version by Limekys
  82. ///x,y: Coordinates to draw
  83. ///str: String to draw
  84. ///outwidth: Width of outline in pixels
  85. ///outcol: Colour of outline (main text draws with regular set colour)
  86. ///outfidelity: Fidelity of outline (recommended: 4 for small, 8 for medium, 16 for larger. Watch your performance!)
  87. function DrawTextOutlineTransformed(_x, _y, _string, _xscale, _yscale, _outwidth, _outcolor, _outfidelity) {
  88.     var _dto_dcol = draw_get_color();
  89.     draw_set_color(_outcolor);
  90.     for(var dto_i = 45; dto_i < 405; dto_i += 360/_outfidelity) {
  91.         draw_text_transformed(_x + lengthdir_x(_outwidth, dto_i), _y + lengthdir_y(_outwidth, dto_i), _string, _xscale, _yscale, 0);
  92.     }
  93.     draw_set_color(_dto_dcol);
  94.     draw_text_transformed(_x,_y,_string,_xscale,_yscale,0);
  95. }
  96.  
  97. function DrawTextShadow(_x, _y, _string) {
  98.     var _colour = draw_get_colour();
  99.     draw_set_colour(c_black);
  100.     draw_text(_x+1, _y+1, _string);
  101.     draw_set_colour(_colour);
  102.     draw_text(_x, _y, _string);
  103. }
  104.  
  105. ///@desc example: DrawTextSoftShadow(10,10,"Hello World!", font_name, c_white, c_black, 0,5,6,0.01, );
  106. function DrawTextSoftShadow(_x, _y, _string, _font, _offset_x, _offset_y, _blurfactor, _shadow_colour, _shadow_strenght, _text_colour) {
  107.     draw_set_font(_font);
  108.     var _shadow_strenght_calc = _shadow_strenght/(_blurfactor * _blurfactor)
  109.     draw_set_alpha(_shadow_strenght_calc);
  110.     draw_set_colour(_shadow_colour);
  111.    
  112.     var _bx = _blurfactor/2;
  113.     var _by = _blurfactor/2;
  114.  
  115.     for (var ix = 0; ix < _blurfactor; ix++) {
  116.         for (var iy = 0; iy < _blurfactor; iy++) {
  117.             draw_text((_x-_bx) +_offset_x + ix, (_y-_by) +_offset_y + iy, _string);
  118.         }
  119.     }
  120.     draw_set_alpha(1);
  121.     draw_set_colour(_text_colour);
  122.     draw_text(_x, _y, _string);
  123. }
  124.  
  125. function DrawRectangleWidth(x1, y1, x2, y2, _width, _inside, _outline) {
  126.     if _inside == false
  127.     for (var i = 0; i < _width; ++i) {
  128.         draw_rectangle(x1-i,y1-i,x2+i,y2+i,_outline);
  129.     }
  130.     else
  131.     for (var i = 0; i < _width; ++i) {
  132.         draw_rectangle(x1+i,y1+i,x2-i,y2-i,_outline);
  133.     }
  134. }
  135.  
  136. ///@desc StringZeroes(string,nubmer)
  137. ///Returns _string as a string with zeroes if it has fewer than _nubmer characters
  138. ///eg StringZeroes(150,6) returns "000150" or
  139. ///StringZeroes(mins,2)+":"+StringZeroes(secs,2) might return "21:02"
  140. ///Created by Andrew McCluskey, use it freely
  141. function StringZeroes(_string, _nubmer) {
  142.     var _str = "";
  143.     if string_length(string(_string)) < _nubmer {
  144.         repeat(_nubmer-string_length(string(_string))) _str += "0";
  145.     }
  146.     _str += string(_string);
  147.     return _str;
  148. }
  149.  
  150. ///@desc DrawCircleCurve(x,y,r,bones,ang,angadd,width,outline)
  151. ///x,y — Center of circle.
  152. ///r — Radius.
  153. ///bones — Amount of bones. More bones = more quality, but less speed. Minimum — 3.
  154. ///ang — Angle of first circle's point.
  155. ///angadd — Angle of last circle's point (relative to ang).
  156. ///width — Width of circle (may be positive or negative).
  157. ///outline — 0 = curve, 1 = sector.
  158. function DrawCircleCurve(_xx, _yy, _radius, _bones, _angle, _angleadd, _width, _outline) {
  159.     _bones = max(3,_bones);
  160.    
  161.     var a,lp,lm,dp,dm,AAa,Wh;
  162.     a = _angleadd/_bones;
  163.     Wh = _width/2;
  164.     lp = _radius+Wh;
  165.     lm = _radius-Wh;
  166.     AAa = _angle+_angleadd;
  167.    
  168.     if _outline {
  169.         //OUTLINE
  170.         draw_primitive_begin(pr_trianglestrip); //Change to pr_linestrip, to see how it works.
  171.         draw_vertex(_xx+lengthdir_x(lm,_angle),_yy+lengthdir_y(lm,_angle)); //First point.
  172.         for(var i=1; i<=_bones; ++i)
  173.         {
  174.             dp = _angle+a*i;
  175.             dm = dp-a;
  176.             draw_vertex(_xx+lengthdir_x(lp,dm),_yy+lengthdir_y(lp,dm));
  177.             draw_vertex(_xx+lengthdir_x(lm,dp),_yy+lengthdir_y(lm,dp));
  178.         }
  179.         draw_vertex(_xx+lengthdir_x(lp,AAa),_yy+lengthdir_y(lp,AAa));
  180.         draw_vertex(_xx+lengthdir_x(lm,AAa),_yy+lengthdir_y(lm,AAa)); //Last two points to make circle look right.
  181.         //OUTLINE
  182.     } else {
  183.         //SECTOR
  184.         draw_primitive_begin(pr_trianglefan); //Change to pr_linestrip, to see how it works.
  185.         draw_vertex(_xx,_yy); //First point in the circle's center.
  186.         for(var i=1; i<=_bones; ++i)
  187.         {
  188.             dp = _angle+a*i;
  189.             dm = dp-a;
  190.             draw_vertex(_xx+lengthdir_x(lp,dm),_yy+lengthdir_y(lp,dm));
  191.         }
  192.         draw_vertex(_xx+lengthdir_x(lp,AAa),_yy+lengthdir_y(lp,AAa)); //Last point.
  193.         //SECTOR
  194.     }
  195.     draw_primitive_end();
  196. }
  197.  
  198. function DrawHealthbarCircular(center_x, center_y, _radius, _start_ang, _health, _sprite) {
  199.     var tex,steps,thick,oc;
  200.     tex = sprite_get_texture(_sprite,0);
  201.     steps = 200;
  202.     thick = sprite_get_height(_sprite);
  203.  
  204.     if ceil(steps*(_health/100)) >= 1 {
  205.        
  206.         oc = draw_get_color();
  207.         draw_set_color(c_white);
  208.        
  209.         var step,ang,side,hps,hpd;
  210.         step = 0;
  211.         ang = _start_ang;
  212.         side = 0;
  213.         draw_primitive_begin_texture(pr_trianglestrip,tex);
  214.         draw_vertex_texture(center_x+lengthdir_x(_radius-thick/2+thick*side,ang),center_y+lengthdir_y(_radius-thick/2+thick*side,ang),side,side);
  215.         side = !side;
  216.         draw_vertex_texture(center_x+lengthdir_x(_radius-thick/2+thick*side,ang),center_y+lengthdir_y(_radius-thick/2+thick*side,ang),side,side);
  217.         side = !side;
  218.         draw_vertex_texture(center_x+lengthdir_x(_radius-thick/2+thick*side,ang+360/steps),center_y+lengthdir_y(_radius-thick/2+thick*side,ang+360/steps),side,side);
  219.         side = !side;
  220.         hps = _health/(ceil(steps*(_health/100))+1);
  221.         hpd = 0;
  222.         repeat ceil(steps*(_health/100)+1) {
  223.             step++;
  224.             if step == ceil(steps*(_health/100)+1) { //final step
  225.                 ang += (360/steps)*(_health - hpd)/2;
  226.                 if ang>_start_ang+360 ang=_start_ang+360
  227.                 draw_vertex_texture(center_x+lengthdir_x(_radius-thick/2+thick*side,ang),center_y+lengthdir_y(_radius-thick/2+thick*side,ang),side,side);
  228.                 side = !side;
  229.                 draw_vertex_texture(center_x+lengthdir_x(_radius-thick/2+thick*side,ang),center_y+lengthdir_y(_radius-thick/2+thick*side,ang),side,side);
  230.             }
  231.             else {
  232.                 ang+=360/steps;
  233.                 draw_vertex_texture(center_x+lengthdir_x(_radius-thick/2+thick*side,ang),center_y+lengthdir_y(_radius-thick/2+thick*side,ang),side,side);
  234.                 side = !side;
  235.             }
  236.             hpd += hps;
  237.         }
  238.         draw_primitive_end();
  239.         draw_set_color(oc);
  240.     }
  241. }
  242.  
  243. function print_format(_string, struct) {
  244.     var list = variable_struct_get_names(struct);
  245.     for(var i = 0; i < array_length(list); i++){
  246.         var find = "${"+list[i]+"}"
  247.         _string = string_replace_all(_string, find, struct[$ list[i]]);
  248.     }
  249.     return _string;
  250. }
  251.  
  252. function print() {
  253.     var time = print_format("[${hour}:${minute}:${second}]",{
  254.         hour: current_hour,
  255.         minute: current_minute,
  256.         second: current_second
  257.     });
  258.     var caller = print_format("[${caller}]",{caller: argument[0]});
  259.     var msg = "";
  260.     for(var i = 1; i < argument_count; i++){
  261.         msg += string(argument[i]) + " ";
  262.     }
  263.     var result = time+caller+": "+msg;
  264.     show_debug_message(result);
  265.     //ds_list_insert(global.console_output, 0, result);
  266.     //var file = file_text_open_append(global.LOG_FILE);
  267.     //file_text_write_string(file,result);
  268.     //file_text_writeln(file);
  269.     //file_text_close(file);
  270. }
  271.  
  272. ///@desc Saves a successively numbered screenshot within the working directory
  273. ///Returns true on success, false otherwise.
  274. ///name prefix to assign screenshots, string
  275. ///folder subfolder to save to (eg. "screens\"), string
  276. function SaveScreenshot(name) {
  277.     var i = 0, fname;
  278.    
  279.     if !directory_exists(working_directory + "Screenshots") directory_create(working_directory+"Screenshots");
  280.     // If there is a file with the current name and number,
  281.     // advance counter and keep looking:
  282.     do {
  283.         fname = working_directory+"\\" + "Screenshots\\" + name + "_" + string(i) + ".png";
  284.         i++;
  285.     } until (!file_exists(fname));
  286.     // Once we've got a unused number we'll save the screenshot under it:
  287.     screen_save(fname);
  288.     return file_exists(fname);
  289. }
  290.  
  291. ///@desc Returns the background data captured on the screen which can then be drawn later on.
  292. function MakeScreenshot() {
  293.     var ret = -1;
  294.     var sfc_width = surface_get_width(application_surface);
  295.     var sfc_height = surface_get_height(application_surface);
  296.  
  297.     // Create drawing surface
  298.     var sfc = surface_create(sfc_width,sfc_height);
  299.     surface_set_target(sfc);
  300.     // Clear screen;
  301.     // Both draw_clear and draw_rectangle will clear your screen BUT
  302.     // on some systems, it creates ghostly images, for example, when
  303.     // sprites are animated. To prevent those images, both are used.
  304.     var _gpu_cwe = gpu_get_colorwriteenable();
  305.     gpu_set_colorwriteenable(true, true, true, true);
  306.     draw_clear_alpha(c_white, 0);
  307.     draw_rectangle_colour(0,0,sfc_width,sfc_height,c_black,c_black,c_black,c_black,false);
  308.     gpu_set_colorwriteenable(true, true, true, false);
  309.     // Capture screen
  310.     draw_surface(application_surface,0,0);
  311.     ret = sprite_create_from_surface(sfc, 0, 0, sfc_width, sfc_height, false, false, 0, 0);
  312.     // Finalise drawing process and clear surface from memory
  313.     surface_reset_target();
  314.     gpu_set_colorwriteenable(_gpu_cwe[0], _gpu_cwe[1], _gpu_cwe[2], _gpu_cwe[3]);
  315.     surface_free(sfc);
  316.  
  317.     return ret;
  318. }
  319.  
  320. function DrawSpriteOffset(sprite, subimg, pos_x, pos_y, xscale = 1, yscale = 1, rotation = 0, color = c_white, alpha = 1, x_offset = 0, y_offset = 0) {
  321.     //Calculate rotation
  322.     var _c = dcos(rotation);
  323.     var _s = dsin(rotation);
  324.     var _rot_x = _c * x_offset + _s * y_offset;
  325.     var _rot_y = _c * y_offset - _s * x_offset;
  326.     //Draw
  327.     draw_sprite_ext(sprite, subimg, pos_x - _rot_x, pos_y - _rot_y, xscale, yscale, rotation, color, alpha);
  328. }
  329.  
  330. function DrawTextSpriteShadow(pos_x, pos_y, text, sprite_shadow) {
  331.     var _txt_w = string_width(text) + 70;
  332.     //var _txt_h = string_height(_text);
  333.    
  334.     draw_sprite_stretched_ext(sprite_shadow, 0, pos_x - _txt_w/2, pos_y - 44, _txt_w, 88, c_white, 0.5);
  335.     draw_text(pos_x, pos_y, text);
  336. }
  337.  
  338. ///@desc Returns new range from old range
  339. function Range(value, old_min, old_max, new_min, new_max) {
  340.     return ((value - old_min) / (old_max - old_min)) * (new_max - new_min) + new_min;
  341. }
  342.  
  343. ///@arg {String} name
  344. ///@arg {Real} seconds
  345. ///@arg {Function} _function
  346. ///@arg {Real} start_from
  347. function IntervalUpdateFunction(name, seconds, _function, start_from = -1) {
  348.     var n1 = name + "_interval";
  349.     var n2 = name + "_interval_lenght";
  350.     if !variable_instance_exists(self, n1) self[$ n1] = 0;
  351.     if !variable_instance_exists(self, n2) self[$ n2] = start_from == -1 ? seconds : start_from;
  352.    
  353.     self[$ n1] += DT;
  354.     if self[$ n1] >= self[$ n2] {
  355.         self[$ n1] -= self[$ n2];
  356.         _function();
  357.     }
  358. }
  359.  
  360. ///@arg {Array} _array
  361. function ArrayShuffle(_array) {
  362.     var _size = array_length(_array), i, j, k;
  363.     for (i = 0; i < _size; i++)
  364.     {
  365.         j = irandom_range(i, _size - 1);
  366.         if (i != j)
  367.         {
  368.             k = _array[i];
  369.             _array[i] = _array[j];
  370.             _array[j] = k;
  371.         }
  372.     }
  373. }
Tags: Game Maker
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement