/ld27

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

« back to all changes in this revision

Viewing changes to main.lua

  • Committer: Josh C
  • Date: 2013-08-25 02:04:52 UTC
  • Revision ID: josh@9ix.org-20130825020452-py8fijli8m5te5gv
improve performance with (sketchy, experimental) sprite batches

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
STRICT = false
2
 
DEBUG = false
3
 
 
4
 
if SDL then
5
 
  print = SDL.log
6
 
end
7
 
 
8
 
--lovebird = require 'lovebird'
 
1
STRICT = true
 
2
DEBUG = true
 
3
 
9
4
require 'zoetrope'
10
 
--require 'pepperprof'
11
 
--inspect = require 'inspect'
12
5
 
13
 
require 'sprite'
14
 
require 'util'
 
6
--require 'group'
15
7
 
16
8
require 'version'
 
9
--require 'wrap_tile'
17
10
require 'player'
18
11
require 'goal'
19
 
require 'pause_view'
20
 
require 'win_view'
21
12
 
22
13
GameView = View:extend {
23
14
   gameStart = 0,
24
 
   switchSounds = {},
25
15
   onNew = function (self)
26
16
              self:loadLayers('data/map.lua')
27
17
              self.focus = the.player
28
18
              self:clampTo(self.bg)
29
19
 
30
 
              the.app:setScaling(self)
31
 
 
32
20
              self.mazes = {self.maze1, self.maze2, self.maze3}
33
21
              self.bgs = {self.bg, self.bg, self.bg2}
34
22
              for _, maze in ipairs(self.mazes) do maze:die() end
41
29
              self.maze2.playerScale = 0.5
42
30
              self.maze3.playerScale = 1
43
31
 
44
 
              self.goals = {{},{},{}}
45
 
              the.goalsAchieved = 0
 
32
              self.goals = {}
46
33
              for _, obj in ipairs(self.objects.sprites) do
47
34
                 if obj:instanceOf(Goal) then
48
35
                    obj.visible = false
49
 
                    table.insert(self.goals[tonumber(obj.map)], obj)
 
36
                    self.goals[tonumber(obj.map)] = obj
50
37
                 end
51
38
              end
52
39
 
53
 
              local sndFiles = {'data/Explosion2.wav', 'data/Explosion3.wav', 'data/Hit_Hurt3.wav'}
54
 
 
55
 
              for _, sndFile in ipairs(sndFiles) do
56
 
                 local snd = love.audio.newSource(sndFile, 'static')
57
 
                 table.insert(self.switchSounds, snd)
58
 
              end
59
 
 
60
40
              --the.interface = Group:new()
61
41
 
62
42
              --self:add(the.interface)
63
43
 
64
44
              self:switchMaze()
65
45
 
66
 
              -- profiling...
67
 
              --the.profiler = newProfiler()
68
 
              --the.profiler:start()
69
 
 
70
 
              self.gameStart = love.timer.getTime()
71
 
              self.lastChange = love.timer.getTime()
 
46
              self.gameStart = love.timer.getMicroTime()
 
47
              self.lastChange = love.timer.getMicroTime()
72
48
           end,
73
49
   onUpdate = function(self, dt)
74
 
                 if the.player.active and love.timer.getTime() > self.lastChange + 10 then
 
50
                 if the.player.active and love.timer.getMicroTime() > self.lastChange + 10 then
75
51
                    -- switch maze
76
52
                    self:switchMaze()
77
53
                 end
78
54
 
79
55
                 if not (DEBUG and the.console.visible) then
80
 
                    for _, goal in ipairs(the.activeGoals) do
81
 
                       the.player.collider:collide(goal)
82
 
                    end
 
56
                    the.activeMaze:collide(the.player)
 
57
                    the.player:collide(the.activeGoal)
83
58
                 end
84
59
 
85
 
                 if the.keys:justPressed('escape', 'q') then
86
 
                    PauseView:new():activate()
87
 
                 --elseif the.keys:justPressed('w') then
88
 
                 --   WinView:new():activate()
89
 
                 end
 
60
                 -- for _, mirror in ipairs(the.mirrors.sprites) do
 
61
                 --    if not mirror.of then
 
62
                 --       print('mirror:' .. inspect(mirror))
 
63
                 --       error('mirror OF NOTHING')
 
64
                 --    end
 
65
                 -- end
90
66
              end,
91
67
   switchMaze = function(self)
92
68
                   if the.activeMaze then
100
76
                      the.activeBg.visible = false
101
77
                      the.activeBg = self.bgs[newMaze]
102
78
 
103
 
                      for _, goal in ipairs(the.activeGoals) do
104
 
                         goal.visible = false
 
79
                      if the.activeGoal then
 
80
                         the.activeGoal.visible = false
105
81
                      end
106
 
                      the.activeGoals = self.goals[newMaze]
107
 
 
108
 
                      love.audio.play(self.switchSounds[math.random(#self.switchSounds)])
 
82
                      the.activeGoal = self.goals[newMaze]
109
83
                   else
110
84
                      the.activeMaze = self.maze2
111
85
                      the.activeBg = self.bg
112
 
                      the.activeGoals = self.goals[2]
 
86
                      the.activeGoal = self.goals[2]
113
87
                   end
114
88
 
115
89
                   the.activeMaze:revive()
116
90
                   the.activeBg.visible = true
117
 
                   for _, goal in ipairs(the.activeGoals) do
118
 
                      goal.visible = true
 
91
                   if the.activeGoal then
 
92
                      the.activeGoal.visible = true
119
93
                   end
120
94
                   the.player.scale = the.activeMaze.playerScale
121
 
                   the.player.collider.width = the.player.width * the.activeMaze.playerScale
122
 
                   the.player.collider.height = the.player.height * the.activeMaze.playerScale
123
95
 
124
96
                   self._fx = {0,0,0,0}
125
97
                   self.tween:start(self._fx, 4, 175, 10, 'quadIn')
126
98
 
127
 
                   self.lastChange = love.timer.getTime()
 
99
                   self.lastChange = love.timer.getMicroTime()
128
100
                end,
129
101
   onEndFrame = function(self)
130
102
                   --the.interface.translate.x = the.player.x - the.app.width / 2 + the.player.width / 2
133
105
}
134
106
 
135
107
MenuScreen = View:extend {
136
 
   bg = Tile:new{image = 'data/intro.png', x = 0, y = 0},
 
108
   title = Text:new{text = "Press a key to start", font = 48, wordWrap = false},
 
109
   --title = Tile:new{image = 'data/title.png', x = 0, y = 0},
137
110
   onNew = function(self)
138
 
              the.app:setScaling(self)
139
 
 
140
 
              self:add(self.bg)
141
 
              --self.title:centerAround(400, 200)
142
 
 
143
 
              local nr = 'data/NewRocker-Regular.otf'
144
 
              self:add(Text:new{
145
 
                          text = 'Treasures of the Decimaze',
146
 
                          --x = 0,
147
 
                          y = 100,
148
 
                          width = self.bg.width,
149
 
                          font = {nr, 48},
150
 
                          align = 'center',
151
 
                          tint = {0,0,0}
152
 
                       })
153
 
 
154
 
              self:add(Text:new{
155
 
                          text = 'Find the 4 lost shinies!',
156
 
                          y = 170,
157
 
                          width = self.bg.width,
158
 
                          font = {nr, 28},
159
 
                          align = 'center',
160
 
                          tint = {0,0,0}
161
 
                       })
162
 
 
163
 
              self:add(Text:new{
164
 
                          text = 'Use the arrow keys to move',
165
 
                          y = 375,
166
 
                          width = self.bg.width,
167
 
                          font = {nr, 24},
168
 
                          align = 'center',
169
 
                          tint = {0,0,0}
170
 
                       })
171
 
 
172
 
              self:add(Text:new{
173
 
                          text = 'Press a key to start',
174
 
                          y = 425,
175
 
                          width = self.bg.width,
176
 
                          font = {nr, 24},
177
 
                          align = 'center',
178
 
                          tint = {0,0,0}
179
 
                       })
180
 
 
181
 
 
 
111
              self:add(self.title)
 
112
              self.title:centerAround(400, 200)
182
113
           end,
183
114
   onUpdate = function(self, elapsed)
184
 
     if the.keys:allJustPressed() or the.mouse:pressed() then
185
 
       the.app.view = GameView:new()
186
 
     end
187
 
   end
 
115
                 if the.keys:allJustPressed() then
 
116
                    the.app.view = GameView:new()
 
117
                 end
 
118
              end
188
119
}
189
120
 
190
121
the.app = App:new {
191
 
   width = 800,
192
 
   height = 600,
193
 
   name = "Treasures of the Decimaze",
194
122
   onRun = function (self)
195
123
              print('Version: ' .. VERSION)
196
124
 
197
 
              self.view = MenuScreen:new()
198
 
              self:setScaling()
 
125
              self.view = GameView:new()
199
126
 
200
127
              if DEBUG then
201
128
                 self.console:watch('VERSION', 'VERSION')
202
 
                 self.console:watch('player.x', 'the.player.x')
203
 
                 self.console:watch('player.y', 'the.player.y')
204
129
 
205
130
                 -- back off that dark overlay a bit
206
131
                 self.console.fill.fill[4] = 75
207
132
              end
208
133
           end,
209
134
   onUpdate = function (self, dt)
210
 
                 if the.keys:justPressed('f1') then
211
 
                    local ss = love.graphics.newScreenshot()
212
 
                    ss:encode('screenshot-' ..love.timer.getTime()..'.png')
 
135
                 if the.keys:justPressed('escape') then
 
136
                    self.quit()
213
137
                 end
214
 
              end,
215
 
   update = function(self, dt)
216
 
     if lovebird then
217
 
       fps = love.timer.getFPS()
218
 
       lovebird.update()
219
 
     end
220
 
     App.update(self, dt)
221
 
   end,
222
 
   setScaling = function(self, view)
223
 
     local view = view or the.app.view
224
 
 
225
 
     local winw, winh = love.window.getMode()
226
 
     --if love.window.getFullscreen() then
227
 
     if winw == self.width and winh == self.height then
228
 
       self.scale = 1
229
 
       view.realTranslate = { x=0, y=0 }
230
 
       love.graphics.setScissor()
231
 
     else
232
 
       --self.scale = math.min(
233
 
       --  math.floor(winw / the.app.viewWidth),
234
 
       --  math.floor(winh / the.app.viewHeight)
235
 
       --)
236
 
       if winh >= 1050 then
237
 
         self.scale = 1.5
238
 
       elseif winh < the.app.height then
239
 
         self.scale = winh / the.app.height
240
 
       end
241
 
       view.realTranslate.x = math.floor((winw - self.width * self.scale) / 2)
242
 
       view.realTranslate.y = math.floor((winh - self.height * self.scale) / 2)
243
 
       --print('translate: ' .. view.realTranslate.x .. ', ' .. view.realTranslate.y)
244
 
 
245
 
       love.graphics.setColor(0,0,0)
246
 
       love.graphics.rectangle('fill', 0, 0, winw, winh)
247
 
       love.graphics.setScissor( view.realTranslate.x,
248
 
                                 view.realTranslate.y,
249
 
                                 self.width * self.scale,
250
 
                                 self.height * self.scale)
251
 
     end
252
 
 
253
 
     view.scale = self.scale
254
 
   end
 
138
              end
255
139
}
256
 
 
257
 
function love.resize(w, h)
258
 
  print('love.resize')
259
 
  the.app:setScaling()
260
 
end