-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.html
281 lines (247 loc) · 11.3 KB
/
game.html
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<html>
<style>
body {
font-family: Arial, sans-serif;
background-color:black;
background-image:
radial-gradient(white, rgba(255,255,255,.2) 2px, transparent 40px),
radial-gradient(white, rgba(255,255,255,.15) 1px, transparent 30px),
radial-gradient(white, rgba(208, 184, 184, 0.1) 2px, transparent 40px),
radial-gradient(rgba(255,255,255,.4), rgba(255,255,255,.1) 2px, transparent 30px);
background-size: 550px 550px, 350px 350px, 250px 250px, 150px 150px;
background-position: 0 0, 40px 60px, 130px 270px, 70px 100px;
color: white;
}
abbr {
text-decoration: none;
}
.column {
float: left;
width: 50%;
}
.box-left-mini{
float:left;
width:292px;
height:141px;
}
.box-left-mini .front {
display: block;
z-index: 5;
position: relative;
}
.box-left-mini .front span {
background: #fff
}
.box-left-mini .behind_container {
background-color: #ff0;
position: relative;
top: -18px;
}
.box-left-mini .behind {
display: block;
z-index: 3;
}
.row {
display: flex;
}
.column {
flex: 50%;
}
</style>
<body>
<div class="column">
<button onclick="current_player.fly_to_system()">Fly to New System (🚀-1)</button>
<p id="describe">Welcome</p>
</div>
<div class="column">
<table>
<tr>
<td>🚀<span id="ftl">0</span> </td>
<td>🏠<span id="colony">0</span> </td>
<td>🧪<span id="research">0</span> </td>
</tr>
</table>
<p>
Previous Systems:
<span id="prev_systems"></span>
</p>
</div>
</body>
<script>
function random_string(length=3,colour=false) {
let result = '';
if(colour){
characters = 'ABCDEF0123456789';
} else{
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
}
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
var systems = [];
class planet {
constructor(data={},random=false) {
this.name = data.name ?? random_string();
this.moons = [];
if(random){
Math.random() > 0.9 ? this.habitable = true : this.habitable = false;
this.habitable && Math.random() > 0.5 ? this.life = true : this.life = false;
this.type = 'NOT IMPLEMENTED';
Math.random() > 0.7 ? this.rings = true : this.rings = false;
this.colour = random_string(6,true);
this.size = Math.floor(Math.random()*90+10);
while (Math.random() < 0.3){
this.moons.push(new moon({planet_size:this.size},true))
}
} else {
this.habitable = data.habitable ?? false;
this.life = data.life ?? false;
this.type = data.type ?? false;
this.rings = data.rings ?? false;
this.colour = data.colour ?? "D3D3D3";
this.size = data.size ?? 50;
this.moons = data.moons ?? [];
}
}
}
class system {
constructor(data={},random=false) {
this.planets = [];
if(random){
this.name = random_string(); //TODO: need to ensure this is unique
this.star = random_string();
this.star_size = data.star_size ?? 400 + 200 * Math.random();
this.star_rgb_g = data.star_rgb_g ?? Math.floor(100 + Math.random() * 100);
while (Math.random() < 0.9){
this.planets.push(new planet({},true))
}
} else {
this.name = data.name ?? "Unknown";
this.star = data.star ?? 'Sun';
this.star_size = data.star_size ?? 500;
this.star_rgb_g = data.star_rgb_g ?? 169;
}
systems.push(this)
}
add_planet(planet) {
this.planets.push(planet);
}
describe() {
let html_text = "";
html_text += `System name: ${this.name}<br><br>`;
html_text += `<div class="row"><div class="column">`;
html_text += `Star: ${this.star}<br><div id="circle" onclick="alert('Do not look direcltly at the sun!')"" style="margin: auto; width: 50%; position: relative; left: ${this.star_size*0.2-this.star_size}px; width: ${this.star_size}px;height: ${this.star_size}px;-webkit-border-radius: ${this.star_size/2}px;-moz-border-radius: ${this.star_size/2}px;border-radius: ${this.star_size/2}px;background: rgb(250 ${this.star_rgb_g} 7);"></div>`;
html_text += `</div><div class="column">`;
if (this.planets) {
html_text += `Planets:<br>`;
html_text += '<table>';
for (const i in this.planets){
var planet_ = this.planets[i];
html_text += '<tr>';
html_text += `<td><div id="circle" style="margin: auto; width: 50%; width: ${planet_.size}px;height: ${planet_.size}px;-webkit-border-radius: ${planet_.size/2}px;-moz-border-radius: ${planet_.size/2}px;border-radius: ${planet_.size/2}px;background: radial-gradient(circle at 100%, #${planet_.colour}, #${planet_.colour} 50%, #eee 75%, #${planet_.colour} 75%);"></div></td>`;
html_text += `<td>${planet_.name}</td>`;
html_text += `<td>`;
for (const ii in planet_.moons){
var moon_ = planet_.moons[ii];
html_text += `<abbr title="${moon_.name}"><span style="color: #${moon_.colour}; font-size: ${moon_.size}px;">•</span></abbr>`;
}
html_text += `</td>`
html_text += '</tr>'
}
html_text += `</div></div>`
html_text += '</table>'
}
document.getElementById("describe").innerHTML = html_text;
}
}
class moon {
constructor(data={planet_size:40},random=false){
this.name = data.name ?? random_string();
this.colour = data.colour ?? random_string(6,true);
if(data.planet_size){
this.size = data.planet_size * (0.5 + Math.random() * 0.5);
}else{
this.size = data.size ?? 40;
}
}
}
class player {
constructor(data={}) {
this.name = data.name ?? 'Player 1';
this.resources = {colony_ships:2,ftl_fuel:5,research:0};
this.display_resources()
}
display_resources(){
document.getElementById("ftl").innerHTML = this.resources.ftl_fuel;
document.getElementById("colony").innerHTML = this.resources.colony_ships;
document.getElementById("research").innerHTML = this.resources.research;
}
display_previous_systems(){
var html_text = ''
systems.forEach(function (item, index) {
html_text += `<br><button onclick="current_player.fly_to_system('${item.name}')">Fly to ${item.name} (🚀-1)</button>`
});
document.getElementById("prev_systems").innerHTML = html_text;
}
use_resources(resources){
var new_resources = {};
var possible = true;
for (const resource in this.resources) {
new_resources[resource] = this.resources[resource] - (resources[resource] ?? 0);
console.log(resource)
console.log(this.resources)
console.log(this.resources[resource])
console.log(new_resources[resource])
if (new_resources[resource] < 0) {
possible = false;
}
}
if (!possible) {
alert(`Not enough resources, you would be left with ${JSON.stringify(new_resources)}`)
return false
}
this.resources = new_resources
this.display_resources()
return true
}
fly_to_system(existing_system=''){
if(this.use_resources({ftl_fuel:1})){
if(existing_system){
systems.forEach(function(system,index){
if(system.name==existing_system){
system.describe()
}
})
}
else {
new system({},true).describe()
}
this.display_previous_systems()
}
}
}
//set up the starting system
solar_system = new system({'name':'Solar System'})
earth_moon = new moon({name:'Moon',size:30,colour:'D3D3D3'})
earth = new planet({'name':'Earth','habitable':true,'life':true,'colour':'00FFFF',moons:[earth_moon]})
solar_system.add_planet(new planet({'name':'Mercury','size':40}))
solar_system.add_planet(new planet({'name':'Venus','size':45,colour:'FFEBCD'}))
solar_system.add_planet(earth)
solar_system.add_planet(new planet({'name':'Mars','habitable':false,'life':false,size:42,colour:'8B0000'}))
solar_system.add_planet(new planet({'name':'Jupiter','habitable':false,'life':false,'type':'gas giant',size:80,colour:'FF8C00'}))
solar_system.add_planet(new planet({'name':'Saturn','habitable':false,'life':false,'type':'gas giant','rings':true,size:60,colour:'FFEBCD'}))
solar_system.add_planet(new planet({'name':'Uranus','habitable':false,'life':false,'type':'ice giant','rings':true,size:55,colour:'00FFFF'}))
solar_system.add_planet(new planet({'name':'Neptune','habitable':false,'life':false,'type':'ice giant',size:52,colour:'00008B'}))
solar_system.add_planet(new planet({'name':'Pluto','habitable':false,'life':false,'type':'dwarf',size:20,colour:'D3D3D3'}))
solar_system.describe()
function new_system(){
new system({},true).describe()
}
current_player = new player()
</script>
</html>