/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-07 22:24:32 UTC
  • Revision ID: josh@9ix.org-20130507222432-74d3ul1p8ffq40s5
only fire if cursor is in target area.  change cursor to reflect this.

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