/traderous

To get this branch, use:
bzr branch /bzr/traderous
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
STRICT = true
DEBUG = true

require 'zoetrope'
vector = require 'vector'
--inspect = require 'inspect'

require 'group'

require 'version'
require 'wrap_tile'
require 'mirror'
require 'player'
require 'enemy'
require 'cursor'
require 'bullet'
require 'rock'
require 'boom'
require 'planet'
require 'trade_view'

util = {
   signOf = function(value)
               if value >= 0 then
                  return 1
               else
                  return -1
               end
            end,
   shortestVector = function(from, to)
                       if STRICT then
                          if from.x < the.app.width / 2 or
                             from.x > the.bg.width - the.app.width / 2 or
                             from.y < the.app.height / 2 or
                             from.y > the.bg.height - the.app.height / 2 then
                             error('"from" coordinate out of bounds: X='..from.x..' Y='..from.y)
                          end

                          if to.x < the.app.width / 2 or
                             to.x > the.bg.width - the.app.width / 2 or
                             to.y < the.app.height / 2 or
                             to.y > the.bg.height - the.app.height / 2 then
                             error('"to" coordinate out of bounds: X='..to.x..' Y='..to.y)
                          end
                       end

                       -- normalize grid to account for mirror zones
                       local fx = from.x - the.app.width / 2
                       local fy = from.y - the.app.height / 2
                       local tx = to.x - the.app.width / 2
                       local ty = to.y - the.app.height / 2

                       local short = {}

                       -- pick shorter x
                       if math.abs(tx - fx) < math.abs(tx - fx - (the.bg.width - the.app.width)) then
                          -- straight path is shorter
                          short.x = tx - fx
                       else
                          short.x = tx - fx - (the.bg.width - the.app.width)
                       end

                       -- pick shorter y
                       if math.abs(ty - fy) < math.abs(ty - fy - (the.bg.height - the.app.height)) then
                          -- straight path is shorter
                          short.y = ty - fy
                       else
                          short.y = ty - fy - (the.bg.height - the.app.height)
                       end

                       return vector.new(short.x, short.y)
                    end
}

GameView = View:extend {
   lastRock = 0,
   rockInterval = 1,
   gameStart = 0,
   onNew = function (self)
              -- for x = 1,30 do
              --    for y = 1,30 do
              --       self:add(Fill:new{x=x*400, y=y*400,
              --                         width = 32, height = 32,
              --                         fill = {0,0,255}
              --                      })
              --    end
              -- end

              the.storage = Storage:new{filename = 'scores.lua'}
              the.storage:load()
              if not the.storage.data.highScore then
                 print('initializing storage')
                 the.storage.data = {highScore = 0}
              end

              --the.rockColliders = Group:new()
              the.bullets = Group:new()
              the.mirrors = Group:new()
              --the.rocks = Group:new()
              the.interface = Group:new()
              the.planets = Group:new()
              the.indicators = Group:new()

              the.bg = Tile:new{
                 image = 'data/stars3.png',
                 width = 13660,
                 height = 7680
              }
              self:add(the.bg)

              self:add(the.planets)

              --the.player = CrystalPlayer:new{x=400,y=300}
              the.player = SpacePlayer:new{x=the.bg.width / 2, y=the.bg.height / 2}
              self:add(the.player)
              self:add(the.player.thrust)

              --self:add(Enemy:new{x=400, y=300})

              self:add(the.bullets)
              --self:add(the.rockColliders)
              self:add(the.mirrors)
              --self:add(the.rocks)
              self:add(the.indicators)
              self:add(the.interface)

              for _ = 1, math.random(6) do
                 local planet = Planet:new{
                    x = math.random(the.app.width / 2,
                                    the.bg.width - the.app.width / 2),
                    y = math.random(the.app.height / 2,
                                    the.bg.height - the.app.height / 2),
                    rotation = math.random() * math.pi
                 }
                 the.planets:add(planet)
              end

              the.cursor = Cursor:new()
              self:add(the.cursor)

              the.score = Text:new{
                 x = 8,
                 y = 8,
                 width = the.app.width,
                 --align = 'center',
                 font = 25}
              --the.interface:add(the.score)

              local hs = the.storage.data.highScore
              local m = hs / 60
              local s = hs % 60

              the.highScore = Text:new{
                 x = -8,
                 y = 8,
                 width = the.app.width,
                 align = 'right',
                 font = 25,
                 text = string.format('High Score: %d:%02d', m, s)
              }
              --the.interface:add(the.highScore)

              the.over = Text:new{
                 y = the.app.height / 2,
                 width = the.app.width,
                 align = 'center',
                 font = 25,
                 text = "Game Over",
                 visible = false
              }
              the.interface:add(the.over)


              the.instructions = Text:new{
                 y = the.app.height / 2 + 32,
                 width = the.app.width,
                 align = 'center',
                 font = 12,
                 text = "Press Enter to start a new game\nPress Q to quit",
                 visible = false
              }
              the.interface:add(the.instructions)

              love.mouse.setGrab(true)
              love.mouse.setVisible(false)
              love.mouse.setPosition(the.app.width / 2, the.app.height / 2)

              --self:loadLayers('data/map.lua')
              self.focus = the.player
              --self:clampTo(self.map)

              self.gameStart = love.timer.getTime()
           end,
   onUpdate = function(self, dt)
                 if false and the.player.active and love.timer.getTime() > self.lastRock + self.rockInterval then
                    local unseenRock = nil
                    while not unseenRock do
                       local rock = Rock:new{
                          x = math.random(the.app.width / 2,
                                          the.bg.width - the.app.width / 2),
                          y = math.random(the.app.height / 2,
                                          the.bg.height - the.app.height / 2),
                          velocity = {
                             x = math.random(-300, 300),
                             y = math.random(-300, 300),
                             rotation = math.random(-7, 7)
                          },
                          scale = math.random() + 0.5
                       }

                       local rockToPlayer = util.shortestVector(rock, the.player)
                       if math.abs(rockToPlayer.x) > the.app.width / 2 + rock.width * rock.scale and
                           math.abs(rockToPlayer.y) > the.app.height / 2 + rock.height * rock.scale then
                         unseenRock = rock
                        end
                    end

                    the.rocks:add(unseenRock)

                    self.lastRock = love.timer.getTime()
                 end

                 the.bullets:collide(the.rockColliders)

                 -- this should really go somewhere else...
                 for _, planet in ipairs(the.planets.sprites) do
                    local indx, indy
                    local pvec = vector.new(
                       planet.x - the.player.x + planet.width / 2,
                       planet.y - the.player.y + planet.height / 2 )

                    -- TODO: is there a better way to specify the
                    -- screen rectangle?
                    if planet:intersects(the.player.x - the.app.width / 2,
                                         the.player.y - the.app.height / 2,
                                         the.app.width,
                                         the.app.height) then
                       -- planet is on the screen
                       planet.indicator.visible = false
                    else
                       planet.indicator.visible = true

                       if math.abs(pvec.x) / math.abs(pvec.y) > the.app.width / the.app.height then
                          indx = (the.app.width / 2 - 10) * util.signOf(pvec.x) + 8
                          indy = (the.app.width / 2 - 10) * pvec.y / math.abs(pvec.x)
                       else
                          indy = (the.app.height / 2 - 10) * util.signOf(pvec.y) + 8
                          indx = (the.app.height / 2 - 10) * pvec.x / math.abs(pvec.y)
                       end

                       planet.indicator.x = the.player.x + indx
                       planet.indicator.y = the.player.y + indy
                    end
                 end

                 -- for _, mirror in ipairs(the.mirrors.sprites) do
                 --    if not mirror.of then
                 --       print('mirror:' .. inspect(mirror))
                 --       error('mirror OF NOTHING')
                 --    end
                 -- end
              end,
   onEndFrame = function(self)
                   the.interface.translate.x = the.player.x - the.app.width / 2 + the.player.width / 2
                   the.interface.translate.y = the.player.y - the.app.height / 2 + the.player.height / 2

                   if the.player.active then
                      self:updateScore()
                   end
                end,
   draw = function (self, x, y)
             View.draw(self, x, y)
             --love.graphics.print('FPS:' .. love.timer.getFPS(), 20, 20)
          end,
   updateScore = function(self)
                    local t = love.timer.getTime() - self.gameStart
                    local m = t / 60
                    local s = t % 60

                    the.score.text = string.format('Score: %d:%02d', m, s)
                    --the.score.y = the.player.y - the.app.height / 2 + the.player.height
                    --the.score.x = the.player.x - the.app.width / 2 + the.player.width

                    --the.highScore.y = the.player.y - the.app.height / 2 + the.player.height
                    --the.highScore.x = the.player.x - the.app.width / 2
                 end
}

MenuScreen = View:extend {
   title = Text:new{text = "Press a key to start", font = 48, wordWrap = false},
   --title = Tile:new{image = 'data/title.png', x = 0, y = 0},
   onNew = function(self)
              self:add(self.title)
              self.title:centerAround(400, 200)
           end,
   onUpdate = function(self, elapsed)
                 if the.keys:allJustPressed() then
                    the.app.view = GameView:new()
                 end
              end
}

the.app = App:new {
   onRun = function (self)
              print('Version: ' .. VERSION)

              math.randomseed(os.time())

              self.view = GameView:new()

              if DEBUG then
                 self.console:watch('VERSION', 'VERSION')
                 self.console:watch('updateTook', 'the.updateTook')
                 self.console:watch('the.player.x', 'the.player.x')
                 self.console:watch('the.player.y', 'the.player.y')
                 self.console:watch('the.app.width', 'the.app.width')
                 self.console:watch('the.app.height', 'the.app.height')
                 self.console:watch('num mirrors', '#the.mirrors.sprites')
                 self.console:watch('num rocks', '#the.rocks.sprites')
                 self.console:watch('num planets', '#the.planets.sprites')
                 --self.console:watch('drawTook', 'the.drawTook')

                 -- back off that dark overlay a bit
                 self.console.fill.fill[4] = 75
              end
           end,
   onUpdate = function (self, dt)
                 if not (DEBUG and the.console.visible) then
                    if the.keys:justPressed('q') then
                       self.quit()
                    elseif the.keys:justPressed('return') then
                       if the.keys:pressed('alt') then
                          love.graphics.toggleFullscreen()
                       else
                          self.view = GameView:new()
                       end
                    elseif the.keys:justPressed('f1') then
                       local ss = love.graphics.newScreenshot()
                       ss:encode('screenshot-' ..love.timer.getTime()..'.png')
                    elseif the.keys:justPressed('f11') then
                       love.graphics.toggleFullscreen()
                    end
                 end
              end,
   update = function (self, dt)
               the.updateStart = love.timer.getMicroTime()
               App.update(self, dt)
               if the.updateStart then
                  the.updateTook = love.timer.getMicroTime() - the.updateStart
               end
            end
}

realRun = love.run
function love.run()
   -- should fail silently if it can't go to fullscreen...
   love.graphics.toggleFullscreen()

   realRun()
end