-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.coffee
99 lines (73 loc) · 2.6 KB
/
renderer.coffee
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
# Render the game screen as a <pre/>
class Renderer
constructor: (pre, @ypos) ->
@pre = -> pre
@indent = -> 20
@cycle = [' 0_O',
' `y ',
' _O ']
@wheelie = ['0_O',
' +|',
' O']
render: (track, count, speed, time, bikeX, bikeY, wheelie) =>
result = ''
result += @renderCount(count, speed, time)
result += @renderTrack(track, bikeX, bikeY, wheelie)
@pre().textContent = result
padNum: (num, width, pre) ->
x = pre + pre + pre + pre + pre + "#{num}"
return x.substr(x.length - width)
timefmt: (seconds) ->
minutes = @padNum(Math.floor(seconds / 60), 2, '0')
seconds = @padNum(seconds % 60, 2, '0')
return "#{minutes}:#{seconds}"
renderCount: (count, speed, time) ->
"#{@padNum(count,3,'0')}m #{@timefmt(time)} #{Math.floor(speed*KM)}km/h\n"
renderTrack: (obstList, bikeX, bikeY, wheelie) =>
buffer = @renderLanes(obstList)
@addBike(buffer, bikeX, bikeY, wheelie)
return @flattenTrack(buffer)
addBike: (buffer, bikeX, bikeY, wheelie) =>
sy = buffer.length - (@ypos + 3) - bikeY # sy is y pos of rear wheel
sx = (@indent() - 4) + 2*bikeX - bikeY # sx is x pos of rear wheel
wheelx = sx + 2 # 2 leading spaces
sprite = if wheelie then @wheelie else @cycle
sprite = sprite.map (l) -> l.split ''
for y in [0 .. sprite.length - 1]
for x in [0 .. sprite[0].length - 1]
bp = sprite[y][x]
xpos = x+sx + y # starting point + offset + compensation for slope
ypos = y+sy
# Hide the bike behind uprights from ramps in the lane to the
# right.
continue if buffer[ypos][xpos] == '|' && x == (4 - y)
# Render the bike. Spaces are transparent; '_' is a
# non-transparent space.
buffer[ypos][xpos] = bp if bp != ' ' && bp != '_'
buffer[ypos][xpos] = ' ' if bp == '_'
renderLanes: (track) =>
count = track.length
result = []
for line in track
outln = ([0..@indent()].map -> ' ').join('')
outln += '/'
for c in line
seq = switch c
when " " then " /"
when "*" then "*/"
when "/" then "|=|/"
dl = (seq.length - 2) + 1
outln = outln[0..-dl] if seq.length > 3
outln += seq
result.push(outln.split '')
return result
flattenTrack: (track) =>
result = []
width = track.length
for line in track
outln = ([0..width].map -> ' ').join('')
width -= 1
outln += line.join("")
outln += "\n"
result.push outln
return result.join("")