Skip to content

Commit ff7258a

Browse files
committed
Start to integrate player with editor
1 parent bcead6c commit ff7258a

File tree

11 files changed

+196
-881
lines changed

11 files changed

+196
-881
lines changed

dist/plugins/iwsy.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,34 @@ const EasyCoder_IWSY = {
1111

1212
const lino = compiler.getLino();
1313
const action = compiler.nextToken();
14-
if ([`init`, `show`].includes(action)) {
15-
compiler.next();
16-
compiler.addCommand({
17-
domain: `iwsy`,
18-
keyword: `iwsy`,
19-
lino,
20-
action
21-
});
22-
return true;
23-
}
14+
switch (action) {
15+
case `load`:
16+
if (compiler.nextIsSymbol()) {
17+
const playerRecord = compiler.getSymbolRecord();
18+
if (playerRecord.keyword === `div`) {
19+
const script = compiler.getNextValue();
20+
compiler.addCommand({
21+
domain: `iwsy`,
22+
keyword: `iwsy`,
23+
lino,
24+
action,
25+
player: playerRecord.name,
26+
script
27+
});
28+
return true;
29+
}
30+
}
31+
break;
32+
default:
33+
compiler.next();
34+
compiler.addCommand({
35+
domain: `iwsy`,
36+
keyword: `iwsy`,
37+
lino,
38+
action
39+
});
40+
return true;
41+
}
2442
return false;
2543
},
2644

@@ -33,6 +51,12 @@ const EasyCoder_IWSY = {
3351
function () {
3452
program.run(command.pc + 1);
3553
});
54+
return 0;
55+
case `load`:
56+
const playerRecord = program.getSymbolRecord(command.player);
57+
const player = playerRecord.element[playerRecord.index];
58+
const script = program.getValue(command.script);
59+
IWSY(player, JSON.parse(script));
3660
break;
3761
case `show`:
3862
break;

iwsy/iwsy.js

Lines changed: 33 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -722,104 +722,42 @@ const IWSY = (container, script) => {
722722
handler(step);
723723
};
724724

725-
// Initialization
726-
const setup = () => {
727-
container.innerHTML = ``;
728-
document.removeEventListener(`click`, init);
729-
if (mode === `auto`) {
730-
document.addEventListener(`click`, onClick);
731-
}
732-
document.onkeydown = null;
733-
script.container = container;
734-
container.style.position = `relative`;
735-
container.style.overflow = `hidden`;
736-
container.style.cursor = 'none';
737-
container.style[`background-size`] = `cover`;
738-
script.speed = `normal`;
739-
script.labels = {};
740-
for (const [index, step] of script.steps.entries()) {
741-
step.index = index;
742-
step.script = script;
743-
if (typeof step.label !== `undefined`) {
744-
script.labels[step.label] = index;
745-
}
746-
if (index < script.steps.length - 1) {
747-
step.next = () => {
748-
const next = step.index + 1;
749-
setTimeout(() => {
750-
doStep(script.steps[next]);
751-
}, 0);
752-
}
725+
container.innerHTML = ``;
726+
document.removeEventListener(`click`, init);
727+
if (mode === `auto`) {
728+
document.addEventListener(`click`, onClick);
729+
}
730+
document.onkeydown = null;
731+
script.container = container;
732+
container.style.position = `relative`;
733+
container.style.overflow = `hidden`;
734+
container.style.cursor = 'none';
735+
container.style[`background-size`] = `cover`;
736+
script.speed = `normal`;
737+
script.labels = {};
738+
for (const [index, step] of script.steps.entries()) {
739+
step.index = index;
740+
step.script = script;
741+
if (typeof step.label !== `undefined`) {
742+
script.labels[step.label] = index;
743+
}
744+
if (index < script.steps.length - 1) {
745+
step.next = () => {
746+
const next = step.index + 1;
747+
setTimeout(() => {
748+
doStep(script.steps[next]);
749+
}, 0);
753750
}
754-
else {
755-
step.next = () => {
756-
console.log(`Step ${index + 1}: Finished`);
757-
container.style.cursor = 'pointer';
758-
}
759-
};
760-
}
761-
IWSY.plugins = {};
762-
initBlocks();
763-
preloadImages();
764-
doStep(script.steps[0]);
765-
};
766-
767-
// Wait for a click/tap or a keypress to start
768-
document.addEventListener(`click`, init);
769-
document.onkeydown = function (event) {
770-
if (event.code === `Enter`) {
771-
mode = `auto`;
772751
}
773-
setup();
774-
return true;
775-
};
776-
};
777-
778-
window.onload = () => {
779-
const createCORSRequest = (url) => {
780-
let xhr = new XMLHttpRequest();
781-
if (`withCredentials` in xhr) {
782-
783-
// Check if the XMLHttpRequest object has a "withCredentials" property.
784-
// "withCredentials" only exists on XMLHTTPRequest2 objects.
785-
xhr.open(`GET`, url, true);
786-
787-
} else if (typeof XDomainRequest != `undefined`) {
788-
789-
// Otherwise, check if XDomainRequest.
790-
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
791-
xhr = new XDomainRequest();
792-
xhr.open(`GET`, url);
793-
794-
} else {
795-
796-
// Otherwise, CORS is not supported by the browser.
797-
xhr = null;
798-
799-
}
800-
return xhr;
801-
};
802-
803-
const scriptElement = document.getElementById(`iwsy-script`);
804-
if (scriptElement) {
805-
const request = createCORSRequest(`${scriptElement.innerText}?v=${Math.floor(Date.now())}`);
806-
if (!request) {
807-
throw Error(`Unable to access the JSON script`);
808-
}
809-
810-
request.onload = () => {
811-
if (200 <= request.status && request.status < 400) {
812-
const script = JSON.parse(request.responseText);
813-
IWSY(document.getElementById(`iwsy-container`), script);
814-
} else {
815-
throw Error(`Unable to access the JSON script`);
752+
else {
753+
step.next = () => {
754+
console.log(`Step ${index + 1}: Finished`);
755+
container.style.cursor = 'pointer';
816756
}
817757
};
818-
819-
request.onerror = () => {
820-
throw Error(`Unable to access the JSON script`);
821-
};
822-
823-
request.send();
824758
}
759+
IWSY.plugins = {};
760+
initBlocks();
761+
preloadImages();
762+
doStep(script.steps[0]);
825763
};

iwsy/iwsystart.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
window.onload = () => {
2+
const createCORSRequest = (url) => {
3+
let xhr = new XMLHttpRequest();
4+
if (`withCredentials` in xhr) {
5+
6+
// Check if the XMLHttpRequest object has a "withCredentials" property.
7+
// "withCredentials" only exists on XMLHTTPRequest2 objects.
8+
xhr.open(`GET`, url, true);
9+
10+
} else if (typeof XDomainRequest != `undefined`) {
11+
12+
// Otherwise, check if XDomainRequest.
13+
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
14+
xhr = new XDomainRequest();
15+
xhr.open(`GET`, url);
16+
17+
} else {
18+
19+
// Otherwise, CORS is not supported by the browser.
20+
xhr = null;
21+
22+
}
23+
return xhr;
24+
};
25+
26+
const scriptElement = document.getElementById(`iwsy-script`);
27+
if (scriptElement) {
28+
const request = createCORSRequest(`${scriptElement.innerText}?v=${Math.floor(Date.now())}`);
29+
if (!request) {
30+
throw Error(`Unable to access the JSON script`);
31+
}
32+
33+
request.onload = () => {
34+
if (200 <= request.status && request.status < 400) {
35+
const script = JSON.parse(request.responseText);
36+
IWSY(document.getElementById(`iwsy-container`), script);
37+
} else {
38+
throw Error(`Unable to access the JSON script`);
39+
}
40+
};
41+
42+
request.onerror = () => {
43+
throw Error(`Unable to access the JSON script`);
44+
};
45+
46+
request.send();
47+
}
48+
};
49+
50+
// Wait for a click/tap or a keypress to start
51+
document.addEventListener(`click`, init);
52+
document.onkeydown = function (event) {
53+
if (event.code === `Enter`) {
54+
mode = `auto`;
55+
}
56+
setup();
57+
return true;
58+
};

iwsy/resources/ecs/container.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)