Skip to content

Instantly share code, notes, and snippets.

@Joncom
Joncom / gist:e8e8d18ebe7fe55c3894
Last active September 11, 2021 00:29
Check if two line segments intersect
// Adapted from: http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect/1968345#1968345
function line_intersects(p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y) {
var s1_x, s1_y, s2_x, s2_y;
s1_x = p1_x - p0_x;
s1_y = p1_y - p0_y;
s2_x = p3_x - p2_x;
s2_y = p3_y - p2_y;
var s, t;
@Joncom
Joncom / impact-edit-map-util.ts
Last active May 25, 2020 08:23
Helper functions for ImpactJS to double or halve the tilesize of a BackgroundMap (without breaking their appearance)
export function halveTilesize( layer: Layer, tileset: Image ) {
if( layer.tilesize % 2 !== 0 || (layer.name !== 'collision' && !tileset.loaded) ) {
return false;
}
const oldTileWidth = tileset.width / layer.tilesize;
const newTileWidth = (tileset.width / layer.tilesize) * 2;
const newData = [];
for( let y = 0; y < layer.height; y++ ) {
newData[y * 2] = [];
@Joncom
Joncom / gist:bc7170c0767550eeeebcb77a97c0b362
Created March 3, 2019 08:23
How to download canvas.toDataURL image to disk
var link = document.createElement('a');
link.href = Main.backbuffer.g2canvas.canvas.canvas.toDataURL();
link.download = 'coolimage.jpg';
document.body.appendChild(link);
link.click();
@Joncom
Joncom / timestamp.php
Created December 18, 2017 15:58
Converting to and from UTC and Unix time
<?php
// Convert UTC time string to Unix time interger.
$timestamp = strtotime ( '2014-12-29 12:30:21+0000' );
// Convert Unix time integer back to UTC time string.
echo gmdate(DateTime::ISO8601, $timestamp);
@Joncom
Joncom / gist:59856f30f351510a656b3ffbf5ec633f
Created July 6, 2016 20:55
Sorting An Array By Multiple Criteria
ig.game.backgroundMaps.sort(function(a,b) {
var o1 = (a.repeat ? 0 : 1);
var o2 = (b.repeat ? 0 : 1);
var p1 = (a.foreground ? 1 : 0);
var p2 = (b.foreground ? 1 : 0);
if(o1 === o2) {
return (p1 < p2) ? -1 : (p1 > p2) ? 1 : 0;
} else {
return (o1 < o2) ? -1 : 1;
}
@Joncom
Joncom / mixin-grid-movement.js
Last active July 6, 2016 18:45
ImpactJS Mixin That Adds Tile-Based Grid Movement to Entities
ig.module('plugins.mixin-grid-movement')
.requires()
.defines(function() {
MixinGridMovement = {
src_tile: null,
dst_tile: null,
grid_move: function(direction, seconds) {
// The purpose of this program is to "pre-render" pixel
// perfect circles, so that they can be drawn from an image
// instead of using something like an arc function. The
// output image is a tilesheet that can be used in 2D pixel
// art style games.
// Originally I ran into an issue with common algorthithms
// (such as the midpoint circle algorithm) only supporting
// an increase in diameter of 2 pixels at a time. I wanted
// to capture single pixel size increases (thus doubling
@Joncom
Joncom / gist:7002036
Created October 16, 2013 03:03
Pokemon Ruby ROM Animation Research
P = ASM Pointer
S = Steps
F = Speed Factor
B = Speed Bitmask?
Q = Animation Slot
T = Start Tile
A = Tile Amount
I = Is Special Part Flag
H = Has Special Part Flag
R = Rotation Shifted Flag
@Joncom
Joncom / gist:6401880
Created September 1, 2013 02:16
Pokemon Ruby Map Tile Bits
Hex: 0130
Bin: 0000 0001 0011 00.00
Tile: 001
Attr: 0C
Hex: D405
Bin: 1101 0100 0000 01.01
Tile: 1D4
Attr: 01
@Joncom
Joncom / gist:6302577
Created August 22, 2013 02:35
Object.defineProperty Example
test = 1;
obj = { test: null };
Object.defineProperty(obj, 'test', {
get: function() { return test; },
set: function(value) { test = value }
});