/traderous

To get this branch, use:
bzr branch /bzr/traderous

« back to all changes in this revision

Viewing changes to enemy.lua

  • Committer: Josh C
  • Date: 2013-06-16 04:21:01 UTC
  • Revision ID: josh@9ix.org-20130616042101-tp1rddbd3subc8lg
enemy is back

Show diffs side-by-side

added added

removed removed

1
 
local accelLimit = 300 * 2 -- same as the player, yes?
2
 
local velLimit = 600 -- same as player
 
1
-- player = 300 accel, 400 velLimit
 
2
local accelLimit = 150
 
3
local velLimit = 200
3
4
Enemy = Tile:extend {
4
5
   image = 'data/enemy.png',
5
6
   maxVelocity = {x=velLimit,y=velLimit},
6
7
   minVelocity = {x=-velLimit, y=-velLimit},
 
8
   lastFired = 0,
 
9
   onNew = function (self)
 
10
              self.thrust = Tile:new{image = 'data/thrust.png'}
 
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
                     self.rotation = math.atan2(pvec.y, pvec.x)
 
18
 
 
19
                     local pdist2 = pvec:len2()
 
20
 
 
21
                     if pdist2 > 400^2 then -- ... if player dist > 64?
 
22
                        self.acceleration = vector.new(300, 0)
 
23
                        self.acceleration:rotate_inplace(self.rotation)
 
24
                        self.thrust.visible = true
 
25
                     else
 
26
                        self.acceleration = {x=0, y=0}
 
27
                        self.thrust.visible = false
 
28
                     end
 
29
 
 
30
                     if pdist2 < 400^2 and
 
31
                       love.timer.getTime() - self.lastFired > 0.25 then
 
32
                       --print('pew')
 
33
                         b = Bullet:new{
 
34
                            x = self.x + self.width / 2,
 
35
                            y = self.y + self.height / 2,
 
36
                            rotation = self.rotation
 
37
                         }
 
38
                         self.lastFired = love.timer.getTime()
 
39
                     end
 
40
 
 
41
                  end,
7
42
   onUpdate = function(self)
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
 
43
                 self.thrust.x = self.x - self.width
 
44
                 self.thrust.y = self.y
 
45
                 self.thrust.rotation = self.rotation
 
46
              end,
 
47
 
26
48
}
 
 
'\\ No newline at end of file'