1
1
-- player = 300 accel, 400 velLimit
2
2
local accelLimit = 150
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},
10
9
onNew = function (self)
11
10
self.thrust = Tile:new{image = 'data/thrust.png'}
12
11
--the.app.view:add(self.thrust)
13
self.shield = Shield:new{ship = self}
17
13
onStartFrame = function (self)
19
local tvec = vector.new(self.target.x - self.x,
20
self.target.y - self.y)
23
local tAngle = math.atan2(tvec.y, tvec.x)
24
local aDiff = self.rotation - tAngle
26
while aDiff < (-math.pi) do
27
--print(aDiff .. ' < -π')
28
aDiff = aDiff + 2 * math.pi
30
while aDiff > math.pi do
31
--print(aDiff .. ' > π')
32
aDiff = aDiff - 2 * math.pi
35
if math.abs(aDiff) > 0.1 then
36
-- rotation really shouldn't be negative. I
37
-- don't understand why that is. :\
38
self.velocity.rotation = -2 * util.signOf(aDiff)
40
--print('not rotating')
41
self.velocity.rotation = 0
44
if self.state == 'combat' and not the.player.active then
45
self.state = 'patrolling'
48
local pdist2 = vector.new(the.player.x - self.x,
49
the.player.y - self.y):len2()
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 then
55
self.target = the.player
57
elseif pdist2 > (3 * the.app.width)^2 then
58
if self.state == 'combat' then
59
self.state = 'patrolling'
64
local tdist2 = tvec:len2()
66
if tdist2 > 400^2 then
15
local pvec = vector.new(the.player.x - self.x,
16
the.player.y - self.y)
18
self.rotation = math.atan2(pvec.y, pvec.x)
20
local pdist2 = pvec:len2()
22
if pdist2 > 400^2 then -- ... if player dist > 64?
67
23
self.acceleration = vector.new(300, 0)
68
24
self.acceleration:rotate_inplace(self.rotation)
69
25
self.thrust.visible = true
72
28
self.thrust.visible = false
75
if tdist2 < 400^2 then
76
if self.state == 'patrolling' then
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
83
x = self.x + self.width / 2,
84
y = self.y + self.height / 2,
85
rotation = self.rotation,
88
self.lastFired = love.timer.getTime()
32
love.timer.getTime() - self.lastFired > 0.25 then
35
x = self.x + self.width / 2,
36
y = self.y + self.height / 2,
37
rotation = self.rotation
39
self.lastFired = love.timer.getTime()
96
45
self.thrust.y = self.y
97
46
self.thrust.rotation = self.rotation
99
onCollide = function (self, other)
100
if other:instanceOf(Bullet) and other.source ~= self then
101
if self.shield.strength == 0 then
103
the.app.view:add( Boom:new {
106
velocity = { rotation = 5 }
112
the.enemies:remove(self)
113
the.app.view:remove(self.thrust)
114
the.app.view:remove(self.shield)
120
the.bullets:remove(other)
123
chooseTarget = function (self)
125
x = math.random(the.bg.width),
126
y = math.random(the.bg.height)
b'\\ No newline at end of file'