-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpurple.lua
More file actions
113 lines (91 loc) · 2.78 KB
/
purple.lua
File metadata and controls
113 lines (91 loc) · 2.78 KB
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
Purple = {}
function Purple:new()
local object = { src = { path = "assets/purple.png", size = 8 } }
self.__index = self
return setmetatable(object, self)
end
function Purple:init(width, height)
-- load the sprite sheet
self.spritesheet = love.graphics.newImage(self.src.path)
self.spritesheet:setFilter('nearest', 'nearest')
-- set up the frames
self.still = love.graphics.newQuad(0, 0, self.src.size, self.src.size, 3*self.src.size, self.src.size)
self.stepLeft = love.graphics.newQuad(self.src.size, 0, self.src.size, self.src.size, 3*self.src.size, self.src.size)
self.stepRight = love.graphics.newQuad(2*self.src.size, 0, self.src.size, self.src.size, 3*self.src.size, self.src.size)
self.current = self.still
-- player's metadata
self.facing = "right"
self.pos = { x = width/(2*scale)-(self.src.size/2), y = height/(2*scale)-(self.src.size/2) }
self.velocity = { x = 0, y = 0 }
self.speed = 1
-- for the updater
self.counter = 0
end
function Purple:draw()
-- scale -1 on x axis if facing left
if self.facing == "right" then
love.graphics.draw(self.spritesheet, self.current, math.floor(self.pos.x), math.floor(self.pos.y))
else
love.graphics.draw(self.spritesheet, self.current, math.floor(self.pos.x)+self.src.size, math.floor(self.pos.y), 0, -1, 1)
end
end
function Purple:update(dt, width, height)
-- input checks for movement
-- UP
if love.keyboard.isDown('up') then
self.pos.y = self.pos.y - self.speed
if self.pos.y < 0 then
self.pos.y = 0
end
end
-- DOWN
if love.keyboard.isDown('down') then
self.pos.y = self.pos.y + self.speed
if self.pos.y > height-self.src.size then
self.pos.y = height-self.src.size
end
end
-- LEFT
if love.keyboard.isDown('left') then
self.pos.x = self.pos.x - self.speed
if self.pos.x < 0 then
self.pos.x = 0
end
end
-- RIGHT
if love.keyboard.isDown('right') then
self.pos.x = self.pos.x + self.speed
if self.pos.x > width-self.src.size then
self.pos.x = width-self.src.size
end
end
-- decide which leg will be out
self.counter = (self.counter + 120 * dt) % 120 -- twice a second i think
if love.keyboard.isDown('up') or love.keyboard.isDown('down') or love.keyboard.isDown('left') or love.keyboard.isDown('right') then
if math.floor(self.counter/15) % 2 == 0 then
self.current = self.stepLeft
else
self.current = self.stepRight
end
else
self.current = self.still
end
end
function Purple:keypressed(key)
if key == 'left' or key == 'right' then
self.facing = key
elseif key == 'lshift' then
self.speed = 2
end
end
function Purple:keyreleased(key)
if key == self.facing and (love.keyboard.isDown('left') or love.keyboard.isDown('right')) then
if key == 'left' then
self.facing = 'right'
else
self.facing = 'left'
end
elseif key == 'lshift' then
self.speed = 1
end
end