/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-04 20:47:29 UTC
  • Revision ID: josh@9ix.org-20130504204729-0g2wmw6k4nvsh58s
basecode main.lua

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
local accelLimit = 150
2
 
local velLimit = 200
3
 
Enemy = Tile:extend {
4
 
   image = 'data/enemy.png',
5
 
   maxVelocity = {x=velLimit,y=velLimit},
6
 
   minVelocity = {x=-velLimit, y=-velLimit},
7
 
   lastFired = 0,
8
 
   onNew = function (self)
9
 
              self.thrust = Tile:new{image = 'data/thrust.png'}
10
 
              --the.app.view:add(self.thrust)
11
 
           end,
12
 
   onStartFrame = function (self)
13
 
 
14
 
                     local pvec = vector.new(the.player.x - self.x,
15
 
                                             the.player.y - self.y)
16
 
 
17
 
 
18
 
                     local pAngle = math.atan2(pvec.y, pvec.x)
19
 
                     local aDiff = self.rotation - pAngle
20
 
 
21
 
                     while aDiff < (-math.pi) do
22
 
                        --print(aDiff .. ' < -π')
23
 
                        aDiff = aDiff + 2 * math.pi
24
 
                     end
25
 
                     while aDiff > math.pi do
26
 
                        --print(aDiff .. ' > π')
27
 
                        aDiff = aDiff - 2 * math.pi
28
 
                     end
29
 
 
30
 
                     if math.abs(aDiff) > 0.1 then
31
 
                        -- rotation really shouldn't be negative.  I
32
 
                        -- don't understand why that is.  :\
33
 
                        self.velocity.rotation = -2 * util.signOf(aDiff)
34
 
                     else
35
 
                        --print('not rotating')
36
 
                        self.velocity.rotation = 0
37
 
                     end
38
 
 
39
 
                     local pdist2 = pvec:len2()
40
 
 
41
 
                     if pdist2 > 400^2 then -- ... if player dist > 64?
42
 
                        self.acceleration = vector.new(300, 0)
43
 
                        self.acceleration:rotate_inplace(self.rotation)
44
 
                        self.thrust.visible = true
45
 
                     else
46
 
                        self.acceleration = {x=0, y=0}
47
 
                        self.thrust.visible = false
48
 
                     end
49
 
 
50
 
                     if math.abs(aDiff) < 0.5 * math.pi and
51
 
                       pdist2 < 400^2 and
52
 
                       love.timer.getTime() - self.lastFired > 0.25 then
53
 
                       --print('pew')
54
 
                         b = Bullet:new{
55
 
                            x = self.x + self.width / 2,
56
 
                            y = self.y + self.height / 2,
57
 
                            rotation = self.rotation
58
 
                         }
59
 
                         self.lastFired = love.timer.getTime()
60
 
                     end
61
 
 
62
 
                  end,
63
 
   onUpdate = function(self)
64
 
                 self.thrust.x = self.x - self.width
65
 
                 self.thrust.y = self.y
66
 
                 self.thrust.rotation = self.rotation
67
 
              end,
68
 
 
69
 
}
 
 
b'\\ No newline at end of file'