SHOW:
|
|
- or go back to the newest paste.
1 | /** | |
2 | * create a new snowball | |
3 | * | |
4 | * @param x - int | |
5 | * @param y - int | |
6 | */ | |
7 | xmas.Entity.Image.Snowball = function(x, y) { | |
8 | ||
9 | this.src = 'snowball'; | |
10 | ||
11 | // setting an offset to the image to have the ball spawn in the middle of the mouse click | |
12 | this.offsetX = 0; | |
13 | this.offsetY = 0; | |
14 | ||
15 | // use radius to check for collision later | |
16 | this.r = 0; | |
17 | ||
18 | // actual draw points - should include some kind of offset calculation | |
19 | this.drawX = 0; | |
20 | this.drawY = 0; | |
21 | ||
22 | this.scaleX = 1; | |
23 | this.scaleY = 1; | |
24 | ||
25 | this.z = 0; | |
26 | this.vx = 0; | |
27 | this.vy = 3; | |
28 | - | this.vz = 5; |
28 | + | this.vz = 1; |
29 | ||
30 | this.gravity = - 0.097; | |
31 | ||
32 | this._constructor(this.src, x, y, 1); | |
33 | ||
34 | this.update = function() { | |
35 | ||
36 | this.x += this.vx; | |
37 | this.y += this.vy; | |
38 | this.z += this.vz; | |
39 | ||
40 | this.vy -= this.gravity; | |
41 | ||
42 | // need to check if on the ground later | |
43 | // todo | |
44 | ||
45 | // did I hit something somewhere "back" in the environment..? | |
46 | // todo | |
47 | // this.r = ? | |
48 | ||
49 | // this.vx *= 0.99; | |
50 | // this.vy *= 0.99; | |
51 | // this.vz *= 0.99; | |
52 | ||
53 | }; | |
54 | ||
55 | ||
56 | this.onImgLoaded = function() { | |
57 | this.img.inViewport = true; | |
58 | this.img.widthScaled = this.img.width; | |
59 | this.img.heightScaled = this.img.height; | |
60 | ||
61 | // set radius for later collision detection because the image is a ball :) | |
62 | this.r = this.img.widthScaled/2; | |
63 | ||
64 | // update coords for the offset to have the ball spawn at the center of the mouse position | |
65 | this.offsetX = - this.img.widthScaled/2; | |
66 | this.offsetY = - this.img.heightScaled/2; | |
67 | this.x += this.offsetX; | |
68 | this.y += this.offsetY; | |
69 | }; | |
70 | ||
71 | this.debug = function() { | |
72 | console.log("coord", this.x, this.y, this.z); | |
73 | console.log("pos ", this.drawX, this.drawY); | |
74 | console.log("scale", this.scaleX, this.scaleY); | |
75 | console.log("imgO ", this.img.width, this.img.height) | |
76 | console.log("img ", this.img.widthScaled, this.img.heightScaled); | |
77 | console.log("---------------------------------"); | |
78 | }; | |
79 | ||
80 | /** | |
81 | * renders the image | |
82 | */ | |
83 | this.render = function() { | |
84 | if(this.isVisible() && this.img.isLoaded && this.img.inViewport) { | |
85 | ||
86 | ||
87 | this.drawX = this.x / this.z; | |
88 | this.drawY = this.y / this.z; | |
89 | // using this instead will simply drop the ball | |
90 | //this.drawX = this.x; | |
91 | //this.drawY = this.y; | |
92 | ||
93 | // scaleX and scaleY becoming *very* low - about 10^(-100) - so no scaling is happening | |
94 | this.scaleX = this.scaleX / this.z; | |
95 | this.scaleY = this.scaleY / this.z; | |
96 | ||
97 | - | this.img.widthScaled = this.img.width - this.img.width * this.scaleX; |
97 | + | this.img.widthScaled = this.img.width * this.scaleX; |
98 | - | this.img.heightScaled = this.img.width - this.img.width * this.scaleY; |
98 | + | this.img.heightScaled = this.img.width * this.scaleY; |
99 | ||
100 | //this.debug(); | |
101 | ||
102 | // xmas.Draw.image(this.img, this.drawX, this.drawY, this.img.widthScaled, this.img.heightScaled); | |
103 | ||
104 | xmas.ctxPreRender.drawImage(this.img, this.drawX, this.drawY, this.img.widthScaled, this.img.heightScaled); | |
105 | ||
106 | } | |
107 | }; | |
108 | ||
109 | ||
110 | } | |
111 | ||
112 | ||
113 | ||
114 | /** | |
115 | * create a new image | |
116 | * | |
117 | * @param src - string | |
118 | * @param x - int | |
119 | * @param y - int | |
120 | * @param opacity - float | |
121 | */ | |
122 | xmas.Entity.Image = function(src, x, y, opacity) { | |
123 | ||
124 | this.type = 'image'; | |
125 | this.group = null; | |
126 | this.isLoaded = false; | |
127 | this.img = null; | |
128 | ||
129 | var self = this; | |
130 | ||
131 | this._constructor = function(src, x, y, opacity) { | |
132 | ||
133 | this.x = x; // the x coordinate | |
134 | this.y = y; // the y coordinate | |
135 | this.opacity = opacity || 1; // initial opacity; the dot will fade out | |
136 | // create image | |
137 | this.img = new Image(); | |
138 | this.img.opacity = this.opacity; | |
139 | ||
140 | this.loadImage(src); | |
141 | ||
142 | }; | |
143 | ||
144 | this.setOpacity = function(opacity) { | |
145 | this.opacity = opacity; | |
146 | this.img.opacity = opacity; | |
147 | }; | |
148 | ||
149 | /** | |
150 | * loads an image | |
151 | * | |
152 | * @param src - string | |
153 | */ | |
154 | this.loadImage = function(src) { | |
155 | ||
156 | var self = this; | |
157 | this.img.isLoaded = false; | |
158 | this.img.inViewport = false; | |
159 | ||
160 | this.img.onload = function() { | |
161 | this.isLoaded = true; | |
162 | this.calculateCenter(); | |
163 | self.isLoaded = true; | |
164 | self.onImgLoaded(); | |
165 | } | |
166 | ||
167 | var source = xmas.Resources.get(this.type, src); | |
168 | ||
169 | if(source) { | |
170 | this.img.src = source; | |
171 | } | |
172 | }; | |
173 | ||
174 | this.init = function() { | |
175 | // nothing to do here | |
176 | }; | |
177 | ||
178 | this.update = function() { | |
179 | // nothing to do here | |
180 | }; | |
181 | ||
182 | this.onImgLoaded = function() { | |
183 | // do when image is loaded, nothing to do here | |
184 | } | |
185 | ||
186 | /** | |
187 | * when changing condition | |
188 | */ | |
189 | this.onChangeCondition = function() { | |
190 | this.setImageSpriteByCondition(this.getCondition()); | |
191 | }; | |
192 | ||
193 | /** | |
194 | * set the image sprite by status, assuming all sprites have equal withs | |
195 | * and are set in one row | |
196 | * | |
197 | * sx = 0 sx = w sx = w*2 sx = w*3 | |
198 | * |-----------|-----------|-----------|-----------| | |
199 | * | Default | Hover | Active | InActive | | |
200 | * |-----------|-----------|-----------|-----------| | |
201 | * | |
202 | * @param condition - int | |
203 | */ | |
204 | this.setImageSpriteByCondition = function(condition) { | |
205 | if(this.img && this.img.isLoaded) { | |
206 | ||
207 | this.sy = 0; | |
208 | this.sw = this.w; | |
209 | this.sh = this.h; | |
210 | ||
211 | switch(condition) { | |
212 | case xmas.Entity.CONDITION._default: | |
213 | this.sx = 0; | |
214 | break; | |
215 | case xmas.Entity.CONDITION._hover: | |
216 | this.sx = this.w; | |
217 | break; | |
218 | case xmas.Entity.CONDITION._active: | |
219 | this.sx = this.w * 2; | |
220 | break; | |
221 | case xmas.Entity.CONDITION._inactive: | |
222 | this.sx = this.w * 3; | |
223 | break; | |
224 | } | |
225 | ||
226 | } | |
227 | }; | |
228 | ||
229 | /** | |
230 | * renders the image | |
231 | * @param dw - int new width (optional) | |
232 | * @param dh - int new height (optional) | |
233 | */ | |
234 | this.render = function(dw, dh) { | |
235 | if(this.isVisible() && this.img.isLoaded && this.img.inViewport) { | |
236 | xmas.Draw.image(this.img, this.x, this.y, dw, dh); | |
237 | } | |
238 | }; | |
239 | ||
240 | this._constructor(src, x, y, opacity); | |
241 | ||
242 | } | |
243 | ||
244 | ||
245 | // inheritance | |
246 | xmas.Entity.Image.prototype = new xmas.Entity(); | |
247 | xmas.Entity.Image.constructor = xmas.Entity.Image; | |
248 | xmas.Entity.Image.Snowball.prototype = new xmas.Entity.Image(); | |
249 | xmas.Entity.Image.Snowball.constructor = xmas.Entity.Image.Snowball; |