/traderous

To get this branch, use:
bzr branch http://9ix.org/bzr/traderous
65 by Josh C
enemy is back
1
-- player = 300 accel, 400 velLimit
2
local accelLimit = 150
3
local velLimit = 200
7 by Josh C
enemy
4
Enemy = Tile:extend {
5
   image = 'data/enemy.png',
6
   maxVelocity = {x=velLimit,y=velLimit},
7
   minVelocity = {x=-velLimit, y=-velLimit},
65 by Josh C
enemy is back
8
   lastFired = 0,
71 by Josh C
enemy "patrol path" (pick random destination)
9
   state = 'patrolling',
65 by Josh C
enemy is back
10
   onNew = function (self)
11
              self.thrust = Tile:new{image = 'data/thrust.png'}
66 by Josh C
enemy thrust
12
              --the.app.view:add(self.thrust)
69 by Josh C
enemy shields and death
13
              self.shield = Shield:new{ship = self}
71 by Josh C
enemy "patrol path" (pick random destination)
14
15
              self:chooseTarget()
65 by Josh C
enemy is back
16
           end,
17
   onStartFrame = function (self)
18
71 by Josh C
enemy "patrol path" (pick random destination)
19
                     local pvec = vector.new(self.target.x - self.x,
20
                                             self.target.y - self.y)
65 by Josh C
enemy is back
21
67 by Josh C
rotate enemy ship slower
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
65 by Josh C
enemy is back
43
44
                     local pdist2 = pvec:len2()
45
46
                     if pdist2 > 400^2 then -- ... if player dist > 64?
47
                        self.acceleration = vector.new(300, 0)
48
                        self.acceleration:rotate_inplace(self.rotation)
49
                        self.thrust.visible = true
50
                     else
51
                        self.acceleration = {x=0, y=0}
52
                        self.thrust.visible = false
53
                     end
54
71 by Josh C
enemy "patrol path" (pick random destination)
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
65 by Josh C
enemy is back
71
                     end
72
73
                  end,
7 by Josh C
enemy
74
   onUpdate = function(self)
65 by Josh C
enemy is back
75
                 self.thrust.x = self.x - self.width
76
                 self.thrust.y = self.y
77
                 self.thrust.rotation = self.rotation
78
              end,
69 by Josh C
enemy shields and death
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
71 by Josh C
enemy "patrol path" (pick random destination)
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
7 by Josh C
enemy
109
}