/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-06-26 00:45:08 UTC
  • Revision ID: josh@9ix.org-20130626004508-6in13i6vjpu77els
give planets names

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
   potentialNames = {
 
15
      'Planet A',
 
16
      'Planet B',
 
17
      'Planet C',
 
18
      'Planet D',
 
19
      'Planet E',
 
20
      'Planet F',
 
21
   },
 
22
   goods = {},
 
23
   onNew = function (self)
 
24
              self.indicator = Tile:new{ image = 'data/planet1ind.png' }
 
25
              the.indicators:add(self.indicator)
 
26
 
 
27
              while not self.name do
 
28
                 local name = self.potentialNames[math.random(#self.potentialNames)]
 
29
                 local inUse = false
 
30
                 for _, planet in ipairs(the.planets.sprites) do
 
31
                    if name == planet.name then inUse = true end
 
32
                 end
 
33
 
 
34
                 if not inUse then
 
35
                    self.name = name
 
36
                 end
 
37
              end
 
38
 
 
39
              while #self.goods == 0 do
 
40
                 for _, good in ipairs(self.potentialGoods) do
 
41
                    if math.random() < good.chance then
 
42
                       local tier = math.random(4) -- betw 1 and 4
 
43
                       if tier == 1 then
 
44
                          table.insert(self.goods, {good.name, good.low})
 
45
                       elseif tier ==2 then
 
46
                          table.insert(self.goods, {good.name, good.high})
 
47
                       else
 
48
                          -- double chance for med (3 or 4)
 
49
                          table.insert(self.goods, {good.name, good.med})
 
50
                       end
 
51
                    end
 
52
                 end
 
53
              end
 
54
           end,
 
55
   onUpdate = function (self)
 
56
                 if the.keys:justPressed('l') then
 
57
                    local hw = self.width / 2
 
58
                    local pvec = vector.new(the.player.x - (self.x + hw),
 
59
                                            the.player.y - (self.y + hw))
 
60
 
 
61
                    if pvec:len2() < hw ^ 2 then
 
62
                       the.player.velocity = {x=0, y=0}
 
63
                       the.player.acceleration = {x=0, y=0}
 
64
 
 
65
                       -- save player data
 
66
                       the.storage.data.player = {x = the.player.x,
 
67
                                                  y = the.player.y,
 
68
                                                  money = the.player.money,
 
69
                                                  goods = the.player.goods,
 
70
                                                  cargoSpace = the.player.cargoSpace
 
71
                                               }
 
72
                       the.storage:save()
 
73
 
 
74
                       tradeView = TradeView:new{ planet = self }
 
75
                       tradeView:activate()
 
76
                    end
 
77
                 end
 
78
              end,
 
79
   onEndFrame = function (self)
 
80
                   local indx, indy
 
81
                   local pvec = vector.new(
 
82
                      self.x - the.player.x + self.width / 2,
 
83
                      self.y - the.player.y + self.height / 2 )
 
84
 
 
85
                   -- TODO: is there a better way to specify the
 
86
                   -- screen rectangle?
 
87
                   if self:intersects(the.player.x - the.app.width / 2,
 
88
                                      the.player.y - the.app.height / 2,
 
89
                                      the.app.width,
 
90
                                      the.app.height) then
 
91
                      -- planet is on the screen
 
92
                      self.indicator.visible = false
 
93
                   else
 
94
                      self.indicator.visible = true
 
95
 
 
96
                      if math.abs(pvec.x) / math.abs(pvec.y) > the.app.width / the.app.height then
 
97
                         indx = (the.app.width / 2 - 10) * util.signOf(pvec.x) + 8
 
98
                         indy = (the.app.width / 2 - 10) * pvec.y / math.abs(pvec.x)
 
99
                      else
 
100
                         indy = (the.app.height / 2 - 10) * util.signOf(pvec.y) + 8
 
101
                         indx = (the.app.height / 2 - 10) * pvec.x / math.abs(pvec.y)
 
102
                      end
 
103
 
 
104
                      self.indicator.x = the.player.x + indx
 
105
                      self.indicator.y = the.player.y + indy
 
106
                   end
 
107
 
 
108
                end,
 
109
   onCollide = function (self, other)
 
110
                  if other:instanceOf(Bullet) then
 
111
                     the.bullets:remove(other)
 
112
                     other:die()
 
113
                  end
 
114
               end
 
115
}
 
 
b'\\ No newline at end of file'