/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-05-14 17:49:03 UTC
  • Revision ID: josh@9ix.org-20130514174903-790agtxqfvz8lnzv
player death

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
local accelLimit = 150
2
 
local velLimit = 200
 
1
local accelLimit = 300 * 2 -- same as the player, yes?
 
2
local velLimit = 600 -- same as player
3
3
Enemy = Tile:extend {
4
4
   image = 'data/enemy.png',
5
5
   maxVelocity = {x=velLimit,y=velLimit},
6
6
   minVelocity = {x=-velLimit, y=-velLimit},
7
 
   lastFired = 0,
8
 
   onNew = function (self)
9
 
              self.thrust = Tile:new{image = 'data/thrust.png'}
10
 
              --the.app.view:add(self.thrust)
11
 
              self.shield = Shield:new{ship = self}
12
 
           end,
13
 
   onStartFrame = function (self)
14
 
 
15
 
                     local pvec = vector.new(the.player.x - self.x,
16
 
                                             the.player.y - self.y)
17
 
 
18
 
 
19
 
                     local pAngle = math.atan2(pvec.y, pvec.x)
20
 
                     local aDiff = self.rotation - pAngle
21
 
 
22
 
                     while aDiff < (-math.pi) do
23
 
                        --print(aDiff .. ' < -π')
24
 
                        aDiff = aDiff + 2 * math.pi
25
 
                     end
26
 
                     while aDiff > math.pi do
27
 
                        --print(aDiff .. ' > π')
28
 
                        aDiff = aDiff - 2 * math.pi
29
 
                     end
30
 
 
31
 
                     if math.abs(aDiff) > 0.1 then
32
 
                        -- rotation really shouldn't be negative.  I
33
 
                        -- don't understand why that is.  :\
34
 
                        self.velocity.rotation = -2 * util.signOf(aDiff)
35
 
                     else
36
 
                        --print('not rotating')
37
 
                        self.velocity.rotation = 0
38
 
                     end
39
 
 
40
 
                     local pdist2 = pvec:len2()
41
 
 
42
 
                     if pdist2 > 400^2 then -- ... if player dist > 64?
43
 
                        self.acceleration = vector.new(300, 0)
44
 
                        self.acceleration:rotate_inplace(self.rotation)
45
 
                        self.thrust.visible = true
46
 
                     else
47
 
                        self.acceleration = {x=0, y=0}
48
 
                        self.thrust.visible = false
49
 
                     end
50
 
 
51
 
                     if math.abs(aDiff) < 0.5 * math.pi and
52
 
                       pdist2 < 400^2 and
53
 
                       love.timer.getTime() - self.lastFired > 0.25 then
54
 
                       --print('pew')
55
 
                         b = Bullet:new{
56
 
                            x = self.x + self.width / 2,
57
 
                            y = self.y + self.height / 2,
58
 
                            rotation = self.rotation,
59
 
                            source = self
60
 
                         }
61
 
                         self.lastFired = love.timer.getTime()
62
 
                     end
63
 
 
64
 
                  end,
65
7
   onUpdate = function(self)
66
 
                 self.thrust.x = self.x - self.width
67
 
                 self.thrust.y = self.y
68
 
                 self.thrust.rotation = self.rotation
69
 
              end,
70
 
   onCollide = function (self, other)
71
 
                  if other:instanceOf(Bullet) and other.source ~= self then
72
 
                     if self.shield.strength == 0 then
73
 
                        -- die
74
 
                        the.app.view:add( Boom:new {
75
 
                                             x = self.x,
76
 
                                             y = self.y,
77
 
                                             velocity = { rotation = 5 }
78
 
                                          })
79
 
 
80
 
                        self:die()
81
 
                        self.thrust:die()
82
 
                        self.shield:die()
83
 
                        the.enemies:remove(self)
84
 
                        the.app.view:remove(self.thrust)
85
 
                        the.app.view:remove(self.shield)
86
 
                     else
87
 
                        self.shield:onHit()
88
 
                     end
89
 
 
90
 
                     other:die()
91
 
                     the.bullets:remove(other)
92
 
                  end
93
 
               end
 
8
                 -- find where player is relative to us
 
9
                 local dx = the.player.x - self.x
 
10
                 local dy = the.player.y - self.y
 
11
 
 
12
                 local ax, ay = dx, dy
 
13
                 if math.abs(dx) > math.abs(accelLimit) then
 
14
                    ax = accelLimit * util.signOf(dx)
 
15
                    ay = accelLimit * math.abs(dx) / dy
 
16
                 elseif math.abs(dy) > math.abs(accelLimit) then
 
17
                    ay = accelLimit * util.signOf(dy)
 
18
                    ax = accelLimit * math.abs(dy) / dx
 
19
                 end
 
20
 
 
21
                 self.acceleration.x = ax
 
22
                 self.acceleration.y = ay
 
23
 
 
24
                 self.rotation = math.atan2(self.velocity.y, self.velocity.x)
 
25
              end
94
26
}
 
 
b'\\ No newline at end of file'