-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
286 lines (223 loc) · 7.72 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
background-color: #000;
font-size: 14px;
}
#scene {
position: relative;
width: 500px;
margin: 0 auto;
margin-top: 100px;
}
#control {
width: 500px;
margin: 0 auto;
margin-top: 100px;
border: 1px solid white;
}
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">
<button id="btn">建造新飞船</button>
</div>
<script>
var scene = document.getElementById('scene');
var control = document.getElementById('control');
var createbtn = document.getElementById('btn');
var ships = [];
var num = 0;
function Ship() {
this.id = scene.getElementsByTagName('div').length;
this.speed = 10;
this.energy = 100;
this.state = "paused";
this.recoverspeed = 2;
this.expendspeed = 5;
this.destroy = 0;
var that = this;
function createship() {
var ship_element = document.createElement('div');
ship_element.className = "ship";
scene.appendChild(ship_element);
}
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.energy < 5) {
that.state = "paused";
that.cpu();
}
if (that.state == "running") {
that.energy -= that.expendspeed;
}
}
//能源恢复
this.recover = function() {
if (that.energy < 100) {
that.energy += that.recoverspeed;
}
}
this.int1 = setInterval(function() {
that.expend();
// console.log(that.id + ' is expend')
}, 1000);
this.int2 = setInterval(function() {
that.recover()
// console.log(that.id + ' is recover')
}, 1000);
createship();
createcontrol();
}
//指令接收器
Ship.prototype.acceptor = function(signal) {
if (signal.id == this.id) {
this.state = signal.state;
if (signal.destroy) {
this.destroy = signal.destroy;
}
this.cpu();
}
}
//指令处理器
Ship.prototype.cpu = function() {
if (this.energy < 5) {
this.state = "paused";
}
scene.getElementsByTagName('div')[this.id].style.animationPlayState = this.state;
if (this.destroy == 1) {
scene.getElementsByTagName('div')[this.id].style.display = "none";
control.getElementsByTagName('div')[this.id - 1].style.display = "none";
clearInterval(this.int1);
clearInterval(this.int2);
}
}
// Signal 构造函数
function Signal(id, argument) {
this.id = id;
if (argument == 0 || argument == 1) {
this.destroy = argument;
} else {
this.state = argument;
}
}
// Mediator
var Mediator = function(sign) {
// 模拟30%丢包率
var me = Math.random() * 100;
if (me >= 30) {
for (var i = 0; i < ships.length; i++) {
ships[i].acceptor(sign);
}
}
}
// 新建飞船
createbtn.onclick = function(argument) {
if (num < 4) {
ships.push(new Ship());
num += 1;
}
controlf();
}
// 对每个飞船的控制按键进行事件绑定,并且传输命令
function controlf(argument) {
var controls = control.getElementsByTagName('div');
for (var i = 0; i < controls.length; i++) {
(function(a) {
var buttons = controls[a].getElementsByTagName('button');
buttons[0].onclick = function() {
var sig = new Signal(a + 1, "running");
Mediator(sig);
}
buttons[1].onclick = function() {
var sig = new Signal(a + 1, "paused");
Mediator(sig);
}
buttons[2].onclick = function() {
var sig = new Signal(a + 1, 1);
Mediator(sig);
num -= 1;
}
})(i)
}
}
var init = function() {
num = 2;
// 默认两艘飞船
ships.push(new Ship());
ships.push(new Ship());
console.log(ships);
// 绑定控制按钮事件
controlf();
}
init();
</script>
</body>
</html>