/traderous

To get this branch, use:
bzr branch http://9ix.org/bzr/traderous

« back to all changes in this revision

Viewing changes to planet.lua

  • Committer: Josh C
  • Date: 2013-05-13 21:25:26 UTC
  • Revision ID: josh@9ix.org-20130513212526-3q6md6n9xzhcmoa6
and that fixes the corner bug

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Planet = Tile:extend {
2
 
   image = 'data/planet1.png',
3
 
   potentialGoods = {
4
 
      {name = 'Bobble-Headed Dolls',
5
 
       low = 50, med = 100, high = 150,
6
 
       chance = 0.5},
7
 
      {name = 'Alliance Foodstuffs',
8
 
       low = 100, med = 250, high = 400,
9
 
       chance = 0.8},
10
 
      {name = 'Antique Weapons',
11
 
       low = 5000, med = 6000, high = 8000,
12
 
       chance = 0.1}
13
 
   },
14
 
   goods = {},
15
 
   onNew = function (self)
16
 
              self.indicator = Tile:new{ image = 'data/planet1ind.png' }
17
 
              the.indicators:add(self.indicator)
18
 
 
19
 
              while not self.name do
20
 
                 local name = PlanetNames[math.random(#PlanetNames)]
21
 
                 local inUse = false
22
 
                 for _, planet in ipairs(the.planets.sprites) do
23
 
                    if name == planet.name then inUse = true end
24
 
                 end
25
 
 
26
 
                 if not inUse then
27
 
                    self.name = name
28
 
                 end
29
 
              end
30
 
 
31
 
              while #self.goods == 0 do
32
 
                 for _, good in ipairs(self.potentialGoods) do
33
 
                    if math.random() < good.chance then
34
 
                       local tier = math.random(4) -- betw 1 and 4
35
 
                       if tier == 1 then
36
 
                          table.insert(self.goods, {good.name, good.low})
37
 
                       elseif tier ==2 then
38
 
                          table.insert(self.goods, {good.name, good.high})
39
 
                       else
40
 
                          -- double chance for med (3 or 4)
41
 
                          table.insert(self.goods, {good.name, good.med})
42
 
                       end
43
 
                    end
44
 
                 end
45
 
              end
46
 
 
47
 
              self.label = Text:new {
48
 
                 text = self.name,
49
 
                 x = self.x,
50
 
                 y = self.y - 32,
51
 
                 width = self.width,
52
 
                 align = 'center',
53
 
                 font = 20
54
 
              }
55
 
              the.planetLabels:add(self.label)
56
 
 
57
 
              self.keyLabel = Text:new {
58
 
                 text = 'Press L to land',
59
 
                 x = self.x,
60
 
                 y = self.y + self.height + 12,
61
 
                 width = self.width,
62
 
                 align = 'center',
63
 
                 font = 20
64
 
              }
65
 
              the.planetLabels:add(self.keyLabel)
66
 
           end,
67
 
   onUpdate = function (self)
68
 
                 local hw = self.width / 2
69
 
                 local pvec = vector.new(the.player.x - (self.x + hw),
70
 
                                         the.player.y - (self.y + hw))
71
 
 
72
 
                 if pvec:len2() < hw ^ 2 then
73
 
                    self.label.visible = true
74
 
                    self.keyLabel.visible = true
75
 
                    the.player.onPlanet = self
76
 
 
77
 
                    if the.keys:justPressed('l') then
78
 
                       the.player.velocity = {x=0, y=0}
79
 
                       the.player.acceleration = {x=0, y=0}
80
 
 
81
 
                       -- save player data
82
 
                       the.storage.data.player = {x = the.player.x,
83
 
                                                  y = the.player.y,
84
 
                                                  money = the.player.money,
85
 
                                                  goods = the.player.goods,
86
 
                                                  cargoSpace = the.player.cargoSpace
87
 
                                               }
88
 
                       the.storage:save()
89
 
 
90
 
                       tradeView = TradeView:new{ planet = self }
91
 
                       tradeView:activate()
92
 
                    end
93
 
                 else
94
 
                    self.label.visible = false
95
 
                    self.keyLabel.visible = false
96
 
                    if the.player.onPlanet == self then
97
 
                       the.player.onPlanet = false
98
 
                    end
99
 
                 end
100
 
              end,
101
 
   onEndFrame = function (self)
102
 
                   local indx, indy
103
 
                   local pvec = vector.new(
104
 
                      self.x - the.player.x + self.width / 2,
105
 
                      self.y - the.player.y + self.height / 2 )
106
 
 
107
 
                   -- TODO: is there a better way to specify the
108
 
                   -- screen rectangle?
109
 
                   if self:intersects(the.player.x - the.app.width / 2,
110
 
                                      the.player.y - the.app.height / 2,
111
 
                                      the.app.width,
112
 
                                      the.app.height) then
113
 
                      -- planet is on the screen
114
 
                      self.indicator.visible = false
115
 
                   else
116
 
                      self.indicator.visible = true
117
 
 
118
 
                      if math.abs(pvec.x) / math.abs(pvec.y) > the.app.width / the.app.height then
119
 
                         indx = (the.app.width / 2 - 10) * util.signOf(pvec.x) + 8
120
 
                         indy = (the.app.width / 2 - 10) * pvec.y / math.abs(pvec.x)
121
 
                      else
122
 
                         indy = (the.app.height / 2 - 10) * util.signOf(pvec.y) + 8
123
 
                         indx = (the.app.height / 2 - 10) * pvec.x / math.abs(pvec.y)
124
 
                      end
125
 
 
126
 
                      self.indicator.x = the.player.x + indx
127
 
                      self.indicator.y = the.player.y + indy
128
 
                   end
129
 
 
130
 
                end,
131
 
   onCollide = function (self, other)
132
 
                  if other:instanceOf(Bullet) then
133
 
                     the.bullets:remove(other)
134
 
                     other:die()
135
 
                  end
136
 
               end
137
 
}
 
 
b'\\ No newline at end of file'