-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.js
265 lines (203 loc) · 8.18 KB
/
Scene.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
////////////////////////////////////////////////////////////////////////////////
// NAME: scene
// DESCRIPTION: Renders the scene
// DATE: november 2011
// AUTHOR: xopr
// LICENSE: TBD. Currently the script's intellectual property copyright applies to xopr, 2011, but will change to (L)GPL/APL/MPL/BSD or the like
// All images are property of their respectful owner.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// class Scene
//
////////////////////////////////////////////////////////////////////////////////
function Scene( _attributes )
{
this.horizon = _attributes.horizon;
this.ground = _attributes.ground;
this.angle = _attributes.angle;
this.airColor = _attributes.airColor;
this.horizonColor = _attributes.horizonColor;
this.oceanColor = _attributes.oceanColor;
this.groundColor = _attributes.groundColor;
this.waterInFront = _attributes.waterInFront;
this.relativeHeight = _attributes.relativeHeight;
this.relativeWidth = _attributes.relativeWidth;
this.canvasWidth = _attributes.canvasWidth;
this.canvasHeight = _attributes.canvasHeight;
this.cloudyness = 25;
this.maxPosition = 20*1000; // 1000 'meter' a 20 pixels
this.position = 0;
this.horizonItems = this.loadImages( _attributes.horizonItems );
this.cloudsBack = this.loadCloudImages( _attributes.cloudsBack );
this.cloudsFront = this.loadCloudImages( _attributes.cloudsFront );
this.distantItems = this.loadImages( _attributes.distantItems );
this.sceneryItems = this.loadImages( _attributes.sceneryItems );
this.backgroundItems = this.loadImages( _attributes.backgroundItems );
this.foregroundItems = this.loadImages( _attributes.foregroundItems);
this.frontItems = this.loadImages( _attributes.frontItems );
}
Scene.prototype.finished = null;
Scene.prototype.finishedTime = null;
Scene.prototype.startTime = null;
Scene.prototype.loadImages = function( _arrImages )
{
arrImages = new Array();
for ( var idx = 0; idx < _arrImages.length; idx++ )
{
// Skip blanks
if ( !_arrImages.hasOwnProperty( idx ))
continue;
var img = _arrImages[ idx ];
var image = new Image( );
image.loaded = false;
image.onload = function( _event )
{
_event.target.loaded = true;
}
image.tile = img.tile;
image._x = img.x;
image._y = img.y;
image.fixed = img.fixed;
image._width = 0;
image._height = 0;
if ( img.hasOwnProperty( "width" ))
image._width = img.width;
if ( img.hasOwnProperty( "height" ))
image._height = img.height;
// Load the image
image.src = img.img;
arrImages.push( image );
}
return arrImages;
}
Scene.prototype.loadCloudImages = function( _arrImages )
{
arrImages = new Array();
arrClouds = new Array();
for ( var idx = 0; idx < _arrImages.length; idx++ )
{
// Skip blanks
if ( !_arrImages.hasOwnProperty( idx ))
continue;
var image = new Image( );
image.loaded = false;
image.onload = function( _event )
{
_event.target.loaded = true;
}
// Load the image
image.src = _arrImages[ idx ];
arrImages.push( image );
}
for ( var idx = 0; idx < this.cloudyness; idx++ )
{
var cloud = arrImages[ parseInt( Math.random() * arrImages.length ) ];
cloud.tile = false;
//cloud._x = Math.random() * ( this.canvasWidth );
//cloud._x = Math.random() * ( this.canvasWidth + this.maxPosition );
//cloud._x = Math.random() * ( 3 * this.canvasWidth );
cloud._x = Math.random() * (3.2 * this.canvasWidth); // 0.15 0.11
cloud._y = Math.random() * this.canvasHeight * this.horizon * 0.7;
cloud._width = 0;
cloud._height = 0;
cloud.fixed = false;
arrClouds.push( cloud );
}
return arrClouds;
}
Scene.prototype.draw = function( _context, _foreground )
{
// Draw all stuff
// Save original context
_context.save();
this.canvasWidth = document.getElementById('canvas').width;
this.canvasHeight = document.getElementById('canvas').height;
if ( !_foreground )
{
// Quick hack: clear the screen
document.getElementById('canvas').width = document.getElementById('canvas').width;
// Air
_context.fillStyle = this.airColor;
_context.fillRect( 0, 0, this.canvasWidth, this.canvasHeight * this.horizon );
//if ( this.waterInFront )
// Water
_context.fillStyle = this.oceanColor;
_context.fillRect( 0, this.canvasHeight * this.horizon, this.canvasWidth, this.canvasHeight * (this.ground - this.horizon) );
// Ground
_context.fillStyle = this.groundColor;
_context.fillRect( 0, this.canvasHeight * this.ground, this.canvasWidth, this.canvasHeight * ( 1 - this.ground ));
_context.restore();
// Draw all images
var idx;
for (idx=0;idx < this.horizonItems.length;idx++ )
this.drawImage( this.horizonItems[idx], _context, 0.1 )
// Clouds are a special case since they are placed random and moving
for (idx=0;idx < this.cloudsBack.length;idx++ )
this.drawImage( this.cloudsBack[idx], _context, 0.11 )
for (idx=0;idx < this.cloudsFront.length;idx++ )
this.drawImage( this.cloudsFront[idx], _context, 0.15 )
for (idx=0;idx < this.distantItems.length;idx++ )
this.drawImage( this.distantItems[idx], _context, 0.2 )
for (idx=0;idx < this.sceneryItems.length;idx++ )
this.drawImage( this.sceneryItems[idx], _context, 0.6 )
for (idx=0;idx < this.backgroundItems.length;idx++ )
this.drawImage( this.backgroundItems[idx], _context, 0.9 )
} else {
for (idx=0;idx < this.foregroundItems.length;idx++ )
this.drawImage( this.foregroundItems[idx], _context, 1.1 )
for (idx=0;idx < this.frontItems.length;idx++ )
this.drawImage( this.frontItems[idx], _context, 2.5 )
}
}
Scene.prototype.setFinished = function( _finished )
{
this.finished = _finished
if ( !this.finishedTime )
this.finishedTime = new Date();
};
Scene.prototype.drawImage = function( _image, _context, _perspective )
{
// Draw the image
if ( ! _image.loaded )
return;
_context.save();
var width = _image._width ? _image._width : _image.width;
var height = _image._height ? _image._height : _image.height;
var x = _image._x;
var y = _image._y;
// Percentage to absolute pixel
if ( (x > -1) && (x < 1) )
x = parseInt( x * this.canvasWidth);
if ( y > -1 && y < 1 )
y = parseInt( y * this.canvasHeight);
// Pixel alignment trick
/*if ( x < 0 )
x = (-x) - width;*/
if ( y < 0 )
y = (-y) - height;
if ( _image.tile )
{
var perspectiveAdjustment = 0;
if ( !_image.fixed )
perspectiveAdjustment = (this.position * _perspective);
_context.fillStyle = _context.createPattern(_image, 'repeat');
_context.translate(x - perspectiveAdjustment, y);
// Scaling and its relative movement compensation
_context.scale( _image._width ? _image._width / _image.width: 1, _image._height ? _image._height / _image.height : 1 );
if ( _image._width )
perspectiveAdjustment /= _image._width / _image.width;
_context.fillRect(perspectiveAdjustment,0,(_image._x === false ? this.canvasWidth: _image.width),(_image._y === false ? this.canvasHeight: _image.height));
} else {
// Adjust x as we 'move'
if ( !_image.fixed )
x -= (this.position * _perspective)
_context.drawImage( _image, x, y, width, height );
}
_context.restore();
}
Scene.prototype.setPosition = function( _nPosition )
{
this.position = _nPosition;
}
// end Scene class
///////////////////