/
snake.lua
203 lines (197 loc) · 4.79 KB
/
snake.lua
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'hexhelper'
Snake = {}
Snake.__index = Snake
-- orientation
-- 3
-- 2 4
-- 1 5
-- 0
function Snake:new(idx, str)
local o = {}
setmetatable(o, Snake)
o.idx = idx
o.starti = tonumber(string.sub(str,1,1))
o.startj = tonumber(string.sub(str,2,2))
o.orient = tonumber(string.sub(str,3,3))
o.str = string.sub(str,4,-1)
return o
end
function Snake:draw_head(winning)
local rgba
if self.idx == 1 then
rgba = {249, 23, 32, 255}
elseif self.idx == 2 then
rgba = {255, 209, 0, 255}
elseif self.idx == 3 then
rgba = {114, 222, 58, 255}
elseif self.idx == 4 then
rgba = {0, 214, 253, 255}
elseif self.idx == 5 then
rgba = {214, 0, 253, 255}
elseif self.idx == 6 then
rgba = {58, 114, 222, 255}
end
local ticks = 30
if winning and winning > 0.0 then
rgba[1] = rgba[1] + winning*(255-rgba[1])
rgba[2] = rgba[2] + winning*(255-rgba[2])
rgba[3] = rgba[3] + winning*(255-rgba[3])
ticks = 60
end
rx = hexx(self.starti, self.startj)
ry = hexy(self.starti, self.startj)
if (love.timer.getTime()*ticks-4) % 30 < 2 then
love.graphics.setColor(255, 255, 255, 255)
else
love.graphics.setColor(rgba)
end
rx = rx + math.random(0,8*winning*winning)
ry = ry + math.random(0,8*winning*winning)
love.graphics.draw(img['fire'], rx, ry, 0, 2 * scale, 2 * scale, 16, 16)
love.graphics.draw(img['fire'], rx, ry, 0, 2 * scale, 2 * scale, 16, 16)
love.graphics.draw(img['fire'], rx, ry, 0, 2 * scale, 2 * scale, 16, 16)
love.graphics.draw(img['fire'], rx, ry, 0, 2 * scale, 2 * scale, 16, 16)
love.graphics.setColor(255, 255, 255, 255)
end
function Snake:draw(winning)
local blink = 0
local rgba
if self.idx == 1 then
rgba = {249, 23, 32, 255}
elseif self.idx == 2 then
rgba = {255, 209, 0, 255}
elseif self.idx == 3 then
rgba = {114, 222, 58, 255}
elseif self.idx == 4 then
rgba = {0, 214, 253, 255}
elseif self.idx == 5 then
rgba = {214, 0, 253, 255}
elseif self.idx == 6 then
rgba = {58, 114, 222, 255}
end
local ticks = 30
if winning and winning > 0.0 then
rgba[1] = rgba[1] + winning*(255-rgba[1])
rgba[2] = rgba[2] + winning*(255-rgba[2])
rgba[3] = rgba[3] + winning*(255-rgba[3])
ticks = 60
end
love.graphics.setColor(rgba)
local ci = self.starti
local cj = self.startj
local co = self.orient
for c in string.gmatch(self.str, '.') do
rx = hexx(ci, cj)
ry = hexy(ci, cj)
if not rx or not ry then
break
end
-- render
if c == 'H' then
-- nothing
elseif c == 'S' then
-- nothing
elseif c == 'T' then
-- nothing
elseif c == 'l' then
co = (co + 5) % 6
elseif c == 'L' then
co = (co + 4) % 6
elseif c == 'r' then
co = (co + 1) % 6
elseif c == 'R' then
co = (co + 2) % 6
end
if c ~= 'H' then
for i = 1, 16 do
if (love.timer.getTime()*ticks-blink) % 30 < 2 then
love.graphics.setColor(255, 255, 255, 255)
end
local tx = lx+(rx-lx)/16*i + math.random(0,8*winning*winning)
local ty = ly+(ry-ly)/16*i + math.random(0,8*winning*winning)
love.graphics.draw(img['fire'], tx, ty, 0, scale, scale, 16, 16)
love.graphics.setColor(rgba)
blink = blink + 1
end
end
lx = rx
ly = ry
-- update
if co == 0 then
cj = cj + 1
elseif co == 1 then
ci = ci - 1
if ci >= 5 then
cj = cj +1
end
elseif co == 2 then
ci = ci - 1
if ci < 5 then
cj = cj - 1
end
elseif co == 3 then
cj = cj - 1
elseif co == 4 then
ci = ci + 1
if ci > 5 then
cj = cj - 1
end
elseif co == 5 then
ci = ci + 1
if ci <= 5 then
cj = cj + 1
end
end
end
love.graphics.setColor(255, 255, 255, 255)
end
function Snake:try(newi, newj, newo)
local result = {}
local ci = newi or self.starti
local cj = newj or self.startj
local co = newo or self.orient
for c in string.gmatch(self.str, '.') do
if not hex_valid(ci, cj) then
return false
end
table.insert(result, {ci,cj})
-- walk through
if c == 'l' then
co = (co + 5) % 6
elseif c == 'L' then
co = (co + 4) % 6
elseif c == 'r' then
co = (co + 1) % 6
elseif c == 'R' then
co = (co + 2) % 6
elseif c == 'T' then
return result
end
-- update
if co == 0 then
cj = cj + 1
elseif co == 1 then
ci = ci - 1
if ci >= 5 then
cj = cj +1
end
elseif co == 2 then
ci = ci - 1
if ci < 5 then
cj = cj - 1
end
elseif co == 3 then
cj = cj - 1
elseif co == 4 then
ci = ci + 1
if ci > 5 then
cj = cj - 1
end
elseif co == 5 then
ci = ci + 1
if ci <= 5 then
cj = cj + 1
end
end
end
end