/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-20 17:57:53 UTC
  • Revision ID: josh@9ix.org-20130520175753-yvanutc9jnm8lzbt
quit and restart

Show diffs side-by-side

added added

removed removed

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