Added everything from chapter 4 and set up ESlint

This commit is contained in:
Arne van Iterson 2020-03-21 17:22:28 +01:00
parent 3490dd07ae
commit 72633d8c0f
23 changed files with 1727 additions and 416 deletions

2
.eslintignore Normal file
View File

@ -0,0 +1,2 @@
projects
examples

33
.eslintrc.js Normal file
View File

@ -0,0 +1,33 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
]
}
};

63
lib/AnimManager.js Normal file
View File

@ -0,0 +1,63 @@
class Anim {
constructor(frames, rate) {
this.frames = frames;
this.rate = rate;
this.reset();
}
update(dt) {
const { rate, frames } = this;
if ((this.curTime += dt) > rate) {
this.curFrame++;
this.frame = frames[this.curFrame % frames.length];
this.curTime -= rate;
}
}
reset() {
this.frame = this.frames[0];
this.curFrame = 0;
this.curTime = 0;
}
}
class AnimManager {
constructor(e) {
this.anims = {};
this.running = false;
this.frameSource = e.frame || e;
this.currrent = null;
}
add(name, frames, speed) {
this.anims[name] = new Anim(frames, speed);
return this.anims[name];
}
update(dt) {
const { current, anims, frameSource } = this;
if (!current) {
return;
}
const anim = anims[current];
anim.update(dt);
frameSource.x = anim.frame.x;
frameSource.y = anim.frame.y;
}
play(anim) {
const { current, anims } = this;
if (anim === current ) {
return;
}
this.current = anim;
anims[anim].reset();
}
stop() {
this.current = null;
}
}
module.exports = AnimManager;

54
lib/Camera.js Normal file
View File

@ -0,0 +1,54 @@
const Container = require("./Container");
const math = require("./utilities/math");
class Camera extends Container {
constructor(subject, viewport, worldSize = viewport) {
super();
this.w = viewport.w;
this.h = viewport.h;
this.worldSize = worldSize;
this.setSubject(subject);
}
setSubject(e) {
this.subject = e ? e.pos || e : this.pos;
this.offset = { x: 0, y: 0 };
// Center on the entity
if (e && e.w) {
this.offset.x += e.w / 2;
this.offset.y += e.h / 2;
}
if (e && e.anchor) {
this.offset.x -= e.anchor.x;
this.offset.y -= e.anchor.y;
}
this.focus();
}
focus() {
const { pos, w, h, worldSize, subject, offset } = this;
const centeredX = subject.x + offset.x - w / 2;
const maxX = worldSize.w - w;
const x = -math.clamp(centeredX, 0, maxX);
const centeredY = subject.y + offset.y - h / 2;
const maxY = worldSize.h - h;
const y = -math.clamp(centeredY, 0, maxY);
pos.x = x;
pos.y = y;
}
update(dt, t) {
super.update(dt, t);
if (this.subject) {
this.focus();
}
}
}
module.exports = Camera;

View File

@ -1,30 +1,30 @@
class Container {
constructor() {
this.pos = { x: 0, y: 0 };
this.children = [];
}
constructor() {
this.pos = { x: 0, y: 0 };
this.children = [];
}
add(child) {
this.children.push(child);
return child;
}
add(child) {
this.children.push(child);
return child;
}
remove(child) {
this.children = this.children.filter(c => c !== child);
return child;
}
remove(child) {
this.children = this.children.filter(c => c !== child);
return child;
}
map(f) {
return this.children.map(f);
}
map(f) {
return this.children.map(f);
}
update(dt, t) {
this.children = this.children.filter(child => {
if (child.update) {
child.update(dt, t, this);
}
return child.dead ? false : true;
});
}
update(dt, t) {
this.children = this.children.filter(child => {
if (child.update) {
child.update(dt, t, this);
}
return child.dead ? false : true;
});
}
}
module.exports = Container;

View File

@ -1,40 +1,40 @@
var Container = require("./Container"),
CanvasRenderer = require('./renderer/CanvasRenderer')
CanvasRenderer = require("./renderer/CanvasRenderer")
;
const STEP = 1 / 60;
const FRAME_MAX = 5 * STEP;
class Game {
constructor(w, h, pixelated, parent = "#board") {
this.w = w;
this.h = h;
this.renderer = new CanvasRenderer(w, h);
document.querySelector(parent).appendChild(this.renderer.view);
constructor(w, h, pixelated, parent = "#board") {
this.w = w;
this.h = h;
this.renderer = new CanvasRenderer(w, h);
document.querySelector(parent).appendChild(this.renderer.view);
if (pixelated) {
this.renderer.setPixelated();
}
if (pixelated) {
this.renderer.setPixelated();
}
this.scene = new Container();
}
this.scene = new Container();
}
run(gameUpdate = () => { }) {
let dt = 0;
let last = 0;
const loop = ms => {
requestAnimationFrame(loop);
run(gameUpdate = () => { }) {
let dt = 0;
let last = 0;
const loop = ms => {
requestAnimationFrame(loop);
const t = ms / 1000;
dt = Math.min(t - last, FRAME_MAX);
last = t;
const t = ms / 1000;
dt = Math.min(t - last, FRAME_MAX);
last = t;
this.scene.update(dt, t);
gameUpdate(dt, t);
this.renderer.render(this.scene);
};
requestAnimationFrame(loop);
}
this.scene.update(dt, t);
gameUpdate(dt, t);
this.renderer.render(this.scene);
};
requestAnimationFrame(loop);
}
}
module.exports = Game;

View File

@ -1,10 +1,10 @@
class Rect {
constructor(w, h, style = { fill: "#333" }) {
this.pos = { x: 0, y: 0 };
this.w = w;
this.h = h;
this.style = style;
}
}
constructor(w, h, style = { fill: "#333" }) {
this.pos = { x: 0, y: 0 };
this.w = w;
this.h = h;
this.style = style;
}
}
module.exports = Rect;

View File

@ -1,10 +1,10 @@
class Sprite {
constructor(texture) {
this.texture = texture;
this.pos = { x: 0, y: 0 };
this.anchor = { x: 0, y: 0 };
this.scale = { x: 1, y: 1 };
this.rotation = 0;
}
constructor(texture) {
this.texture = texture;
this.pos = { x: 0, y: 0 };
this.anchor = { x: 0, y: 0 };
this.scale = { x: 1, y: 1 };
this.rotation = 0;
}
}
module.exports = Sprite;

View File

@ -1,39 +1,39 @@
class SpriteSheetXML {
constructor(url) {
this.array = [];
this.fetchXMLtoArray(url);
}
constructor(url) {
this.array = [];
this.fetchXMLtoArray(url);
}
fetchXMLtoArray(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.send(null);
fetchXMLtoArray(url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.send(null);
if (xhr.status === 200) {
var children = xhr.responseXML.children[0].children;
for (let index = 0; index < children.length; index++) {
const element = children[index];
this.array.push({
name: element.attributes.name.nodeValue,
x: element.attributes.x.nodeValue,
y: element.attributes.y.nodeValue,
width: element.attributes.width.nodeValue,
height: element.attributes.height.nodeValue
});
}
} else {
console.error('XML file cannot be loaded!')
}
}
if (xhr.status === 200) {
var children = xhr.responseXML.children[0].children;
for (let index = 0; index < children.length; index++) {
const element = children[index];
this.array.push({
name: element.attributes.name.nodeValue,
x: element.attributes.x.nodeValue,
y: element.attributes.y.nodeValue,
width: element.attributes.width.nodeValue,
height: element.attributes.height.nodeValue
});
}
} else {
console.error("XML file cannot be loaded!");
}
}
findIndex(attribute, value) {
for (let index = 0; index < this.array.length; index++) {
const element = this.array[index];
if (element[attribute] == value) {
return index;
}
}
}
findIndex(attribute, value) {
for (let index = 0; index < this.array.length; index++) {
const element = this.array[index];
if (element[attribute] == value) {
return index;
}
}
}
}
module.exports = SpriteSheetXML;

View File

@ -1,8 +1,8 @@
class Text {
constructor(text = "", style = {}) {
this.pos = { x: 0, y: 0 };
this.text = text;
this.style = style;
}
constructor(text = "", style = {}) {
this.pos = { x: 0, y: 0 };
this.text = text;
this.style = style;
}
}
module.exports = Text;

View File

@ -1,7 +1,7 @@
class Texture {
constructor(url) {
this.img = new Image();
this.img.src = url;
}
constructor(url) {
this.img = new Image();
this.img.src = url;
}
}
module.exports = Texture;

View File

@ -1,25 +1,59 @@
var Container = require("./Container"),
TileSprite = require("./TileSprite")
TileSprite = require("./TileSprite")
;
class TileMap extends Container {
constructor(tiles, mapW, mapH, tileW, tileH, texture) {
super();
this.mapW = mapW;
this.mapH = mapH;
this.tileW = tileW;
this.tileH = tileH;
this.w = mapW * tileW;
this.h = mapH * tileH;
constructor(tiles, mapW, mapH, tileW, tileH, texture) {
super();
this.mapW = mapW;
this.mapH = mapH;
this.tileW = tileW;
this.tileH = tileH;
this.w = mapW * tileW;
this.h = mapH * tileH;
this.children = tiles.map((frame, i) => {
const s = new TileSprite(texture, tileW, tileH);
s.frame = frame;
s.pos.x = i % mapW * tileW;
s.pos.y = Math.floor(i / mapW) * tileH;
return s;
});
}
this.children = tiles.map((frame, i) => {
const s = new TileSprite(texture, tileW, tileH);
s.frame = frame;
s.pos.x = i % mapW * tileW;
s.pos.y = Math.floor(i / mapW) * tileH;
return s;
});
}
pixelToMapPos(pos) {
const { tileW, tileH } = this;
return {
x: Math.floor(pos.x / tileW),
y: Math.floor(pos.y / tileH)
};
}
mapToPixelPos(mapPos) {
const { tileW, tileH } = this;
return {
x: mapPos.x * tileW,
y: mapPos.y * tileH
};
}
tileAtMapPos(mapPos) {
return this.children[mapPos.y * this.mapW + mapPos.x];
}
tileAtPixelPos(pos) {
return this.tileAtMapPos(this.pixelToMapPos(pos));
}
setFrameAtMapPos(mapPos, frame) {
const tile = this.tileAtMapPos(mapPos);
tile.frame = frame;
return tile;
}
setFrameAtPixelPos(pos, frame) {
return this.setFrameAtMapPos(this.pixelToMapPos(pos), frame);
}
}
module.exports = TileMap;

View File

@ -1,25 +1,25 @@
var Container = require("./Container"),
TileSpriteXML = require("./TileSpriteXML")
TileSpriteXML = require("./TileSpriteXML")
;
class TileMapXML extends Container {
constructor(tiles, mapW, mapH, texture, xml) {
super(texture);
this.mapW = mapW;
this.mapH = mapH;
this.tileW = xml.array[tiles[0]].width;
this.tileH = xml.array[tiles[0]].height;
this.w = mapW * this.tileW;
this.h = mapH * this.tileH;
constructor(tiles, mapW, mapH, texture, xml) {
super(texture);
this.mapW = mapW;
this.mapH = mapH;
this.tileW = xml.array[tiles[0]].width;
this.tileH = xml.array[tiles[0]].height;
this.w = mapW * this.tileW;
this.h = mapH * this.tileH;
this.children = tiles.map((frame, i) => {
const s = new TileSpriteXML(texture, xml, frame);
s.frame = frame;
s.pos.x = i % mapW * this.tileW;
s.pos.y = Math.floor(i / mapW) * this.tileH;
return s;
});
}
this.children = tiles.map((frame, i) => {
const s = new TileSpriteXML(texture, xml, frame);
s.frame = frame;
s.pos.x = i % mapW * this.tileW;
s.pos.y = Math.floor(i / mapW) * this.tileH;
return s;
});
}
}
module.exports = TileMapXML;

View File

@ -1,12 +1,12 @@
var Sprite = require("./Sprite");
class TileSprite extends Sprite {
constructor(texture, w, h) {
super(texture);
this.tileW = w;
this.tileH = h;
this.frame = { x: 0, y: 0 };
}
constructor(texture, w, h) {
super(texture);
this.tileW = w;
this.tileH = h;
this.frame = { x: 0, y: 0 };
}
}
module.exports = TileSprite;

View File

@ -1,13 +1,13 @@
var Sprite = require("./Sprite");
class TileSpriteXML extends Sprite {
constructor(texture, xml, index) {
super(texture);
var src = xml.array[index];
this.imgPos = { x: src['x'], y: src['y'] };
this.width = src['width'];
this.height = src['height'];
}
constructor(texture, xml, index) {
super(texture);
var src = xml.array[index];
this.imgPos = { x: src["x"], y: src["y"] };
this.width = src["width"];
this.height = src["height"];
}
}
module.exports = TileSpriteXML;

View File

@ -1,61 +1,61 @@
class KeyControls {
constructor() {
this.keys = {};
// Bind event handlers
document.addEventListener("keydown", e => {
if ([37, 38, 39, 40].indexOf(e.which) >= 0) {
e.preventDefault();
}
this.keys[e.which] = true;
}, false);
document.addEventListener('keyup', e => {
this.keys[e.which] = false;
}, false);
}
constructor() {
this.keys = {};
// Bind event handlers
document.addEventListener("keydown", e => {
if ([37, 38, 39, 40].indexOf(e.which) >= 0) {
e.preventDefault();
}
this.keys[e.which] = true;
}, false);
document.addEventListener("keyup", e => {
this.keys[e.which] = false;
}, false);
}
get action() {
// Spacebar
return this.keys[32];
}
get action() {
// Spacebar
return this.keys[32];
}
get x() {
// Arrow Left or A (WASD)
if (this.keys[37] || this.keys[65]) {
return -1;
}
// Arrow Right or D (WASD)
if (this.keys[39] || this.keys[68]) {
return 1;
}
return 0;
}
get x() {
// Arrow Left or A (WASD)
if (this.keys[37] || this.keys[65]) {
return -1;
}
// Arrow Right or D (WASD)
if (this.keys[39] || this.keys[68]) {
return 1;
}
return 0;
}
get y() {
// Arrow Up or W (WASD)
if (this.keys[38] || this.keys[87]) {
return -1;
}
// Arrow Down or S (WASD)
if (this.keys[40] || this.keys[83]) {
return 1;
}
return 0;
}
get y() {
// Arrow Up or W (WASD)
if (this.keys[38] || this.keys[87]) {
return -1;
}
// Arrow Down or S (WASD)
if (this.keys[40] || this.keys[83]) {
return 1;
}
return 0;
}
key(key, value) {
if (value !== undefined) {
this.keys[key] = value;
}
return this.keys[key];
}
key(key, value) {
if (value !== undefined) {
this.keys[key] = value;
}
return this.keys[key];
}
reset() {
for (let key in this.keys) {
this.keys[key] = false;
}
}
reset() {
for (let key in this.keys) {
this.keys[key] = false;
}
}
}

View File

@ -1,45 +1,45 @@
class MouseControls {
constructor(container) {
this.el = container || document.body;
// State
this.pos = { x: 0, y: 0 };
this.isDown = false;
this.pressed = false;
this.released = false;
// Handlers
document.addEventListener('mousemove', this.move.bind(this), false);
document.addEventListener('mousedown', this.down.bind(this), false);
document.addEventListener('mouseup', this.up.bind(this), false);
}
constructor(container) {
this.el = container || document.body;
// State
this.pos = { x: 0, y: 0 };
this.isDown = false;
this.pressed = false;
this.released = false;
// Handlers
document.addEventListener("mousemove", this.move.bind(this), false);
document.addEventListener("mousedown", this.down.bind(this), false);
document.addEventListener("mouseup", this.up.bind(this), false);
}
mousePosFromEvent({ clientX, clientY }) {
const { el, pos } = this;
const rect = el.getBoundingClientRect();
const xr = el.width / el.clientWidth;
const yr = el.height / el.clientHeight;
pos.x = (clientX - rect.left) * xr;
pos.y = (clientY - rect.top) * yr;
}
mousePosFromEvent({ clientX, clientY }) {
const { el, pos } = this;
const rect = el.getBoundingClientRect();
const xr = el.width / el.clientWidth;
const yr = el.height / el.clientHeight;
pos.x = (clientX - rect.left) * xr;
pos.y = (clientY - rect.top) * yr;
}
move(e) {
this.mousePosFromEvent(e);
}
move(e) {
this.mousePosFromEvent(e);
}
down(e) {
this.isDown = true;
this.pressed = true;
this.mousePosFromEvent(e);
}
down(e) {
this.isDown = true;
this.pressed = true;
this.mousePosFromEvent(e);
}
up() {
this.isDown = false;
this.released = true;
}
up() {
this.isDown = false;
this.released = true;
}
update() {
this.released = false;
this.pressed = false;
}
update() {
this.released = false;
this.pressed = false;
}
}
module.exports = MouseControls;

View File

@ -1,36 +1,40 @@
var Container = require("./Container.js"),
CanvasRenderer = require("./renderer/CanvasRenderer.js"),
Game = require("./Game.js"),
math = require("./utilities/math.js"),
entity = require("./utilities/entity.js"),
Rect = require("./Rect.js"),
KeyControls = require("./controls/KeyControls.js"),
MouseControls = require("./controls/MouseControls.js"),
Sprite = require("./Sprite.js"),
TileMap = require("./TileMap.js"),
TileMapXML = require("./TileMapXML.js"),
TileSprite = require("./TileSprite.js"),
TileSpriteXML = require("./TileSpriteXML.js"),
Text = require("./Text.js"),
Texture = require("./Texture.js"),
SpriteSheetXML = require("./SpriteSheetXML.js")
var AnimManager = require("./AnimManager.js"),
Camera = require("./Camera.js"),
Container = require("./Container.js"),
CanvasRenderer = require("./renderer/CanvasRenderer.js"),
Game = require("./Game.js"),
math = require("./utilities/math.js"),
entity = require("./utilities/entity.js"),
Rect = require("./Rect.js"),
KeyControls = require("./controls/KeyControls.js"),
MouseControls = require("./controls/MouseControls.js"),
Sprite = require("./Sprite.js"),
TileMap = require("./TileMap.js"),
TileMapXML = require("./TileMapXML.js"),
TileSprite = require("./TileSprite.js"),
TileSpriteXML = require("./TileSpriteXML.js"),
Text = require("./Text.js"),
Texture = require("./Texture.js"),
SpriteSheetXML = require("./SpriteSheetXML.js")
;
module.exports = {
CanvasRenderer,
Container,
Game,
math,
entity,
Rect,
KeyControls,
MouseControls,
Sprite,
TileMap,
TileMapXML,
TileSprite,
SpriteSheetXML,
TileSpriteXML,
Text,
Texture
AnimManager,
CanvasRenderer,
Camera,
Container,
Game,
math,
entity,
Rect,
KeyControls,
MouseControls,
Sprite,
TileMap,
TileMapXML,
TileSprite,
SpriteSheetXML,
TileSpriteXML,
Text,
Texture
};

View File

@ -1,100 +1,100 @@
class CanvasRenderer {
constructor(w, h) {
const canvas = document.createElement("canvas");
this.w = canvas.width = w;
this.h = canvas.height = h;
this.view = canvas;
this.ctx = canvas.getContext("2d");
this.ctx.textBaseLine = "top";
}
constructor(w, h) {
const canvas = document.createElement("canvas");
this.w = canvas.width = w;
this.h = canvas.height = h;
this.view = canvas;
this.ctx = canvas.getContext("2d");
this.ctx.textBaseLine = "top";
}
setPixelated() {
this.ctx['imageSmoothingEnabled'] = false; /* standard */
this.ctx['mozImageSmoothingEnabled'] = false; /* Firefox */
this.ctx['oImageSmoothingEnabled'] = false; /* Opera */
this.ctx['webkitImageSmoothingEnabled'] = false; /* Safari */
this.ctx['msImageSmoothingEnabled'] = false; /* IE */
}
setPixelated() {
this.ctx["imageSmoothingEnabled"] = false; /* standard */
this.ctx["mozImageSmoothingEnabled"] = false; /* Firefox */
this.ctx["oImageSmoothingEnabled"] = false; /* Opera */
this.ctx["webkitImageSmoothingEnabled"] = false; /* Safari */
this.ctx["msImageSmoothingEnabled"] = false; /* IE */
}
render(container, clear = true) {
const { ctx } = this;
function renderRec(container) {
// Render container children
container.children.forEach(child => {
if (child.visible == false) {
return;
}
render(container, clear = true) {
const { ctx } = this;
function renderRec(container) {
// Render container children
container.children.forEach(child => {
if (child.visible == false) {
return;
}
ctx.save();
ctx.save();
if (child.pos) {
ctx.translate(Math.round(child.pos.x), Math.round(child.pos.y));
}
if (child.pos) {
ctx.translate(Math.round(child.pos.x), Math.round(child.pos.y));
}
if (child.anchor) {
ctx.translate(child.anchor.x, child.anchor.y);
}
if (child.anchor) {
ctx.translate(child.anchor.x, child.anchor.y);
}
if (child.scale) {
ctx.scale(child.scale.x, child.scale.y);
}
if (child.scale) {
ctx.scale(child.scale.x, child.scale.y);
}
if (child.rotation) {
const px = child.pivot ? child.pivot.x : 0;
const py = child.pivot ? child.pivot.y : 0;
ctx.translate(px, py);
ctx.rotate(child.rotation);
ctx.translate(-px, -py);
}
if (child.rotation) {
const px = child.pivot ? child.pivot.x : 0;
const py = child.pivot ? child.pivot.y : 0;
ctx.translate(px, py);
ctx.rotate(child.rotation);
ctx.translate(-px, -py);
}
if (child.text) {
const { font, fill, align } = child.style;
if (font) ctx.font = font;
if (fill) ctx.fillStyle = fill;
if (align) ctx.textAlign = align;
ctx.fillText(child.text, 0, 0);
}
if (child.text) {
const { font, fill, align } = child.style;
if (font) ctx.font = font;
if (fill) ctx.fillStyle = fill;
if (align) ctx.textAlign = align;
ctx.fillText(child.text, 0, 0);
}
else if (child.texture) {
const img = child.texture.img;
if (child.tileW && child.tileH) {
ctx.drawImage(
img,
child.frame.x * child.tileW,
child.frame.y * child.tileH,
child.tileW, child.tileH,
0, 0,
child.tileW, child.tileH
);
} else if (child.imgPos && child.width && child.height) {
ctx.drawImage(
img,
child.imgPos.x,
child.imgPos.y,
child.width, child.height,
0, 0,
child.width, child.height
);
} else if (child.style && child.w && child.h) {
ctx.fillStyle = child.style.fill;
ctx.fillRect(0, 0, child.w, child.h)
} else {
ctx.drawImage(img, 0, 0);
}
}
else if (child.texture) {
const img = child.texture.img;
if (child.tileW && child.tileH) {
ctx.drawImage(
img,
child.frame.x * child.tileW,
child.frame.y * child.tileH,
child.tileW, child.tileH,
0, 0,
child.tileW, child.tileH
);
} else if (child.imgPos && child.width && child.height) {
ctx.drawImage(
img,
child.imgPos.x,
child.imgPos.y,
child.width, child.height,
0, 0,
child.width, child.height
);
} else if (child.style && child.w && child.h) {
ctx.fillStyle = child.style.fill;
ctx.fillRect(0, 0, child.w, child.h);
} else {
ctx.drawImage(img, 0, 0);
}
}
// Handle children with children
if (child.children) {
renderRec(child);
}
ctx.restore();
})
}
if (clear) {
ctx.clearRect(0, 0, this.w, this.h);
}
renderRec(container);
}
// Handle children with children
if (child.children) {
renderRec(child);
}
ctx.restore();
});
}
if (clear) {
ctx.clearRect(0, 0, this.w, this.h);
}
renderRec(container);
}
}
module.exports = CanvasRenderer;

View File

@ -1,79 +1,79 @@
const math = require('./math');
const Rect = require('../Rect');
const math = require("./math");
const Rect = require("../Rect");
function addDebug(e) {
e.children = e.children || [];
const bb = new Rect(e.w, e.h, { fill: "rgba(255, 0, 0, 0.3)" });
e.children.push(bb);
if (e.hitBox) {
const { x, y, w, h } = e.hitBox;
const hb = new Rect(w, h, { fill: "rgba(255, 0, 0, 0.5)" });
hb.pos.x = x;
hb.pos.y = y;
e.children.push(hb);
}
return e;
e.children = e.children || [];
const bb = new Rect(e.w, e.h, { fill: "rgba(255, 0, 0, 0.3)" });
e.children.push(bb);
if (e.hitBox) {
const { x, y, w, h } = e.hitBox;
const hb = new Rect(w, h, { fill: "rgba(255, 0, 0, 0.5)" });
hb.pos.x = x;
hb.pos.y = y;
e.children.push(hb);
}
return e;
}
function angle(a, b) {
return math.angle(center(a), center(b));
return math.angle(center(a), center(b));
}
function bounds(entity) {
const { w, h, pos, hitBox } = entity;
const hit = hitBox || { x: 0, y: 0, w, h };
return {
x: hit.x + pos.x,
y: hit.y + pos.y,
w: hit.w - 1,
h: hit.h - 1
};
const { w, h, pos, hitBox } = entity;
const hit = hitBox || { x: 0, y: 0, w, h };
return {
x: hit.x + pos.x,
y: hit.y + pos.y,
w: hit.w - 1,
h: hit.h - 1
};
}
function center(entity) {
const { pos, w, h } = entity;
return {
x: pos.x + w / 2,
y: pos.y + h / 2
};
const { pos, w, h } = entity;
return {
x: pos.x + w / 2,
y: pos.y + h / 2
};
}
function distance(a, b) {
return math.distance(center(a), center(b));
return math.distance(center(a), center(b));
}
function hit(e1, e2) {
const a = bounds(e1);
const b = bounds(e2);
return (
a.x + a.w >= b.x &&
const a = bounds(e1);
const b = bounds(e2);
return (
a.x + a.w >= b.x &&
a.x <= b.x + b.w &&
a.y + a.h >= b.y &&
a.y <= b.y + b.h
);
);
}
function hits(entity, container, hitCallback) {
const a = bounds(entity);
container.map(e2 => {
const b = bounds(e2);
if (
a.x + a.w >= b.x &&
const a = bounds(entity);
container.map(e2 => {
const b = bounds(e2);
if (
a.x + a.w >= b.x &&
a.x <= b.x + b.w &&
a.y + a.h >= b.y &&
a.y <= b.y + b.h
) {
hitCallback(e2);
}
});
) {
hitCallback(e2);
}
});
}
module.exports = {
addDebug,
angle,
bounds,
center,
distance,
hit,
hits
addDebug,
angle,
bounds,
center,
distance,
hit,
hits
};

View File

@ -1,26 +1,48 @@
function angle(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
const angle = Math.atan2(dy, dx);
return angle;
}
function clamp(x, min, max) {
return Math.max(min, Math.min(x, max));
}
function distance (a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
function rand(min, max) {
return Math.floor(randf(min, max));
return Math.floor(randf(min, max));
}
function randf(min, max) {
if (max == null) {
max = min || 1;
min = 0;
}
return Math.random() * (max - min) + min;
if (max == null) {
max = min || 1;
min = 0;
}
return Math.random() * (max - min) + min;
}
function randOneFrom(items) {
return items[rand(items.length)];
return items[rand(items.length)];
}
function randOneIn(max = 2) {
return rand(0, max) === 0;
return rand(0, max) === 0;
}
module.exports = {
rand,
randf,
randOneFrom,
randOneIn
angle,
clamp,
distance,
rand,
randf,
randOneFrom,
randOneIn
};

1096
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -17,5 +17,8 @@
"name": "Arne van Iterson",
"url": "https://gitea.arnweb.nl/arne/"
},
"license": "ISC"
"license": "ISC",
"devDependencies": {
"eslint": "^6.8.0"
}
}