forked from microsoft/tilecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.ts
131 lines (122 loc) · 4.46 KB
/
utilities.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
// Add your code here
namespace tileworld {
const zeroCode = "0".charCodeAt(0);
export function getHelp(help: string, col: number, row: number) {
if (!help)
return null;
let index = 0;
while (index >= 0 && index < help.length) {
let curr = index;
let nextCol = help.substr(curr, 1).charCodeAt(0) - zeroCode;
let nextRow = help.substr(curr + 1, 1).charCodeAt(0) - zeroCode;
let comma = help.indexOf(",", index);
if (nextCol == col && nextRow == row)
return help.substr(curr + 2, comma - curr - 2);
index = comma + 1;
}
return null;
}
export function cursorAnimation(cursor: Sprite, second: Image) {
let anim = animation.createAnimation(0, 300);
anim.addAnimationFrame(cursor.image);
anim.addAnimationFrame(second);
animation.attachAnimation(cursor, anim)
animation.setAction(cursor, 0)
}
// cache these???
export function greyImage(img: Image): Image {
let ret: Image = img.clone();
for(let x=0; x<ret.width(); x++) {
for (let y = 0; y < ret.height(); y++) {
let pix = ret.getPixel(x,y);
ret.setPixel(x,y,pix == 0 ? 0 : 12)
}
}
return ret;
}
export function splitImage(imgLeft: Image, imgRight: Image): Image {
let ret: Image = imgLeft.clone();
for(let x=(ret.width()>>1); x<ret.width(); x++) {
for (let y = 0; y < ret.height(); y++) {
ret.setPixel(x,y,imgRight.getPixel(x,y));
}
}
return ret;
}
export function drawHalfSize(img: Image, nx: number, ny: number, transparent: boolean = false) {
if (!transparent) {
for (let i = 0; i < img.width; i += 2) {
for (let j = 0; j < img.height; j += 2) {
screen.setPixel(nx + (i >> 1), ny + (j >> 1), img.getPixel(i, j))
}
}
} else {
for (let i = 0; i < img.width; i += 2) {
for (let j = 0; j < img.height; j += 2) {
let pix = img.getPixel(i,j);
if (pix)
screen.setPixel(nx + (i >> 1), ny + (j >> 1), pix);
}
}
}
}
export function imageToBuffer(img: Image) {
// worst case = 1 byte per pixel
let buf = control.createBuffer(2 + (img.width() * img.height()));
let index = 0;
buf.setNumber(NumberFormat.Int8LE, index++, img.width());
buf.setNumber(NumberFormat.Int8LE, index++, img.height());
let pixel = 17;
let length = 0;
for(let x = 0; x < img.width(); x++) {
for (let y = 0; y < img.height(); y++) {
let newPixel = img.getPixel(x, y);
if (newPixel != pixel) {
if (length > 0) {
// output run
buf.setUint8(index++, ((length & 0xf) << 4) | (pixel & 0xf));
}
// start new run
pixel = newPixel;
length = 1;
} else {
if (length == 14) {
// output run
buf.setUint8(index++, 0xf0 | (pixel & 0xf));
// reset
pixel = 17;
length = 0;
} else {
length++;
}
}
}
}
// last bit (if needed)
if (length > 0) {
buf.setUint8(index++, ((length & 0xf) << 4) | (pixel & 0xf));
}
// return exactly the amount used.
return buf.slice(0, index);
}
export function bufferToImage(buf: Buffer) {
let width = buf.getNumber(NumberFormat.Int8LE, 0);
let height = buf.getNumber(NumberFormat.Int8LE, 1);
let index = 2;
let img = image.create(width, height);
let x = 0;
let y = 0;
while (index < buf.length) {
let pair = buf.getUint8(index++);
let pixel = pair & 0xf;
let len = (pair & 0xf0) >> 4;
while (len > 0) {
img.setPixel(x, y, pixel);
if (y == height -1 ) { x++; y = 0; } else { y++; }
len--;
}
}
control.assert(index == buf.length, 54);
return img;
}
}