]> Untitled Git - lightcycles-love.git/blob - player.lua
6c38099e06c5f8ecebb102b151279166dd668b06
[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     o.vector = {x=o.acceleration, y=0}
17     return o
18 end
19
20 function Player:drawPath()
21     if #self.path >= 4 then
22         love.graphics.setColor(self.color)
23         love.graphics.line(self.path)
24     end
25 end
26
27 function Player:draw()
28     love.graphics.setColor(self.color)
29     love.graphics.rectangle('fill', self.position.x, self.position.y, self.width, self.height)
30
31     -- add current position
32     self:recordPosition()
33     self:drawPath()
34     table.remove(self.path)
35     table.remove(self.path)
36 end
37
38 function Player:recordPosition()
39     print(self.position.x .. ', ' .. self.position.y)
40     table.insert(self.path, self.position.x + self.width/2)
41     table.insert(self.path, self.position.y + self.height/2)
42 end
43
44 function Player:update(dt)
45     if love.keyboard.isDown("w") then
46         self.vector = {x=0, y=-self.acceleration}
47         self:recordPosition()
48     elseif love.keyboard.isDown("s") then
49         self.vector = {x=0, y=self.acceleration}
50         self:recordPosition()
51     elseif love.keyboard.isDown("a") then
52         self.vector = {x=-self.acceleration, y=0}
53         self:recordPosition()
54     elseif love.keyboard.isDown("d") then
55         self.vector = {x=self.acceleration, y=0}
56         self:recordPosition()
57     end
58
59     self.position.x = self.position.x + self.vector.x * dt
60     self.position.y = self.position.y + self.vector.y * dt
61 end