/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
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
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 not self.name do
                 local name = PlanetNames[math.random(#PlanetNames)]
                 local inUse = false
                 for _, planet in ipairs(the.planets.sprites) do
                    if name == planet.name then inUse = true end
                 end

                 if not inUse then
                    self.name = name
                 end
              end

              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

              self.label = Text:new {
                 text = self.name,
                 x = self.x,
                 y = self.y - 32,
                 width = self.width,
                 align = 'center',
                 font = 20
              }
              the.planetLabels:add(self.label)

              self.keyLabel = Text:new {
                 text = 'Press L to land',
                 x = self.x,
                 y = self.y + self.height + 12,
                 width = self.width,
                 align = 'center',
                 font = 20
              }
              the.planetLabels:add(self.keyLabel)
           end,
   onUpdate = function (self)
                 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
                    self.label.visible = true
                    self.keyLabel.visible = true
                    the.player.onPlanet = self

                    if the.keys:justPressed('l') 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
                                               }
                       the.storage:save()

                       tradeView = TradeView:new{ planet = self }
                       tradeView:activate()
                    end
                 else
                    self.label.visible = false
                    self.keyLabel.visible = false
                    if the.player.onPlanet == self then
                       the.player.onPlanet = false
                    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
}