/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-06-15 23:20:49 UTC
  • Revision ID: josh@9ix.org-20130615232049-foxrdwozglvama5w
close button

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
require 'boom'
19
19
require 'planet'
20
20
require 'trade_view'
21
 
require 'shield'
22
 
require 'pause_view'
23
 
require 'game_over'
24
 
require 'names'
25
21
 
26
22
util = {
27
23
   signOf = function(value)
31
27
                  return -1
32
28
               end
33
29
            end,
 
30
   shortestVector = function(from, to)
 
31
                       if STRICT then
 
32
                          if from.x < the.app.width / 2 or
 
33
                             from.x > the.bg.width - the.app.width / 2 or
 
34
                             from.y < the.app.height / 2 or
 
35
                             from.y > the.bg.height - the.app.height / 2 then
 
36
                             error('"from" coordinate out of bounds: X='..from.x..' Y='..from.y)
 
37
                          end
 
38
 
 
39
                          if to.x < the.app.width / 2 or
 
40
                             to.x > the.bg.width - the.app.width / 2 or
 
41
                             to.y < the.app.height / 2 or
 
42
                             to.y > the.bg.height - the.app.height / 2 then
 
43
                             error('"to" coordinate out of bounds: X='..to.x..' Y='..to.y)
 
44
                          end
 
45
                       end
 
46
 
 
47
                       -- normalize grid to account for mirror zones
 
48
                       local fx = from.x - the.app.width / 2
 
49
                       local fy = from.y - the.app.height / 2
 
50
                       local tx = to.x - the.app.width / 2
 
51
                       local ty = to.y - the.app.height / 2
 
52
 
 
53
                       local short = {}
 
54
 
 
55
                       -- pick shorter x
 
56
                       if math.abs(tx - fx) < math.abs(tx - fx - (the.bg.width - the.app.width)) then
 
57
                          -- straight path is shorter
 
58
                          short.x = tx - fx
 
59
                       else
 
60
                          short.x = tx - fx - (the.bg.width - the.app.width)
 
61
                       end
 
62
 
 
63
                       -- pick shorter y
 
64
                       if math.abs(ty - fy) < math.abs(ty - fy - (the.bg.height - the.app.height)) then
 
65
                          -- straight path is shorter
 
66
                          short.y = ty - fy
 
67
                       else
 
68
                          short.y = ty - fy - (the.bg.height - the.app.height)
 
69
                       end
 
70
 
 
71
                       return vector.new(short.x, short.y)
 
72
                    end
34
73
}
35
74
 
36
75
GameView = View:extend {
 
76
   lastRock = 0,
 
77
   rockInterval = 1,
 
78
   gameStart = 0,
37
79
   onNew = function (self)
38
 
              the.storage = Storage:new{filename = 'world.lua'}
 
80
              -- for x = 1,30 do
 
81
              --    for y = 1,30 do
 
82
              --       self:add(Fill:new{x=x*400, y=y*400,
 
83
              --                         width = 32, height = 32,
 
84
              --                         fill = {0,0,255}
 
85
              --                      })
 
86
              --    end
 
87
              -- end
 
88
 
 
89
              the.storage = Storage:new{filename = 'scores.lua'}
39
90
              the.storage:load()
40
 
              --if not the.storage.data.highScore then
41
 
              --   print('initializing storage')
42
 
              --   the.storage.data = {highScore = 0}
43
 
              --end
 
91
              if not the.storage.data.highScore then
 
92
                 print('initializing storage')
 
93
                 the.storage.data = {highScore = 0}
 
94
              end
44
95
 
 
96
              --the.rockColliders = Group:new()
45
97
              the.bullets = Group:new()
 
98
              the.mirrors = Group:new()
 
99
              --the.rocks = Group:new()
46
100
              the.interface = Group:new()
47
101
              the.planets = Group:new()
48
 
              the.planetLabels = Group:new()
49
102
              the.indicators = Group:new()
50
 
              the.enemies = Group:new()
51
103
 
52
 
              -- init bg before build/load since planets need to know bg size
53
104
              the.bg = Tile:new{
54
105
                 image = 'data/stars3.png',
55
 
                 width = 27320,
56
 
                 height = 15360
 
106
                 width = 13660,
 
107
                 height = 7680
57
108
              }
58
109
              self:add(the.bg)
59
110
 
60
 
              if self.newWorld or not the.storage.data.player then
61
 
                 the.storage.data = {planets = {}}
62
 
 
63
 
                 -- build planets from random
64
 
                 for _ = 1, math.random(3, 6) do
65
 
                    local planet = Planet:new{
66
 
                       x = math.random(the.app.width / 2,
67
 
                                       the.bg.width - the.app.width / 2),
68
 
                       y = math.random(the.app.height / 2,
69
 
                                       the.bg.height - the.app.height / 2),
70
 
                       rotation = math.random() * math.pi
71
 
                    }
72
 
                    the.planets:add(planet)
73
 
                    table.insert(the.storage.data.planets, {
74
 
                                    x = planet.x,
75
 
                                    y = planet.y,
76
 
                                    rotation = planet.rotation,
77
 
                                    goods = planet.goods,
78
 
                                    name = planet.name
79
 
                                 })
80
 
                 end
81
 
 
82
 
                 -- build fresh player
83
 
                 local player = SpacePlayer:new{x=the.bg.width / 2, y=the.bg.height / 2}
84
 
                 the.player = player
85
 
                 the.storage.data.player = {x = player.x,
86
 
                                            y = player.y,
87
 
                                            money = player.money,
88
 
                                            goods = player.goods,
89
 
                                            cargoSpace = player.cargoSpace
90
 
                                         }
91
 
 
92
 
                 the.storage:save()
93
 
              else
94
 
                 -- load planets with x, y, goods
95
 
                 for _, planetData in ipairs(the.storage.data.planets) do
96
 
                    the.planets:add(Planet:new(planetData))
97
 
                 end
98
 
 
99
 
                 -- load player with cargo, money, position
100
 
                 the.player = SpacePlayer:new(the.storage.data.player)
101
 
 
102
 
                 -- reload storage as we've turned it all into objects
103
 
                 the.storage:load()
104
 
              end
105
 
 
106
111
              self:add(the.planets)
107
 
              self:add(the.planetLabels)
108
112
 
 
113
              --the.player = CrystalPlayer:new{x=400,y=300}
 
114
              the.player = SpacePlayer:new{x=the.bg.width / 2, y=the.bg.height / 2}
109
115
              self:add(the.player)
110
116
              self:add(the.player.thrust)
111
 
              self:add(the.player.shield)
112
 
 
113
 
              self:add(the.enemies)
114
 
 
115
 
              for _ = 1, 20 do
116
 
                 local e = Enemy:new{x = math.random(the.bg.width),
117
 
                                     y = math.random(the.bg.height)}
118
 
                 --local e = Enemy:new{x=the.bg.width / 2, y=the.bg.height / 2}
119
 
                 the.enemies:add(e)
120
 
                 self:add(e.thrust) -- why doesn't this work in Enemy.new?
121
 
                 self:add(e.shield)
122
 
              end
 
117
 
 
118
              --self:add(Enemy:new{x=400, y=300})
123
119
 
124
120
              self:add(the.bullets)
 
121
              --self:add(the.rockColliders)
 
122
              self:add(the.mirrors)
 
123
              --self:add(the.rocks)
125
124
              self:add(the.indicators)
126
125
              self:add(the.interface)
127
126
 
 
127
              for _ = 1, math.random(6) do
 
128
                 local planet = Planet:new{
 
129
                    x = math.random(the.app.width / 2,
 
130
                                    the.bg.width - the.app.width / 2),
 
131
                    y = math.random(the.app.height / 2,
 
132
                                    the.bg.height - the.app.height / 2),
 
133
                    rotation = math.random() * math.pi
 
134
                 }
 
135
                 the.planets:add(planet)
 
136
              end
 
137
 
128
138
              the.cursor = Cursor:new()
129
139
              self:add(the.cursor)
130
140
 
 
141
              the.score = Text:new{
 
142
                 x = 8,
 
143
                 y = 8,
 
144
                 width = the.app.width,
 
145
                 --align = 'center',
 
146
                 font = 25}
 
147
              --the.interface:add(the.score)
 
148
 
 
149
              local hs = the.storage.data.highScore
 
150
              local m = hs / 60
 
151
              local s = hs % 60
 
152
 
 
153
              the.highScore = Text:new{
 
154
                 x = -8,
 
155
                 y = 8,
 
156
                 width = the.app.width,
 
157
                 align = 'right',
 
158
                 font = 25,
 
159
                 text = string.format('High Score: %d:%02d', m, s)
 
160
              }
 
161
              --the.interface:add(the.highScore)
 
162
 
 
163
              the.over = Text:new{
 
164
                 y = the.app.height / 2,
 
165
                 width = the.app.width,
 
166
                 align = 'center',
 
167
                 font = 25,
 
168
                 text = "Game Over",
 
169
                 visible = false
 
170
              }
 
171
              the.interface:add(the.over)
 
172
 
 
173
 
 
174
              the.instructions = Text:new{
 
175
                 y = the.app.height / 2 + 32,
 
176
                 width = the.app.width,
 
177
                 align = 'center',
 
178
                 font = 12,
 
179
                 text = "Press Enter to start a new game\nPress Q to quit",
 
180
                 visible = false
 
181
              }
 
182
              the.interface:add(the.instructions)
 
183
 
131
184
              love.mouse.setGrab(true)
132
185
              love.mouse.setVisible(false)
133
186
              love.mouse.setPosition(the.app.width / 2, the.app.height / 2)
134
187
 
 
188
              --self:loadLayers('data/map.lua')
135
189
              self.focus = the.player
 
190
              --self:clampTo(self.map)
 
191
 
 
192
              self.gameStart = love.timer.getTime()
136
193
           end,
137
194
   onUpdate = function(self, dt)
138
 
                 if the.keys:justPressed('escape') then
139
 
                    PauseView:new():activate()
140
 
                 end
141
 
 
142
 
                 the.bullets:collide(the.planets)
143
 
                 the.bullets:collide(the.player)
144
 
                 the.bullets:collide(the.enemies)
 
195
                 if false and the.player.active and love.timer.getTime() > self.lastRock + self.rockInterval then
 
196
                    local unseenRock = nil
 
197
                    while not unseenRock do
 
198
                       local rock = Rock:new{
 
199
                          x = math.random(the.app.width / 2,
 
200
                                          the.bg.width - the.app.width / 2),
 
201
                          y = math.random(the.app.height / 2,
 
202
                                          the.bg.height - the.app.height / 2),
 
203
                          velocity = {
 
204
                             x = math.random(-300, 300),
 
205
                             y = math.random(-300, 300),
 
206
                             rotation = math.random(-7, 7)
 
207
                          },
 
208
                          scale = math.random() + 0.5
 
209
                       }
 
210
 
 
211
                       local rockToPlayer = util.shortestVector(rock, the.player)
 
212
                       if math.abs(rockToPlayer.x) > the.app.width / 2 + rock.width * rock.scale and
 
213
                           math.abs(rockToPlayer.y) > the.app.height / 2 + rock.height * rock.scale then
 
214
                         unseenRock = rock
 
215
                        end
 
216
                    end
 
217
 
 
218
                    the.rocks:add(unseenRock)
 
219
 
 
220
                    self.lastRock = love.timer.getTime()
 
221
                 end
 
222
 
 
223
                 the.bullets:collide(the.rockColliders)
 
224
 
 
225
                 -- this should really go somewhere else...
 
226
                 for _, planet in ipairs(the.planets.sprites) do
 
227
                    local indx, indy
 
228
                    local pvec = vector.new(
 
229
                       planet.x - the.player.x + planet.width / 2,
 
230
                       planet.y - the.player.y + planet.height / 2 )
 
231
 
 
232
                    -- TODO: is there a better way to specify the
 
233
                    -- screen rectangle?
 
234
                    if planet:intersects(the.player.x - the.app.width / 2,
 
235
                                         the.player.y - the.app.height / 2,
 
236
                                         the.app.width,
 
237
                                         the.app.height) then
 
238
                       -- planet is on the screen
 
239
                       planet.indicator.visible = false
 
240
                    else
 
241
                       planet.indicator.visible = true
 
242
 
 
243
                       if math.abs(pvec.x) / math.abs(pvec.y) > the.app.width / the.app.height then
 
244
                          indx = (the.app.width / 2 - 10) * util.signOf(pvec.x) + 8
 
245
                          indy = (the.app.width / 2 - 10) * pvec.y / math.abs(pvec.x)
 
246
                       else
 
247
                          indy = (the.app.height / 2 - 10) * util.signOf(pvec.y) + 8
 
248
                          indx = (the.app.height / 2 - 10) * pvec.x / math.abs(pvec.y)
 
249
                       end
 
250
 
 
251
                       planet.indicator.x = the.player.x + indx
 
252
                       planet.indicator.y = the.player.y + indy
 
253
                    end
 
254
                 end
 
255
 
 
256
                 -- for _, mirror in ipairs(the.mirrors.sprites) do
 
257
                 --    if not mirror.of then
 
258
                 --       print('mirror:' .. inspect(mirror))
 
259
                 --       error('mirror OF NOTHING')
 
260
                 --    end
 
261
                 -- end
145
262
              end,
146
263
   onEndFrame = function(self)
147
264
                   the.interface.translate.x = the.player.x - the.app.width / 2 + the.player.width / 2
148
265
                   the.interface.translate.y = the.player.y - the.app.height / 2 + the.player.height / 2
 
266
 
 
267
                   if the.player.active then
 
268
                      self:updateScore()
 
269
                   end
149
270
                end,
150
271
   draw = function (self, x, y)
151
272
             View.draw(self, x, y)
152
273
             --love.graphics.print('FPS:' .. love.timer.getFPS(), 20, 20)
153
274
          end,
 
275
   updateScore = function(self)
 
276
                    local t = love.timer.getTime() - self.gameStart
 
277
                    local m = t / 60
 
278
                    local s = t % 60
 
279
 
 
280
                    the.score.text = string.format('Score: %d:%02d', m, s)
 
281
                    --the.score.y = the.player.y - the.app.height / 2 + the.player.height
 
282
                    --the.score.x = the.player.x - the.app.width / 2 + the.player.width
 
283
 
 
284
                    --the.highScore.y = the.player.y - the.app.height / 2 + the.player.height
 
285
                    --the.highScore.x = the.player.x - the.app.width / 2
 
286
                 end
154
287
}
155
288
 
156
289
MenuScreen = View:extend {
185
318
                 self.console:watch('num mirrors', '#the.mirrors.sprites')
186
319
                 self.console:watch('num rocks', '#the.rocks.sprites')
187
320
                 self.console:watch('num planets', '#the.planets.sprites')
188
 
                 self.console:watch('num enemies', 'the.enemies:count()')
189
 
                 self.console:watch('onPlanet', 'the.player.onPlanet')
190
321
                 --self.console:watch('drawTook', 'the.drawTook')
191
322
 
192
323
                 -- back off that dark overlay a bit
195
326
           end,
196
327
   onUpdate = function (self, dt)
197
328
                 if not (DEBUG and the.console.visible) then
198
 
                    if the.keys:justPressed('return') and the.keys:pressed('alt') then
199
 
                       love.graphics.toggleFullscreen()
 
329
                    if the.keys:justPressed('q') then
 
330
                       self.quit()
 
331
                    elseif the.keys:justPressed('return') then
 
332
                       if the.keys:pressed('alt') then
 
333
                          love.graphics.toggleFullscreen()
 
334
                       else
 
335
                          self.view = GameView:new()
 
336
                       end
200
337
                    elseif the.keys:justPressed('f1') then
201
338
                       local ss = love.graphics.newScreenshot()
202
339
                       ss:encode('screenshot-' ..love.timer.getTime()..'.png')