bzr branch
http://9ix.org/bzr/ld27
1
by Josh C
zoetrope 1.4 |
1 |
-- Class: Tile |
2 |
-- A tile repeats a single image across its dimensions. If you do |
|
3 |
-- not specify a width and height, the sprite will size itself so |
|
4 |
-- that it's exactly as big as its source image. |
|
5 |
-- |
|
6 |
-- Extends: |
|
7 |
-- <Sprite> |
|
8 |
||
9 |
Tile = Sprite:extend |
|
10 |
{ |
|
11 |
-- Property: image |
|
12 |
-- The image to tile across the sprite. |
|
13 |
||
14 |
-- Property: imageOffset |
|
15 |
-- Setting this moves the top-left corner of the tile inside |
|
16 |
-- the sprite's rectangle. To draw as normal, set both x and y |
|
17 |
-- to 0. |
|
18 |
imageOffset = { x = 0, y = 0 }, |
|
19 |
||
20 |
-- private property: keeps track of properties that need action |
|
21 |
-- to be taken when they are changed |
|
22 |
-- image must be a nonsense value, not nil, |
|
23 |
-- for the tile to see that an image has been set if it |
|
24 |
-- was initially nil |
|
25 |
_set = { image = -1 }, |
|
26 |
||
27 |
-- private property imageObj: actual Image instance used to draw |
|
28 |
-- this is normally set via the image property, but you may set it directly |
|
29 |
-- so long as you never change that image property afterwards. |
|
30 |
||
31 |
new = function (self, obj) |
|
32 |
obj = obj or {} |
|
33 |
self:extend(obj) |
|
34 |
obj._set.imageOffset = {} |
|
35 |
||
36 |
if obj.image then obj:updateQuad() end |
|
37 |
if obj.onNew then obj:onNew() end |
|
38 |
return obj |
|
39 |
end, |
|
40 |
||
41 |
updateQuad = function (self) |
|
42 |
if self.image then |
|
43 |
self._imageObj = Cached:image(self.image) |
|
44 |
if not self.width then self.width = self._imageObj:getWidth() end |
|
45 |
if not self.height then self.height = self._imageObj:getHeight() end |
|
46 |
||
47 |
self._quad = love.graphics.newQuad(self.imageOffset.x, self.imageOffset.y, |
|
48 |
self.width, self.height, |
|
49 |
self._imageObj:getWidth(), self._imageObj:getHeight()) |
|
50 |
self._imageObj:setWrap('repeat', 'repeat') |
|
51 |
self._set.image = self.image |
|
52 |
self._set.imageOffset.x = self.imageOffset.x |
|
53 |
self._set.imageOffset.y = self.imageOffset.y |
|
54 |
end |
|
55 |
end, |
|
56 |
||
16
by Josh C
improve performance with (sketchy, experimental) sprite batches |
57 |
draw = function (self, x, y, map) |
1
by Josh C
zoetrope 1.4 |
58 |
if not self.visible or self.alpha <= 0 then return end |
59 |
||
60 |
x = math.floor(x or self.x) |
|
61 |
y = math.floor(y or self.y) |
|
62 |
||
63 |
if STRICT then |
|
64 |
assert(type(x) == 'number', 'visible tile does not have a numeric x property') |
|
65 |
assert(type(y) == 'number', 'visible tile does not have a numeric y property') |
|
66 |
assert(type(self.width) == 'number', 'visible tile does not have a numeric width property') |
|
67 |
assert(type(self.height) == 'number', 'visible tile does not have a numeric height property') |
|
68 |
end |
|
69 |
||
70 |
if not self.image then return end |
|
71 |
||
72 |
-- set color if needed |
|
73 |
||
74 |
local colored = self.alpha ~= 1 or self.tint[1] ~= 1 or self.tint[2] ~= 1 or self.tint[3] ~= 1 |
|
75 |
||
76 |
if colored then |
|
77 |
love.graphics.setColor(self.tint[1] * 255, self.tint[2] * 255, self.tint[3] * 255, self.alpha * 255) |
|
78 |
end |
|
79 |
||
80 |
-- if the source image or offset has changed, we need to recreate our quad |
|
81 |
||
82 |
if self.image and (self.image ~= self._set.image or |
|
83 |
self.imageOffset.x ~= self._set.imageOffset.x or |
|
84 |
self.imageOffset.y ~= self._set.imageOffset.y) then |
|
85 |
self:updateQuad() |
|
86 |
end |
|
87 |
||
88 |
-- draw the quad |
|
89 |
local scaleX = self.scale * self.distort.x |
|
90 |
local scaleY = self.scale * self.distort.y |
|
91 |
||
92 |
if self.flipX then scaleX = scaleX * -1 end |
|
93 |
if self.flipY then scaleY = scaleY * -1 end |
|
94 |
||
16
by Josh C
improve performance with (sketchy, experimental) sprite batches |
95 |
if map then |
96 |
batches = map.spriteBatches |
|
97 |
||
98 |
if not batches[self.image] then |
|
99 |
--batches:addBatch(self.image, self._imageObj) |
|
100 |
local maph, mapw = map:getMapSize() |
|
101 |
batches[self.image] = love.graphics.newSpriteBatch(self._imageObj, mapw * maph) |
|
102 |
print('adding new batch') |
|
103 |
end |
|
104 |
||
105 |
batches[self.image]:addq(self._quad, x + self.width / 2, y + self.height / 2, self.rotation, scaleX, scaleY, self.width / 2, self.height / 2) |
|
106 |
else |
|
107 |
||
108 |
love.graphics.drawq(self._imageObj, self._quad, x + self.width / 2, y + self.height / 2, self.rotation, |
|
109 |
scaleX, scaleY, self.width / 2, self.height / 2) |
|
110 |
end |
|
111 |
||
1
by Josh C
zoetrope 1.4 |
112 |
-- reset color |
113 |
||
114 |
if colored then |
|
115 |
love.graphics.setColor(255, 255, 255, 255) |
|
116 |
end |
|
117 |
end, |
|
118 |
||
119 |
__tostring = function (self) |
|
120 |
local result = 'Tile (x: ' .. self.x .. ', y: ' .. self.y .. |
|
121 |
', w: ' .. self.width .. ', h: ' .. self.height .. ', ' |
|
122 |
||
123 |
result = result .. 'image \'' .. self.image .. '\', ' |
|
124 |
||
125 |
if self.active then |
|
126 |
result = result .. 'active, ' |
|
127 |
else |
|
128 |
result = result .. 'inactive, ' |
|
129 |
end |
|
130 |
||
131 |
if self.visible then |
|
132 |
result = result .. 'visible, ' |
|
133 |
else |
|
134 |
result = result .. 'invisible, ' |
|
135 |
end |
|
136 |
||
137 |
if self.solid then |
|
138 |
result = result .. 'solid' |
|
139 |
else |
|
140 |
result = result .. 'not solid' |
|
141 |
end |
|
142 |
||
143 |
return result .. ')' |
|
144 |
end |
|
145 |
} |