/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-18 00:09:56 UTC
  • Revision ID: josh@9ix.org-20130518000956-s9717aensfsq1b3c
fix double-removing sprites (and subsequent zombie mirror bullets).  
also lots of debug stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
local accelLimit = 150
2
 
local velLimit = 300
 
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 tvec = vector.new(self.target.x - self.x,
19
 
                                             self.target.y - self.y)
20
 
 
21
 
 
22
 
                     local tAngle = math.atan2(tvec.y, tvec.x)
23
 
                     local aDiff = self.rotation - tAngle
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
 
                     if self.state == 'combat' and not the.player.active then
44
 
                        self.state = 'patrolling'
45
 
                     end
46
 
 
47
 
                     local pdist2 = vector.new(the.player.x - self.x,
48
 
                                               the.player.y - self.y):len2()
49
 
 
50
 
                     if pdist2 < (the.app.width / 2)^2 then
51
 
                        -- if player is roughly on enemy's screen
52
 
                        if self.state == 'patrolling' and the.player.active and not the.player.onPlanet then
53
 
                           self.state = 'combat'
54
 
                           self.target = the.player
55
 
                        end
56
 
                     elseif pdist2 > (3 * the.app.width)^2 then
57
 
                        if self.state == 'combat' then
58
 
                           self.state = 'patrolling'
59
 
                           self:chooseTarget()
60
 
                        end
61
 
                     end
62
 
 
63
 
                     local tdist2 = tvec:len2()
64
 
 
65
 
                     if tdist2 > 400^2 then
66
 
                        self.acceleration = vector.new(300, 0)
67
 
                        self.acceleration:rotate_inplace(self.rotation)
68
 
                        self.thrust.visible = true
69
 
                     else
70
 
                        self.acceleration = {x=0, y=0}
71
 
                        self.thrust.visible = false
72
 
                     end
73
 
 
74
 
                     if tdist2 < 400^2 then
75
 
                        if self.state == 'patrolling' then
76
 
                           self:chooseTarget()
77
 
                        elseif self.state == 'combat' then
78
 
                           if math.abs(aDiff) < 0.5 * math.pi and
79
 
                             love.timer.getTime() - self.lastFired > 0.25 then
80
 
                             --print('pew')
81
 
                               b = Bullet:new{
82
 
                                  x = self.x + self.width / 2,
83
 
                                  y = self.y + self.height / 2,
84
 
                                  rotation = self.rotation,
85
 
                                  source = self
86
 
                               }
87
 
                               self.lastFired = love.timer.getTime()
88
 
                            end
89
 
                        end
90
 
                     end
91
 
 
92
 
                  end,
93
7
   onUpdate = function(self)
94
 
                 self.thrust.x = self.x - self.width
95
 
                 self.thrust.y = self.y
96
 
                 self.thrust.rotation = self.rotation
97
 
              end,
98
 
   onCollide = function (self, other)
99
 
                  if other:instanceOf(Bullet) and other.source ~= self then
100
 
                     if self.shield.strength == 0 then
101
 
                        -- die
102
 
                        the.app.view:add( Boom:new {
103
 
                                             x = self.x,
104
 
                                             y = self.y,
105
 
                                             velocity = { rotation = 5 }
106
 
                                          })
107
 
 
108
 
                        self:die()
109
 
                        self.thrust:die()
110
 
                        self.shield:die()
111
 
                        the.enemies:remove(self)
112
 
                        the.app.view:remove(self.thrust)
113
 
                        the.app.view:remove(self.shield)
114
 
 
115
 
                        if other.source == the.player then
116
 
                           the.player.kills = the.player.kills + 1
117
 
                        end
118
 
 
119
 
                        -- and add a new enemy somewhere else...
120
 
                        local e = Enemy:new{x = math.random(the.bg.width),
121
 
                                            y = math.random(the.bg.height)}
122
 
                        the.enemies:add(e)
123
 
                        the.view:add(e.thrust)
124
 
                        the.view:add(e.shield)
125
 
                     else
126
 
                        self.shield:onHit()
127
 
                     end
128
 
 
129
 
                     other:die()
130
 
                     the.bullets:remove(other)
131
 
                  end
132
 
               end,
133
 
   chooseTarget = function (self)
134
 
                     self.target = {
135
 
                        x = math.random(the.bg.width),
136
 
                        y = math.random(the.bg.height)
137
 
                     }
138
 
                  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
139
26
}
 
 
b'\\ No newline at end of file'