-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.js
141 lines (123 loc) · 3.38 KB
/
camera.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
/**
Build a new camera object.
mapSize: an array of length 2, indicating the size of the map
// pos: a point, indicating the coordinates of top-left corner of the camera on the map
movingSpeed: the distance that the camera should move when user presses an arrow key once.
img: background image for the minimap.
layer: the kinetic layer that miniMap will be drawn on.
**/
var BuildCamera = function(mapSize, movingSpeed, img, layer) {
var me = this;
// fields
this.mapSize = mapSize;
this.x = 0;
this.y = 0;
this.speed = movingSpeed;
this.isMovingLeft = false;
this.isMovingRight = false;
this.isMovingUp = false;
this.isMovingDown = false;
this.dragged = false;
this.lastTouchCoord = null;
// add background image
var bg = new Kinetic.Image({
x: 0,
y: 0,
image: img,
width: CONSTANTS.width,
height: CONSTANTS.height
});
layer.add(bg);
// add animation to move camera and background image
var me = this;
this.anime = new Kinetic.Animation(function(frame){
if (me.isMovingLeft)
me.moveLeft();
if (me.isMovingRight)
me.moveRight();
if (me.isMovingUp)
me.moveUp();
if (me.isMovingDown)
me.moveDown();
bg.setCrop({
x: me.x,
y: me.y,
width: CONSTANTS.width,
height: CONSTANTS.height
});
}, layer);
this.anime.start();
// add drag support for mobile devices
// should be moved to UI class
bg.on('touchstart', function(event){
// If this is a one-finger touch
if (event.targetTouches.length == 1) {
var touch = event.targetTouches[0];
me.lastTouchCoord = new Point(touch.pageX, touch.pageY);
me.dragged = false;
}
});
bg.on('touchmove', function(event){
// If this is a one-finger touch
if (event.targetTouches.length == 1) {
var touch = event.targetTouches[0];
var currTouchCoord = new Point(touch.pageX, touch.pageY);
me.movePos(me.lastTouchCoord, currTouchCoord);
me.lastTouchCoord = currTouchCoord;
me.dragged = true;
}
});
// methods
this.stop = function(){
if (this.anime) {
this.anime.stop();
}
}
this.setPos = function(p){
var newX = p.X;
var newY = p.Y;
if (newX < 0) newX = 0;
if (newX + CONSTANTS.width > this.mapSize[0])
newX = this.mapSize[0] - CONSTANTS.width;
if (newY < 0) newY = 0;
if (newY + CONSTANTS.height > this.mapSize[1])
newY = this.mapSize[1] - CONSTANTS.height;
this.x = newX;
this.y = newY;
console.log("Set position to (" + this.x + ", " + this.y + ")");
};
this.moveLeft = function(){
var i = me.x - me.speed;
if (i >= 0)
me.x = i;
};
this.moveRight = function(){
var i = me.x + me.speed;
if (i + CONSTANTS.width <= me.mapSize[0])
me.x = i;
};
this.moveUp = function(){
var i = me.y - me.speed;
if (i >= 0)
me.y = i;
};
this.moveDown = function(){
var i = me.y + me.speed;
if (i + CONSTANTS.height <= me.mapSize[1])
me.y = i;
};
this.movePos = function(/*Point*/ p1, /*Point*/ p2){
var pos = new Point(me.x, me.y);
pos.X -= p2.X - p1.X;
pos.Y -= p2.Y - p1.Y;
me.setPos(pos);
};
this.touchstart = function(event){
console.log("Fire touchstart");
bg.fire('touchstart', event);
};
this.touchmove = function(event){
console.log("Fire touchmove");
bg.fire('touchmove', event);
};
};