/zoeplat

To get this branch, use:
bzr branch /bzr/zoeplat

« back to all changes in this revision

Viewing changes to zoetrope/sprites/tile.lua

  • Committer: Josh C
  • Date: 2013-03-02 20:40:57 UTC
  • Revision ID: josh@9ix.org-20130302204057-yrra0a51zgtpq2v2
zoetrope 1.3.1

Show diffs side-by-side

added added

removed removed

 
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, imageOffset = { x = 0, y = 0 } },
 
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
                
 
35
                if obj.image then obj:updateQuad() end
 
36
                if obj.onNew then obj:onNew() end
 
37
                return obj
 
38
        end,
 
39
 
 
40
        updateQuad = function (self)
 
41
                if self.image then
 
42
                        self._imageObj = Cached:image(self.image)
 
43
                        if not self.width then self.width = self._imageObj:getWidth() end
 
44
                        if not self.height then self.height = self._imageObj:getHeight() end
 
45
 
 
46
                        self._quad = love.graphics.newQuad(self.imageOffset.x, self.imageOffset.y,
 
47
                                                                                           self.width, self.height,
 
48
                                                                                           self._imageObj:getWidth(), self._imageObj:getHeight())
 
49
                        self._imageObj:setWrap('repeat', 'repeat')
 
50
                        self._set.image = self.image
 
51
                        self._set.imageOffset.x = self.imageOffset.x
 
52
                        self._set.imageOffset.y = self.imageOffset.y
 
53
                end
 
54
        end,
 
55
 
 
56
        draw = function (self, x, y)
 
57
                if not self.visible or self.alpha <= 0 then return end
 
58
 
 
59
                x = math.floor(x or self.x)
 
60
                y = math.floor(y or self.y)
 
61
        
 
62
                if STRICT then
 
63
                        assert(type(x) == 'number', 'visible fill does not have a numeric x property')
 
64
                        assert(type(y) == 'number', 'visible fill does not have a numeric y property')
 
65
                        assert(type(self.width) == 'number', 'visible fill does not have a numeric width property')
 
66
                        assert(type(self.height) == 'number', 'visible fill does not have a numeric height property')
 
67
                end
 
68
 
 
69
                if not self.image then return end
 
70
                
 
71
                -- set color if needed
 
72
 
 
73
                local colored = self.alpha ~= 1 or self.tint[1] ~= 1 or self.tint[2] ~= 1 or self.tint[3] ~= 1
 
74
 
 
75
                if colored then
 
76
                        love.graphics.setColor(self.tint[1] * 255, self.tint[2] * 255, self.tint[3] * 255, self.alpha * 255)
 
77
                end
 
78
 
 
79
                -- if the source image or offset has changed, we need to recreate our quad
 
80
                
 
81
                if self.image and (self.image ~= self._set.image or
 
82
                   self.imageOffset.x ~= self._set.imageOffset.x or
 
83
                   self.imageOffset.y ~= self._set.imageOffset.y) then
 
84
                        self:updateQuad()
 
85
                end
 
86
                
 
87
                -- draw the quad
 
88
                local scaleX = self.scale * self.distort.x
 
89
                local scaleY = self.scale * self.distort.y
 
90
 
 
91
                if self.flipX then scaleX = scaleX * -1 end
 
92
                if self.flipY then scaleY = scaleY * -1 end
 
93
 
 
94
                love.graphics.drawq(self._imageObj, self._quad, x + self.width / 2, y + self.height / 2, self.rotation,
 
95
                                                        scaleX, scaleY, self.width / 2, self.height / 2)
 
96
                
 
97
                -- reset color
 
98
                
 
99
                if colored then
 
100
                        love.graphics.setColor(255, 255, 255, 255)
 
101
                end
 
102
                        
 
103
                Sprite.draw(self, x, y)
 
104
        end,
 
105
 
 
106
        __tostring = function (self)
 
107
                local result = 'Tile (x: ' .. self.x .. ', y: ' .. self.y ..
 
108
                                           ', w: ' .. self.width .. ', h: ' .. self.height .. ', '
 
109
 
 
110
                result = result .. 'image \'' .. self.image .. '\', '
 
111
 
 
112
                if self.active then
 
113
                        result = result .. 'active, '
 
114
                else
 
115
                        result = result .. 'inactive, '
 
116
                end
 
117
 
 
118
                if self.visible then
 
119
                        result = result .. 'visible, '
 
120
                else
 
121
                        result = result .. 'invisible, '
 
122
                end
 
123
 
 
124
                if self.solid then
 
125
                        result = result .. 'solid'
 
126
                else
 
127
                        result = result .. 'not solid'
 
128
                end
 
129
 
 
130
                return result .. ')'
 
131
        end
 
132
}