Skip to content

Instantly share code, notes, and snippets.

View katsaii's full-sized avatar
🟣
Icon by NightMargin

Kat katsaii

🟣
Icon by NightMargin
View GitHub Profile
@katsaii
katsaii / object_is_singleton.gml
Created September 14, 2019 00:24
A utility for enforcing some singleton prefix on objects in GameMaker. The default prefix is "the", i.e. `the_singletonName`.
gml_pragma("global", @'
// search for singletons
global.singletons = ds_list_create();
for (var i = 0; object_exists(i); i++) {
if (object_prefix(i) == SINGLETON_PREFIX) {
ds_list_add(global.singletons, i);
// automatically insert singleton into the first room
object_set_persistent(i, true);
room_instance_add(room_first, 0, 0, i);
}
@katsaii
katsaii / draw_sprite_region.gml
Last active September 14, 2019 00:44
Draws a sprite within a fixed region, resizing where necessary. Maintains the aspect ratio and 'whitespace' of the sprite.
/// @desc Draws a sprite in the middle of some fixed region, scaling where appropriate.
/// @param sprite {Integer} The id of the sprite to draw.
/// @param index {Integer} The image index of the sprite to draw.
/// @param x1 {Integer} The left-most position of the region.
/// @param y1 {Integer} The top-most position of the region.
/// @param x2 {Integer} The right-most position of the region.
/// @param y2 {Integer} The bottom-most position of the region.
/// @author Kat @katsaii
var x1 = argument2 < argument4 ? argument2 : argument4;
var y1 = argument3 < argument5 ? argument3 : argument5;
@katsaii
katsaii / draw_sprite_bbox.gml
Last active September 14, 2019 00:17
Draws a sprites corrected bounding box.
/// @desc Draws the bounding box outline for this sprite. Only supports axis-aligned and oriented bounding boxes.
/// @param sprite {Real} The sprite to draw.
/// @param subimg {Real} The subimage of the sprite to use.
/// @param x {Real} X position.
/// @param y {Real} Y position.
/// @param xscale {Real} X scale.
/// @param yscale {Real} Y scale.
/// @param rot {Real} Rotation.
/// @param outline {Boolean} Whether to draw an outline or use a solid fill.
/// @author Kat @katsaii
@katsaii
katsaii / mouse_click_enabled.gml
Last active September 16, 2019 18:37
A script for enabling/disabling mouse input in GameMaker.
gml_pragma("global", @'
global.mouse_clicks = true;');
/// @desc Disables/enables mouse clicks.
/// @param enable {Bool} Whether to enable mouse clicks.
/// @author Kat @katsaii
var enable = bool(argument0);
if (enable ^^ global.mouse_clicks) then global.mouse_clicks = enable;
@katsaii
katsaii / keyboard_enabled.gml
Last active September 13, 2019 23:20
A script for enabling/disabling keyboard input in GameMaker.
gml_pragma("global", @'
global.keyboard_mapping = undefined;');
/// @desc Disables/enables the keyboard.
/// @param enable {Bool} Whether to enable the keyboard.
/// @author Kat @katsaii
if (argument0) {
// revert remaps
if (global.keyboard_mapping != undefined) {
var maps = global.keyboard_mapping;
var key = ds_map_find_first(maps);
@katsaii
katsaii / download.bat
Last active August 14, 2019 14:22
A wrapper to shorten the youtube-dl command 'youtube-dl --extract-audio --audio-format (fmt) (url)' to simply 'download (fmt) (url)'.
@echo off
set fmt=%1
set url=%*
:: Strip fmt from url
set s=%fmt%
:loop
if [%s%] NEQ [] (
set s=%s:~1%
set url=%url:~1%
goto loop
@katsaii
katsaii / BooleanAlgebra.agda
Last active March 18, 2019 17:43
A basic Boolean algebra written in Agda, supporting all typical logic gates plus converse implication, equality, and order relations.
-- define boolean class
data Boolean : Set where
true false : Boolean
{-
truth table
(a.k.a xnor)
false | A and B | A < B | B | A > B | A | A xor B | A or B | A nor B | A == B | not A | A imply B | not B | A con B | A nand B | true
------|---------|-------|---|-------|---|---------|--------|---------|--------|-------|-----------|-------|---------|----------|------
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
@katsaii
katsaii / window_set_size_centre.gml
Last active September 13, 2019 23:20
Sets the size of a window in GameMaker and centres to the first display in a single frame.
gml_pragma("forceinline");
/// @desc Sets the window to the desired size and centres it.
/// @param w {Integer} The width to set the window to.
/// @param h {Integer} The height to set the window to.
window_set_rectangle(
(display_get_width() - argument0) div 2,
(display_get_height() - argument1) div 2,
argument0, argument1);
@katsaii
katsaii / draw_grid.gml
Last active September 13, 2019 22:47
Obfuscated code to draw the contents of a 2D array to the screen on a single line + JSDoc information.
///@param id,size,x,y
var G=argument0,W,H,S=argument1,i=0,j,k,a,b;if ds_exists(G,5){W=ds_grid_width(G)H=ds_grid_height(G)while(i<W*H){j=i%W;k=i++div W;a=argument2+j*S;b=argument3+k*S;draw_rectangle(a,b,a+S,b+S,1)draw_text(a,b,G[#j,k])}}
@katsaii
katsaii / point_distance_line.gml
Last active September 13, 2019 23:20
Finds the distance between a point and a line
/// @desc Finds the minimum distance between a point and a 2D line.
/// @param x {Real} The X position of the point.
/// @param y {Real} The Y position of the point.
/// @param u1 {Real} The first X position of the line.
/// @param w1 {Real} The first Y position of the line.
/// @param u2 {Real} The second X position of the line.
/// @param w2 {Real} The second Y position of the line.
var qx = argument0;
var qy = argument1;
var px = argument2;