-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaction.js
78 lines (70 loc) · 1.8 KB
/
faction.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
// @ts-check
export default class Faction {
/** @type {string} */ color = "#FF00FF";
/** @type {number} */ rgb = 0xFF00FF;
/** @type {string} */ name;
/**
* @param {string} name
* @param {?number} color
*/
constructor(name = "", color = null) {
this.name = name;
this.setColor(color);
}
/**
* @param {?number} color
*/
setColor(color = null) {
color ??= Math.floor(Math.random() * 0x00FF_FFFF);
this.rgb = color;
this.color = makeColor(color);
}
}
/**
* @param {number} count
* @returns {Faction[]}
*/
export function factionList(count = 2) {
let factions = new Array(count);
for (let i = 0; i < count; i++) {
factions[i] = new Faction();
}
return factions;
}
/**
* @param {Faction[]} factions
*/
export function factionsPrint(factions) {
const table = document.createElement('div');
factions.forEach(function(v, i) {
let entry = document.createElement('div');
entry.style.backgroundColor = v.color;
entry.innerText = i.toString();
table.append(entry);
});
document.body.append(table);
}
/**
* @param {?number} color
* @returns {string}
*/
export function makeColor(color = null) {
color ??= Math.floor(Math.random() * 0x00FF_FFFF);
let colorHex = color.toString(16);
return "#" + ("000000" + colorHex).slice(-6);
}
export class FactionInfo {
/** @type {HTMLElement} */ container;
/** @type {HTMLElement} */ counter;
/** @type {boolean} */ isDead;
/**
* @param {HTMLElement} container
* @param {HTMLElement} counter
* @param {boolean} isDead
*/
constructor(container, counter, isDead = false) {
this.container = container;
this.counter = counter;
this.isDead = isDead;
}
}