Skip to content

Commit

Permalink
Merge branch 'main' into pixelspersecond
Browse files Browse the repository at this point in the history
  • Loading branch information
leomcelroy committed Apr 4, 2022
2 parents 059a95a + 08db26b commit 797409f
Show file tree
Hide file tree
Showing 7 changed files with 505 additions and 33 deletions.
24 changes: 11 additions & 13 deletions Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,27 @@ function initSprite(spriteData, that) {
}

const VALID_PARAMS = [
"x",
"y",
"vx",
"vy",
"tags",
"sprite",
"x",
"y",
"vx",
"vy",
"tags",
"sprite",
"scale",
"rotate",
"collides",
"update",
"rotate",
"collides",
"update",
"solid",
"bounce",
"origin",
"props",
"fps",
// not doced?
"click",
"drawBounds"
];

class Object {
constructor(params, engine) {

for (const k in params) {
if (!VALID_PARAMS.includes(k)) {
const msg = `Sprite's "${k}" set to "${params[k]}", but sprites don't have "${k}"s`;
Expand Down Expand Up @@ -232,7 +231,6 @@ class Object {
this.click = params.click ?? null;
this.update = params.update ?? null;
this.collides = params.collides ?? null;
this.drawBounds = params.drawBounds ?? false;
this.props = params.props ?? {};

const fps = params.fps;
Expand Down Expand Up @@ -269,7 +267,7 @@ class Object {
return this._sprite;
}

telport(x, y) {
teleport(x, y) {
this.lastX = this.x = x;
this.lasty = this.y = y;
}
Expand Down
9 changes: 5 additions & 4 deletions components/menuButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ const newProjectImageLink = (state) => {
}

const newProjectClick = () => {
if (confirm("This will clear the existing project. Continue?")) {
dispatch("SOUND", "click");
dispatch("LOAD_DEFAULT_CARTRIDGE");
}
alert(`If you want to get back to this project, simply click "?"`);
dispatch("SOUND", "click");
dispatch("LOAD_DEFAULT_CARTRIDGE").then(() => {
dispatch("GENERATE_NAME");
});
}

const newProjectButton = state => button({
Expand Down
4 changes: 2 additions & 2 deletions components/renderDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ playTune(tune_asset_name);
playTune(tune_0, tune_1, tune_2);
</pre>
To play a tune on repeat:
<pre>
<pre>
loopTune(tune_asset_name);
// or loop multiple tunes
loopTune(tune_0, tune_1, tune_2);
</pre>
To stop a tune on repeat:
<pre>
<pre>
const tuneToStop = loopTune(tune_asset_name);
tuneToStop.end();
</pre>
Expand Down
26 changes: 15 additions & 11 deletions dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ const STATE = {
challengeIndex: -1,
};

function randString(length) {
var randomChars = "abcdefghijklmnopqrstuvwxyz";
var result = "";
for (var i = 0; i < length; i++) {
result += randomChars.charAt(
Math.floor(Math.random() * randomChars.length)
);
}
return result;
}

const evalGameScript = createEval();

const ACTIONS = {
Expand Down Expand Up @@ -299,6 +310,10 @@ const ACTIONS = {
state.mouseY = mouseY;
dispatch("RENDER");
},
GENERATE_NAME({}, state) {
state.name = "gamelab-" + randString(3);
dispatch("RENDER");
},
LOAD_DEFAULT_CARTRIDGE: async ({}, state) => {
state.loadFileStatus = "loading";
await dispatch("LOAD_CARTRIDGE", { saved: await loadFromDefault() });
Expand Down Expand Up @@ -374,17 +389,6 @@ const ACTIONS = {

if (state.assetEditor && state.assetEditor.end) state.assetEditor.end();

function randString(length) {
var randomChars = "abcdefghijklmnopqrstuvwxyz";
var result = "";
for (var i = 0; i < length; i++) {
result += randomChars.charAt(
Math.floor(Math.random() * randomChars.length)
);
}
return result;
}

if (assetType === "sprite") {
state.assetEditor = createPixelEditor(
document.querySelector(".asset-editor")
Expand Down
24 changes: 21 additions & 3 deletions events.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,27 @@ export function events(state) {
}
});

window.addEventListener("beforeunload", () => {
window.localStorage.setItem("hc-game-lab", dispatch("GET_SAVE_STATE"));
});
const save = () => {
let all = JSON.parse(window.localStorage.getItem("hc-game-lab"));
const fresh = JSON.parse(dispatch("GET_SAVE_STATE"));

(() => {
if (Array.isArray(all)) {
const existing = all.findIndex((x) => x.name == fresh.name);
if (existing > -1) return (all[existing] = fresh);
}

if (all == null) all = [];
else if (!Array.isArray(all)) all = [all];
all.push(fresh);
})();

window.localStorage.setItem("hc-game-lab", JSON.stringify(all));
};

window.addEventListener("beforeunload", save);
window.addEventListener("onkeydown", save);
window.addEventListener("onmousedown", save);

addVerticalBarDrag(state, bodyListener);
addHorzBarDrag(state, bodyListener);
Expand Down
20 changes: 20 additions & 0 deletions init.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function removeParam(key) {
}

export function loadFromDefault() {
dispatch("GENERATE_NAME");
return loadFromS3(DEFAULT_CARTRIDGE);
}

Expand All @@ -28,6 +29,25 @@ function loadFromStorage() {
}
const saved = JSON.parse(storedData);

if (Array.isArray(saved))
return dispatch("NOTIFICATION", {
message: html`
The following games were found in your storage:<br />
${saved.map(
(save) => html`
<button
@click=${() => {
dispatch("LOAD_CARTRIDGE", { saved: save });
}}
>
${save.name}
</button>
`
)}
`,
open: false,
});

dispatch("NOTIFICATION", {
message: html`
An old game you were working on was found in your storage.<br />
Expand Down
Loading

0 comments on commit 797409f

Please sign in to comment.