/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-19 01:44:47 UTC
  • Revision ID: josh@9ix.org-20130619014447-mtpps0g4w7aeckpg
enemy "patrol path" (pick random destination)

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',
9
10
   onNew = function (self)
10
11
              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()
11
16
           end,
12
17
   onStartFrame = function (self)
13
18
 
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)
 
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
18
43
 
19
44
                     local pdist2 = pvec:len2()
20
45
 
27
52
                        self.thrust.visible = false
28
53
                     end
29
54
 
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()
 
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
39
71
                     end
40
72
 
41
73
                  end,
44
76
                 self.thrust.y = self.y
45
77
                 self.thrust.rotation = self.rotation
46
78
              end,
47
 
 
 
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
48
109
}
 
 
b'\\ No newline at end of file'