plainscript-lang 1.0.3 → 1.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +836 -188
- package/compiler/cli.js +68 -4
- package/compiler/dependency-detector.js +6 -2
- package/compiler/generator.js +737 -51
- package/compiler/lexer.js +12 -0
- package/compiler/parser.js +184 -14
- package/compiler/version.js +1 -1
- package/docs/CAPABILITY_GAP_AUDIT.md +32 -5
- package/docs/GAME-PROMPT.md +915 -0
- package/docs/PLAINSCRIPT-SPEC.md +264 -3
- package/docs/index.html +41 -33
- package/docs/logo-pln.jpg +0 -0
- package/docs/visualization-example.svg +32 -0
- package/docs/website/script.js +130 -1
- package/docs/website/style.css +253 -122
- package/examples/ai-providers.pln +1 -1
- package/examples/arrays.pln +1 -1
- package/examples/async-errors.pln +1 -1
- package/examples/auth-api.pln +1 -1
- package/examples/auth-sessions.pln +1 -1
- package/examples/basics.pln +1 -1
- package/examples/bots.pln +1 -1
- package/examples/browser-interactive/README.md +29 -0
- package/examples/browser-interactive/public/index.html +83 -0
- package/examples/browser-interactive/public/interactive.js +27 -0
- package/examples/browser-interactive/src/interactive.pln +34 -0
- package/examples/cache-and-jobs.pln +1 -1
- package/examples/cache-schedule.pln +1 -1
- package/examples/canvas-game/README.md +34 -0
- package/examples/canvas-game/public/game.js +131 -0
- package/examples/canvas-game/public/index.html +48 -0
- package/examples/canvas-game/src/game.pln +139 -0
- package/examples/collections.pln +1 -1
- package/examples/concurrency.pln +1 -1
- package/examples/conditions.pln +1 -1
- package/examples/database.pln +1 -1
- package/examples/day2.pln +1 -1
- package/examples/day3.pln +1 -1
- package/examples/deployment.pln +1 -1
- package/examples/email-send.pln +1 -1
- package/examples/error-handling.pln +1 -1
- package/examples/expressions.pln +1 -1
- package/examples/file-uploads.pln +1 -1
- package/examples/files-and-config.pln +1 -1
- package/examples/files.pln +1 -1
- package/examples/football-backend/app.pln +1 -1
- package/examples/functions.pln +1 -1
- package/examples/hello.pln +1 -1
- package/examples/http-client.pln +1 -1
- package/examples/http.pln +1 -1
- package/examples/id-verification/app.pln +1 -1
- package/examples/javascript-library/README.md +54 -0
- package/examples/javascript-library/package-lock.json +21 -0
- package/examples/javascript-library/package.json +8 -0
- package/examples/javascript-library/public/config.json +4 -0
- package/examples/javascript-library/public/index.html +40 -0
- package/examples/javascript-library/src/interop.pln +53 -0
- package/examples/json.pln +1 -1
- package/examples/loops.pln +1 -1
- package/examples/modules/app.pln +1 -1
- package/examples/modules/main.pln +1 -1
- package/examples/modules/math.pln +1 -1
- package/examples/mongo.pln +1 -1
- package/examples/objects.pln +1 -1
- package/examples/ocr.pln +1 -1
- package/examples/packages.pln +1 -1
- package/examples/record-kinds.pln +1 -1
- package/examples/records.pln +1 -1
- package/examples/rest-api-fetch.pln +1 -1
- package/examples/rest-api.pln +1 -1
- package/examples/scheduled-tasks.pln +1 -1
- package/examples/search-and-filter.pln +1 -1
- package/examples/server.pln +1 -1
- package/examples/sessions-and-cookies.pln +1 -1
- package/examples/sqlite.pln +1 -1
- package/examples/start.pln +1 -1
- package/examples/stdlib.pln +1 -1
- package/examples/string-verbs.pln +1 -1
- package/examples/telegram-ai-bot.pln +1 -1
- package/examples/telegram-bot.pln +1 -1
- package/examples/testing.pln +1 -1
- package/examples/three-dimensional/README.md +51 -0
- package/examples/three-dimensional/public/index.html +33 -0
- package/examples/three-dimensional/src/scene.pln +34 -0
- package/examples/visualization.pln +12 -0
- package/examples/web-api.pln +1 -1
- package/examples/web-app.pln +1 -1
- package/examples/websocket-server.pln +1 -1
- package/examples/websocket.pln +1 -1
- package/examples/whatsapp-bot/pairing.pln +24 -2
- package/examples/whatsapp-bot/qr.pln +18 -2
- package/package.json +5 -3
- package/docs/logo.jpg +0 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// PlainScript 1.0.36: a DOM-first clicker with a run log.
|
|
2
|
+
|
|
3
|
+
remember scoreEl as document.querySelector("#score")
|
|
4
|
+
remember historyEl as document.querySelector("#history")
|
|
5
|
+
remember clickButton as document.querySelector("#click-button")
|
|
6
|
+
remember logButton as document.querySelector("#log-button")
|
|
7
|
+
|
|
8
|
+
remember entryCount as 0
|
|
9
|
+
|
|
10
|
+
show "PlainScript clicker dashboard ready"
|
|
11
|
+
|
|
12
|
+
when clickButton "click" happens
|
|
13
|
+
remember current as parseInt(scoreEl.textContent)
|
|
14
|
+
remember updated as current + 10
|
|
15
|
+
scoreEl.innerHTML becomes String(updated)
|
|
16
|
+
if updated is at least 50
|
|
17
|
+
clickButton.classList.add("level-up")
|
|
18
|
+
done
|
|
19
|
+
done
|
|
20
|
+
|
|
21
|
+
when logButton "click" happens
|
|
22
|
+
entryCount becomes entryCount + 1
|
|
23
|
+
remember item as document.createElement("li")
|
|
24
|
+
item.textContent becomes "Entry " + entryCount + " (score " + scoreEl.textContent + ")"
|
|
25
|
+
historyEl.appendChild(item)
|
|
26
|
+
done
|
|
27
|
+
|
|
28
|
+
when document "keydown" happens as e
|
|
29
|
+
if e.key is "r" or e.key is "R"
|
|
30
|
+
scoreEl.textContent becomes "0"
|
|
31
|
+
clickButton.classList.remove("level-up")
|
|
32
|
+
historyEl.innerHTML becomes ""
|
|
33
|
+
done
|
|
34
|
+
done
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Canvas 2D dodge mini-game
|
|
2
|
+
|
|
3
|
+
A self-contained PlainScript game: a green square you steer with WASD or the
|
|
4
|
+
arrow keys while red blocks fall from the top. Score a point for every block
|
|
5
|
+
that drifts past you; lose a life when one collides with you. Run out of lives
|
|
6
|
+
and the game freezes until you press Restart.
|
|
7
|
+
|
|
8
|
+
It demonstrates the v1.0.36 browser API:
|
|
9
|
+
|
|
10
|
+
- `when document "keydown"/"keyup" happens` handlers that set flags on a
|
|
11
|
+
`keys{}` map — the game loop reads the map, the event handlers never move the
|
|
12
|
+
player directly.
|
|
13
|
+
- `every frame ... done`, a `requestAnimationFrame` loop with delta time from
|
|
14
|
+
`performance.now()` so movement speed is frame-rate independent.
|
|
15
|
+
- `define a kind called "Enemy"` records, `new Enemy`, and `Math.random()`
|
|
16
|
+
spawning.
|
|
17
|
+
- Direct canvas interop: `canvas.getContext("2d")`, `clearRect`, `fillRect`,
|
|
18
|
+
`fillStyle`, `fillText`, and `strokeStyle` via `becomes`.
|
|
19
|
+
- A Restart button wired up with `when restartButton "click" happens`.
|
|
20
|
+
|
|
21
|
+
## Run
|
|
22
|
+
|
|
23
|
+
Build the compiled script, then open the page (any static file server works —
|
|
24
|
+
`npx serve public` is one option):
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npx plainscript build src/game.pln -o public/game.js
|
|
28
|
+
npx serve public
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Then open http://localhost:3000.
|
|
32
|
+
|
|
33
|
+
The page needs a server only for the HTML script tag, not for the game logic —
|
|
34
|
+
opening `public/index.html` directly from disk works in most browsers too.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
function __makeKind_Enemy(EnemyFields) {
|
|
2
|
+
const __rec = { "x": 0, "y": 0, "size": 18, "speed": 0 };
|
|
3
|
+
if (EnemyFields != null) {
|
|
4
|
+
for (const __k in EnemyFields) {
|
|
5
|
+
if (!(Object.prototype.hasOwnProperty.call(__rec, __k))) throw new Error("\"Enemy\" has no field named \"" + __k + "\".");
|
|
6
|
+
__rec[__k] = EnemyFields[__k];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return __rec;
|
|
10
|
+
}
|
|
11
|
+
const Enemy = __makeKind_Enemy;
|
|
12
|
+
let canvas = document.getElementById("game");
|
|
13
|
+
let ctx = canvas.getContext("2d");
|
|
14
|
+
let restartButton = document.getElementById("restart");
|
|
15
|
+
let keys = { };
|
|
16
|
+
let enemies = [];
|
|
17
|
+
let widthValue = 640;
|
|
18
|
+
let heightValue = 480;
|
|
19
|
+
let playerSize = 24;
|
|
20
|
+
let playerX = 308;
|
|
21
|
+
let playerY = 420;
|
|
22
|
+
let playerSpeed = 240;
|
|
23
|
+
let scoreValue = 0;
|
|
24
|
+
let lives = 3;
|
|
25
|
+
let gameOver = false;
|
|
26
|
+
let previousMs = -1;
|
|
27
|
+
let spawnTimer = 0.5;
|
|
28
|
+
function resetEnemy(e) {
|
|
29
|
+
e.x = 20 + (Math.random() * 600);
|
|
30
|
+
e.y = -30;
|
|
31
|
+
e.speed = 60 + (Math.random() * 120);
|
|
32
|
+
}
|
|
33
|
+
function spawnEnemy() {
|
|
34
|
+
let e = new Enemy();
|
|
35
|
+
resetEnemy(e);
|
|
36
|
+
enemies.push(e);
|
|
37
|
+
}
|
|
38
|
+
for (let startCount = 0; (0 <= 4 ? startCount <= 4 : startCount >= 4); startCount += (0 <= 4 ? 1 : -1)) {
|
|
39
|
+
spawnEnemy();
|
|
40
|
+
}
|
|
41
|
+
document.addEventListener("keydown", function (e) {
|
|
42
|
+
keys[e.key] = true;
|
|
43
|
+
});
|
|
44
|
+
document.addEventListener("keyup", function (e) {
|
|
45
|
+
keys[e.key] = false;
|
|
46
|
+
});
|
|
47
|
+
restartButton.addEventListener("click", function (event) {
|
|
48
|
+
scoreValue = 0;
|
|
49
|
+
lives = 3;
|
|
50
|
+
gameOver = false;
|
|
51
|
+
playerX = 308;
|
|
52
|
+
playerY = 420;
|
|
53
|
+
previousMs = -1;
|
|
54
|
+
});
|
|
55
|
+
requestAnimationFrame(function __frame(__frameTime) {
|
|
56
|
+
let nowMs = performance.now();
|
|
57
|
+
let delta = 0;
|
|
58
|
+
if (previousMs > 0) {
|
|
59
|
+
delta = (nowMs - previousMs) / 1000;
|
|
60
|
+
}
|
|
61
|
+
previousMs = nowMs;
|
|
62
|
+
if (gameOver === false) {
|
|
63
|
+
if (keys["ArrowLeft"] === true || keys["a"] === true) {
|
|
64
|
+
playerX = playerX - (playerSpeed * delta);
|
|
65
|
+
}
|
|
66
|
+
if (keys["ArrowRight"] === true || keys["d"] === true) {
|
|
67
|
+
playerX = playerX + (playerSpeed * delta);
|
|
68
|
+
}
|
|
69
|
+
if (keys["ArrowUp"] === true || keys["w"] === true) {
|
|
70
|
+
playerY = playerY - (playerSpeed * delta);
|
|
71
|
+
}
|
|
72
|
+
if (keys["ArrowDown"] === true || keys["s"] === true) {
|
|
73
|
+
playerY = playerY + (playerSpeed * delta);
|
|
74
|
+
}
|
|
75
|
+
if (playerX < 0) {
|
|
76
|
+
playerX = 0;
|
|
77
|
+
}
|
|
78
|
+
if (playerX > widthValue - playerSize) {
|
|
79
|
+
playerX = widthValue - playerSize;
|
|
80
|
+
}
|
|
81
|
+
if (playerY < 0) {
|
|
82
|
+
playerY = 0;
|
|
83
|
+
}
|
|
84
|
+
if (playerY > heightValue - playerSize) {
|
|
85
|
+
playerY = heightValue - playerSize;
|
|
86
|
+
}
|
|
87
|
+
spawnTimer = spawnTimer - delta;
|
|
88
|
+
if (spawnTimer < 0) {
|
|
89
|
+
spawnEnemy();
|
|
90
|
+
spawnTimer = 1.1;
|
|
91
|
+
}
|
|
92
|
+
for (const e of enemies) {
|
|
93
|
+
e.y = e.y + (e.speed * delta);
|
|
94
|
+
if (e.y > heightValue) {
|
|
95
|
+
scoreValue = scoreValue + 1;
|
|
96
|
+
resetEnemy(e);
|
|
97
|
+
}
|
|
98
|
+
if (e.x < playerX + playerSize && e.x + e.size > playerX && e.y < playerY + playerSize && e.y + e.size > playerY) {
|
|
99
|
+
lives = lives - 1;
|
|
100
|
+
resetEnemy(e);
|
|
101
|
+
if (lives === 0) {
|
|
102
|
+
gameOver = true;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
ctx.clearRect(0, 0, widthValue, heightValue);
|
|
108
|
+
ctx.fillStyle = "#0d1117";
|
|
109
|
+
ctx.fillRect(0, 0, widthValue, heightValue);
|
|
110
|
+
ctx.fillStyle = "#40c463";
|
|
111
|
+
ctx.fillRect(playerX, playerY, playerSize, playerSize);
|
|
112
|
+
ctx.strokeStyle = "#2ea043";
|
|
113
|
+
ctx.strokeRect(playerX, playerY, playerSize, playerSize);
|
|
114
|
+
ctx.fillStyle = "#ef4444";
|
|
115
|
+
for (const e of enemies) {
|
|
116
|
+
ctx.fillRect(e.x, e.y, e.size, e.size);
|
|
117
|
+
}
|
|
118
|
+
ctx.fillStyle = "#e6edf3";
|
|
119
|
+
ctx.font = "18px sans-serif";
|
|
120
|
+
ctx.fillText("Score: " + scoreValue, 12, 28);
|
|
121
|
+
ctx.fillText("Lives: " + lives, 12, 52);
|
|
122
|
+
if (gameOver === true) {
|
|
123
|
+
ctx.fillStyle = "#f6f8fa";
|
|
124
|
+
ctx.font = "34px sans-serif";
|
|
125
|
+
ctx.fillText("Game Over", 230, 208);
|
|
126
|
+
ctx.font = "18px sans-serif";
|
|
127
|
+
ctx.fillText("Press Restart to play again", 196, 244);
|
|
128
|
+
}
|
|
129
|
+
requestAnimationFrame(__frame);
|
|
130
|
+
});
|
|
131
|
+
if (typeof module !== 'undefined') { module.exports = { resetEnemy, spawnEnemy }; }
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>PlainScript Dodge</title>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
margin: 0;
|
|
10
|
+
min-height: 100vh;
|
|
11
|
+
display: flex;
|
|
12
|
+
flex-direction: column;
|
|
13
|
+
align-items: center;
|
|
14
|
+
justify-content: center;
|
|
15
|
+
gap: 14px;
|
|
16
|
+
background: #0d1117;
|
|
17
|
+
color: #e6edf3;
|
|
18
|
+
font-family: system-ui, "Segoe UI", sans-serif;
|
|
19
|
+
}
|
|
20
|
+
h1 { font-size: 22px; margin: 0; }
|
|
21
|
+
p { margin: 0; font-size: 14px; color: #8b949e; }
|
|
22
|
+
canvas {
|
|
23
|
+
border: 2px solid #30363d;
|
|
24
|
+
border-radius: 8px;
|
|
25
|
+
background: #0d1117;
|
|
26
|
+
display: block;
|
|
27
|
+
}
|
|
28
|
+
button {
|
|
29
|
+
padding: 10px 22px;
|
|
30
|
+
font-size: 15px;
|
|
31
|
+
font-weight: 600;
|
|
32
|
+
color: #0d1117;
|
|
33
|
+
background: #40c463;
|
|
34
|
+
border: none;
|
|
35
|
+
border-radius: 8px;
|
|
36
|
+
cursor: pointer;
|
|
37
|
+
}
|
|
38
|
+
button:hover { filter: brightness(1.1); }
|
|
39
|
+
</style>
|
|
40
|
+
</head>
|
|
41
|
+
<body>
|
|
42
|
+
<h1>PlainScript Dodge</h1>
|
|
43
|
+
<p>Use WASD or arrow keys to dodge the falling blocks.</p>
|
|
44
|
+
<canvas id="game" width="640" height="480"></canvas>
|
|
45
|
+
<button id="restart" type="button">Restart</button>
|
|
46
|
+
<script src="game.js"></script>
|
|
47
|
+
</body>
|
|
48
|
+
</html>
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// PlainScript 1.0.36: a self-contained canvas 2D dodge mini-game.
|
|
2
|
+
|
|
3
|
+
define a kind called "Enemy" with
|
|
4
|
+
x is 0
|
|
5
|
+
y is 0
|
|
6
|
+
size is 18
|
|
7
|
+
speed is 0
|
|
8
|
+
done
|
|
9
|
+
|
|
10
|
+
remember canvas as document.getElementById("game")
|
|
11
|
+
remember ctx as canvas.getContext("2d")
|
|
12
|
+
remember restartButton as document.getElementById("restart")
|
|
13
|
+
|
|
14
|
+
remember keys as {}
|
|
15
|
+
remember enemies as []
|
|
16
|
+
remember widthValue as 640
|
|
17
|
+
remember heightValue as 480
|
|
18
|
+
remember playerSize as 24
|
|
19
|
+
remember playerX as 308
|
|
20
|
+
remember playerY as 420
|
|
21
|
+
remember playerSpeed as 240
|
|
22
|
+
remember scoreValue as 0
|
|
23
|
+
remember lives as 3
|
|
24
|
+
remember gameOver as false
|
|
25
|
+
remember previousMs as -1
|
|
26
|
+
remember spawnTimer as 0.5
|
|
27
|
+
|
|
28
|
+
make resetEnemy(e)
|
|
29
|
+
e.x becomes 20 + Math.random() * 600
|
|
30
|
+
e.y becomes -30
|
|
31
|
+
e.speed becomes 60 + Math.random() * 120
|
|
32
|
+
done
|
|
33
|
+
|
|
34
|
+
make spawnEnemy()
|
|
35
|
+
remember e as new Enemy
|
|
36
|
+
resetEnemy(e)
|
|
37
|
+
enemies.push(e)
|
|
38
|
+
done
|
|
39
|
+
|
|
40
|
+
for index startCount from 0 to 4
|
|
41
|
+
spawnEnemy()
|
|
42
|
+
done
|
|
43
|
+
|
|
44
|
+
when document "keydown" happens as e
|
|
45
|
+
keys[e.key] becomes true
|
|
46
|
+
done
|
|
47
|
+
|
|
48
|
+
when document "keyup" happens as e
|
|
49
|
+
keys[e.key] becomes false
|
|
50
|
+
done
|
|
51
|
+
|
|
52
|
+
when restartButton "click" happens
|
|
53
|
+
scoreValue becomes 0
|
|
54
|
+
lives becomes 3
|
|
55
|
+
gameOver becomes false
|
|
56
|
+
playerX becomes 308
|
|
57
|
+
playerY becomes 420
|
|
58
|
+
previousMs becomes -1
|
|
59
|
+
done
|
|
60
|
+
|
|
61
|
+
every frame
|
|
62
|
+
remember nowMs as performance.now()
|
|
63
|
+
remember delta as 0
|
|
64
|
+
if previousMs is above 0
|
|
65
|
+
delta becomes (nowMs - previousMs) / 1000
|
|
66
|
+
done
|
|
67
|
+
previousMs becomes nowMs
|
|
68
|
+
|
|
69
|
+
if gameOver is false
|
|
70
|
+
if keys["ArrowLeft"] is true or keys["a"] is true
|
|
71
|
+
playerX becomes playerX - playerSpeed * delta
|
|
72
|
+
done
|
|
73
|
+
if keys["ArrowRight"] is true or keys["d"] is true
|
|
74
|
+
playerX becomes playerX + playerSpeed * delta
|
|
75
|
+
done
|
|
76
|
+
if keys["ArrowUp"] is true or keys["w"] is true
|
|
77
|
+
playerY becomes playerY - playerSpeed * delta
|
|
78
|
+
done
|
|
79
|
+
if keys["ArrowDown"] is true or keys["s"] is true
|
|
80
|
+
playerY becomes playerY + playerSpeed * delta
|
|
81
|
+
done
|
|
82
|
+
if playerX is less than 0
|
|
83
|
+
playerX becomes 0
|
|
84
|
+
done
|
|
85
|
+
if playerX is greater than widthValue - playerSize
|
|
86
|
+
playerX becomes widthValue - playerSize
|
|
87
|
+
done
|
|
88
|
+
if playerY is less than 0
|
|
89
|
+
playerY becomes 0
|
|
90
|
+
done
|
|
91
|
+
if playerY is greater than heightValue - playerSize
|
|
92
|
+
playerY becomes heightValue - playerSize
|
|
93
|
+
done
|
|
94
|
+
|
|
95
|
+
spawnTimer becomes spawnTimer - delta
|
|
96
|
+
if spawnTimer is less than 0
|
|
97
|
+
spawnEnemy()
|
|
98
|
+
spawnTimer becomes 1.1
|
|
99
|
+
done
|
|
100
|
+
|
|
101
|
+
for each e in enemies
|
|
102
|
+
e.y becomes e.y + e.speed * delta
|
|
103
|
+
if e.y is above heightValue
|
|
104
|
+
scoreValue becomes scoreValue + 1
|
|
105
|
+
resetEnemy(e)
|
|
106
|
+
done
|
|
107
|
+
if e.x is less than playerX + playerSize and e.x + e.size is greater than playerX and e.y is less than playerY + playerSize and e.y + e.size is greater than playerY
|
|
108
|
+
lives becomes lives - 1
|
|
109
|
+
resetEnemy(e)
|
|
110
|
+
if lives is 0
|
|
111
|
+
gameOver becomes true
|
|
112
|
+
done
|
|
113
|
+
done
|
|
114
|
+
done
|
|
115
|
+
done
|
|
116
|
+
|
|
117
|
+
ctx.clearRect(0, 0, widthValue, heightValue)
|
|
118
|
+
ctx.fillStyle becomes "#0d1117"
|
|
119
|
+
ctx.fillRect(0, 0, widthValue, heightValue)
|
|
120
|
+
ctx.fillStyle becomes "#40c463"
|
|
121
|
+
ctx.fillRect(playerX, playerY, playerSize, playerSize)
|
|
122
|
+
ctx.strokeStyle becomes "#2ea043"
|
|
123
|
+
ctx.strokeRect(playerX, playerY, playerSize, playerSize)
|
|
124
|
+
ctx.fillStyle becomes "#ef4444"
|
|
125
|
+
for each e in enemies
|
|
126
|
+
ctx.fillRect(e.x, e.y, e.size, e.size)
|
|
127
|
+
done
|
|
128
|
+
ctx.fillStyle becomes "#e6edf3"
|
|
129
|
+
ctx.font becomes "18px sans-serif"
|
|
130
|
+
ctx.fillText("Score: " + scoreValue, 12, 28)
|
|
131
|
+
ctx.fillText("Lives: " + lives, 12, 52)
|
|
132
|
+
if gameOver is true
|
|
133
|
+
ctx.fillStyle becomes "#f6f8fa"
|
|
134
|
+
ctx.font becomes "34px sans-serif"
|
|
135
|
+
ctx.fillText("Game Over", 230, 208)
|
|
136
|
+
ctx.font becomes "18px sans-serif"
|
|
137
|
+
ctx.fillText("Press Restart to play again", 196, 244)
|
|
138
|
+
done
|
|
139
|
+
done
|
package/examples/collections.pln
CHANGED
package/examples/concurrency.pln
CHANGED
package/examples/conditions.pln
CHANGED
package/examples/database.pln
CHANGED
package/examples/day2.pln
CHANGED
package/examples/day3.pln
CHANGED
package/examples/deployment.pln
CHANGED
package/examples/email-send.pln
CHANGED
package/examples/expressions.pln
CHANGED
package/examples/files.pln
CHANGED
package/examples/functions.pln
CHANGED
package/examples/hello.pln
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// PlainScript 1.0.
|
|
1
|
+
// PlainScript 1.0.36: hello world.
|
|
2
2
|
show "Hello, PlainScript!"
|
package/examples/http-client.pln
CHANGED
package/examples/http.pln
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Dropping into a JavaScript library
|
|
2
|
+
|
|
3
|
+
The full-interop showcase: import an npm package with `bring`, call members and
|
|
4
|
+
`new` constructors on it, pass PlainScript functions as native callbacks, and
|
|
5
|
+
optionally shape the scene with a JSON config fetched at runtime. This is the
|
|
6
|
+
**import path** — unlike `examples/three-dimensional`, which maps `THREE` to a
|
|
7
|
+
browser global, this file demands the package be bundled for the browser.
|
|
8
|
+
|
|
9
|
+
What the script exercises:
|
|
10
|
+
|
|
11
|
+
- `bring THREE from "three"` emits `require('three')` at the top of the build.
|
|
12
|
+
- `new THREE.Scene()`, `new THREE.WebGLRenderer()`, `new THREE.SphereGeometry(...)`
|
|
13
|
+
and `new THREE.Mesh(geo, mat)` fill a starfield of 150 primitives. Colors use
|
|
14
|
+
v1.0.36 hex literals (`0x40c463`), so the palette is legible at a glance.
|
|
15
|
+
- A resize callback `make fitWindow() ... done` passed straight to
|
|
16
|
+
`window.addEventListener("resize", fitWindow)`.
|
|
17
|
+
- `fetchJson("config.json")` (auto-awaiting) reads `{stars, color}` out of a
|
|
18
|
+
served file and re-sizes/re-colors the field. In `try ... recover as err ... done`
|
|
19
|
+
a failing fetch logs the error and falls back to the built-in defaults —
|
|
20
|
+
serve the folder over HTTP so the fetch succeeds, and you'll see the fallback
|
|
21
|
+
path fire if you ever open the page from `file://`.
|
|
22
|
+
- `every frame ... done` rotates the whole scene and calls `renderer.render(...)`.
|
|
23
|
+
- `show "..."` lines land in the browser console because `show` compiles to
|
|
24
|
+
`console.log`.
|
|
25
|
+
|
|
26
|
+
## Run
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
plainscript add three
|
|
30
|
+
npx plainscript build src/interop.pln -o public/interop.js
|
|
31
|
+
npx esbuild public/interop.js --bundle --outfile=public/interop.bundle.js
|
|
32
|
+
npx serve public
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Then open http://localhost:3000 (check the console). `index.html` already points
|
|
36
|
+
at `interop.bundle.js`.
|
|
37
|
+
|
|
38
|
+
`plainscript add <package>` installs a package into the current project and
|
|
39
|
+
saves it to `package.json` (`npm install <package> --save` under the hood). The
|
|
40
|
+
esbuild one-liner above is all the bundling the import path needs; vite or
|
|
41
|
+
rollup work equally well. Build twice — once by PlainScript, once by a bundler —
|
|
42
|
+
is the whole trick: PlainScript emits ordinary CommonJS `require()`s and the
|
|
43
|
+
bundler turns them into browser-safe modules.
|
|
44
|
+
|
|
45
|
+
`check` never needs the package installed: `npx plainscript check src` validates
|
|
46
|
+
imports from the source alone.
|
|
47
|
+
|
|
48
|
+
## What to tweak
|
|
49
|
+
|
|
50
|
+
Point `fetchJson` at a remote URL or drop a different `config.json` next to the
|
|
51
|
+
page. Anything you can reach with `fetch` — `fetchJson` returns
|
|
52
|
+
`{ok, status, data, text, parseError}` — is fair game, and the
|
|
53
|
+
`try ... recover` block shows the friendly-fallback pattern when the network
|
|
54
|
+
misbehaves.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "javascript-library",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"lockfileVersion": 3,
|
|
5
|
+
"requires": true,
|
|
6
|
+
"packages": {
|
|
7
|
+
"": {
|
|
8
|
+
"name": "javascript-library",
|
|
9
|
+
"version": "0.1.0",
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"three": "^0.185.1"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"node_modules/three": {
|
|
15
|
+
"version": "0.185.1",
|
|
16
|
+
"resolved": "https://registry.npmjs.org/three/-/three-0.185.1.tgz",
|
|
17
|
+
"integrity": "sha512-5aojFCXKwnjBRZvUnt3WFfEcvUJgkN5LlijRFN95hMy8WVkG4I0QNcJE+OuWvuJ0bOdStrbfXn0pkd6/QyiAlg==",
|
|
18
|
+
"license": "MIT"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>PlainScript Interop</title>
|
|
7
|
+
<style>
|
|
8
|
+
body {
|
|
9
|
+
margin: 0;
|
|
10
|
+
overflow: hidden;
|
|
11
|
+
background: #0d1117;
|
|
12
|
+
color: #e6edf3;
|
|
13
|
+
font-family: system-ui, "Segoe UI", sans-serif;
|
|
14
|
+
}
|
|
15
|
+
.note {
|
|
16
|
+
position: fixed;
|
|
17
|
+
bottom: 12px;
|
|
18
|
+
left: 50%;
|
|
19
|
+
transform: translateX(-50%);
|
|
20
|
+
width: 90%;
|
|
21
|
+
max-width: 640px;
|
|
22
|
+
padding: 8px 14px;
|
|
23
|
+
border-radius: 10px;
|
|
24
|
+
background: rgba(13, 17, 23, 0.75);
|
|
25
|
+
font-size: 13px;
|
|
26
|
+
color: #8b949e;
|
|
27
|
+
text-align: center;
|
|
28
|
+
pointer-events: none;
|
|
29
|
+
}
|
|
30
|
+
</style>
|
|
31
|
+
</head>
|
|
32
|
+
<body>
|
|
33
|
+
<div class="note">
|
|
34
|
+
Fetching <code>config.json</code> from the server — serve the folder over HTTP
|
|
35
|
+
and the star count / color come from the file; otherwise the scene falls back
|
|
36
|
+
to the built-in defaults (check the console for the log lines).
|
|
37
|
+
</div>
|
|
38
|
+
<script src="interop.bundle.js"></script>
|
|
39
|
+
</body>
|
|
40
|
+
</html>
|