/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-26 01:11:27 UTC
  • Revision ID: josh@9ix.org-20130626011127-7e7z5ed0r0xmtva3
show planet labels, get better names

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'
21
25
 
22
26
util = {
23
27
   signOf = function(value)
27
31
                  return -1
28
32
               end
29
33
            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
73
34
}
74
35
 
75
36
GameView = View:extend {
76
37
   onNew = function (self)
77
 
              the.storage = Storage:new{filename = 'scores.lua'}
 
38
              the.storage = Storage:new{filename = 'world.lua'}
78
39
              the.storage:load()
79
40
              --if not the.storage.data.highScore then
80
41
              --   print('initializing storage')
84
45
              the.bullets = Group:new()
85
46
              the.interface = Group:new()
86
47
              the.planets = Group:new()
 
48
              the.planetLabels = Group:new()
87
49
              the.indicators = Group:new()
 
50
              the.enemies = Group:new()
88
51
 
 
52
              -- init bg before build/load since planets need to know bg size
89
53
              the.bg = Tile:new{
90
54
                 image = 'data/stars3.png',
91
 
                 width = 13660,
92
 
                 height = 7680
 
55
                 width = 27320,
 
56
                 height = 15360
93
57
              }
94
58
              self:add(the.bg)
95
59
 
 
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
 
96
106
              self:add(the.planets)
 
107
              self:add(the.planetLabels)
97
108
 
98
 
              --the.player = CrystalPlayer:new{x=400,y=300}
99
 
              the.player = SpacePlayer:new{x=the.bg.width / 2, y=the.bg.height / 2}
100
109
              self:add(the.player)
101
110
              self:add(the.player.thrust)
102
 
 
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?
 
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
107
123
 
108
124
              self:add(the.bullets)
109
125
              self:add(the.indicators)
110
126
              self:add(the.interface)
111
127
 
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
128
              the.cursor = Cursor:new()
124
129
              self:add(the.cursor)
125
130
 
130
135
              self.focus = the.player
131
136
           end,
132
137
   onUpdate = function(self, dt)
 
138
                 if the.keys:justPressed('escape') then
 
139
                    PauseView:new():activate()
 
140
                 end
 
141
 
133
142
                 the.bullets:collide(the.planets)
 
143
                 the.bullets:collide(the.player)
 
144
                 the.bullets:collide(the.enemies)
134
145
              end,
135
146
   onEndFrame = function(self)
136
147
                   the.interface.translate.x = the.player.x - the.app.width / 2 + the.player.width / 2
174
185
                 self.console:watch('num mirrors', '#the.mirrors.sprites')
175
186
                 self.console:watch('num rocks', '#the.rocks.sprites')
176
187
                 self.console:watch('num planets', '#the.planets.sprites')
 
188
                 self.console:watch('num enemies', 'the.enemies:count()')
177
189
                 --self.console:watch('drawTook', 'the.drawTook')
178
190
 
179
191
                 -- back off that dark overlay a bit
182
194
           end,
183
195
   onUpdate = function (self, dt)
184
196
                 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
 
197
                    if the.keys:justPressed('return') and the.keys:pressed('alt') then
 
198
                       love.graphics.toggleFullscreen()
193
199
                    elseif the.keys:justPressed('f1') then
194
200
                       local ss = love.graphics.newScreenshot()
195
201
                       ss:encode('screenshot-' ..love.timer.getTime()..'.png')