forked from robertkleffner/mariohtml5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackgroundGenerator.js
More file actions
executable file
·181 lines (163 loc) · 5.83 KB
/
Copy pathbackgroundGenerator.js
File metadata and controls
executable file
·181 lines (163 loc) · 5.83 KB
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
/**
Generates the backgrounds for a level.
Code by Rob Kleffner, 2011
*/
Mario.BackgroundGenerator = function(width, height, distant, type) {
this.Width = width;
this.Height = height;
this.Distant = distant;
this.Type = type;
};
Mario.BackgroundGenerator.prototype = {
SetValues: function(width, height, distant, type) {
this.Width = width;
this.Height = height;
this.Distant = distant;
this.Type = type;
},
CreateLevel: function() {
var level = new Mario.Level(this.Width, this.Height);
switch (this.Type) {
case Mario.LevelType.Overground:
this.GenerateOverground(level);
break;
case Mario.LevelType.Underground:
this.GenerateUnderground(level);
break;
case Mario.LevelType.Castle:
this.GenerateCastle(level);
break;
}
return level;
},
GenerateOverground: function(level) {
var range = this.Distant ? 4 : 6;
var offs = this.Distant ? 2 : 1;
var oh = Math.floor(Math.random() * range) + offs;
var h = Math.floor(Math.random() * range) + offs;
var x = 0, y = 0, h0 = 0, h1 = 0, s = 2;
for (x = 0; x < this.Width; x++) {
oh = h;
while (oh === h) {
h = Math.floor(Math.random() * range) + offs;
}
for (y = 0; y < this.Height; y++) {
h0 = (oh < h) ? oh : h;
h1 = (oh < h) ? h : oh;
s = 2;
if (y < h0) {
if (this.Distant){
s = 2;
if (y < 2) { s = y; }
level.SetBlock(x, y, 4 + s * 8);
} else {
level.SetBlock(x, y, 5);
}
} else if (y === h0) {
s = h0 === h ? 0 : 1;
s += this.Distant ? 2 : 0;
level.SetBlock(x, y, s);
} else if (y === h1) {
s = h0 === h ? 0 : 1;
s += this.Distant ? 2 : 0;
level.SetBlock(x, y, s + 16);
} else {
s = (y > h1) ? 1 : 0;
if (h0 === oh) { s = 1 - s; }
s += this.Distant ? 2 : 0;
level.SetBlock(x, y, s + 8);
}
}
}
},
GenerateUnderground: function(level) {
var x = 0, y = 0, t = 0, yy = 0;
if (this.Distant) {
var tt = 0;
for (x = 0; x < this.Width; x++) {
if (Math.random() < 0.75) { tt = 1 - tt; }
for (y = 0; y < this.Height; y++) {
t = tt;
yy = y - 2;
if (yy < 0 || yy > 4) {
yy = 2;
t = 0;
}
level.SetBlock(x, y, (4 + t + (3 + yy) * 8));
}
}
} else {
for (x = 0; x < this.Width; x++) {
for (y = 0; y < this.Height; y++) {
t = x % 2;
yy = y - 1;
if (yy < 0 || yy > 7) {
yy = 7;
t = 0;
}
if (t === 0 && yy > 1 && yy < 5) {
t = -1;
yy = 0;
}
level.SetBlock(x, y, (6 + t + yy * 8));
}
}
}
},
GenerateCastle: function(level) {
var x = 0, y = 0, t = 0, yy = 0;
if (this.Distant) {
for (x = 0; x < this.Width; x++) {
for (y = 0; y < this.Height; y++) {
t = x % 2;
yy = y - 1;
if (yy > 2 && yy < 5) {
yy = 2;
} else if (yy >= 5) {
yy -= 2;
}
if (yy < 0) {
t = 0;
yy = 5;
} else if (yy > 4) {
t = 1;
yy = 5;
} else if (t < 1 && yy === 3) {
t = 0;
yy = 3;
} else if (t < 1 && yy > 0 && yy < 3) {
t = 0;
yy = 2;
}
level.SetBlock(x, y, (1 + t + (yy + 4) * 8));
}
}
} else {
for (x = 0; x < this.Width; x++) {
for (y = 0; y < this.Height; y++) {
t = x % 3;
yy = y - 1;
if (yy > 2 && yy < 5) {
yy = 2;
} else if (yy >= 5) {
yy -= 2;
}
if (yy < 0) {
t = 1;
yy = 5;
} else if (yy > 4) {
t = 2;
yy = 5;
} else if (t < 2 && yy === 4) {
t = 2;
yy = 4;
} else if (t < 2 && yy > 0 && yy < 4) {
t = 4;
yy = -3;
}
level.SetBlock(x, y, (1 + t + (yy + 3) * 8));
}
}
}
}
};