/ld28

To get this branch, use:
bzr branch http://9ix.org/bzr/ld28

« back to all changes in this revision

Viewing changes to player.lua

  • Committer: Josh C
  • Date: 2013-12-14 22:33:14 UTC
  • Revision ID: josh@9ix.org-20131214223314-gxlkcixtm6ma1mlr
put some walls and a goal... doesn't feel quite right.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
local dirs = {'left', 'right', 'up', 'down'}
 
2
 
1
3
Player = Fill:extend {
2
4
   fill = {255,0,0},
3
5
   width = 16, height = 16,
4
6
   queue = deque.new(),
5
7
   onNew = function(self)
6
 
              for _ = 1, 4 do
7
 
                 self.queue:push_right('')
 
8
              for _ = 1, 2 do
 
9
                 self.queue:push_right(dirs[math.random(1,4)])
8
10
              end
9
11
           end,
10
 
   onStartFrame = function(self)
11
 
                     local keys = {the.keys:allJustPressed()}
12
 
                     for _, k in ipairs(keys) do
13
 
                        self.queue:push_right(k)
14
 
 
15
 
                        local inst = self.queue:pop_left()
16
 
                        if inst == 'left' then
17
 
                           self.x = self.x - self.width
18
 
                        elseif inst == 'right' then
19
 
                           self.x = self.x + self.width
20
 
                        elseif inst == 'up' then
21
 
                           self.y = self.y - self.height
22
 
                        elseif inst == 'down' then
23
 
                           self.y = self.y + self.height
24
 
                        end
25
 
                     end
 
12
   onUpdate = function(self)
 
13
                 local keys = {the.keys:allJustPressed()}
 
14
                 if keys[1] then
 
15
                    local dirKey = false
 
16
                    for _, dir in ipairs(dirs) do
 
17
                       if dir == keys[1] then
 
18
                          dirKey = true
 
19
                       end
 
20
                    end
 
21
 
 
22
                    if dirKey then
 
23
                       --print('key: ' .. keys[1])
 
24
                       self.queue:push_right(keys[1])
 
25
 
 
26
                       self.direction = self.queue:pop_left()
 
27
                       if self.direction == 'left' then
 
28
                          self.x = self.x - self.width
 
29
                       elseif self.direction == 'right' then
 
30
                          self.x = self.x + self.width
 
31
                       elseif self.direction == 'up' then
 
32
                          self.y = self.y - self.height
 
33
                       elseif self.direction == 'down' then
 
34
                          self.y = self.y + self.height
 
35
                       end
 
36
 
 
37
                       self.moved = true
 
38
                    end
 
39
                 end
 
40
              end,
 
41
   onCollide = function(self, other)
 
42
                  if other ~= the.view.map then
 
43
                     --print('collision')
 
44
 
 
45
                     other:displaceDir(self,
 
46
                                       util.dirToXY(self.direction),
 
47
                                       - util.dirToPosNeg(self.direction))
26
48
                  end
 
49
               end
27
50
}
 
 
b'\\ No newline at end of file'