/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-19 01:44:47 UTC
  • Revision ID: josh@9ix.org-20130619014447-mtpps0g4w7aeckpg
enemy "patrol path" (pick random destination)

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 = 300
 
3
local velLimit = 200
4
4
Enemy = Tile:extend {
5
5
   image = 'data/enemy.png',
6
6
   maxVelocity = {x=velLimit,y=velLimit},
16
16
           end,
17
17
   onStartFrame = function (self)
18
18
 
19
 
                     local tvec = vector.new(self.target.x - self.x,
 
19
                     local pvec = vector.new(self.target.x - self.x,
20
20
                                             self.target.y - self.y)
21
21
 
22
22
 
23
 
                     local tAngle = math.atan2(tvec.y, tvec.x)
24
 
                     local aDiff = self.rotation - tAngle
 
23
                     local pAngle = math.atan2(pvec.y, pvec.x)
 
24
                     local aDiff = self.rotation - pAngle
25
25
 
26
26
                     while aDiff < (-math.pi) do
27
27
                        --print(aDiff .. ' < -π')
41
41
                        self.velocity.rotation = 0
42
42
                     end
43
43
 
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
                     local pdist2 = pvec:len2()
 
45
 
 
46
                     if pdist2 > 400^2 then -- ... if player dist > 64?
67
47
                        self.acceleration = vector.new(300, 0)
68
48
                        self.acceleration:rotate_inplace(self.rotation)
69
49
                        self.thrust.visible = true
72
52
                        self.thrust.visible = false
73
53
                     end
74
54
 
75
 
                     if tdist2 < 400^2 then
 
55
                     if pdist2 < 400^2 then
76
56
                        if self.state == 'patrolling' then
77
57
                           self:chooseTarget()
78
58
                        elseif self.state == 'combat' then
112
92
                        the.enemies:remove(self)
113
93
                        the.app.view:remove(self.thrust)
114
94
                        the.app.view:remove(self.shield)
115
 
 
116
 
                        if other.source == the.player then
117
 
                           the.player.kills = the.player.kills + 1
118
 
                        end
119
 
 
120
 
                        -- and add a new enemy somewhere else...
121
 
                        local e = Enemy:new{x = math.random(the.bg.width),
122
 
                                            y = math.random(the.bg.height)}
123
 
                        the.enemies:add(e)
124
 
                        the.view:add(e.thrust)
125
 
                        the.view:add(e.shield)
126
95
                     else
127
96
                        self.shield:onHit()
128
97
                     end