-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathle.js
94 lines (88 loc) · 2.37 KB
/
le.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
// #persistant
LevelEditor = function(u, mainctx){
var le = this;
var canvas = mainctx.canvas;
le.selected = null;
var mouseDown = [false, false, false];
var mx, my;
var pmx, pmy;
var offset_mode_modifier = "ctrl";
var offset_mode_button = false;
var offset_mode_started = false;
var offset_start_mx, offset_start_my;
var currentTile;
le.update = function(editing){
//hey it does something probably
if(le.selected && mouseDown[0]){
if(offset_mode_button){
currentTile = {
//offset_mx = mx - offset_start_mx
spriteX:le.selected.spriteX+mx-offset_start_mx,
spriteY:le.selected.spriteY+my-offset_start_my,
sprite:le.selected.sprite
};
var img = aa.getImage(currentTile.sprite);
if(currentTile.spriteX < 0
|| currentTile.spriteY < 0
|| currentTile.spriteX >= (img.width/TS)
|| currentTile.spriteY >= (img.height/TS)){
currentTile = null;
}
} else {
currentTile = le.selected;
}
draw(currentTile);
}else if(mouseDown[2]){
draw(null)
}
if(offset_mode_button){
//preview palette
mainctx.globalAlpha = 0.2;
mainctx.drawImage(
aa.getImage(le.selected.sprite),
TS*((offset_mode_started? offset_start_mx :mx) - le.selected.spriteX),
TS*((offset_mode_started? offset_start_my :my) - le.selected.spriteY)
);
mainctx.globalAlpha = 1;
}
function draw(block){
for(var name in u.rooms){
var room = u.rooms[name];
var rmx = mx - room.x;
var rmy = my - room.y;
if(rmx>=0 && rmy>=0 && rmx<room.width && rmy<room.height){
room.rows[rmy][rmx] = block;//@TODO #BUG implement copyof operator
return;
}
}
}
};
canvas.addEventListener("mousemove",function(e){
mx = Math.floor(e.clientX / TS);
my = Math.floor(e.clientY / TS);
});
addEventListener("keyup", function(e){
if(!e[offset_mode_modifier+"Key"]){
offset_mode_button = false;
offset_mode_started = false;
}
});
addEventListener("keydown", function(e){
if(e[offset_mode_modifier+"Key"]){
offset_mode_button = true;
}
});
canvas.addEventListener("mousedown",function(e){
mx = Math.floor(e.clientX / TS);
my = Math.floor(e.clientY / TS);
if(offset_mode_button && !offset_mode_started){
offset_start_mx = mx;
offset_start_my = my;
offset_mode_started = true;
}
mouseDown[e.button] = true;
});
addEventListener("mouseup",function(e){
mouseDown[e.button] = false;
});
};