-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhp.js
70 lines (61 loc) · 2.03 KB
/
hp.js
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
pc.script.create('hp', function (context) {
var Hp = function (entity) {
var css = [
"#hpBar {",
" position: absolute;",
" top: 16px;",
" left: 35%;",
" width: 30%;",
" height: 6px;",
" background-color: rgba(164, 164, 164, .7);",
"}",
"#hpBar > .bar {",
" width: 100%;",
" height: 6px;",
" background-color: #2ecc71;",
" -webkit-transition: 200ms;",
" -moz-transition: 200ms;",
" -ms-transition: 200ms;",
" transition: 200ms;",
"}",
"#score {",
" position: absolute;",
" top: 22px;",
" left: calc(50% - 16px);",
" width: 32px;",
" height: 32px;",
" line-height: 32px;",
" background-color: rgb(33, 34, 36);",
" text-align: center;",
" font-size: 24px;",
" color: #fff;",
"}",
].join('\n');
var style = document.createElement('style');
style.innerHTML = css;
document.querySelector('head').appendChild(style);
var div = document.createElement('div');
div.id = 'hpBar';
document.body.appendChild(div);
var hp = this.hp = document.createElement('div');
hp.classList.add('bar');
div.appendChild(hp);
var score = this.score = document.createElement('div');
score.id = 'score';
score.textContent = '0';
document.body.appendChild(score);
this.points = 0;
};
Hp.prototype = {
set: function(hp) {
if (this.points !== hp) {
this.points = hp;
this.hp.style.width = Math.floor((hp / 10) * 100) + '%';
}
},
setScore: function(score) {
this.score.textContent = score;
}
};
return Hp;
});