-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask27.html
339 lines (303 loc) · 7.88 KB
/
task27.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>行星与飞船</title>
<style>
body{
background-color: #000;
font-size: 14px;
}
#scene{
position: relative;
width: 500px;
margin:0 auto;
margin-top: 100px;
}
#log{
position: absolute;
width: 300px;
height: 300px;
/*float: right;*/
right: 0;
top: 0;
border: 1px solid #fff;
color: red;
overflow:auto;
}
#control{
color: #fff;
width: 800px;
margin:0 auto;
margin-top: 100px;
border: 1px solid white;
}
#controlcreate{
border-bottom: 1px solid white;
padding: 10px;
}
label{
color: white;
}
#star{
/*position: absolute;*/
margin:0 auto;
margin-top: 100px;
width: 300px;
height: 300px;
background: blue;
-moz-border-radius: 150px;
-webkit-border-radius: 150px;
border-radius: 150px;
}
.ship{
position: absolute;
margin:0px auto;
margin-left: 210px;
width: 80px;
margin-top: -175px;
height: 50px;
background: white;
font-size: 14px;
/*border: 1px solid white;*/
animation: action 10s linear 0s infinite;
animation-play-state:paused;
-webkit-animation-play-state:paused;
}
@keyframes action {
from {
transform: rotate(0deg) translate(200px) ;
}
to {
transform: rotate(360deg) translate(200px) ;
}
}
</style>
</head>
<body>
<div id="scene">
<div id="star"></div>
</div>
<div id="control">
<div id="controlcreate">
动力系统选择:
<label><input name="power" checked="true" type="radio" value="" />前进号(速率30px/s,能耗5%/s)</label>
<label><input name="power" type="radio" value="" />奔腾号(速率50px/s,能耗7%/s)</label>
<label><input name="power" type="radio" value="" />超越号(速率80px/s,能耗9%/s)</label>
<br>
能源系统选择:
<label><input name="energy" checked="true" type="radio" value="" />劲量型(补充能源速度2%/s)</label>
<label><input name="energy" type="radio" value="" />光能型(补充能源速度3%/s)</label>
<label><input name="energy" type="radio" value="" />永久型(补充能源速度4%/s)</label>
<br>
<button id="btn">建造新飞船</button>
</div>
</div>
<div id="log"></div>
<script>
var scene=document.getElementById('scene');
var control=document.getElementById('control');
var createbtn=document.getElementById('btn');
var power=document.getElementsByName('power');
var oEnergy=document.getElementsByName('energy');
var oLog=document.getElementById('log');
var ships=[];
var powersystem=[{name:"前进号",speed:30,expendspeed:5},{name:"奔腾号",speed:50,expendspeed:7},{name:"超越号",speed:80,expendspeed:9}];
var energysystem=[{name:"劲量型",recoverspeed:2},{name:"光能型",recoverspeed:3},{name:"永久型",recoverspeed:4}];
var protoship={
energy:100,
state:"paused",
destroy:0,
adapter:function (signal) {//指令接收器
var re =/\d{4}/g;
var signs=signal.match(re);
var sign_e={
id:1,
destroy:0,
state:"paused"
};
var str;
sign_e.id=parseInt(signs[0]);
if (signs[1]=="1100") {
sign_e.destroy=1;
str="自我销毁";
} else if(signs[1]=="0001"){
sign_e.state="running";
str="开始飞行";
}else{
sign_e.state="paused";
str="停止飞行";
}
if (sign_e.id==this.id) {
this.state=sign_e.state;
if (sign_e.destroy) {
this.destroy=sign_e.destroy;
}
this.cpu();
oLog.innerHTML+=sign_e.id+"号飞船收到命令,"+str+"<br>";
}
},
cpu:function () {//指令处理器
scene.getElementsByTagName('div')[this.id].style.animationPlayState=this.state;
if (this.destroy==1) {
scene.getElementsByTagName('div')[this.id].style.display="none";
clearInterval(this.int1);
clearInterval(this.int2);
}
}
}
var ship=function () {
this.id=scene.getElementsByTagName('div').length;
var str="";
for (var i = 0; i < power.length; i++) {
if(power[i].checked==true)
{
this.speed=powersystem[i].speed;
this.expendspeed=powersystem[i].expendspeed;
str=str+"新建“"+powersystem[i].name+"”飞船,";
}
}
for (var i = 0; i < oEnergy.length; i++) {
if(oEnergy[i].checked==true)
{
this.recoverspeed=energysystem[i].recoverspeed;
str=str+"采用"+energysystem[i].name+"能源系统";
}
}
oLog.innerHTML+=str+'<br>';
var that=this;
var PI=Math.PI;
function createship() {
var ship_element=document.createElement('div');
ship_element.className="ship";
scene.appendChild(ship_element);
ship_element.style.animationDuration=(400*PI)/that.speed+"s";
};
function createcontrol(argument) {
var textnode1=document.createTextNode("对"+that.id+"号飞船下达命令:");
var textnode2=document.createTextNode("开始飞行");
var textnode3=document.createTextNode("停止飞行");
var textnode4=document.createTextNode("销毁");
var control_element=document.createElement('div');
control_element.className="shipcontrol"+that.id;
control.appendChild(control_element);
var label_element=document.createElement('label');
label_element.appendChild(textnode1);
control_element.appendChild(label_element);
var button_element1=document.createElement('button');
button_element1.className="btn1";
button_element1.appendChild(textnode2);
control_element.appendChild(button_element1);
var button_element2=document.createElement('button');
button_element2.className="btn2";
button_element2.appendChild(textnode3);
control_element.appendChild(button_element2);
var button_element3=document.createElement('button');
button_element3.className="btn3";
button_element3.appendChild(textnode4);
control_element.appendChild(button_element3);
}
this.expend=function () {
//能源消耗
if (that.state=="running") {
if (that.energy<that.expendspeed) {
that.state="paused";
that.cpu();
oLog.innerHTML+=that.id+"号飞船能源耗尽,停止飞行<br>"
}else{
that.energy-=that.expendspeed;
}
}
};
this.recover=function () {
//能源恢复
if (that.energy<100) {
if (that.energy>100-that.recoverspeed) {
that.energy=100;
}else{
that.energy+=that.recoverspeed;
}
}
scene.getElementsByTagName('div')[that.id].innerHTML=that.id+"号-"+that.energy+"%";
};
this.int1=setInterval(this.expend,1000);
this.int2=setInterval(this.recover,1000);
createship();
createcontrol();
}
ship.prototype=protoship;
var num=0;//飞船数量,起始为0
//signal
var signal=function (id,argument) {
this.id=id;
if (argument==0||argument==1) {
this.destroy=argument;
}else{
this.state=argument;
}
}
//Adapter
var Adapter=function (sign) {
var binary="000"+sign.id;
if (sign.destroy) {
binary+="1100";
}else{
if (sign.state=="running") {
binary+="0001";
}else{
binary+="0010";
}
}
return binary;
}
//BUS
var BUS=function (sign) {
var me=Math.random()*100;//模拟10%丢包率
var n=0;
while(me<10){
n++;
oLog.innerHTML+="传播失败,继续尝试"+n+"次<br>"
me=Math.random()*100;
}
oLog.innerHTML+="发送成功<br>"
setTimeout(function () {
for (var i = 0; i < ships.length; i++) {
ships[i].adapter(sign);
}
},300);
}
//新建飞船
createbtn.onclick=function (argument) {
if (num<4) {
ships.push(new ship());
num+=1;
}
controlf();
}
//control
function controlf(argument) {
var controls=control.getElementsByTagName('div');
for (var i = 1; i < controls.length; i++) {
(function (a) {
var buttons=controls[a].getElementsByTagName('button');
buttons[0].onclick=function () {
var sig=new signal(a,"running");
BUS(Adapter(sig));
}
buttons[1].onclick=function () {
var sig=new signal(a,"paused");
BUS(Adapter(sig));
}
buttons[2].onclick=function () {
var sig=new signal(a,1);
BUS(Adapter(sig));
control.getElementsByTagName('div')[a].style.display="none";
num-=1;
}
})(i)
}
}
controlf();
</script>
</body>
</html>