/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:06:14 UTC
  • Revision ID: josh@9ix.org-20130619010614-j3rbcv1c0my2tw0f
enemy shields and death

Show diffs side-by-side

added added

removed removed

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