/zoeplat

To get this branch, use:
bzr branch http://9ix.org/bzr/zoeplat

« back to all changes in this revision

Viewing changes to zoetrope/core/view.lua

  • Committer: Josh C
  • Date: 2013-04-10 00:41:47 UTC
  • Revision ID: josh@9ix.org-20130410004147-fj20d61atq2cgyqb
record/playback system (doesn't really work)

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
-- Extends:
9
9
--              <Group>
10
10
 
11
 
View = Group:extend{
 
11
View = Group:extend
 
12
{
12
13
        -- Property: timer
13
14
        -- A built-in <Timer> object for use as needed.
14
15
 
69
70
                return obj
70
71
        end,
71
72
 
72
 
        -- Method: loadLayers
73
 
        -- Loads layers from a Lua source file (as generated by Tiled -- http://mapeditor.org).
74
 
        -- Each layer is created as a <Group> and added to preserve its ordering. Tile layers
75
 
        -- are created as <Map> instances; object layers will try to create instances of a class
76
 
        -- named by the object's name property. If no class exists by this name, or the object
77
 
        -- has no name property, a gray fill will be created instead, as a placeholder. If the
78
 
        -- object has a property named _the, then this will set the.[whatever] to it.
79
 
        --
80
 
        -- Arguments:
81
 
        --              file - filename to load
82
 
        --
83
 
        -- Returns:
84
 
        --              nothing
85
 
 
86
 
        loadLayers = function (self, file)
87
 
                local ok, data = pcall(loadstring(Cached:text(file)))
88
 
                local _, _, directory = string.find(file, '^(.*[/\\])')
89
 
                directory = directory or ''
90
 
 
91
 
                if ok then
92
 
                        -- store tile properties by gid
93
 
                        
94
 
                        local tileProtos = {}
95
 
 
96
 
                        for _, tileset in pairs(data.tilesets) do
97
 
                                for _, tile in pairs(tileset.tiles) do
98
 
                                        local id = tileset.firstgid + tile.id
99
 
                                        
100
 
                                        for key, value in pairs(tile.properties) do
101
 
                                                tile.properties[key] = tovalue(value)
102
 
                                        end
103
 
 
104
 
                                        tileProtos[id] = tile
105
 
                                        tileProtos[id].width = tileset.tilewidth
106
 
                                        tileProtos[id].height = tileset.tileheight
107
 
                                end
108
 
                        end
109
 
 
110
 
                        for _, layer in pairs(data.layers) do
111
 
                                if View[layer.name] then
112
 
                                        error('the View class reserves the ' .. layer.name .. ' property for its own use; you cannot load a layer with that name')
113
 
                                end
114
 
 
115
 
                                if STRICT and self[layer.name] then
116
 
                                        local info = debug.getinfo(2, 'Sl')
117
 
                                        print('Warning: a property named ' .. layer.name .. ' already exists in the current view (' ..
118
 
                                                  info.short_src .. ', line ' .. info.currentline .. ')')
119
 
                                end
120
 
 
121
 
                                if layer.type == 'tilelayer' then
122
 
                                        local map = Map:new{ spriteWidth = data.tilewidth, spriteHeight = data.tileheight }
123
 
                                        map:empty(layer.width, layer.height)
124
 
 
125
 
                                        -- load tiles
126
 
 
127
 
                                        for _, tiles in pairs(data.tilesets) do
128
 
                                                map:loadTiles(directory .. tiles.image, Tile, tiles.firstgid)
129
 
 
130
 
                                                -- and mix in properties where applicable
131
 
 
132
 
                                                for id, tile in pairs(tileProtos) do
133
 
                                                        if map.sprites[id] then
134
 
                                                                map.sprites[id]:mixin(tile.properties)
135
 
                                                        end
136
 
                                                end
137
 
                                        end
138
 
 
139
 
                                        -- load tile data
140
 
 
141
 
                                        local x = 1
142
 
                                        local y = 1
143
 
 
144
 
                                        for _, val in ipairs(layer.data) do
145
 
                                                map.map[x][y] = val
146
 
                                                x = x + 1
147
 
 
148
 
                                                if x > layer.width then
149
 
                                                        x = 1
150
 
                                                        y = y + 1
151
 
                                                end
152
 
                                        end
153
 
 
154
 
                                        self[layer.name] = map
155
 
                                        self:add(map)
156
 
                                elseif layer.type == 'objectgroup' then
157
 
                                        local group = Group:new()
158
 
 
159
 
                                        for _, obj in pairs(layer.objects) do
160
 
                                                -- roll in tile properties if based on a tile
161
 
 
162
 
                                                if obj.gid and tileProtos[obj.gid] then
163
 
                                                        local tile = tileProtos[obj.gid]
164
 
 
165
 
                                                        obj.name = tile.properties.name
166
 
                                                        obj.width = tile.width
167
 
                                                        obj.height = tile.height
168
 
 
169
 
                                                        for key, value in pairs(tile.properties) do
170
 
                                                                obj.properties[key] = tovalue(value)
171
 
                                                        end
172
 
 
173
 
                                                        -- Tiled tile-based objects measure their y
174
 
                                                        -- position at their lower-left corner, instead
175
 
                                                        -- of their upper-left corner as usual
176
 
 
177
 
                                                        obj.y = obj.y - obj.height
178
 
                                                end
179
 
 
180
 
                                                -- create a new object if the class does exist
181
 
 
182
 
                                                local spr
183
 
 
184
 
                                                if _G[obj.name] then
185
 
                                                        obj.properties.x = obj.x
186
 
                                                        obj.properties.y = obj.y
187
 
                                                        obj.properties.width = obj.width
188
 
                                                        obj.properties.height = obj.height
189
 
 
190
 
                                                        spr = _G[obj.name]:new(obj.properties)
191
 
                                                else
192
 
                                                        spr = Fill:new{ x = obj.x, y = obj.y, width = obj.width, height = obj.height, fill = { 128, 128, 128 } }
193
 
                                                end
194
 
 
195
 
                                                if obj.properties._the then
196
 
                                                        the[obj.properties._the] = spr
197
 
                                                end
198
 
 
199
 
                                                group:add(spr)
200
 
                                        end
201
 
 
202
 
                                        self[layer.name] = group
203
 
                                        self:add(group)
204
 
                                else
205
 
                                        error("don't know how to create a " .. layer.type .. " layer from file data")
206
 
                                end
207
 
                        end
208
 
                else
209
 
                        error('could not load view data from file: ' .. data)
210
 
                end
211
 
        end,
212
 
 
213
73
        -- Method: clampTo
214
74
        -- Clamps the view so that it never scrolls past a sprite's boundaries.
215
75
        -- This only looks at the sprite's position at this instant in time,
370
230
        end,
371
231
 
372
232
        update = function (self, elapsed)
 
233
                Group.update(self, elapsed)
 
234
 
 
235
                -- follow the focused sprite
 
236
 
373
237
                local screenWidth = the.app.width
374
238
                local screenHeight = the.app.height
375
 
 
376
 
                -- follow the focused sprite
377
239
                
378
240
                if self.focus and self.focus.width < screenWidth
379
241
                   and self.focus.height < screenHeight then
400
262
                if self.translate.y < screenHeight - self.maxVisible.y then
401
263
                        self.translate.y = screenHeight - self.maxVisible.y
402
264
                end
403
 
 
404
 
                Group.update(self, elapsed)
405
265
        end,
406
266
 
407
267
        draw = function (self, x, y)