/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-25 19:56:26 UTC
  • Revision ID: josh@9ix.org-20130625195626-00x63mxdyk1k6ud7
last fix for saving

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
GameView = View:extend {
35
35
   onNew = function (self)
36
 
              the.storage = Storage:new{filename = 'scores.lua'}
 
36
              the.storage = Storage:new{filename = 'world.lua'}
37
37
              the.storage:load()
38
38
              --if not the.storage.data.highScore then
39
39
              --   print('initializing storage')
46
46
              the.indicators = Group:new()
47
47
              the.enemies = Group:new()
48
48
 
 
49
              -- init bg before build/load since planets need to know bg size
49
50
              the.bg = Tile:new{
50
51
                 image = 'data/stars3.png',
51
52
                 width = 27320,
53
54
              }
54
55
              self:add(the.bg)
55
56
 
 
57
              if self.newWorld or not the.storage.data.player then
 
58
                 the.storage.data = {planets = {}}
 
59
 
 
60
                 -- build planets from random
 
61
                 for _ = 1, math.random(3, 6) do
 
62
                    local planet = Planet:new{
 
63
                       x = math.random(the.app.width / 2,
 
64
                                       the.bg.width - the.app.width / 2),
 
65
                       y = math.random(the.app.height / 2,
 
66
                                       the.bg.height - the.app.height / 2),
 
67
                       rotation = math.random() * math.pi
 
68
                    }
 
69
                    the.planets:add(planet)
 
70
                    table.insert(the.storage.data.planets, {
 
71
                                    x = planet.x,
 
72
                                    y = planet.y,
 
73
                                    rotation = planet.rotation,
 
74
                                    goods = planet.goods
 
75
                                 })
 
76
                 end
 
77
 
 
78
                 -- build fresh player
 
79
                 local player = SpacePlayer:new{x=the.bg.width / 2, y=the.bg.height / 2}
 
80
                 the.player = player
 
81
                 the.storage.data.player = {x = player.x,
 
82
                                            y = player.y,
 
83
                                            money = player.money,
 
84
                                            goods = player.goods,
 
85
                                            cargoSpace = player.cargoSpace
 
86
                                         }
 
87
 
 
88
                 the.storage:save()
 
89
              else
 
90
                 -- load planets with x, y, goods
 
91
                 for _, planetData in ipairs(the.storage.data.planets) do
 
92
                    the.planets:add(Planet:new(planetData))
 
93
                 end
 
94
 
 
95
                 -- load player with cargo, money, position
 
96
                 the.player = SpacePlayer:new(the.storage.data.player)
 
97
 
 
98
                 -- reload storage as we've turned it all into objects
 
99
                 the.storage:load()
 
100
              end
 
101
 
56
102
              self:add(the.planets)
57
103
 
58
 
              --the.player = CrystalPlayer:new{x=400,y=300}
59
 
              the.player = SpacePlayer:new{x=the.bg.width / 2, y=the.bg.height / 2}
60
104
              self:add(the.player)
61
105
              self:add(the.player.thrust)
62
106
              self:add(the.player.shield)
76
120
              self:add(the.indicators)
77
121
              self:add(the.interface)
78
122
 
79
 
              for _ = 1, math.random(3, 6) do
80
 
                 local planet = Planet:new{
81
 
                    x = math.random(the.app.width / 2,
82
 
                                    the.bg.width - the.app.width / 2),
83
 
                    y = math.random(the.app.height / 2,
84
 
                                    the.bg.height - the.app.height / 2),
85
 
                    rotation = math.random() * math.pi
86
 
                 }
87
 
                 the.planets:add(planet)
88
 
              end
89
 
 
90
123
              the.cursor = Cursor:new()
91
124
              self:add(the.cursor)
92
125
 
168
201
                 self.console:watch('num mirrors', '#the.mirrors.sprites')
169
202
                 self.console:watch('num rocks', '#the.rocks.sprites')
170
203
                 self.console:watch('num planets', '#the.planets.sprites')
 
204
                 self.console:watch('num enemies', 'the.enemies:count()')
171
205
                 --self.console:watch('drawTook', 'the.drawTook')
172
206
 
173
207
                 -- back off that dark overlay a bit