/traderous

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

« back to all changes in this revision

Viewing changes to player.lua

  • Committer: Josh C
  • Date: 2013-06-30 22:39:43 UTC
  • Revision ID: josh@9ix.org-20130630223943-863q2kff3f3bqp7v
save on arriving at & leaving a planet both

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
                  end
20
20
}
21
21
 
22
 
local velLimit = 400
 
22
local velLimit = 300
23
23
--local velLimit = 50
24
24
SpacePlayer = Tile:extend{
25
25
   image = 'data/ship.png',
26
26
   maxVelocity = {x=velLimit,y=velLimit},
27
27
   minVelocity = {x=-velLimit, y=-velLimit},
28
28
   lastFired = 0,
 
29
   money = 2000, -- what to use for money symbol? ✪ or ☥ or Ⓐ?
 
30
   goods = {},
 
31
   cargoSpace = 20,
 
32
   onPlanet = false,
 
33
   onNew = function(self)
 
34
              self.thrust = Tile:new{image = 'data/thrust.png'}
 
35
              self.shield = Shield:new{ship = self}
 
36
           end,
29
37
   onStartFrame = function(self)
30
38
                     --- TAKE 2
31
39
 
39
47
                     if mouseVec:len2() > 64^2 then
40
48
                        self.acceleration = vector.new(300, 0)
41
49
                        self.acceleration:rotate_inplace(self.rotation)
 
50
                        self.thrust.visible = true
42
51
                     else
43
52
                        self.acceleration = {x=0, y=0}
 
53
                        self.thrust.visible = false
 
54
                        if DEBUG and the.console.visible then
 
55
                           self.velocity = {x=0, y=0}
 
56
                        end
44
57
                     end
45
58
 
46
59
                     if the.mouse:pressed() and -- assumes left button
48
61
                       the.cursor:inTargetArea() then
49
62
                           --print('pew')
50
63
                           b = Bullet:new{
51
 
                              x = self.x, y = self.y,
 
64
                              x = self.x + self.width / 2,
 
65
                              y = self.y + self.height / 2,
 
66
                              rotation = self.rotation,
 
67
                              source = self
52
68
                           }
53
 
                           the.app.view:add(b)
54
69
                           self.lastFired = love.timer.getTime()
55
70
                     end
56
71
                  end,
57
72
   onUpdate = function(self)
58
 
                 if self.x < the.app.width / 2 then
59
 
                    self.x = the.bg.width - the.app.width / 2
60
 
                 elseif self.x > the.bg.width - the.app.width / 2 then
61
 
                    self.x = the.app.width / 2
62
 
                 end
63
 
 
64
 
                 if self.y < the.app.height / 2 then
65
 
                    self.y = the.bg.height - the.app.height / 2
66
 
                 elseif self.y > the.bg.height - the.app.height / 2 then
67
 
                    self.y = the.app.height / 2
68
 
                 end
69
 
 
70
 
              end
 
73
                 self.thrust.x = self.x - self.width
 
74
                 self.thrust.y = self.y
 
75
                 self.thrust.rotation = self.rotation
 
76
              end,
 
77
   onEndFrame = function(self)
 
78
                   self:collide(the.rockColliders)
 
79
                end,
 
80
   onCollide = function(self, other)
 
81
                  if other:instanceOf(Bullet) and other.source ~= self then
 
82
                     if self.shield.strength == 0 then
 
83
                        -- die
 
84
                        the.app.view:add( Boom:new {
 
85
                                             x = self.x,
 
86
                                             y = self.y,
 
87
                                             velocity = { rotation = 5 }
 
88
                                          })
 
89
 
 
90
                        self:die()
 
91
                        self.thrust:die()
 
92
                     else
 
93
                        self.shield:onHit()
 
94
                     end
 
95
 
 
96
                     other:die()
 
97
                     the.bullets:remove(other)
 
98
                  end
 
99
 
 
100
                  if other:instanceOf(RockCollider) then
 
101
                     the.app.view:add( Boom:new {
 
102
                                          x = self.x,
 
103
                                          y = self.y,
 
104
                                          velocity = { rotation = 5 }
 
105
                                       })
 
106
 
 
107
                     self:die()
 
108
                     self.thrust:die()
 
109
                  end
 
110
 
 
111
                  if not self.active then
 
112
                     the.interface:add(GameOver:new())
 
113
                  end
 
114
               end,
 
115
   buy = function(self, good, price)
 
116
            if self.money >= price and self:availableSpace() > 0 then
 
117
               self.goods[good] = (self.goods[good] or 0) + 1
 
118
               self.money = self.money - price
 
119
            end
 
120
         end,
 
121
   sell = function(self, good, price)
 
122
             if self.goods[good] and self.goods[good] > 0 then
 
123
                self.goods[good] = self.goods[good] - 1
 
124
                self.money = self.money + price
 
125
             end
 
126
          end,
 
127
   availableSpace = function(self)
 
128
                       local cargo = 0
 
129
                       for _, amt in pairs(self.goods) do
 
130
                          cargo = cargo + amt
 
131
                       end
 
132
 
 
133
                       return self.cargoSpace - cargo
 
134
                    end,
 
135
   save = function(self)
 
136
             -- save player data
 
137
             the.storage.data.player = {x = self.x,
 
138
                                        y = self.y,
 
139
                                        money = self.money,
 
140
                                        goods = self.goods,
 
141
                                        cargoSpace = self.cargoSpace
 
142
                                     }
 
143
             the.storage:save()
 
144
          end
71
145
}
 
 
b'\\ No newline at end of file'