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
|
local dirs = {'left', 'right', 'up', 'down'}
Player = Fill:extend {
fill = {255,0,0},
width = 16, height = 16,
queue = deque.new(),
onNew = function(self)
for _ = 1, 2 do
self.queue:push_right(dirs[math.random(1,4)])
end
end,
onUpdate = function(self)
local keys = {the.keys:allJustPressed()}
if keys[1] then
local dirKey = false
for _, dir in ipairs(dirs) do
if dir == keys[1] then
dirKey = true
end
end
if dirKey then
--print('key: ' .. keys[1])
self.queue:push_right(keys[1])
self.direction = self.queue:pop_left()
if self.direction == 'left' then
self.x = self.x - self.width
elseif self.direction == 'right' then
self.x = self.x + self.width
elseif self.direction == 'up' then
self.y = self.y - self.height
elseif self.direction == 'down' then
self.y = self.y + self.height
end
self.moved = true
end
end
end,
onCollide = function(self, other)
if other ~= the.view.map then
--print('collision')
other:displaceDir(self,
util.dirToXY(self.direction),
- util.dirToPosNeg(self.direction))
end
end
}
|