// SPDX-FileCopyrightText: Chris Pressey, the creator of this work, has dedicated it to the public domain.
// For more information, please refer to <https://unlicense.org/>
// SPDX-License-Identifier: Unlicense
function launch(prefix, container, config) {
if (typeof container === 'string') {
container = document.getElementById(container);
}
config = config || {};
function loadThese(deps, callback) {
var loaded = 0;
for (var i = 0; i < deps.length; i++) {
var elem = document.createElement('script');
elem.src = prefix + deps[i];
elem.onload = function() {
if (++loaded < deps.length) return;
callback();
}
document.body.appendChild(elem);
}
}
loadThese([
"yoob/controller.js",
"yoob/playfield.js",
"yoob/playfield-canvas-view.js",
"yoob/cursor.js",
"dam/dam.js",
"dam/dam-editable.js",
"dam/dam-saveable.js"
], function() {
loadThese(["gemooy.js"], function() {
var div=DAM.maker('div'), textarea=DAM.maker('textarea'), canvas=DAM.maker('canvas');
var subPanel = div();
var display = canvas({ width: 400, height: 400, style: "display: inline-block" });
var editor = textarea({ rows: "25", cols: "40", style: "display: inline-block" });
var controlPanel = div(
subPanel,
div(
DAM.makeSaveable({
editor: editor,
onUpdate: function(text) { c.performReset(text); },
nameSpacePrefix: "yoob:gemooy.js:",
presets: examplePrograms
}),
DAM.makeEditable({
editor: editor,
display: display,
onEdit: function(text) {
c.clickReset(text);
subPanel.style.pointerEvents = "none";
subPanel.style.opacity = "0.5";
},
onUpdate: function(text) {
c.setResetState(text);
c.clickReset(text);
subPanel.style.pointerEvents = "inherit";
subPanel.style.opacity = "inherit";
}
})
),
display,
editor
)
container.appendChild(controlPanel);
var v = (new yoob.PlayfieldCanvasView()).init({ canvas: display });
v.setCellDimensions(undefined, 20);
var c = (new GemooyController()).init({
panelContainer: subPanel,
view: v
});
function convertControllerToLimiting(controller, getPlayfield) {
var originalStep = controller.step;
var consecutiveGrowthSteps = 0;
var lastPlayfieldSize = null;
var originalPerformStart = controller.performStart;
controller.performStart = function() {
consecutiveGrowthSteps = 0;
lastPlayfieldSize = null;
originalPerformStart.call(this);
};
controller.step = function() {
var pf = getPlayfield(this);
if (pf) {
var currentSize = pf.getCursoredExtentX() * pf.getCursoredExtentY();
if (lastPlayfieldSize !== null) {
if (currentSize > lastPlayfieldSize) {
consecutiveGrowthSteps++;
if (consecutiveGrowthSteps >= 100) {
return 'stop';
}
} else {
consecutiveGrowthSteps = 0;
}
}
lastPlayfieldSize = currentSize;
}
return originalStep.call(this);
};
}
convertControllerToLimiting(c, function() { return c.getPlayfield(); })
c.clickStop();
c.performReset(examplePrograms[0][1]);
editor.value = examplePrograms[0][1];
});
});
}