forked from nan-academy/tron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics-super-hd.js
146 lines (132 loc) · 3.85 KB
/
graphics-super-hd.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
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
import state from './state.js'
import { SIZE, CASE_SIZE as S } from './config.js'
import h from './izi/h.js'
const names = Object.create(null)
const canvas = h.canvas({ width: `${SIZE * S}`, height: `${SIZE * S}`})
const ctx = canvas.getContext('2d')
const createCaseEl = h.style({
height: `${S}px`,
width: `${S}px`,
})
const memo = fn => (c => a => c[a] || (c[a] = fn(a)))(Object.create(null))
const toHex = memo(color => '#'+ `00000${color.toString(16)}`.slice(-6))
const draw = rect => {
rect.drawCount++
ctx.fillStyle = toHex(rect.color)
ctx.fillRect(rect.x * S, rect.y * S, S, S)
}
Object.assign(document.body.style, {
margin: 0,
background: '#111',
})
const translate = (x, y) => `translate(${~~(x * S)}px, ${~~(y * S)}px)`
let fadingRects = []
const _padNumber = n => ` #${n}`.slice(-3)
const _pad = str => `${str} `.slice(0, 10)
const getRank = (i, players, length) =>
_padNumber(players.length - (length - (i + 1)))
const isFading = rect => rect.drawCount < 128
const fadeTrail = () => {
fadingRects = fadingRects.filter(isFading)
fadingRects.forEach(draw)
}
const ctrlBtn = h('button.ctrl-btn', {
style: {
border: 'none',
padding: '8px',
width: '30px',
height: '30px',
outline: 0,
backgroundColor: 'transparent',
color: 'white',
}
})
const pauseBtn = ctrlBtn({
onclick: () => state.paused.set(!state.paused()),
title: 'space to pause',
}, state.paused() ? '▷' : '❘❘')
state.paused(paused => pauseBtn.textContent = paused ? '▷' : '❘❘')
const reloadBtn = ctrlBtn({
onclick: () => location,
title: 'r to reload',
}, '↺')
const speedDisplay = h.span.style({
border: 'none',
display: 'inline-block',
boxSizing: 'border-box',
whiteSpace: 'nowrap',
padding: '8px',
width: '100px',
height: '30px',
outline: 0,
backgroundColor: 'transparent',
color: 'white',
}, `speed x${state.speedFactor()}`)
state.speedFactor(speedFactor => speedDisplay.textContent = `speed x${state.speedFactor()}`)
export default {
init: players => {
ctx.fillStyle = 'black'
ctx.fillRect(0, 0, SIZE * S, SIZE * S)
h.replaceContent(document.body, h.div.style({
display: 'flex',
fontFamily: 'monospace',
flexWrap: 'wrap',
position: 'relative',
margin: '0 0 0 auto',
width: `${S * SIZE}px`,
overflow: 'hidden',
}, players.map(({ name, x, y, color }) => names[name] = h.div.style({
position: 'absolute',
left: 0,
top: 0,
qsdqd: draw({ x, y, color }),
transform: translate(x, y),
transition: 'transform 0.1s',
whiteSpace: 'pre',
color: toHex(color),
opacity: 1,
background: 'rgba(0, 0, 0, 0.25)',
textShadow: [
'-1px -1px black',
'-1px 1px black',
'1px -1px black',
'1px 1px black',
].join(', m')
}, name)).concat([
canvas,
// control bar
h.div.style({
position: 'absolute',
top: 0,
right: 0,
backgroundColor: 'rgba(255, 255, 255, 0.1)',
}, [
speedDisplay,
pauseBtn,
reloadBtn,
])
])))
},
setScore: players => players
.filter(p => p.dead)
.sort((a, b) => b.score === a.score
? a.name - b.name
: b.score - a.score)
.forEach(({ name, x, y, cause, score }, i, { length }) => {
const el = names[name]
el.textContent = `${getRank(i, players, length)} - ${_pad(name)} (${score})`
el.style.transition = 'transform 0.5s ease-in, opacity 5s ease-out'
el.style.transform = translate(0, i * 2)
el.style.position = 'fixed'
}),
update: nextMoves => {
nextMoves.forEach(rect => {
const r = { x: rect.x, y: rect.y, drawCount: 0, color: rect.color }
draw(r)
//fadingRects.push(r)
rect.dead || (names[rect.name].style.transform = translate(rect.x, rect.y))
})
//fadeTrail()
},
end: () => setInterval(fadeTrail, 16),
}