/traderous

To get this branch, use:
bzr branch http://9ix.org/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
Planet = Tile:extend {
   image = 'data/planet1.png',
   potentialGoods = {
      {name = 'Bobble-Headed Dolls',
       low = 50, med = 100, high = 150,
       chance = 0.5},
      {name = 'Alliance Foodstuffs',
       low = 100, med = 250, high = 400,
       chance = 0.8},
      {name = 'Antique Weapons',
       low = 5000, med = 6000, high = 8000,
       chance = 0.1}
   },
   goods = {},
   onNew = function (self)
              self.indicator = Tile:new{ image = 'data/planet1ind.png' }
              the.indicators:add(self.indicator)

              while #self.goods == 0 do
                 for _, good in ipairs(self.potentialGoods) do
                    if math.random() < good.chance then
                       local tier = math.random(4) -- betw 1 and 4
                       if tier == 1 then
                          table.insert(self.goods, {good.name, good.low})
                       elseif tier ==2 then
                          table.insert(self.goods, {good.name, good.high})
                       else
                          -- double chance for med (3 or 4)
                          table.insert(self.goods, {good.name, good.med})
                       end
                    end
                 end
              end
           end,
   onUpdate = function (self)
                 if the.keys:justPressed('l') then
                    local hw = self.width / 2
                    local pvec = vector.new(the.player.x - (self.x + hw),
                                            the.player.y - (self.y + hw))

                    if pvec:len2() < hw ^ 2 then
                       the.player.velocity = {x=0, y=0}
                       the.player.acceleration = {x=0, y=0}

                       -- save player data
                       the.storage.data.player = {x = the.player.x,
                                                  y = the.player.y,
                                                  money = the.player.money,
                                                  goods = the.player.goods,
                                                  cargoSpace = the.player.cargoSpace
                                               }
                       print('saving')
                       the.storage:save()
                       print('finished saving')

                       tradeView = TradeView:new{ planet = self }
                       tradeView:activate()
                    end
                 end
              end,
   onEndFrame = function (self)
                   local indx, indy
                   local pvec = vector.new(
                      self.x - the.player.x + self.width / 2,
                      self.y - the.player.y + self.height / 2 )

                   -- TODO: is there a better way to specify the
                   -- screen rectangle?
                   if self: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
                      self.indicator.visible = false
                   else
                      self.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

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

                end,
   onCollide = function (self, other)
                  if other:instanceOf(Bullet) then
                     the.bullets:remove(other)
                     other:die()
                  end
               end
}