-
Notifications
You must be signed in to change notification settings - Fork 1
/
actorbin.js
142 lines (117 loc) · 3.79 KB
/
actorbin.js
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
function ActorBrush(actor) {
this.type = "actor";
this.value = actor;
this.origin = actor.origin;
this.onPaint = function(x, y, e) {
switch (e.which) {
case 1:
if (!map.getCell([x,y]).allowsActor()) return;
var b = map.getCell([x,y]).actor;
if (b && !b.destroyed) {
// Give the part back
map.parts[b.name]++;
}
map.parts[this.value.name]--;
map.getCell([x,y]).actor = this.value;
var actors = map.getActorsAndCounts();
if (b && !b.destroyed)
brush = new ActorBrush(b);
else if (actors[this.value.name] != 0)
brush = new ActorBrush(actorTypes[this.value.name]);
else
brush = new SelectionBrush();
break;
case 3:
brush = new SelectionBrush();
break;
}
drawActorbin();
};
this.rotate = function() {
this.value = this.value.rotate();
this.origin = this.value.origin;
};
this.draw = function(ctx) {
if (nearestTileX == null) return;
// Actor
ctx.globalAlpha = 0.5;
ctx.drawImage(imageSheet, this.origin[0] * tileSize,
this.origin[1] * tileSize, tileSize, tileSize,
nearestTileX, nearestTileY, tileSize, tileSize);
ctx.globalAlpha = 1;
// Selection rect
var cy = map.getCell([nearestTileX / tileSize, nearestTileY / tileSize]).allowsActor() ? 14 : 15;
ctx.drawImage(imageSheet, (4 + (tick >> 1)%3) * tileSize,
cy * tileSize, tileSize, tileSize,
nearestTileX, nearestTileY, tileSize, tileSize);
}
}
ActorBrush.prototype = new Brush();
function actorbinClicked(e) {
brush = new SelectionBrush();
var ui = $('#actorbin');
var maxPanelsInRow = ui.width() / tileSize;
// Find the tile we clicked in
var x = Math.floor((e.pageX - ui.offset().left) / tileSize);
var y = Math.floor((e.pageY - ui.offset().top) / tileSize);
var i = y*maxPanelsInRow + x;
// derp
var actors = map.getActorsAndCounts();
var types = [];
for (var foo in actors) types.push(foo);
// Make the tile the active brush
if (x < maxPanelsInRow && i < types.length && actors[types[i]] != 0) {
brush = new ActorBrush(actorTypes[types[i]]);
}
drawActorbin();
drawTilebin();
}
function drawDigit(ctx,x,y,n)
{
var m = 0+n;
if (n < 0 || n == undefined) m = 10;
if (n == 'x') m = 11;
col = m & 3;
row = m >> 2;
ctx.drawImage(imageSheet, 4 * 32 + col*16, 12 * 32 + row*16, 16, 16,
x, y, 16, 16 );
}
function drawActorbin() {
var ui = $('#actorbin');
var ctx = ui[0].getContext('2d');
ctx.clearRect(0,0,ui.width(),ui.height());
var maxPanelsInRow = ui.width() / tileSize;
var x = 0;
var y = 0;
var actors = map.getActorsAndCounts();
for (var p in actors) {
var a = actorTypes[p];
var count = actors[p];
if (!count) ctx.globalAlpha = 0.3;
// Draw selection rect
if (brush && brush.type == "actor" && brush.value == a) {
ctx.lineWidth = 3;
ctx.strokeStyle = "limegreen";
ctx.strokeRect(x * tileSize+1, y * tileSize+1,
tileSize-2, tileSize-2);
}
ctx.drawImage(imageSheet, a.origin[0] * tileSize,
a.origin[1] * tileSize, tileSize,
tileSize, x * tileSize, y * tileSize, tileSize, tileSize);
var u = x * tileSize + 24;
var first = true;
while( first || count > 0 ) {
drawDigit( ctx, u, y * tileSize + 21, count % 10 );
u -= 8;
count = Math.floor( count / 10 );
first = false;
}
drawDigit( ctx, u, y * tileSize + 21, 'x' );
ctx.globalAlpha = 1;
x++;
if (x >= maxPanelsInRow) {
x = 0;
y++;
}
}
}