10 | 10 |
* methods.
|
11 | 11 |
*/
|
12 | 12 |
yoob.Sprite = function() {
|
13 | |
this.isDraggable = false;
|
14 | |
this.isClickable = false;
|
15 | |
|
16 | |
this.init = function(x, y, w, h) {
|
|
13 |
|
|
14 |
/*
|
|
15 |
* x and y always represent the CENTRE of the Sprite().
|
|
16 |
*/
|
|
17 |
this.init = function(cfg) {
|
|
18 |
this.x = cfg.x;
|
|
19 |
this.y = cfg.y;
|
|
20 |
this.width = cfg.width;
|
|
21 |
this.height = cfg.height;
|
|
22 |
this.dx = cfg.dx || 0;
|
|
23 |
this.dy = cfg.dy || 0;
|
|
24 |
this.isDraggable = cfg.isDraggable || false;
|
|
25 |
this.isClickable = cfg.isClickable || false;
|
|
26 |
this.selected = false;
|
|
27 |
return this;
|
|
28 |
};
|
|
29 |
|
|
30 |
this.getX = function() {
|
|
31 |
return this.x;
|
|
32 |
};
|
|
33 |
|
|
34 |
this.getLeftX = function() {
|
|
35 |
return this.x - this.width / 2;
|
|
36 |
};
|
|
37 |
|
|
38 |
this.getRightX = function() {
|
|
39 |
return this.x + this.width / 2;
|
|
40 |
};
|
|
41 |
|
|
42 |
this.getY = function() {
|
|
43 |
return this.y;
|
|
44 |
};
|
|
45 |
|
|
46 |
this.getTopY = function() {
|
|
47 |
return this.y - this.height / 2;
|
|
48 |
};
|
|
49 |
|
|
50 |
this.getBottomY = function() {
|
|
51 |
return this.y + this.height / 2;
|
|
52 |
};
|
|
53 |
|
|
54 |
this.getWidth = function() {
|
|
55 |
return this.width;
|
|
56 |
};
|
|
57 |
|
|
58 |
this.getHeight = function() {
|
|
59 |
return this.height;
|
|
60 |
};
|
|
61 |
|
|
62 |
this.setPosition = function(x, y) {
|
17 | 63 |
this.x = x;
|
18 | 64 |
this.y = y;
|
19 | |
this.w = w;
|
20 | |
this.h = h;
|
21 | |
this.dx = 0;
|
22 | |
this.dy = 0;
|
23 | |
this.selected = false;
|
24 | |
};
|
25 | |
|
26 | |
this.getX = function() {
|
27 | |
return this.x;
|
28 | |
};
|
29 | |
|
30 | |
this.getY = function() {
|
31 | |
return this.y;
|
32 | |
};
|
33 | |
|
34 | |
this.getCenterX = function() {
|
35 | |
return this.x + this.w / 2;
|
36 | |
};
|
37 | |
|
38 | |
this.getCenterY = function() {
|
39 | |
return this.y + this.h / 2;
|
40 | |
};
|
41 | |
|
42 | |
this.getWidth = function() {
|
43 | |
return this.w;
|
44 | |
};
|
45 | |
|
46 | |
this.getHeight = function() {
|
47 | |
return this.h;
|
|
65 |
};
|
|
66 |
|
|
67 |
this.setDimensions = function(width, height) {
|
|
68 |
this.width = width;
|
|
69 |
this.height = height;
|
48 | 70 |
};
|
49 | 71 |
|
50 | 72 |
this.setVelocity = function(dx, dy) {
|
|
55 | 77 |
this.setDestination = function(x, y, ticks) {
|
56 | 78 |
this.destX = x;
|
57 | 79 |
this.destY = y;
|
58 | |
this.dx = (this.destX - this.getX()) / ticks;
|
59 | |
this.dy = (this.destY - this.getY()) / ticks;
|
|
80 |
this.setVelocity((this.destX - this.getX()) / ticks, (this.destY - this.getY()) / ticks);
|
60 | 81 |
this.destCounter = ticks;
|
61 | 82 |
};
|
62 | 83 |
|
|
68 | 89 |
this.destCounter--;
|
69 | 90 |
if (this.destCounter <= 0) {
|
70 | 91 |
this.destCounter = undefined;
|
71 | |
this.x = this.destX;
|
72 | |
this.y = this.destY;
|
|
92 |
this.setPosition(this.destX, this.destY);
|
73 | 93 |
this.onreachdestination();
|
74 | 94 |
}
|
75 | 95 |
}
|
76 | 96 |
};
|
77 | 97 |
|
78 | |
this.moveTo = function(x, y) {
|
79 | |
this.x = x;
|
80 | |
this.y = y;
|
81 | |
};
|
82 | |
|
83 | |
this.moveCenterTo = function(x, y) {
|
84 | |
this.x = x - this.getWidth() / 2;
|
85 | |
this.y = y - this.getHeight() / 2;
|
86 | |
};
|
87 | |
|
88 | 98 |
this.containsPoint = function(x, y) {
|
89 | |
return (x >= this.getX() && x <= this.getX() + this.getWidth() &&
|
90 | |
y >= this.getY() && y <= this.getY() + this.getHeight());
|
|
99 |
return (x >= this.getLeftX() && x <= this.getRightX() &&
|
|
100 |
y >= this.getTopY() && y <= this.getBottomY());
|
91 | 101 |
};
|
92 | 102 |
|
93 | 103 |
// you will probably want to override this
|
94 | 104 |
this.draw = function(ctx) {
|
95 | 105 |
ctx.fillStyle = this.fillStyle || "green";
|
96 | |
ctx.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
|
106 |
ctx.fillRect(this.getLeftX(), this.getTopY(), this.getWidth(), this.getHeight());
|
97 | 107 |
};
|
98 | 108 |
|
99 | 109 |
// event handlers. override to detect these events.
|
|
117 | 127 |
* This still has a few shortcomings at the moment.
|
118 | 128 |
*/
|
119 | 129 |
yoob.SpriteManager = function() {
|
120 | |
this.canvas = undefined;
|
121 | |
this.canvasX = undefined;
|
122 | |
this.canvasY = undefined;
|
123 | |
this.offsetX = undefined;
|
124 | |
this.offsetY = undefined;
|
125 | |
this.dragging = undefined;
|
126 | |
this.sprites = [];
|
|
130 |
/*
|
|
131 |
* Attach this SpriteManager to a canvas.
|
|
132 |
*/
|
|
133 |
this.init = function(cfg) {
|
|
134 |
this.canvasX = undefined;
|
|
135 |
this.canvasY = undefined;
|
|
136 |
this.offsetX = undefined;
|
|
137 |
this.offsetY = undefined;
|
|
138 |
this.dragging = undefined;
|
|
139 |
this.sprites = [];
|
|
140 |
|
|
141 |
this.canvas = cfg.canvas;
|
|
142 |
|
|
143 |
var $this = this;
|
|
144 |
this.canvas.addEventListener('mousedown', function(e) {
|
|
145 |
return $this.onmousedown(e, e);
|
|
146 |
});
|
|
147 |
this.canvas.addEventListener('touchstart', function(e) {
|
|
148 |
return $this.onmousedown(e, e.touches[0]);
|
|
149 |
});
|
|
150 |
|
|
151 |
this.canvas.addEventListener('mousemove', function(e) {
|
|
152 |
return $this.onmousemove(e, e);
|
|
153 |
});
|
|
154 |
this.canvas.addEventListener('touchmove', function(e) {
|
|
155 |
return $this.onmousemove(e, e.touches[0]);
|
|
156 |
});
|
|
157 |
|
|
158 |
this.canvas.addEventListener('mouseup', function(e) {
|
|
159 |
return $this.onmouseup(e, e);
|
|
160 |
});
|
|
161 |
this.canvas.addEventListener('touchend', function(e) {
|
|
162 |
return $this.onmouseup(e, e.touches[0]);
|
|
163 |
});
|
|
164 |
|
|
165 |
return this;
|
|
166 |
};
|
127 | 167 |
|
128 | 168 |
/*
|
129 | 169 |
* Common handling of mouse and touch events
|
|
154 | 194 |
this.canvasX = touch.pageX - this.canvas.offsetLeft;
|
155 | 195 |
this.canvasY = touch.pageY - this.canvas.offsetTop;
|
156 | 196 |
|
157 | |
this.dragging.moveTo(this.canvasX + this.offsetX,
|
158 | |
this.canvasY + this.offsetY);
|
|
197 |
this.dragging.setPosition(this.canvasX + this.offsetX,
|
|
198 |
this.canvasY + this.offsetY);
|
159 | 199 |
};
|
160 | 200 |
|
161 | 201 |
this.onmouseup = function(e, touch) {
|
|
172 | 212 |
this.dragging = undefined;
|
173 | 213 |
this.canvas.style.cursor = "auto";
|
174 | 214 |
};
|
175 | |
|
176 | |
/*
|
177 | |
* Attach this SpriteManager to a canvas.
|
178 | |
*/
|
179 | |
this.init = function(canvas) {
|
180 | |
this.canvas = canvas;
|
181 | |
|
182 | |
var self = this;
|
183 | |
this.canvas.addEventListener('mousedown', function(e) {
|
184 | |
return self.onmousedown(e, e);
|
185 | |
});
|
186 | |
this.canvas.addEventListener('touchstart', function(e) {
|
187 | |
return self.onmousedown(e, e.touches[0]);
|
188 | |
});
|
189 | |
|
190 | |
this.canvas.addEventListener('mousemove', function(e) {
|
191 | |
return self.onmousemove(e, e);
|
192 | |
});
|
193 | |
this.canvas.addEventListener('touchmove', function(e) {
|
194 | |
return self.onmousemove(e, e.touches[0]);
|
195 | |
});
|
196 | |
|
197 | |
this.canvas.addEventListener('mouseup', function(e) {
|
198 | |
return self.onmouseup(e, e);
|
199 | |
});
|
200 | |
this.canvas.addEventListener('touchend', function(e) {
|
201 | |
return self.onmouseup(e, e.touches[0]);
|
202 | |
});
|
203 | |
};
|
204 | 215 |
|
205 | 216 |
this.move = function() {
|
206 | |
this.foreach(function(sprite) { sprite.move(); });
|
|
217 |
this.foreach(function(sprite) {
|
|
218 |
sprite.move();
|
|
219 |
});
|
207 | 220 |
};
|
208 | 221 |
|
209 | 222 |
this.draw = function(ctx) {
|
210 | 223 |
if (ctx === undefined) {
|
211 | 224 |
ctx = this.canvas.getContext('2d');
|
212 | 225 |
}
|
213 | |
for (var i = 0; i < this.sprites.length; i++) {
|
214 | |
this.sprites[i].draw(ctx);
|
215 | |
}
|
|
226 |
this.foreach(function(sprite) {
|
|
227 |
sprite.draw(ctx);
|
|
228 |
});
|
216 | 229 |
};
|
217 | 230 |
|
218 | 231 |
this.addSprite = function(sprite) {
|