-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
136 lines (130 loc) · 3.97 KB
/
utilities.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
var GraphicsRooster = {
arrNames : new Array(),
arrImages : new Array(),
countImages: 0,
addImage: function (nameStr, imageSrc, imgWidth, imgHeight)
{
var imageObj = new ImageWrapper(nameStr, imageSrc, imgWidth, imgHeight);
GraphicsRooster.arrNames.push(nameStr);
GraphicsRooster.arrImages.push(imageObj);
GraphicsRooster.countImages ++;
},
getImgByName: function(nameStr)
{
nameStr = nameStr.toLowerCase();
for(var i = 0; i < GraphicsRooster.countImages; i ++)
{
if (nameStr == GraphicsRooster.arrNames[i].toLowerCase())
{
return GraphicsRooster.arrImages[i];
}
}
}
}
function ImageWrapper(nameStr, imageSrc, imgWidth, imgHeight)
{
this.name = nameStr
this.src = new Image()
this.src.src = imageSrc;
this.origWidth = this.width = imgWidth;
this.origHeight = this.height = imgHeight;
this.animSteps = new Array();
this.minStepDuration = 200;
this.curAnimStep = 0;
this.lastAnimUpdate = 0;
this.updateAnimeStep = function()
{
if(0 == this.lastAnimUpdate)
{
this.lastAnimUpdate = Viewport.curTime;
} else if((Viewport.curTime - this.lastAnimUpdate) > this.minStepDuration)
{
if(0 < this.animSteps.length)
{
this.curAnimStep = (this.curAnimStep + 1) % this.animSteps.length;
}
}
}
this.addAnimStepsPerRow = function (frameSize, frameOffset, frameCount)
{
for (var i = 0; i < frameCount; i++)
{
this.animSteps.push([
frameOffset[0] + frameSize[0] * i,
frameOffset[1],
frameSize[0],
frameSize[1]
]);
}
}
this.getAnimStep = function (indexAnimStep)
{
return this.animSteps[indexAnimStep % this.animSteps.length];
}
this.getStepCount = function ()
{
return this.animSteps.length;
}
}
var ParticlesTemplateRooster = {
arrTemplateNames: new Array(),
arrTemplateObj: new Array(),
countTemplates: 0,
addTemplate: function (templateName, objTemplate)
{
ParticlesTemplateRooster.arrTemplateNames.push(templateName);
ParticlesTemplateRooster.arrTemplateObj.push(objTemplate);
ParticlesTemplateRooster.countTemplates ++;
},
getTemplate: function (searchedTemplate)
{
searchedTemplate = searchedTemplate.toLowerCase();
for (var i = 0; i < ParticlesTemplateRooster.countTemplates; i++)
{
if(searchedTemplate == ParticlesTemplateRooster.arrTemplateNames[i].toLowerCase())
{
return ParticlesTemplateRooster.arrTemplateObj[i];
}
}
}
};
function ParticleTemplate(paramImage)
{
this.img = GraphicsRooster.getImgByName(paramImage);
this.animSteps = new Array();
this.minStepDuration = 100;
this.addAnimStepsPerRow = function (frameSize, frameOffset, frameCount)
{
for (var i = 0; i < frameCount; i++)
{
this.animSteps.push([
frameOffset[0] + frameSize[0] * i,
frameOffset[1],
frameSize[0],
frameSize[1]
]);
}
}
this.getAnimStep = function (indexAnimStep)
{
return this.animSteps[indexAnimStep % this.animSteps.length];
}
this.getStepCount = function ()
{
return this.animSteps.length;
}
}
function getHexForRGB(red, green, blue)
{
var hexValues = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
var colorArr = [red, green, blue];
var colorHex = "";
for (var i = 0; i < 3; i++)
{
if (colorArr[i] > 255) colorArr[i] = 255;
else if (colorArr[i] < 0) colorArr[i] = 0;
colorHex += hexValues[Math.floor(colorArr[i] / 16)];
colorHex += hexValues[colorArr[i] % 16];
}
return colorHex;
}