|
@@ -9,11 +9,15 @@
|
9
|
9
|
<script src="res/data/layout.js" type="text/javascript"></script>
|
10
|
10
|
<script type="text/javascript">
|
11
|
11
|
$(function() {
|
12
|
|
- var activeTower, thisLevel;
|
|
12
|
+ var thisLevel;
|
13
|
13
|
|
14
|
14
|
$.fn.rotate = function(rot) {
|
|
15
|
+ this.data('rotation', rot);
|
|
16
|
+
|
15
|
17
|
return this.css('-webkit-transform', 'rotate(' + rot + 'deg)')
|
16
|
|
- .css('-moz-transform', 'rotate(' + rot + 'deg');
|
|
18
|
+ .css('-moz-transform', 'rotate(' + rot + 'deg')
|
|
19
|
+ .css('-o-transform', 'rotate(' + rot + 'deg')
|
|
20
|
+ .css('transform', 'rotate(' + rot + 'deg');
|
17
|
21
|
};
|
18
|
22
|
|
19
|
23
|
$.fn.offsetFrom = function(el) {
|
|
@@ -22,6 +26,11 @@
|
22
|
26
|
return {top: offset.top - otherOffset.top, left: offset.left - otherOffset.left};
|
23
|
27
|
}
|
24
|
28
|
|
|
29
|
+ $.fn.offsetCentre = function() {
|
|
30
|
+ var offset = this.offset();
|
|
31
|
+ return {top: offset.top + this.height() / 2, left: offset.left + this.width() / 2};
|
|
32
|
+ }
|
|
33
|
+
|
25
|
34
|
function createBaseElForTower(key) {
|
26
|
35
|
var type = towers[key];
|
27
|
36
|
|
|
@@ -35,10 +44,44 @@
|
35
|
44
|
function createElForTower(tower) {
|
36
|
45
|
return createBaseElForTower(tower.type)
|
37
|
46
|
.data('tower', tower)
|
38
|
|
- .draggable()
|
|
47
|
+ .draggable({
|
|
48
|
+ start: function(evt) {
|
|
49
|
+ return !evt.shiftKey;
|
|
50
|
+ }
|
|
51
|
+ })
|
39
|
52
|
.css('position', 'absolute')
|
40
|
53
|
.offset(adjustMapOffset(tower.position, thisLevel))
|
41
|
|
- .rotate(tower.rotation);
|
|
54
|
+ .rotate(tower.rotation)
|
|
55
|
+ .mousedown(function(e) {
|
|
56
|
+ if (!e.shiftKey) {
|
|
57
|
+ return;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ var el = $(this);
|
|
61
|
+ var centre = el.offsetCentre();
|
|
62
|
+ var mouseX = e.pageX - centre.left, mouseY = e.pageY - centre.top;
|
|
63
|
+ var initialMouseAngle = Math.atan2(mouseY, mouseX);
|
|
64
|
+ var initialRotation = el.data('rotation');
|
|
65
|
+
|
|
66
|
+ var moveHandler = function(evt) {
|
|
67
|
+ var mouseX = evt.pageX - centre.left, mouseY = evt.pageY - centre.top;
|
|
68
|
+ var newMouseAngle = Math.atan2(mouseY, mouseX);
|
|
69
|
+ var mouseDelta = newMouseAngle - initialMouseAngle;
|
|
70
|
+ var rotation = initialRotation + newMouseAngle * (180 / Math.PI);
|
|
71
|
+ el.data('tower').rotation = rotation;
|
|
72
|
+ el.rotate(rotation);
|
|
73
|
+ };
|
|
74
|
+
|
|
75
|
+ var upHandler = function() {
|
|
76
|
+ $(document).unbind('mousemove', moveHandler);
|
|
77
|
+ $(document).unbind('mouseup', upHandler);
|
|
78
|
+ };
|
|
79
|
+
|
|
80
|
+ $(document).mousemove(moveHandler);
|
|
81
|
+ $(document).mouseup(upHandler);
|
|
82
|
+
|
|
83
|
+ return false;
|
|
84
|
+ });
|
42
|
85
|
}
|
43
|
86
|
|
44
|
87
|
function adjustMapOffset(towerOffset, level, reverse) {
|