/traderous

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

« back to all changes in this revision

Viewing changes to trade_view.lua

  • Committer: Josh C
  • Date: 2013-07-04 18:15:15 UTC
  • Revision ID: josh@9ix.org-20130704181515-pmp44y2vv3vw170x
reset kill counter on reward

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
TradeView = Subview:extend {
2
2
   drawParent = true, --default?
 
3
   have = {},
3
4
   onNew = function (self)
4
5
              -- create all the interface bits from the planet
5
6
              -- and add them to the view
6
7
 
7
 
              local boxL = the.app.width / 2 - 200
8
 
              local boxT = the.app.height / 2 - 200
9
 
              local boxR = boxL + 400
 
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
10
12
 
11
13
              local lh = 17 -- line height
12
14
 
14
16
                          fill = {255,255,255},
15
17
                          x = boxL - 1,
16
18
                          y = boxT - 1,
17
 
                          width = 402,
18
 
                          height = 402,
 
19
                          width = 452,
 
20
                          height = 302,
19
21
                       })
20
22
 
21
23
              self:add(Fill:new{
22
24
                          fill = {0,0,0},
23
25
                          x = boxL,
24
26
                          y = boxT,
25
 
                          width = 400,
26
 
                          height = 400,
 
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
27
51
                       })
28
52
 
29
53
              for i, good in ipairs(self.planet.goods) do
30
54
                 local t = Text:new{
31
55
                    text = good[1],
32
56
                    x = boxL + 10,
33
 
                    y = boxT + 10 + (i-1) * lh,
 
57
                    y = boxT + 10 + i * lh,
34
58
                    width = 380
35
59
                 }
36
60
 
39
63
                 t = Text:new{
40
64
                    text = good[2],
41
65
                    x = boxL + 10,
42
 
                    y = boxT + 10 + (i-1) * lh,
 
66
                    y = boxT + 10 + i * lh,
43
67
                    width = 300,
44
68
                    align = 'right'
45
69
                 }
46
70
                 self:add(t)
47
71
 
48
72
                 local b = Button:new{
49
 
                    x = boxR - 80,
50
 
                    y = boxT + 10 + (i-1) * lh - 1,
 
73
                    x = boxR - 130,
 
74
                    y = boxT + 10 + i * lh - 1,
51
75
                    label = Text:new{ text = 'BUY', x = 1 },
 
76
                    onMouseDown = function (self)
 
77
                                     the.player:buy(good[1], good[2])
 
78
                                  end
52
79
                 }
53
80
                 local tw, th = b.label:getSize()
54
81
                 b.background = Fill:new{
59
86
                 self:add(b)
60
87
 
61
88
                 b = Button:new{
62
 
                    x = boxR - 40,
63
 
                    y = boxT + 10 + (i-1) * lh - 1,
 
89
                    x = boxR - 90,
 
90
                    y = boxT + 10 + i * lh - 1,
64
91
                    label = Text:new{ text = 'SELL', x = 1 },
 
92
                    onMouseDown = function (self)
 
93
                                     the.player:sell(good[1], good[2])
 
94
                                  end
65
95
                 }
66
96
                 local tw, th = b.label:getSize()
67
97
                 b.background = Fill:new{
70
100
                    height = th
71
101
                 }
72
102
                 self:add(b)
 
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
 
73
113
              end
74
114
 
75
115
              local b = Button:new{
87
127
                 height = th + 4
88
128
              }
89
129
              b.x = boxR - b.background.width - 10
90
 
              b.y = boxT + 400 - b.background.height - 10
 
130
              b.y = boxB - b.background.height - 10
91
131
 
92
132
              self:add(b)
93
133
 
 
134
              local sh = the.player.shield
 
135
              local cost = (sh.max - sh.strength) * sh.cost
 
136
              b = Button:new{
 
137
                 x = boxL, y = boxT,
 
138
                 width = 800,
 
139
                 -- WTF: trailing space makes it not wrap?
 
140
                 label = Text:new{ text = 'Repair shields (' .. cost .. ') ',
 
141
                                   x = 3, y = 2 },
 
142
                 onMouseDown = function(self)
 
143
                                  if the.player.money >= cost and sh.strength < sh.max then
 
144
                                     the.player.money = the.player.money - cost
 
145
                                     sh.strength = sh.max
 
146
                                     self.visible = false
 
147
                                  end
 
148
                               end
 
149
              }
 
150
 
 
151
              local tw, th = b.label:getSize()
 
152
              b.background = Fill:new{
 
153
                 fill = {100,100,100},
 
154
                 width = tw + 3,
 
155
                 height = th + 4
 
156
              }
 
157
              b.x = boxR - b.background.width - 60
 
158
              b.y = boxB - b.background.height - 10
 
159
 
 
160
              if sh.strength < sh.max then
 
161
                 self:add(b)
 
162
              end
 
163
 
 
164
              self.playerMoney = Text:new{
 
165
                 text = 'Your money: ',
 
166
                 width = 200,
 
167
                 x = boxL + 10,
 
168
                 y = boxB - th - 10
 
169
              }
 
170
              self:add(self.playerMoney)
 
171
 
 
172
              self.availSpace = Text:new{
 
173
                 text = 'Available cargo space: ',
 
174
                 width = 200,
 
175
                 x = boxL + 10,
 
176
                 y = boxB - 2 * th - 12
 
177
              }
 
178
              self:add(self.availSpace)
 
179
 
 
180
              if the.player.kills > 0 then
 
181
                 local award = the.player.kills * 100
 
182
                 self:add(Text:new{
 
183
                             text = 'You have been awarded ' ..
 
184
                                award ..
 
185
                                ' credits for killing bandits.',
 
186
                             width = 450,
 
187
                             align = 'center',
 
188
                             x = boxL,
 
189
                             y = boxB - 4 * th - 12
 
190
                          })
 
191
 
 
192
                 the.player.money = the.player.money + award
 
193
                 the.player.kills = 0
 
194
              end
 
195
 
94
196
              -- give the buttons a cycle to get out of the T/L corner
95
197
              self:update(0)
96
198
           end,
98
200
                 the.cursor.visible = false
99
201
                 love.mouse.setVisible(true)
100
202
                 love.mouse.setGrab(false)
 
203
                 love.mouse.setPosition(the.app.width / 2, the.app.height / 2)
101
204
 
102
205
                 Subview.activate(self)
103
206
              end,
104
207
   close = function (self)
 
208
              the.player:save()
 
209
 
105
210
              the.cursor.visible = true
106
211
              love.mouse.setVisible(false)
107
212
              love.mouse.setGrab(true)
 
213
              love.mouse.setPosition(the.app.width / 2, the.app.height / 2)
108
214
 
109
215
              self:deactivate()
110
216
           end,
112
218
                 if the.keys:justPressed('escape') then
113
219
                    self:close()
114
220
                 end
 
221
 
 
222
                 self.playerMoney.text = 'Your money: ' .. the.player.money
 
223
                 self.availSpace.text = 'Available cargo space: ' .. the.player:availableSpace()
 
224
 
 
225
                 for good, have in pairs(self.have) do
 
226
                    have.text = the.player.goods[good] or 0
 
227
                 end
115
228
              end
116
229
}
 
 
b'\\ No newline at end of file'