summaryrefslogtreecommitdiff
path: root/prop.lua
blob: 67358e1f85bcfbb9858f6bc76404f604d2198832 (plain)
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
-- Metatable.
local Prop = {
	x = 0,
	y = 0,
	width = 0,
	height = 0,
	frame = 1,
	animation = "closed",
	style = "door",
}
Prop.__index = Prop

-- Constructor.
local function newProp()
	return setmetatable({}, Prop)
end

-- Dimensions related functions.
function Prop:getDimensions()
	return self.width, self.height
end
function Prop:setDimensions(width, height)
	self.width, self.height = width, height
	return self
end

-- Position related functions.
function Prop:getPosition()
	return self.x, self.y
end
function Prop:setPosition(x, y)
	self.x, self.y = x, y
	return self
end

-- Don't mistake with seduce.
function Prop:setUse(func)
	if type(func) == "function" then
		self.use = func
	end
	return self
end

-- Animation.
function Prop:changeAnimation(name)
	if self.animation ~= name and name then
		self.animation = name
		self.frame = 1
	end
	return self
end

-- Stylish.
function Prop:setStyle(style)
	if self.style ~= style and style then
		self.style = style
	end
	return self
end

-- Returns true if given position is inside Prop's hitbox.
function Prop:testPosition(x, y)
	local px, py = self:getPosition()
	local w, h = self:getDimensions()
	if x > px and x < px+w and y > py and y < py+h then
		return self
	end
end

-- Being used by player callback.
function Prop:use() end

-- LÖVE2D callbacks.
function Prop:update()
	-- Animations
	local frames = #Game.quads[self.style][self.animation]
	self.frame = (self.frame%frames)+1
	-- Automatic animation changes
	if self.animation == "opening" and self.frame == 7 then
		self:changeAnimation("open")
	end
end
function Prop:draw(scale)
	local x,y = self:getPosition()
	if Game.inRange(x, y) then
		local sprite = Game.sprites[self.style]
		local quad = Game.quads[self.style][self.animation][self.frame]
		love.graphics.setColor(255,255,255)
		love.graphics.draw(sprite, quad, (x)*scale, (y)*scale, 0, scale, scale)
	end
end

return newProp