-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawable.lua
159 lines (128 loc) · 2.94 KB
/
drawable.lua
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
--[[
drawable.lua
January 17th, 2013
]]
local love = love
require 'animation'
local Object = (require 'object').Object
local pairs, math
= pairs, math
module('objects')
Drawable = Object{}
--
-- Drawable constructor
--
function Drawable:_clone(values)
local o = Object._clone(self,values)
o.DRAWABLE = true
o._screenPos = values._screenPos or { 0, 0 }
o._position = values._position or { 0, 0 }
o._direction = values._direction or 'right'
o._rotation = 0
o:initializeAnimations()
return o
end
--
-- Initializes the animations for this sprite
--
function Drawable:initializeAnimations()
self._animations = {}
for k, v in pairs(self._spriteSheet._animations) do
local a = Animation{ _definition = v, _name = k }
self._animations[k] = a
end
end
--
-- Sets or gets the current direction
--
function Drawable:direction(d)
if not d then return self._direction end
if self._direction ~= d then
self._changeDirection = true
end
self._direction = d
end
--
-- Sets or gets the current animation
--
-- Inputs:
-- a - an animation index or nil
-- r - true if the animation should be reset
--
function Drawable:animation(a, r)
if not a then
return self._currentAnimation
end
-- switch to the new animation
if self._animations[a] then
self._currentAnimation = self._animations[a]
else
self._currentAnimation = self._animations[a .. self._direction]
end
if r then
self._currentAnimation:reset()
end
end
--
-- Draw the drawable
--
function Drawable:draw(camera, drawTable)
local a = self._currentAnimation
local of = a:offset()
self._screenPos[1] =
math.floor((self._position[1] * camera._zoomX) - camera._cwzx)
self._screenPos[2] =
math.floor((self._position[2] * camera._zoomY) - camera._cwzy)
love.graphics
.drawq(
self._spriteSheet._image,
self._spriteSheet._quads[a:quadNumber()],
self._screenPos[1], self._screenPos[2],
self._rotation, camera._zoomX, camera._zoomY,
of[1], of[2]
)
end
--
-- Set or get the position
--
function Drawable:position(x, y)
if not x then
return self._position
end
self._position[1] = x
self._position[2] = y
end
--
-- Get the distance from another drawable
--
function Drawable:distanceFrom(other)
local x = self._position[1] - other._position[1]
local y = self._position[2] - other._position[2]
return math.sqrt(x*x+y*y)
end
--
-- Set or get the screen position
--
function Drawable:screenPos(x, y)
if not x then
return self._screenPos
end
self._screenPos[1] = x
self._screenPos[2] = y
end
--
-- Updates the Drawable
--
function Drawable:update(dt)
-- updates the draw order
self._drawOrder = self._position[2] + self._currentAnimation:offset()[2] - (self._position[1] * 1e-14)
-- updates the animation
if self._currentAnimation then
if self._synchronizer then
self._currentAnimation._currentFrame =
self._synchronizer._currentAnimation._currentFrame
else
self._currentAnimation:update(dt)
end
end
end