/traderous

To get this branch, use:
bzr branch http://9ix.org/bzr/traderous
53 by Josh C
beginnings of trading interface
1
TradeView = Subview:extend {
2
   drawParent = true, --default?
58 by Josh C
status of what player has
3
   have = {},
53 by Josh C
beginnings of trading interface
4
   onNew = function (self)
5
              -- create all the interface bits from the planet
6
              -- and add them to the view
7
58 by Josh C
status of what player has
8
              local boxL = the.app.width / 2 - 225
9
              local boxT = the.app.height / 2 - 150
10
              local boxR = boxL + 450
11
              local boxB = boxT + 300
54 by Josh C
prices, buttons
12
13
              local lh = 17 -- line height
53 by Josh C
beginnings of trading interface
14
15
              self:add(Fill:new{
16
                          fill = {255,255,255},
17
                          x = boxL - 1,
18
                          y = boxT - 1,
58 by Josh C
status of what player has
19
                          width = 452,
20
                          height = 302,
53 by Josh C
beginnings of trading interface
21
                       })
22
23
              self:add(Fill:new{
24
                          fill = {0,0,0},
25
                          x = boxL,
26
                          y = boxT,
58 by Josh C
status of what player has
27
                          width = 450,
28
                          height = 300,
29
                       })
30
31
              self:add(Text:new{
32
                          text = 'Name',
33
                          x = boxL + 10,
34
                          y = boxT + 10,
35
                          width = 380
36
                       })
37
38
              self:add(Text:new{
39
                          text = 'Price',
40
                          x = boxL + 10,
41
                          y = boxT + 10,
42
                          width = 300,
43
                          align = 'right'
44
                       })
45
46
              self:add(Text:new{
47
                          text = 'Have',
48
                          align = 'right',
49
                          y = boxT + 10,
50
                          x = boxR - 40
53 by Josh C
beginnings of trading interface
51
                       })
52
53
              for i, good in ipairs(self.planet.goods) do
54
                 local t = Text:new{
55
                    text = good[1],
56
                    x = boxL + 10,
58 by Josh C
status of what player has
57
                    y = boxT + 10 + i * lh,
53 by Josh C
beginnings of trading interface
58
                    width = 380
59
                 }
60
54 by Josh C
prices, buttons
61
                 self:add(t)
62
63
                 t = Text:new{
64
                    text = good[2],
65
                    x = boxL + 10,
58 by Josh C
status of what player has
66
                    y = boxT + 10 + i * lh,
54 by Josh C
prices, buttons
67
                    width = 300,
68
                    align = 'right'
69
                 }
70
                 self:add(t)
71
72
                 local b = Button:new{
58 by Josh C
status of what player has
73
                    x = boxR - 130,
74
                    y = boxT + 10 + i * lh - 1,
54 by Josh C
prices, buttons
75
                    label = Text:new{ text = 'BUY', x = 1 },
57 by Josh C
buy/sell
76
                    onMouseDown = function (self)
77
                                     the.player:buy(good[1], good[2])
78
                                  end
54 by Josh C
prices, buttons
79
                 }
80
                 local tw, th = b.label:getSize()
81
                 b.background = Fill:new{
82
                    fill = {100,100,100},
83
                    width = tw + 3,
84
                    height = th
85
                 }
86
                 self:add(b)
87
88
                 b = Button:new{
58 by Josh C
status of what player has
89
                    x = boxR - 90,
90
                    y = boxT + 10 + i * lh - 1,
54 by Josh C
prices, buttons
91
                    label = Text:new{ text = 'SELL', x = 1 },
57 by Josh C
buy/sell
92
                    onMouseDown = function (self)
93
                                     the.player:sell(good[1], good[2])
94
                                  end
54 by Josh C
prices, buttons
95
                 }
96
                 local tw, th = b.label:getSize()
97
                 b.background = Fill:new{
98
                    fill = {100,100,100},
99
                    width = tw + 3,
100
                    height = th
101
                 }
102
                 self:add(b)
58 by Josh C
status of what player has
103
104
                 local have = Text:new{
105
                    x = boxR - 110,
106
                    y = boxT + 10 + i * lh,
107
                    width = 100,
108
                    align = 'right',
109
                 }
110
                 self.have[good[1]] = have
111
                 self:add(have)
112
53 by Josh C
beginnings of trading interface
113
              end
55 by Josh C
close button
114
115
              local b = Button:new{
116
                 x = boxL, y = boxT,
117
                 label = Text:new{ text = 'Close', x = 3, y = 2 },
118
                 onMouseDown = function()
119
                                  self:close()
120
                               end
121
              }
122
123
              local tw, th = b.label:getSize()
124
              b.background = Fill:new{
125
                 fill = {100,100,100},
126
                 width = tw + 7,
127
                 height = th + 4
128
              }
129
              b.x = boxR - b.background.width - 10
58 by Josh C
status of what player has
130
              b.y = boxB - b.background.height - 10
55 by Josh C
close button
131
132
              self:add(b)
133
77 by Josh C
pay to fix shield
134
              local sh = the.player.shield
135
              if sh.strength < sh.max then
100 by Josh C
recharge shields automatically for free, remove bandit bounty
136
                 sh.strength = sh.max
137
138
                 self:add(Text:new{
139
                             text = 'Your shield has been recharged',
140
                             width = 450,
141
                             align = 'center',
142
                             x = boxL,
143
                             y = boxB - 4 * th - 12
144
                          })
77 by Josh C
pay to fix shield
145
              end
146
100 by Josh C
recharge shields automatically for free, remove bandit bounty
147
              -- local cost = (sh.max - sh.strength) * sh.cost
148
              -- b = Button:new{
149
              --    x = boxL, y = boxT,
150
              --    width = 800,
151
              --    -- WTF: trailing space makes it not wrap?
152
              --    label = Text:new{ text = 'Repair shields (' .. cost .. ') ',
153
              --                      x = 3, y = 2 },
154
              --    onMouseDown = function(self)
155
              --                     if the.player.money >= cost and sh.strength < sh.max then
156
              --                        the.player.money = the.player.money - cost
157
              --                        sh.strength = sh.max
158
              --                        self.visible = false
159
              --                     end
160
              --                  end
161
              -- }
162
163
              -- local tw, th = b.label:getSize()
164
              -- b.background = Fill:new{
165
              --    fill = {100,100,100},
166
              --    width = tw + 3,
167
              --    height = th + 4
168
              -- }
169
              -- b.x = boxR - b.background.width - 60
170
              -- b.y = boxB - b.background.height - 10
171
172
              -- if sh.strength < sh.max then
173
              --    self:add(b)
174
              -- end
175
56 by Josh C
money
176
              self.playerMoney = Text:new{
177
                 text = 'Your money: ',
178
                 width = 200,
179
                 x = boxL + 10,
58 by Josh C
status of what player has
180
                 y = boxB - th - 10
56 by Josh C
money
181
              }
182
              self:add(self.playerMoney)
183
76 by Josh C
available cargo space
184
              self.availSpace = Text:new{
185
                 text = 'Available cargo space: ',
186
                 width = 200,
187
                 x = boxL + 10,
188
                 y = boxB - 2 * th - 12
189
              }
190
              self:add(self.availSpace)
191
95 by Josh C
award bounty
192
              if the.player.kills > 0 then
100 by Josh C
recharge shields automatically for free, remove bandit bounty
193
                 -- local award = the.player.kills * 100
194
                 -- self:add(Text:new{
195
                 --             text = 'You have been awarded ' ..
196
                 --                award ..
197
                 --                ' credits for killing bandits.',
198
                 --             width = 450,
199
                 --             align = 'center',
200
                 --             x = boxL,
201
                 --             y = boxB - 4 * th - 12
202
                 --          })
95 by Josh C
award bounty
203
100 by Josh C
recharge shields automatically for free, remove bandit bounty
204
                 -- the.player.money = the.player.money + award
99 by Josh C
reset kill counter on reward
205
                 the.player.kills = 0
95 by Josh C
award bounty
206
              end
207
55 by Josh C
close button
208
              -- give the buttons a cycle to get out of the T/L corner
209
              self:update(0)
53 by Josh C
beginnings of trading interface
210
           end,
211
   activate = function (self)
212
                 the.cursor.visible = false
213
                 love.mouse.setVisible(true)
214
                 love.mouse.setGrab(false)
61 by Josh C
move mouse to center when entering/exiting trade screen
215
                 love.mouse.setPosition(the.app.width / 2, the.app.height / 2)
53 by Josh C
beginnings of trading interface
216
217
                 Subview.activate(self)
218
              end,
55 by Josh C
close button
219
   close = function (self)
93 by Josh C
save on arriving at & leaving a planet both
220
              the.player:save()
221
55 by Josh C
close button
222
              the.cursor.visible = true
223
              love.mouse.setVisible(false)
224
              love.mouse.setGrab(true)
61 by Josh C
move mouse to center when entering/exiting trade screen
225
              love.mouse.setPosition(the.app.width / 2, the.app.height / 2)
53 by Josh C
beginnings of trading interface
226
55 by Josh C
close button
227
              self:deactivate()
228
           end,
229
   onUpdate = function (self)
230
                 if the.keys:justPressed('escape') then
231
                    self:close()
232
                 end
56 by Josh C
money
233
234
                 self.playerMoney.text = 'Your money: ' .. the.player.money
76 by Josh C
available cargo space
235
                 self.availSpace.text = 'Available cargo space: ' .. the.player:availableSpace()
58 by Josh C
status of what player has
236
237
                 for good, have in pairs(self.have) do
238
                    have.text = the.player.goods[good] or 0
239
                 end
55 by Josh C
close button
240
              end
53 by Josh C
beginnings of trading interface
241
}