/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
Planet = Tile:extend {
   image = 'data/planet1.png',
   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

              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}

                       the.player:save()

                       -- disengage all the enemies
                       for _, enemy in pairs(the.enemies.sprites) do
                          if enemy.state == 'combat' then
                             enemy.state = 'patrolling'
                          end
                       end

                       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
}