Skip to content

Commit 1271bc8

Browse files
committed
Upgrade animation graphics for Chapter 7
1 parent 9b7db1e commit 1271bc8

9 files changed

+37
-29
lines changed

Diff for: code/animatevillage.js

+23-19
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
let active = null
77

88
const places = {
9-
"Alice's House": {x: 310, y: 90},
10-
"Bob's House": {x: 312, y: 196},
11-
"Cabin": {x: 378, y: 60},
12-
"Post Office": {x: 215, y: 60},
13-
"Town Hall": {x: 213, y: 200},
14-
"Daria's House": {x: 170, y: 282},
15-
"Ernie's House": {x: 69, y: 283},
16-
"Grete's House": {x: 37, y: 193},
17-
"Farm": {x: 61, y: 119},
18-
"Shop": {x: 113, y: 211},
19-
"Marketplace": {x: 200, y: 98}
9+
"Alice's House": {x: 279, y: 100},
10+
"Bob's House": {x: 295, y: 203},
11+
"Cabin": {x: 372, y: 67},
12+
"Daria's House": {x: 183, y: 285},
13+
"Ernie's House": {x: 50, y: 283},
14+
"Farm": {x: 36, y: 118},
15+
"Grete's House": {x: 35, y: 187},
16+
"Marketplace": {x: 162, y: 110},
17+
"Post Office": {x: 205, y: 57},
18+
"Shop": {x: 137, y: 212},
19+
"Town Hall": {x: 202, y: 213}
2020
}
21+
const placeKeys = Object.keys(places)
2122

2223
const speed = 2
2324

@@ -30,14 +31,14 @@
3031

3132
let outer = (window.__sandbox ? window.__sandbox.output.div : document.body), doc = outer.ownerDocument
3233
this.node = outer.appendChild(doc.createElement("div"))
33-
this.node.style.cssText = "position: relative; line-height: 0.1; padding-left: 10px"
34+
this.node.style.cssText = "position: relative; line-height: 0.1; margin-left: 10px"
3435
this.map = this.node.appendChild(doc.createElement("img"))
3536
this.map.src = "img/village2x.png"
3637
this.map.style.cssText = "vertical-align: -8px"
3738
this.robotElt = this.node.appendChild(doc.createElement("div"))
3839
this.robotElt.style.cssText = `position: absolute; transition: left ${0.8 / speed}s, top ${0.8 / speed}s;`
3940
let robotPic = this.robotElt.appendChild(doc.createElement("img"))
40-
robotPic.src = "img/robot2x.png"
41+
robotPic.src = "img/robot_moving2x.gif"
4142
this.parcels = []
4243

4344
this.text = this.node.appendChild(doc.createElement("span"))
@@ -57,8 +58,8 @@
5758

5859
updateView() {
5960
let pos = places[this.worldState.place]
60-
this.robotElt.style.top = (pos.y - this.robotElt.offsetHeight) + "px"
61-
this.robotElt.style.left = (pos.x - (this.robotElt.offsetWidth / 2)) + "px"
61+
this.robotElt.style.top = (pos.y - 38) + "px"
62+
this.robotElt.style.left = (pos.x - 16) + "px"
6263

6364
this.text.textContent = ` Turn ${this.turn} `
6465
}
@@ -70,11 +71,11 @@
7071
let height = heights[place] || (heights[place] = 0)
7172
heights[place] += 11
7273
let node = document.createElement("div")
73-
node.style.cssText = "position: absolute; height: 10px; width: 10px; background-image: url(img/parcel2x.png); font-size: 10px; text-align: center; line-height: 10px; font-family: sans-serif"
74-
node.textContent = address.charAt(0)
74+
let offset = placeKeys.indexOf(address) * 10
75+
node.style.cssText = "position: absolute; height: 10px; width: 10px; background-image: url(img/parcel2x.png); background-position: 0 -" + offset + "px";
7576
if (place == this.worldState.place) {
76-
node.style.right = "-5px"
77-
node.style.bottom = (24 + height) + "px"
77+
node.style.left = "27px"
78+
node.style.bottom = (20 + height) + "px"
7879
this.robotElt.appendChild(node)
7980
} else {
8081
let pos = places[place]
@@ -95,6 +96,7 @@
9596
if (this.worldState.parcels.length == 0) {
9697
this.button.remove()
9798
this.text.textContent = ` Finished after ${this.turn} turns`
99+
this.robotElt.firstChild.src = "img/robot_idle2x.png"
98100
} else {
99101
this.schedule()
100102
}
@@ -108,10 +110,12 @@
108110
if (this.timeout == null) {
109111
this.schedule()
110112
this.button.textContent = "Stop"
113+
this.robotElt.firstChild.src = "img/robot_moving2x.gif"
111114
} else {
112115
clearTimeout(this.timeout)
113116
this.timeout = null
114117
this.button.textContent = "Start"
118+
this.robotElt.firstChild.src = "img/robot_idle2x.png"
115119
}
116120
}
117121
}

Diff for: html/js/sandbox.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
this.framePos = 0
5555

5656
// Loaded CommonJS modules
57-
this.loaded = new Cached(name => resolved.compute(name).then(({name, code}) => this.evalModule(name, code)))
57+
this.loaded = window.loadedModules = new Cached(name => resolved.compute(name).then(({name, code}) => this.evalModule(name, code)))
5858

5959
this.frame = frame
6060
this.win = frame.contentWindow
@@ -78,11 +78,13 @@
7878
this.startedAt = Date.now()
7979
this.extraSecs = 2
8080
this.win.__c = 0
81-
this.prepare(code).then(code => this.win.eval(code)).catch(err => this.error(err))
81+
this.prepare(code)
82+
.then(code => code instanceof Function ? code() : this.win.eval(code))
83+
.catch(err => this.error(err))
8284
}
8385

8486
prepare(text) {
85-
let {code, dependencies} = preprocess(text)
87+
let {code, dependencies} = preprocess(text, this)
8688
return Promise.all(dependencies.map(dep => this.loaded.compute(dep))).then(() => code)
8789
}
8890

@@ -267,17 +269,19 @@
267269

268270
function preprocess(code, sandbox) {
269271
if (typeof code != "string") {
270-
if (code.apply)
271-
return (...args) => {
272-
try { return code.apply(this, args) }
272+
if (code.apply) {
273+
let orig = code
274+
code = (...args) => {
275+
try { return orig.apply(null, args) }
273276
catch(e) { sandbox.error(e) }
274277
}
275-
return code
278+
}
279+
return {code, dependencies: []}
276280
}
277281

278282
let strict = /^(\s|\/\/.*)*["']use strict['"]/.test(code), ast
279283
try { ast = acorn.parse(code, {sourceType: detectSourceType(code)}) }
280-
catch(e) { return code }
284+
catch(e) { return {code, dependencies: []} }
281285
let patches = []
282286
let backJump = "if (++__c % 1000 === 0) __sandbox.tick();"
283287
function loop(node) {
@@ -307,7 +311,7 @@
307311
if (node.specifiers.length == 0) {
308312
text = req
309313
} else if (node.specifiers.length > 1 || node.specifiers[0].type == "ImportDefaultSpecifier") {
310-
let name = modVar(node.source.value)
314+
let name = "m_" + node.source.value.replace(/\W+/g, "_") + "__"
311315
text = "var " + name + " = " + req
312316
node.specifiers.forEach(spec => {
313317
if (spec.type == "ImportDefaultSpecifier")
@@ -428,7 +432,7 @@
428432
}
429433

430434
// Cache for loaded code and resolved unpkg redirects
431-
const resolved = new Cached(name => fetch("https://unpkg.com/" + name.replace(/\/$/, "")).then(resp => {
435+
const resolved = window.resolved = new Cached(name => fetch("https://unpkg.com/" + name.replace(/\/$/, "")).then(resp => {
432436
if (resp.status >= 400) throw new Error(`Failed to resolve package '${name}'`)
433437
let found = resp.url.replace(/.*unpkg\.com\//, "")
434438
let known = resolved.get(found)

Diff for: img/parcel2x.png

146 Bytes
Loading

Diff for: img/robot_idle.png

433 Bytes
Loading

Diff for: img/robot_idle2x.png

460 Bytes
Loading

Diff for: img/robot_moving.gif

1.63 KB
Loading

Diff for: img/robot_moving2x.gif

1.52 KB
Loading

Diff for: img/village.png

7.58 KB
Loading

Diff for: img/village2x.png

14.5 KB
Loading

0 commit comments

Comments
 (0)