forked from microsoft/tilecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.ts
379 lines (336 loc) · 13.9 KB
/
project.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
namespace tileworld {
export let TileWorldVersion = "1.0.0";
export class SwitchExport {
constructor(private p: Project, private backgrounds: boolean = true) {
}
public getImages() {
return this.backgrounds ? this.p.backgroundImages() : this.p.spriteImages();
}
public getImage(kind: number) {
return this.backgrounds ? this.p.getBackgroundImage(kind) : this.p.getSpriteImage(kind);
}
public saveImage(kind: number){
this.backgrounds ? this.p.saveBackgroundImage(kind) : this.p.saveSpriteImage(kind);
}
}
export class AllExport {
private allImages: Image[];
constructor(private p: Project) {
this.allImages = [];
this.p.backgroundImages().forEach(img => this.allImages.push(img));
this.p.spriteImages().forEach(img => this.allImages.push(img));
}
public getImages() {
return this.allImages;
}
public getImage(index: number) {
return this.allImages[index];
}
public saveImage(index: number) {
index < this.p.backCnt() ? this.p.saveBackgroundImage(index) : this.p.saveSpriteImage(index - this.p.backCnt());
}
public getSetAttr(rv: RuleView, whendo: number, aid: number, val:number = 0xffff) {
return aid < this.p.backCnt() ? rv.getSetBgAttr(whendo, aid, val) : rv.getSetSpAttr(whendo, aid - this.p.backCnt(), val);
}
}
export class Project {
private lastRule: RuleView = null;
private _player: number = -1;
private _backgrounds: Image = null;
private _sprites: Image = null;
public debug: boolean = false;
public help: boolean = true;
public version: string;
private rules: RuleView[] = []; // the rules
constructor(
public prefix: string,
private _backgroundsI: Image[], // the user-defined backgrounds
private _spritesI: Image[] // the user-defined sprites
) {
// TODO: enforce that backgroundI and spriteI lengths are from the set { 4, 8, 12, }
// TODO: leaving us room for 4 runtime backgrounds and sprites
}
public setRules(rvl: RuleView[]) {
this.rules = rvl;
}
public getRules() {
return this.rules;
}
public setPlayer(kind: number) {
this._player = kind;
}
public getPlayer() {
return this._player;
}
// a world consists of two images
// - tile backgrounds (values 0-14)
// - tile sprites (values 0-14)
public setWorldBackgrounds(img: Image) {
this._backgrounds = img;
}
public getWorldBackgrounds() {
return this._backgrounds;
}
public setWorldSprites(img: Image) {
this._sprites = img;
}
public getWorldSprites() {
return this._sprites;
}
// images
public backCnt() { return this._backgroundsI.length; }
public spriteCnt() { return this._spritesI.length; }
public allCnt() { return this.backCnt() + this.spriteCnt(); }
public backgroundImages() { return this._backgroundsI; }
public spriteImages() { return this._spritesI; }
public getBackgroundImage(kind: number) {
return 0 <= kind && kind < this.backCnt() ? this._backgroundsI[kind] : null;
}
public getSpriteImage(kind: number) {
return 0 <= kind && kind < this.spriteCnt() ? this._spritesI[kind] : null;
}
public saveBackgroundImage(kind: number) {
let buf = saveImage(this.prefix, kind, this.getBackgroundImage(kind), true);
}
public saveSpriteImage(kind: number) {
let buf = saveImage(this.prefix, kind, this.getSpriteImage(kind), false);
}
public saveRule(rv: RuleView) {
if (rv.getRuleId() == -1)
return;
this.storeRule(this.prefix, rv.getRuleId(), rv.getBaseRule());
}
public makeRule(rt: RuleType, ra: RuleArg | MoveDirection, kind: number = 0xffff) {
let rv = this.wrapRule(makeNewRule(rt, ra));
if (kind != 0xffff) {
// this is a bit of a mess
let wd = rv.makeWhenDo(2, 2);
rv.getSetSpAttr(wd, kind, AttrType.Include);
if (rt == RuleType.ContextChange || rt == RuleType.Collision) {
rv.setWitnessDirection(wd, ra);
}
}
this.saveRule(rv);
return rv;
}
public removeRule(rid: number) {
let r = this.rules.find(r => r.getRuleId() == rid);
if (r) {
this.rules.removeElement(r);
settings.remove(this.prefix + RuleKey + rid.toString());
}
}
public saveWorld() {
let worldBuf = imageToBuffer(this._backgrounds);
settings.writeBuffer(this.prefix + WorldBackgroundsKey, worldBuf);
let spritesBuf = imageToBuffer(this._sprites);
settings.writeBuffer(this.prefix + WorldSpritesKey, spritesBuf);
}
public saveHelp() {
settings.writeNumber(this.prefix+HelpKey, this.help ? 1 : 0);
}
private storeRule(prefix: string, rid: number, rule: Rule) {
let buf = packRule(rule, this.backCnt(), this.spriteCnt());
settings.writeBuffer(prefix + RuleKey + rid.toString(), buf);
return buf;
}
public saveProject() {
let prefix = this.prefix;
settings.writeString(prefix + VersionKey, this.version);
settings.writeNumber(prefix + HelpKey, this.help ? 1 : 0);
settings.writeNumber(prefix + BackImgCntKey, this.backCnt());
settings.writeNumber(prefix + SpriteImgCntKey, this.spriteCnt());
settings.writeNumber(prefix + PlayerIndexKey, this.getPlayer());
this.backgroundImages().forEach((img, i) => {
let buf = saveImage(prefix, i, img, true);
});
this.spriteImages().forEach((img, i) => {
let buf = saveImage(prefix, i, img, false);
});
let worldBuf = imageToBuffer(this.getWorldBackgrounds());
settings.writeBuffer(prefix + WorldBackgroundsKey, worldBuf);
let spritesBuf = imageToBuffer(this.getWorldSprites());
settings.writeBuffer(prefix + WorldSpritesKey, spritesBuf);
this.getRules().forEach(r => {
let buf = this.storeRule(prefix, r.getRuleId(), r.getBaseRule());
});
}
private wrapRule(r: Rule) {
// find a new id that is not in rule list
let rids = this.rules.map(r => r.getRuleId()).sort((a,b) => a - b );
let rid = 0;
for(let i = 0; i< rids.length; i++) {
if (rid != rids[i])
break;
rid = rids[i]+1;
}
let newRule = new RuleView(this, rid, r);
this.rules.push(newRule);
return newRule;
}
public getRulesForSpriteKind(kind: number) {
return this.rules.filter(rv => rv.hasSpriteKind(kind));
}
}
const toHex = "0123456789abcdef";
function outputKeyBuffer(key: string, val: Buffer) {
// create hex literal from buffer
console.log("// buffer length = "+val.length.toString());
console.log("settings.writeBuffer(\""+key+"\", hex`");
let chunk = 40;
let str = "";
for(let i=0;i<val.length;i++) {
let byte = val.getUint8(i);
str += toHex[(byte & 0xf0) >> 4] + toHex[byte & 0x0f];
chunk--;
if (chunk == 0) { console.log(str); chunk = 40; str = ""; }
}
console.log(str+"`);")
}
function settingsReadNumber(key: string, output: boolean) {
let val = settings.readNumber(key);
if (output) console.log("settings.writeNumber(\""+key+"\","+val.toString()+");");
return val;
}
function settingsReadString(key: string, output: boolean) {
let val = settings.readString(key);
if (output) console.log("settings.writeString(\"" + key + "\",\"" + val + "\");");
return val;
}
function settingsReadBuffer(key: string, output: boolean) {
let buf = settings.readBuffer(key);
if (output) outputKeyBuffer(key, buf);
return buf;
}
const VersionKey = "VersionS";
const HelpKey = "HelpN";
const BackImgCntKey = "BackN";
const SpriteImgCntKey = "SpriteN";
const PlayerIndexKey = "PlayerN";
const WorldBackgroundsKey = "WBackM";
const WorldSpritesKey = "WSpriteM";
const BackImageKey = "BackI";
const SpriteImageKey = "SpriteI";
const RuleKey = "RuleB";
function readImages(cnt: number, prefix: string, output: boolean) {
let images: Image[] = []
for (let i = 0; i < cnt; i++) {
let key = prefix + i.toString();
let buf = settingsReadBuffer(key, output);
let img = buf && buf.length > 0 ? bufferToImage(buf) : null;
if (!img) { img = image.create(16, 16); img.fill(1 + i); }
images.push(img);
}
return images;
}
export function loadProject(prefix: string, output: boolean = false) {
let names = settings.list(prefix);
if (names.length == 0)
return null;
if (output) console.log("function create"+prefix.slice(0,-1)+"() {");
let version = settingsReadString(prefix + VersionKey, output);
// get the tile map, handling errors
let buf = settingsReadBuffer(prefix + WorldBackgroundsKey, output);
let world = buf && buf.length > 0 ? bufferToImage(buf) : null;
world = world ? world : image.create(32, 24);
// sprite map
buf = settingsReadBuffer(prefix + WorldSpritesKey, output);
let sprites = buf && buf.length > 0 ? bufferToImage(buf) : null;
sprites = sprites ? sprites : image.create(32, 24);
// background images and sprite images
let backCnt = settingsReadNumber(prefix + BackImgCntKey, output);
let backImages = readImages(backCnt, prefix+BackImageKey, output);
let spriteCnt = settingsReadNumber(prefix + SpriteImgCntKey, output);
let spriteImages = readImages(spriteCnt, prefix + SpriteImageKey, output);
let helpNum = settingsReadNumber(prefix + HelpKey, output);
let help = helpNum ? true: false;
// start project
let p = new Project(prefix, backImages, spriteImages);
// get the rules, at least
let ruleName = prefix + RuleKey;
let ruleids = names.filter(s => s.indexOf(ruleName) == 0).map(s => parseInt(s.substr(ruleName.length())));
let rules: RuleView[] = [];
ruleids.forEach(rid => {
let key = ruleName+rid.toString();
let buf = settingsReadBuffer(key, output);
let rule = unPackRule(buf, backCnt, spriteCnt);
rules.push(new RuleView(p, rid, rule));
});
let player = settingsReadNumber(prefix + PlayerIndexKey, output);
if (output) console.log("}");
p.setRules(rules);
p.setWorldBackgrounds(world);
p.setWorldSprites(sprites);
p.setPlayer(player);
p.help = help;
p.version = version;
return p;
}
function saveImage(prefix: string, kind: number, img: Image, background: boolean) {
let buf = imageToBuffer(img);
settings.writeBuffer(prefix + (background ? BackImageKey : SpriteImageKey) + kind.toString(), buf);
return buf;
}
/* TODO: reimplement using new APIs
function fillAttr(f: number, n: number, i: number, g: number) {
// TODO: redo this using buffer
let res: AttrType[] = [];
for (let j = 0; j < n; j++) {
res.push(j == i ? g : f);
}
return res;
}
function wall () {
return fillAttr(AttrType.OK, 8, 0, AttrType.Exclude);
}
function ok() {
return fillAttr(AttrType.OK, 8, 0, AttrType.OK);
}
function makePushRule(dir: MoveDirection) {
return new Rule(RuleType.ButtonPress, dir, []);
// TODO: finish this off
// [new WhenDo(2+moveXdelta(dir), 2+moveYdelta(dir), wall(), 0, []), new WhenDo(2, 2, ok(), 0, [new Command(CommandType.Move, dir)])]);
}
*/
const player = img`
. . . . . . f f f f . . . . . .
. . . . f f f 2 2 f f f . . . .
. . . f f f 2 3 2 2 f f f . . .
. . f f f e e e e e e f f f . .
. . f f e 2 2 2 2 2 2 e e f . .
. . f e 2 f f f f f f 2 e f . .
. . f f f f e e e e f f f f . .
. f f e f b f 4 4 f b f e f f .
. f e e 4 1 f d d f 1 4 e e f .
. . f e e d d d d d d e e f . .
. . . f e e 4 4 4 4 e e f . . .
. . e 4 f 2 2 2 2 2 2 f 4 e . .
. . 4 d f 2 2 2 2 2 2 f d 4 . .
. . 4 4 f 4 4 5 5 4 4 f 4 4 . .
. . . . . f f f f f f . . . . .
. . . . . f f . . f f . . . . .
`;
export function emptyProject(prefix: string) {
let fixed: Image[] = [];
let movable: Image[] = [];
for(let f=0;f<4;f++) {
fixed.push(galleryTiles[f].clone());
}
movable.push(player.clone());
for (let f = 0; f < 3; f++) {
movable.push(gallerySprites[f].clone());
}
let rules: Rule[] = [];
//for(let dir = 0; dir < 4; dir++) { rules.push(makePushRule(dir)); }
let p = new Project(prefix, fixed, movable); // makeIds(rules));
let world = image.create(32, 24);
helpers.imageFillRect(world, 1, 1, 30, 22, 1);
let sprites = image.create(32, 24);
sprites.fill(0xf);
p.setWorldBackgrounds(world);
p.setWorldSprites(sprites);
p.setPlayer(0);
p.version = TileWorldVersion;
return p;
}
}