Skip to content

Commit

Permalink
mcpack examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mkellner committed Oct 2, 2023
1 parent b7f07be commit f018dfe
Show file tree
Hide file tree
Showing 27 changed files with 708 additions and 0 deletions.
Binary file added examples/packages/balls/balls.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions examples/packages/balls/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2016-2023 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/

import {} from "piu/MC";

const backgroundSkin = new Skin({ fill:"silver" });
const ballTexture = new Texture("balls.png");
const ballSkin = new Skin({ texture:ballTexture, x:0, y:0, width:30, height:30, variants:30 });

class BallBehavior extends Behavior {
onCreate(ball, delta) {
this.dx = delta;
this.dy = delta;
}
onDisplaying(ball) {
this.x = ball.x;
this.y = ball.y;
this.width = ball.container.width - ball.width;
this.height = ball.container.height - ball.height;
ball.start();
}
onTimeChanged(ball) {
var dx = this.dx;
var dy = this.dy;
ball.moveBy(dx, dy);
var x = this.x + dx;
var y = this.y + dy;
if ((x < 0) || (x > this.width)) dx = -dx;
if ((y < 0) || (y > this.height)) dy = -dy;
this.dx = dx;
this.dy = dy;
this.x = x;
this.y = y;
}
};

let BallApplication = Application.template($ => ({
skin:backgroundSkin,
contents: [
Content(6, { left:0, top:0, skin:ballSkin, variant:0, Behavior: BallBehavior } ),
Content(5, { right:0, top:0, skin:ballSkin, variant:1, Behavior: BallBehavior } ),
Content(4, { right:0, bottom:0, skin:ballSkin, variant:2, Behavior: BallBehavior } ),
Content(3, { left:0, bottom:0, skin:ballSkin, variant:3, Behavior: BallBehavior } ),
]
}));

export default new BallApplication(null, { touchCount:0, pixels: screen.width * 4 });
10 changes: 10 additions & 0 deletions examples/packages/balls/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config": {
"touchCount": 0
},
"resources":{
"*": [
"./balls"
]
}
}
14 changes: 14 additions & 0 deletions examples/packages/balls/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@moddable/example-balls",
"version": "1.0.0",
"dependencies": {
},
"main": "./main.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": ""
}
143 changes: 143 additions & 0 deletions examples/packages/fetch/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright (c) 2021-2022 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/

const headers = new Headers([
['Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'],
["Date", Date()],
["User-Agent", "ecma-419 test"]
]);

const body = new URLSearchParams([
["Date", Date()],
["Input", "This is no input!"]
]);

// HTTP

fetch("http://httpbin.org/post", { method:"POST", headers, body })
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.json();
})
.then(json => {
console.log(JSON.stringify(json, null, "\t"));
});

fetch("http://httpbin.org/put", { method:"PUT", body:"This is no data!" })
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.json();
})
.then(json => {
console.log(JSON.stringify(json, null, "\t"));
});

fetch("http://httpbin.org/gzip")
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.arrayBuffer();
})
.then(arrayBuffer => {
const array = new Uint8Array(arrayBuffer);
console.log(array + "\n");
});

fetch("http://httpbin.org/json")
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.json();
})
.then(json => {
console.log(JSON.stringify(json, null, "\t"));
});

fetch("http://httpbin.org/encoding/utf8")
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.text();
})
.then(text => {
const c = text.length;
for (let i = 0; i < c; i++) {
let lf = text.indexOf("\n", i);
if (-1 === lf) lf = c;
console.log(text.substring(i, lf));
i = lf + 1;
}
});

// HTTPS

fetch("https://httpbin.org/post", { method:"POST", headers, body })
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.json();
})
.then(json => {
console.log(JSON.stringify(json, null, "\t"));
});

fetch("https://httpbin.org/put", { method:"PUT", body:"This is no data!" })
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.json();
})
.then(json => {
console.log(JSON.stringify(json, null, "\t"));
});

fetch("https://httpbin.org/gzip")
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.arrayBuffer();
})
.then(arrayBuffer => {
const array = new Uint8Array(arrayBuffer);
console.log(array + "\n");
});

fetch("https://httpbin.org/json")
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.json();
})
.then(json => {
console.log(JSON.stringify(json, null, "\t"));
});

fetch("https://httpbin.org/encoding/utf8")
.then(response => {
console.log(`${response.url} ${response.status} ${response.statusText}\n\n`);
response.headers.forEach((value, key) => console.log(`${key}: ${value}\n`));
return response.text();
})
.then(text => {
const c = text.length;
for (let i = 0; i < c; i++) {
let lf = text.indexOf("\n", i);
if (-1 === lf) lf = c;
console.log(text.substring(i, lf));
i = lf + 1;
}
});

23 changes: 23 additions & 0 deletions examples/packages/fetch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@moddable/example-fetch",
"version": "1.0.0",
"dependencies": {
},
"main": "./main.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"moddable": {
"manifest": {
"data": {
"*": [
"$(MODULES)/crypt/data/ca222"
]
}
}
}
}
22 changes: 22 additions & 0 deletions examples/packages/hello/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from "#test";

import module0 from "./module0.js";
import module1 from "@moddable/example-hello1/mod.js";
import main2 from "@moddable/example-hello2";

console.log(test);
console.log(module0);
console.log(module1);
console.log(main2);

import Mustache from "mustache";

const view = {
title: "Joe",
calc: function () {
return 2 + 4;
}
};

const output = Mustache.render("{{title}} spends {{calc}}", view);
console.log(`${output}\n`);
2 changes: 2 additions & 0 deletions examples/packages/hello/module0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import main2 from "@moddable/example-hello2/main.js";
export default "mod0";
23 changes: 23 additions & 0 deletions examples/packages/hello/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@moddable/example-hello",
"version": "1.0.0",
"dependencies": {
"mustache": "^4.2.0",
"@moddable/example-hello1": "^1.0.2",
"@moddable/example-hello2": "^1.0.2"
},
"imports": {
"#test": {
"moddable": "./test-xs.js",
"default": "./test-node.js"
}
},
"main": "./main.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": ""
}
1 change: 1 addition & 0 deletions examples/packages/hello/test-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "node";
1 change: 1 addition & 0 deletions examples/packages/hello/test-xs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "xs";
Binary file added examples/packages/mod-balls/balls.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions examples/packages/mod-balls/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const backgroundSkin = new Skin({ fill:"silver" });
const ballTexture = new Texture({ archive, path:"balls.png" });
const ballSkin = new Skin({ texture:ballTexture, x:0, y:0, width:30, height:30, variants:30 });

class BallBehavior extends Behavior {
onCreate(ball, delta) {
this.dx = delta;
this.dy = delta;
}
onDisplaying(ball) {
this.x = ball.x;
this.y = ball.y;
this.width = ball.container.width - ball.width;
this.height = ball.container.height - ball.height;
ball.start();
}
onTimeChanged(ball) {
var dx = this.dx;
var dy = this.dy;
ball.moveBy(dx, dy);
var x = this.x + dx;
var y = this.y + dy;
if ((x < 0) || (x > this.width)) dx = -dx;
if ((y < 0) || (y > this.height)) dy = -dy;
this.dx = dx;
this.dy = dy;
this.x = x;
this.y = y;
}
};

export default Application.template($ => ({
left:0, right:0, top:0, bottom: 0, skin:backgroundSkin,
contents: [
Content(6, { left:0, top:0, skin:ballSkin, variant:0, Behavior: BallBehavior } ),
Content(5, { right:0, top:0, skin:ballSkin, variant:1, Behavior: BallBehavior } ),
Content(4, { right:0, bottom:0, skin:ballSkin, variant:2, Behavior: BallBehavior } ),
Content(3, { left:0, bottom:0, skin:ballSkin, variant:3, Behavior: BallBehavior } ),
]
}));
21 changes: 21 additions & 0 deletions examples/packages/mod-balls/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@moddable/example-mod-balls",
"version": "1.0.0",
"dependencies": {
},
"main": "./mod.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"moddable": {
"manifest": {
"resources": {
"*": "./balls"
}
}
}
}

0 comments on commit f018dfe

Please sign in to comment.