/traderous

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

« back to all changes in this revision

Viewing changes to main.lua

  • Committer: Josh C
  • Date: 2013-05-16 02:41:58 UTC
  • Revision ID: josh@9ix.org-20130516024158-d5jtkfvd2qw709xo
record high scores

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
require 'zoetrope'
5
5
vector = require 'vector'
6
 
--inspect = require 'inspect'
7
6
 
8
7
require 'group'
9
8
 
16
15
require 'bullet'
17
16
require 'rock'
18
17
require 'boom'
19
 
require 'planet'
20
 
require 'trade_view'
21
 
require 'shield'
22
18
 
23
19
util = {
24
20
   signOf = function(value)
74
70
}
75
71
 
76
72
GameView = View:extend {
 
73
   lastRock = 0,
 
74
   rockInterval = 1,
 
75
   gameStart = 0,
77
76
   onNew = function (self)
 
77
              -- for x = 1,30 do
 
78
              --    for y = 1,30 do
 
79
              --       self:add(Fill:new{x=x*400, y=y*400,
 
80
              --                         width = 32, height = 32,
 
81
              --                         fill = {0,0,255}
 
82
              --                      })
 
83
              --    end
 
84
              -- end
 
85
 
78
86
              the.storage = Storage:new{filename = 'scores.lua'}
79
87
              the.storage:load()
80
 
              --if not the.storage.data.highScore then
81
 
              --   print('initializing storage')
82
 
              --   the.storage.data = {highScore = 0}
83
 
              --end
 
88
              if not the.storage.data.highScore then
 
89
                 print('initializing storage')
 
90
                 the.storage.data = {highScore = 0}
 
91
              end
84
92
 
 
93
              the.rockColliders = Group:new()
85
94
              the.bullets = Group:new()
86
 
              the.interface = Group:new()
87
 
              the.planets = Group:new()
88
 
              the.indicators = Group:new()
89
 
              the.enemies = Group:new()
 
95
              the.mirrors = Group:new()
 
96
              the.rocks = Group:new()
90
97
 
91
98
              the.bg = Tile:new{
92
99
                 image = 'data/stars3.png',
93
 
                 width = 13660,
94
 
                 height = 7680
 
100
                 -- 1366x768 * 3
 
101
                 width = 4098,
 
102
                 height = 2304
95
103
              }
96
104
              self:add(the.bg)
97
105
 
98
 
              self:add(the.planets)
99
 
 
100
106
              --the.player = CrystalPlayer:new{x=400,y=300}
101
 
              the.player = SpacePlayer:new{x=the.bg.width / 2, y=the.bg.height / 2}
 
107
              the.player = SpacePlayer:new{x=1366,y=768}
102
108
              self:add(the.player)
103
 
              self:add(the.player.thrust)
104
 
              self:add(the.player.shield)
105
 
 
106
 
              self:add(the.enemies)
107
 
 
108
 
              local e = Enemy:new{x=400, y=300}
109
 
              --local e = Enemy:new{x=the.bg.width / 2, y=the.bg.height / 2}
110
 
              the.enemies:add(e)
111
 
              self:add(e.thrust) -- why doesn't this work in Enemy.new?
112
 
              self:add(e.shield)
 
109
 
 
110
              --self:add(Enemy:new{x=400, y=300})
113
111
 
114
112
              self:add(the.bullets)
115
 
              self:add(the.indicators)
116
 
              self:add(the.interface)
117
 
 
118
 
              for _ = 1, math.random(6) do
119
 
                 local planet = Planet:new{
120
 
                    x = math.random(the.app.width / 2,
121
 
                                    the.bg.width - the.app.width / 2),
122
 
                    y = math.random(the.app.height / 2,
123
 
                                    the.bg.height - the.app.height / 2),
124
 
                    rotation = math.random() * math.pi
125
 
                 }
126
 
                 the.planets:add(planet)
127
 
              end
 
113
              self:add(the.rockColliders)
 
114
              self:add(the.mirrors)
 
115
              self:add(the.rocks)
128
116
 
129
117
              the.cursor = Cursor:new()
130
118
              self:add(the.cursor)
131
119
 
 
120
              the.score = Text:new{
 
121
                 width = the.app.width,
 
122
                 align = 'center',
 
123
                 font = 25}
 
124
              self:add(the.score)
 
125
 
 
126
              local hs = the.storage.data.highScore
 
127
              local m = hs / 60
 
128
              local s = hs % 60
 
129
 
 
130
              the.highScore = Text:new{
 
131
                 width = the.app.width,
 
132
                 align = 'right',
 
133
                 font = 25,
 
134
                 text = string.format('High Score: %d:%02d', m, s)
 
135
              }
 
136
              self:add(the.highScore)
 
137
 
132
138
              love.mouse.setGrab(true)
133
139
              love.mouse.setVisible(false)
134
140
              love.mouse.setPosition(the.app.width / 2, the.app.height / 2)
135
141
 
 
142
              --self:loadLayers('data/map.lua')
136
143
              self.focus = the.player
 
144
              --self:clampTo(self.map)
 
145
 
 
146
              self.gameStart = love.timer.getTime()
137
147
           end,
138
148
   onUpdate = function(self, dt)
139
 
                 the.bullets:collide(the.planets)
140
 
                 the.bullets:collide(the.player)
141
 
                 the.bullets:collide(the.enemies)
 
149
                 if love.timer.getTime() > self.lastRock + self.rockInterval then
 
150
                    local unseenRock = nil
 
151
                    while not unseenRock do
 
152
                       local rock = Rock:new{
 
153
                          x = math.random(the.app.width / 2,
 
154
                                          the.bg.width - the.app.width / 2),
 
155
                          y = math.random(the.app.height / 2,
 
156
                                          the.bg.height - the.app.height / 2),
 
157
                          velocity = {
 
158
                             x = math.random(-300, 300),
 
159
                             y = math.random(-300, 300),
 
160
                             rotation = math.random(-7, 7)
 
161
                          },
 
162
                          scale = math.random() + 0.5
 
163
                       }
 
164
 
 
165
                       local rockToPlayer = util.shortestVector(rock, the.player)
 
166
                       if math.abs(rockToPlayer.x) > the.app.width / 2 + rock.width * rock.scale and
 
167
                           math.abs(rockToPlayer.y) > the.app.height / 2 + rock.height * rock.scale then
 
168
                         unseenRock = rock
 
169
                        end
 
170
                    end
 
171
 
 
172
                    the.rocks:add(unseenRock)
 
173
 
 
174
                    self.lastRock = love.timer.getTime()
 
175
                 end
 
176
 
 
177
                 the.bullets:collide(the.rockColliders)
142
178
              end,
143
179
   onEndFrame = function(self)
144
 
                   the.interface.translate.x = the.player.x - the.app.width / 2 + the.player.width / 2
145
 
                   the.interface.translate.y = the.player.y - the.app.height / 2 + the.player.height / 2
 
180
                   if the.player.active then
 
181
                      local t = love.timer.getTime() - self.gameStart
 
182
                      local m = t / 60
 
183
                      local s = t % 60
 
184
 
 
185
                      the.score.text = string.format('Score: %d:%02d', m, s)
 
186
                      the.score.y = the.player.y - the.app.height / 2 + the.player.height
 
187
                      the.score.x = the.player.x - the.app.width / 2
 
188
                   end
 
189
 
 
190
                   the.highScore.y = the.player.y - the.app.height / 2 + the.player.height
 
191
                   the.highScore.x = the.player.x - the.app.width / 2
146
192
                end,
147
193
   draw = function (self, x, y)
148
194
             View.draw(self, x, y)
149
 
             --love.graphics.print('FPS:' .. love.timer.getFPS(), 20, 20)
150
 
          end,
 
195
             love.graphics.print('FPS:' .. love.timer.getFPS(), 20, 20)
 
196
          end
151
197
}
152
198
 
153
199
MenuScreen = View:extend {
171
217
              math.randomseed(os.time())
172
218
 
173
219
              self.view = GameView:new()
174
 
 
175
220
              if DEBUG then
176
221
                 self.console:watch('VERSION', 'VERSION')
177
222
                 self.console:watch('updateTook', 'the.updateTook')
179
224
                 self.console:watch('the.player.y', 'the.player.y')
180
225
                 self.console:watch('the.app.width', 'the.app.width')
181
226
                 self.console:watch('the.app.height', 'the.app.height')
182
 
                 self.console:watch('num mirrors', '#the.mirrors.sprites')
183
 
                 self.console:watch('num rocks', '#the.rocks.sprites')
184
 
                 self.console:watch('num planets', '#the.planets.sprites')
185
227
                 --self.console:watch('drawTook', 'the.drawTook')
186
228
 
187
229
                 -- back off that dark overlay a bit
189
231
              end
190
232
           end,
191
233
   onUpdate = function (self, dt)
192
 
                 if not (DEBUG and the.console.visible) then
193
 
                    if the.keys:justPressed('q') then
194
 
                       self.quit()
195
 
                    elseif the.keys:justPressed('return') then
196
 
                       if the.keys:pressed('alt') then
197
 
                          love.graphics.toggleFullscreen()
198
 
                       else
199
 
                          self.view = GameView:new()
200
 
                       end
201
 
                    elseif the.keys:justPressed('f1') then
202
 
                       local ss = love.graphics.newScreenshot()
203
 
                       ss:encode('screenshot-' ..love.timer.getTime()..'.png')
204
 
                    elseif the.keys:justPressed('f11') then
205
 
                       love.graphics.toggleFullscreen()
206
 
                    end
 
234
                 if the.keys:justPressed('escape') then
 
235
                    self.quit()
207
236
                 end
208
237
              end,
209
238
   update = function (self, dt)
214
243
               end
215
244
            end
216
245
}
217
 
 
218
 
realRun = love.run
219
 
function love.run()
220
 
   -- should fail silently if it can't go to fullscreen...
221
 
   love.graphics.toggleFullscreen()
222
 
 
223
 
   realRun()
224
 
end
 
 
b'\\ No newline at end of file'