-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhud.js
112 lines (82 loc) · 2.3 KB
/
hud.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
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
'use strict';
var EventDispatcher = createjs.EventDispatcher
, EaselEvent = createjs.Event
, config = require('./config');
var c = createjs;
var width, height, hud;
var isDirty = false;
var texts = {};
var values = {
health: config.hero.health,
score: 0
};
var hudService = module.exports = {
init: hud_init,
get: hud_get
};
function hud_init(x, y) {
EventDispatcher.initialize(hudService);
width = x;
height = y;
hud = createHud();
hud.on('tick', onTick);
this.on('set', onSet);
this.on('update', onUpdate);
}
function hud_get() {
return hud;
}
function onUpdate(event) {
if (!event.data) return;
var property = event.data.property;
var value = event.data.value;
if (property && typeof value != 'undefined') {
values[property] += value;
isDirty = true;
}
}
function onSet(event) {
if (!event.data) return;
var property = event.data.property;
var value = event.data.value;
if (property && typeof value != 'undefined') {
values[property] = value;
isDirty = true;
}
}
function onTick() {
if (isDirty) {
for (var key in values) {
if (values.hasOwnProperty(key)) {
var textObj = texts[key];
if (textObj) textObj.text = values[key];
}
}
}
isDirty = false;
}
function createHud() {
var newHud = new c.Container(0, 0);
var header = new c.Text('SPACE SHOOTER', '16px Arial', '#CCC');
header.x = width/2 - header.getMeasuredWidth()/2;
header.y = 8;
newHud.addChild(header);
var scoreLabel = new c.Text('SCORE', '16px Arial', '#CCC');
scoreLabel.x = width - scoreLabel.getMeasuredWidth() - 20;
scoreLabel.y = 8;
newHud.addChild(scoreLabel);
var healthLabel = new c.Text('HEALTH', '16px Arial', '#CCC');
healthLabel.x = 20;
healthLabel.y = 8;
newHud.addChild(healthLabel);
texts.score = new c.Text(values.score, '16px Arial', '#CCC');
texts.score.textAlign = 'right';
texts.score.x = width - texts.score.getMeasuredWidth() - 12;
texts.score.y = 24;
newHud.addChild(texts.score);
texts.health = new c.Text(values.health, '16px Arial', '#CCC');
texts.health.x = 20;
texts.health.y = 24;
newHud.addChild(texts.health);
return newHud;
}