/traderous

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

« back to all changes in this revision

Viewing changes to enemy.lua

  • Committer: Josh C
  • Date: 2013-06-16 04:21:01 UTC
  • Revision ID: josh@9ix.org-20130616042101-tp1rddbd3subc8lg
enemy is back

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
   maxVelocity = {x=velLimit,y=velLimit},
7
7
   minVelocity = {x=-velLimit, y=-velLimit},
8
8
   lastFired = 0,
9
 
   state = 'patrolling',
10
9
   onNew = function (self)
11
10
              self.thrust = Tile:new{image = 'data/thrust.png'}
12
 
              --the.app.view:add(self.thrust)
13
 
              self.shield = Shield:new{ship = self}
14
 
 
15
 
              self:chooseTarget()
16
11
           end,
17
12
   onStartFrame = function (self)
18
13
 
19
 
                     local pvec = vector.new(self.target.x - self.x,
20
 
                                             self.target.y - self.y)
21
 
 
22
 
 
23
 
                     local pAngle = math.atan2(pvec.y, pvec.x)
24
 
                     local aDiff = self.rotation - pAngle
25
 
 
26
 
                     while aDiff < (-math.pi) do
27
 
                        --print(aDiff .. ' < -π')
28
 
                        aDiff = aDiff + 2 * math.pi
29
 
                     end
30
 
                     while aDiff > math.pi do
31
 
                        --print(aDiff .. ' > π')
32
 
                        aDiff = aDiff - 2 * math.pi
33
 
                     end
34
 
 
35
 
                     if math.abs(aDiff) > 0.1 then
36
 
                        -- rotation really shouldn't be negative.  I
37
 
                        -- don't understand why that is.  :\
38
 
                        self.velocity.rotation = -2 * util.signOf(aDiff)
39
 
                     else
40
 
                        --print('not rotating')
41
 
                        self.velocity.rotation = 0
42
 
                     end
 
14
                     local pvec = vector.new(the.player.x - self.x,
 
15
                                             the.player.y - self.y)
 
16
 
 
17
                     self.rotation = math.atan2(pvec.y, pvec.x)
43
18
 
44
19
                     local pdist2 = pvec:len2()
45
20
 
52
27
                        self.thrust.visible = false
53
28
                     end
54
29
 
55
 
                     if pdist2 < 400^2 then
56
 
                        if self.state == 'patrolling' then
57
 
                           self:chooseTarget()
58
 
                        elseif self.state == 'combat' then
59
 
                           if math.abs(aDiff) < 0.5 * math.pi and
60
 
                             love.timer.getTime() - self.lastFired > 0.25 then
61
 
                             --print('pew')
62
 
                               b = Bullet:new{
63
 
                                  x = self.x + self.width / 2,
64
 
                                  y = self.y + self.height / 2,
65
 
                                  rotation = self.rotation,
66
 
                                  source = self
67
 
                               }
68
 
                               self.lastFired = love.timer.getTime()
69
 
                            end
70
 
                        end
 
30
                     if pdist2 < 400^2 and
 
31
                       love.timer.getTime() - self.lastFired > 0.25 then
 
32
                       --print('pew')
 
33
                         b = Bullet:new{
 
34
                            x = self.x + self.width / 2,
 
35
                            y = self.y + self.height / 2,
 
36
                            rotation = self.rotation
 
37
                         }
 
38
                         self.lastFired = love.timer.getTime()
71
39
                     end
72
40
 
73
41
                  end,
76
44
                 self.thrust.y = self.y
77
45
                 self.thrust.rotation = self.rotation
78
46
              end,
79
 
   onCollide = function (self, other)
80
 
                  if other:instanceOf(Bullet) and other.source ~= self then
81
 
                     if self.shield.strength == 0 then
82
 
                        -- die
83
 
                        the.app.view:add( Boom:new {
84
 
                                             x = self.x,
85
 
                                             y = self.y,
86
 
                                             velocity = { rotation = 5 }
87
 
                                          })
88
 
 
89
 
                        self:die()
90
 
                        self.thrust:die()
91
 
                        self.shield:die()
92
 
                        the.enemies:remove(self)
93
 
                        the.app.view:remove(self.thrust)
94
 
                        the.app.view:remove(self.shield)
95
 
                     else
96
 
                        self.shield:onHit()
97
 
                     end
98
 
 
99
 
                     other:die()
100
 
                     the.bullets:remove(other)
101
 
                  end
102
 
               end,
103
 
   chooseTarget = function (self)
104
 
                     self.target = {
105
 
                        x = math.random(the.bg.width),
106
 
                        y = math.random(the.bg.height)
107
 
                     }
108
 
                  end
 
47
 
109
48
}
 
 
b'\\ No newline at end of file'