-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
178 lines (177 loc) · 5.75 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<title>生命游戏</title>
<style>
body {
margin: 0;
}
#scene {
position: fixed;
width: 100vw;
height: 100vh;
background-color: #fff;
}
.img {
position: absolute;
width: 48px;
height: 48px;
pointer-events: none;
transform: translateX(-50%) translateY(-50%);
}
.heart.beated {
animation: shake 0.3s;
}
@keyframes shake {
0% {
transform: scaleX(1) translateX(0);
}
33% {
transform: scaleX(1.5) translateX(-5px) translateY(5px);
}
66% {
transform: scaleX(0.5) translateX(10px) translateY(-10px);
}
100% {
transform: scaleX(1);
}
}
</style>
</head>
<body>
<div id="scene"></div>
<script>
window.utils = window.utils || {}
const posDic = {}
const objDic = {}
let heartCnt = 0;
if (localStorage.isDead) alert('您已阵亡');
scene.addEventListener('touchstart', function(ev){
// 禁用默认事件
ev.preventDefault();
// 如果点到可操作区域,触发对应操作
if (ev.target.className.indexOf('img') > -1) {
ev.target.click()
}
}, true)
const sceneConfig = {
x: 0,
y: 0
}
// 新手村的第一棵树
create({type: 'tree', x: 50, y: 100})
if (!localStorage.isDead) {
create({type: 'heart', x: sceneConfig.x + 30, y: sceneConfig.y + 30})
create({type: 'heart', x: sceneConfig.x + 80, y: sceneConfig.y + 30})
create({type: 'heart', x: sceneConfig.x + 130, y: sceneConfig.y + 30})
create({type: 'hero', x: 80, y: 100})
create({type: 'enemy', x: 80, y: 100, v: 2})
}
function create(config={}) {
const { type } = config;
const img = document.createElement('div');
img.config = config;
const id = Math.random().toString().substr(2);
config.img = img;
config.id = id;
posDic[id] = config;
objDic[id] = img;
img.className = 'img type-'+type;
if (config.x) img.style.left = `${config.x - sceneConfig.x}px`;
if (config.y) img.style.top = `${config.y - sceneConfig.y}px`;
if (config.y) img.style.zIndex = parseInt(config.y);
if (config.w) img.style.width = `${config.w}px`;
if (config.h) img.style.height = `${config.h}px`;
img.destroy = function() {
scene.removeChild(img);
delete objDic[img.id];
}
switch(type) {
case 'tree':
img.style.background='url("img/tree.svg")';
break;
case 'enemy':
img.style.background='url("img/tree.svg")';
img.hurted = function() {
img.destroy();
}
img.closeUser = function() {
let user = document.getElementById('user')
let uX = utils.getNum(user.style.left);
let uY = utils.getNum(user.style.top);
let oX = utils.getNum(img.style.left);
let oY = utils.getNum(img.style.top);
let dX = uX - oX;
let dY = uY - oY;
let d = Math.sqrt(dX**2 + dY**2);
if (d < 1) return; // 太近了就不继续接近,防止鬼畜
let v = img.config.v;
let _dX = dX * v / d;
let _dY = dY * v / d;
img.style.left = (oX + _dX) + 'px';
img.style.top = (oY + _dY) + 'px';
}
break;
case 'hero':
img.id="user";
img.style.background='url("img/hero.svg")';
img.locCb = function(x, y) {
for (key in posDic) {
const tarConfig = posDic[key];
if (tarConfig.type === 'heart') {
if (Math.abs(tarConfig.x - x) < 30 && Math.abs(tarConfig.y - y) < 30) {
tarConfig.img.click();
}
}
}
}
img.hurted = function() {
document.querySelector('.heart').click()
}
break;
case 'heart':
img.style.background='url("img/heartFull.svg")';
img.classList.add('heart');
heartCnt++;
// 第一次被攻击
img.onclick = function() {
if (img.isBeating) return;
img.isBeating = true;
img.style.display = 'none';
img.style.display = 'inline-block';
img.classList.add('beated')
window.heatBeatTimer = setTimeout(()=>{
delete img.isBeating;
img.classList.remove('beated');
img.style.background = 'url("img/heartHalf.svg")';
img.config.isBeated = img.config.isBeated || 0;
img.config.isBeated++;
if (img.config.isBeated >= 2) {
img.style.display = 'none';
img.className = img.className.replace(' heart', ' broken-heart')
delete posDic[img.config.id];
heartCnt--;
if (heartCnt === 0) {
localStorage.isDead = 1;
alert('您已阵亡');
user.style.display = 'none';
}
}
}, 300)
}
break;
default:
}
scene.appendChild(img);
return img
}
</script>
</div>
<link href="css/contrl.css" rel="stylesheet" >
<script src="js/event.js" type="text/javascript"></script>
<script src="js/contrl.js" type="text/javascript"></script>
</body>
</html>