Last active
June 14, 2022 13:41
-
-
Save Sheepolution/f1002b489f4ad83e1e78289eebf0d32e to your computer and use it in GitHub Desktop.
gamera.lua converted to JavaScript. Original: https://github.com/kikito/gamera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gamera.js | |
// Based on gamera.lua v1.0.1 by Enrique García Cota, converted to JavaScript by Sheepolution | |
// Copyright (c) 2018 Enrique García Cota | |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
// Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra | |
// Private attributes and methods | |
const abs = Math.abs; | |
const min = Math.min; | |
const max = Math.max; | |
const clamp = (x, minX, maxX) => { | |
return x < minX ? minX : (x > maxX ? maxX : x); | |
}; | |
const checkNumber = (value, name) => { | |
if (typeof value != 'number') { | |
console.error(name + ' must be a number (was: ' + toString(value) + ')'); | |
} | |
}; | |
const checkPositiveNumber = (value, name) => { | |
if (typeof value != 'number' || value <= 0) { | |
console.error(name + ' must be a positive number (was: ' + toString(value) + ')'); | |
} | |
}; | |
const checkAABB = (l, t, w, h) => { | |
checkNumber(l, 'l'); | |
checkNumber(t, 't'); | |
checkPositiveNumber(w, 'w'); | |
checkPositiveNumber(h, 'h'); | |
}; | |
const getVisibleArea = (self, scale) => { | |
scale = scale ?? self.scale; | |
const sin = abs(self.sin); | |
const cos = abs(self.cos); | |
let w = self.w / scale; | |
let h = self.h / scale; | |
w = cos * w + sin * h; | |
h = sin * w + cos * h; | |
return [min(w, self.ww), min(h, self.wh)]; | |
}; | |
const cornerTransform = (self, x, y) => { | |
const scale = self.scale; | |
const sin = self.sin; | |
const cos = self.cos; | |
x = x - self.x; | |
y = y - self.y; | |
x = -cos * x + sin * y; | |
y = -sin * x - cos * y; | |
return [self.x - (x / scale + self.l), self.y - (y / scale + self.t)]; | |
}; | |
const adjustPosition = (self) => { | |
const wl = self.wl; | |
const wt = self.wt; | |
const ww = self.ww; | |
const wh = self.wh; | |
const [w, h] = getVisibleArea(self); | |
const w2 = w * 0.5; | |
const h2 = h * 0.5; | |
const left = wl + w2; | |
const right = wl + ww - w2; | |
const top = wt + h2; | |
const bottom = wt + wh - h2; | |
self.x = clamp(self.x, left, right); | |
self.y = clamp(self.y, top, bottom); | |
}; | |
const adjustScale = (self) => { | |
const ww = self.ww; | |
const wh = self.wh; | |
const [rw, rh] = getVisibleArea(self, 1); // rotated frame: area around the window, rotated without scaling | |
const sx = rw / ww; | |
const sy = rh / wh; // vert/horiz scale: minimun scales that the window needs to occupy the world | |
const rscale = max(sx, sy); | |
self.scale = max(self.scale, rscale); | |
}; | |
// Public interface | |
const getGamera = (canvas, l, t, w, h) => { | |
const sw = canvas.width; | |
const sh = canvas.height; | |
const cam = { | |
x: 0, | |
y: 0, | |
scale: 1, | |
angle: 0, | |
sin: Math.sin(0), | |
cos: Math.cos(0), | |
l: 0, | |
t: 0, | |
w: sw, | |
h: sh, | |
w2: sw * 0.5, | |
h2: sh * 0.5 | |
}; | |
cam.setWorld = function (l, t, w, h) { | |
checkAABB(l, t, w, h); | |
this.wl = l; | |
this.wt = t; | |
this.ww = w; | |
this.wh = h; | |
adjustPosition(this); | |
}; | |
cam.setWindow = function (l, t, w, h) { | |
checkAABB(l, t, w, h); | |
this.l = l; | |
this.t = t; | |
this.w = w; | |
this.h = h; | |
this.w2 = w * 0.5; | |
this.h2 = h * 0.5; | |
adjustPosition(this); | |
}; | |
cam.setPosition = function (x, y) { | |
checkNumber(x, 'x'); | |
checkNumber(y, 'y'); | |
this.x = x; | |
this.y = y; | |
adjustPosition(this); | |
}; | |
cam.setScale = function (scale) { | |
checkNumber(scale, 'scale'); | |
this.scale = scale; | |
adjustScale(this); | |
adjustPosition(this); | |
}; | |
cam.setAngle = function (angle) { | |
checkNumber(angle, 'angle'); | |
this.angle = angle; | |
this.cos = Math.cos(angle); | |
this.sin = Math.sin(angle); | |
adjustScale(this); | |
adjustPosition(this); | |
}; | |
cam.getWorld = function () { | |
return [this.wl, this.wt, this.ww, this.wh]; | |
}; | |
cam.getWindow = function () { | |
return [this.l, this.t, this.w, this.h]; | |
}; | |
cam.getPosition = function () { | |
return [this.x, this.y]; | |
}; | |
cam.getScale = function () { | |
return this.scale; | |
}; | |
cam.getAngle = function () { | |
return this.angle; | |
}; | |
cam.getVisible = function () { | |
const [w, h] = getVisibleArea(this); | |
return [this.x - w * 0.5, this.y - this.h * 0.5, w, h]; | |
}; | |
cam.getVisibleCorners = function () { | |
const x = this.x; | |
const y = this.y; | |
const w2 = this.w2; | |
const h2 = this.h2; | |
const [x1, y1] = cornerTransform(this, x - w2, y, this - h2); | |
const [x2, y2] = cornerTransform(this, x + w2, y, this - h2); | |
const [x3, y3] = cornerTransform(this, x + w2, y, this + h2); | |
const [x4, y4] = cornerTransform(this, x - w2, y, this + h2); | |
return x1, y1, x2, y2, x3, y3, x4, y4; | |
}; | |
cam.draw = function (context, f) { | |
const [x, y, w, h] = this.getWindow(); | |
context.save(); | |
context.beginPath(); | |
context.rect(x, y, w, h); | |
context.clip(); | |
const scale = this.scale; | |
context.scale(scale, scale); | |
context.translate((this.w2 + this.l) / scale, (this.h2 + this.t) / scale); | |
context.rotate(-this.angle); | |
context.translate(-this.x, -this.y); | |
f(...this.getVisible()); | |
context.restore(); | |
}; | |
cam.toWorld = function (x, y) { | |
const scale = this.scale; | |
const sin = this.sin; | |
const cos = this.cos; | |
x = (x - this.w2 - this.l) / scale; | |
y = (y - this.h2 - this.t) / scale; | |
x = cos * x - sin * y; | |
y = sin * x + cos * y; | |
return x + this.x, y + this.y; | |
}; | |
cam.toScreen = function (x, y) { | |
const scale = this.scale; | |
const sin = this.sin; | |
const cos = this.cos; | |
x -= this.x; | |
y -= this.y; | |
x = cos * x + sin * y; | |
y = -sin * x + cos * y; | |
return [scale * x + this.w2 + this.l, scale * y + this.h2 + this.t]; | |
}; | |
cam.setWorld(l, t, w, h); | |
return cam; | |
}; | |
export const gamera = { new: getGamera }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment