/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-06-30 14:59:35 UTC
  • Revision ID: josh@9ix.org-20130630145935-y34o51cg3lsom5bg
"safe" zone on planets - no new enemies will track you if you're there.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
-- player = 300 accel, 400 velLimit
2
2
local accelLimit = 150
3
 
local velLimit = 200
 
3
local velLimit = 300
4
4
Enemy = Tile:extend {
5
5
   image = 'data/enemy.png',
6
6
   maxVelocity = {x=velLimit,y=velLimit},
7
7
   minVelocity = {x=-velLimit, y=-velLimit},
8
8
   lastFired = 0,
 
9
   state = 'patrolling',
9
10
   onNew = function (self)
10
11
              self.thrust = Tile:new{image = 'data/thrust.png'}
11
12
              --the.app.view:add(self.thrust)
12
13
              self.shield = Shield:new{ship = self}
 
14
 
 
15
              self:chooseTarget()
13
16
           end,
14
17
   onStartFrame = function (self)
15
18
 
16
 
                     local pvec = vector.new(the.player.x - self.x,
17
 
                                             the.player.y - self.y)
18
 
 
19
 
 
20
 
                     local pAngle = math.atan2(pvec.y, pvec.x)
21
 
                     local aDiff = self.rotation - pAngle
 
19
                     local tvec = vector.new(self.target.x - self.x,
 
20
                                             self.target.y - self.y)
 
21
 
 
22
 
 
23
                     local tAngle = math.atan2(tvec.y, tvec.x)
 
24
                     local aDiff = self.rotation - tAngle
22
25
 
23
26
                     while aDiff < (-math.pi) do
24
27
                        --print(aDiff .. ' < -π')
38
41
                        self.velocity.rotation = 0
39
42
                     end
40
43
 
41
 
                     local pdist2 = pvec:len2()
42
 
 
43
 
                     if pdist2 > 400^2 then -- ... if player dist > 64?
 
44
                     if self.state == 'combat' and not the.player.active then
 
45
                        self.state = 'patrolling'
 
46
                     end
 
47
 
 
48
                     local pdist2 = vector.new(the.player.x - self.x,
 
49
                                               the.player.y - self.y):len2()
 
50
 
 
51
                     if pdist2 < (the.app.width / 2)^2 then
 
52
                        -- if player is roughly on enemy's screen
 
53
                        if self.state == 'patrolling' and the.player.active and not the.player.onPlanet then
 
54
                           self.state = 'combat'
 
55
                           self.target = the.player
 
56
                        end
 
57
                     elseif pdist2 > (3 * the.app.width)^2 then
 
58
                        if self.state == 'combat' then
 
59
                           self.state = 'patrolling'
 
60
                           self:chooseTarget()
 
61
                        end
 
62
                     end
 
63
 
 
64
                     local tdist2 = tvec:len2()
 
65
 
 
66
                     if tdist2 > 400^2 then
44
67
                        self.acceleration = vector.new(300, 0)
45
68
                        self.acceleration:rotate_inplace(self.rotation)
46
69
                        self.thrust.visible = true
49
72
                        self.thrust.visible = false
50
73
                     end
51
74
 
52
 
                     if math.abs(aDiff) < 0.5 * math.pi and
53
 
                       pdist2 < 400^2 and
54
 
                       love.timer.getTime() - self.lastFired > 0.25 then
55
 
                       --print('pew')
56
 
                         b = Bullet:new{
57
 
                            x = self.x + self.width / 2,
58
 
                            y = self.y + self.height / 2,
59
 
                            rotation = self.rotation,
60
 
                            source = self
61
 
                         }
62
 
                         self.lastFired = love.timer.getTime()
 
75
                     if tdist2 < 400^2 then
 
76
                        if self.state == 'patrolling' then
 
77
                           self:chooseTarget()
 
78
                        elseif self.state == 'combat' then
 
79
                           if math.abs(aDiff) < 0.5 * math.pi and
 
80
                             love.timer.getTime() - self.lastFired > 0.25 then
 
81
                             --print('pew')
 
82
                               b = Bullet:new{
 
83
                                  x = self.x + self.width / 2,
 
84
                                  y = self.y + self.height / 2,
 
85
                                  rotation = self.rotation,
 
86
                                  source = self
 
87
                               }
 
88
                               self.lastFired = love.timer.getTime()
 
89
                            end
 
90
                        end
63
91
                     end
64
92
 
65
93
                  end,
84
112
                        the.enemies:remove(self)
85
113
                        the.app.view:remove(self.thrust)
86
114
                        the.app.view:remove(self.shield)
 
115
 
 
116
                        -- and add a new enemy somewhere else...
 
117
                        local e = Enemy:new{x = math.random(the.bg.width),
 
118
                                            y = math.random(the.bg.height)}
 
119
                        the.enemies:add(e)
 
120
                        the.view:add(e.thrust)
 
121
                        the.view:add(e.shield)
87
122
                     else
88
123
                        self.shield:onHit()
89
124
                     end
91
126
                     other:die()
92
127
                     the.bullets:remove(other)
93
128
                  end
94
 
               end
 
129
               end,
 
130
   chooseTarget = function (self)
 
131
                     self.target = {
 
132
                        x = math.random(the.bg.width),
 
133
                        y = math.random(the.bg.height)
 
134
                     }
 
135
                  end
95
136
}
 
 
b'\\ No newline at end of file'