]> Untitled Git - lightcycles-love.git/blob - player.lua
Removed debug print and vector from player
[lightcycles-love.git] / player.lua
1 Player = {}
2 Player.name = 'player'
3 Player.color = {255, 0, 0}
4 Player.width = 5
5 Player.height = 5
6 Player.acceleration = 100
7 Player.position = {x=0, y=0}
8 Player.vector = {x=0, y=0}
9 Player.path = {}
10
11 function Player:new(o)
12     o = o or {}
13     setmetatable(o, self)
14     self.__index = self
15     o:recordPosition()
16     return o
17 end
18
19 function Player:drawPath()
20     if #self.path >= 4 then
21         love.graphics.setColor(self.color)
22         love.graphics.line(self.path)
23     end
24 end
25
26 function Player:draw()
27     love.graphics.setColor(self.color)
28     love.graphics.rectangle('fill', self.position.x, self.position.y, self.width, self.height)
29
30     -- add current position
31     self:recordPosition()
32     self:drawPath()
33     table.remove(self.path)
34     table.remove(self.path)
35 end
36
37 function Player:recordPosition()
38     table.insert(self.path, self.position.x + self.width/2)
39     table.insert(self.path, self.position.y + self.height/2)
40 end
41
42 function Player:update(dt)
43     if love.keyboard.isDown("w") then
44         self.vector = {x=0, y=-self.acceleration}
45         self:recordPosition()
46     elseif love.keyboard.isDown("s") then
47         self.vector = {x=0, y=self.acceleration}
48         self:recordPosition()
49     elseif love.keyboard.isDown("a") then
50         self.vector = {x=-self.acceleration, y=0}
51         self:recordPosition()
52     elseif love.keyboard.isDown("d") then
53         self.vector = {x=self.acceleration, y=0}
54         self:recordPosition()
55     end
56
57     self.position.x = self.position.x + self.vector.x * dt
58     self.position.y = self.position.y + self.vector.y * dt
59 end