/traderous

To get this branch, use:
bzr branch http://9ix.org/bzr/traderous
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- player = 300 accel, 400 velLimit
local accelLimit = 150
local velLimit = 200
Enemy = Tile:extend {
   image = 'data/enemy.png',
   maxVelocity = {x=velLimit,y=velLimit},
   minVelocity = {x=-velLimit, y=-velLimit},
   lastFired = 0,
   onNew = function (self)
              self.thrust = Tile:new{image = 'data/thrust.png'}
              --the.app.view:add(self.thrust)
           end,
   onStartFrame = function (self)

                     local pvec = vector.new(the.player.x - self.x,
                                             the.player.y - self.y)


                     local pAngle = math.atan2(pvec.y, pvec.x)
                     local aDiff = self.rotation - pAngle

                     while aDiff < (-math.pi) do
                        --print(aDiff .. ' < -π')
                        aDiff = aDiff + 2 * math.pi
                     end
                     while aDiff > math.pi do
                        --print(aDiff .. ' > π')
                        aDiff = aDiff - 2 * math.pi
                     end

                     if math.abs(aDiff) > 0.1 then
                        -- rotation really shouldn't be negative.  I
                        -- don't understand why that is.  :\
                        self.velocity.rotation = -2 * util.signOf(aDiff)
                     else
                        --print('not rotating')
                        self.velocity.rotation = 0
                     end

                     local pdist2 = pvec:len2()

                     if pdist2 > 400^2 then -- ... if player dist > 64?
                        self.acceleration = vector.new(300, 0)
                        self.acceleration:rotate_inplace(self.rotation)
                        self.thrust.visible = true
                     else
                        self.acceleration = {x=0, y=0}
                        self.thrust.visible = false
                     end

                     if math.abs(aDiff) < 0.5 * math.pi and
                       pdist2 < 400^2 and
                       love.timer.getTime() - self.lastFired > 0.25 then
                       --print('pew')
                         b = Bullet:new{
                            x = self.x + self.width / 2,
                            y = self.y + self.height / 2,
                            rotation = self.rotation
                         }
                         self.lastFired = love.timer.getTime()
                     end

                  end,
   onUpdate = function(self)
                 self.thrust.x = self.x - self.width
                 self.thrust.y = self.y
                 self.thrust.rotation = self.rotation
              end,

}