diff --git a/Makefile b/Makefile index 310e7f1..620e4fa 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ HAML=/usr/bin/haml COFFEE = coffee +TSC=tsc LESSCSS=lessc SED=sed COMPILER=uglifyjs @@ -17,8 +18,14 @@ js/wordlist.js: src/wordlist.coffee $(COFFEE) --compile --no-header --bare --output js/ $< $(SED) -i -e '$$ s/;$$//' $@ -js/sat.js: src/sat.coffee - $(COFFEE) --compile --output js/ $< +js/sat.js: src/sat.ts + $(TSC) $< --outDir js/ --module "commonjs" + +js/test_sat.js: src/test_sat.ts + $(TSC) $< --outDir js/ --module "commonjs" + +test: js/test_sat.js js/sat.js + node_modules/nodeunit/bin/nodeunit js/test_sat.js style.css: src/style.less $(LESSCSS) $< $@ diff --git a/src/sat.ts b/src/sat.ts index 2ae26c2..db15656 100644 --- a/src/sat.ts +++ b/src/sat.ts @@ -113,7 +113,10 @@ var colliding = (shape1, shape2) => { return true; } - +module.exports = { + Vector: Vector, + colliding: colliding +} diff --git a/src/test_sat.ts b/src/test_sat.ts new file mode 100644 index 0000000..bc92de2 --- /dev/null +++ b/src/test_sat.ts @@ -0,0 +1,40 @@ +var testCase = require('nodeunit').testCase; +var Vector = require('./sat').Vector; + +module.exports = testCase({ + "TestAddition": (test) => { + var m = (new Vector(1, 1)).add(new Vector(-1, -1)); + test.ok(m.x == 0 && m.y == 0); + var n = (new Vector(1, 1)).add(new Vector(1, 1)); + test.ok(n.x == 2 && n.y == 2); + test.done(); + }, + + "TestScalar": (test) => { + var m = (new Vector(2, 2)).scalar(2); + test.ok(m.x == 4 && m.y == 4); + test.done(); + }, + + "TestMag2": (test) => { + var m = (new Vector(2, 2)).magnitude2(); + test.ok(m == 8); + test.done(); + }, + + "TestMag": (test) => { + var m = (new Vector(2, 2)).magnitude(); + test.ok(m == Math.sqrt(8)); + test.done(); + }, + + "TestNormalize": (test) => { + var m = (new Vector(5, 0)).normalize(); + test.ok(m.x == 1 && m.y == 0); + m = (new Vector(0, 5)).normalize(); + test.ok(m.x == 0 && m.y == 1); + m = (new Vector(4, 3)).normalize().magnitude2(); + test.ok(m == 1); + test.done(); + } +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..d554f2e --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + // types option has been previously configured + "types": [ + // add node as an option + "node" + ], + // typeroots option has been previously configured + "typeroots": [ + // add path to @types + "../node_modules/@types" + ] + } +}