1
local accelLimit = 300 * 2 -- same as the player, yes?
2
local velLimit = 600 -- same as player
1
-- player = 300 accel, 400 velLimit
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},
10
onNew = function (self)
11
self.thrust = Tile:new{image = 'data/thrust.png'}
12
--the.app.view:add(self.thrust)
13
self.shield = Shield:new{ship = self}
17
onStartFrame = function (self)
19
local pvec = vector.new(self.target.x - self.x,
20
self.target.y - self.y)
23
local pAngle = math.atan2(pvec.y, pvec.x)
24
local aDiff = self.rotation - pAngle
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
local pdist2 = pvec:len2()
46
if pdist2 > 400^2 then -- ... if player dist > 64?
47
self.acceleration = vector.new(300, 0)
48
self.acceleration:rotate_inplace(self.rotation)
49
self.thrust.visible = true
51
self.acceleration = {x=0, y=0}
52
self.thrust.visible = false
55
if pdist2 < 400^2 then
56
if self.state == 'patrolling' then
58
elseif self.state == 'combat' then
59
if math.abs(aDiff) < 0.5 * math.pi and
60
love.timer.getTime() - self.lastFired > 0.25 then
63
x = self.x + self.width / 2,
64
y = self.y + self.height / 2,
65
rotation = self.rotation,
68
self.lastFired = love.timer.getTime()
7
74
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
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
21
self.acceleration.x = ax
22
self.acceleration.y = ay
24
self.rotation = math.atan2(self.velocity.y, self.velocity.x)
75
self.thrust.x = self.x - self.width
76
self.thrust.y = self.y
77
self.thrust.rotation = self.rotation
79
onCollide = function (self, other)
80
if other:instanceOf(Bullet) and other.source ~= self then
81
if self.shield.strength == 0 then
83
the.app.view:add( Boom:new {
86
velocity = { rotation = 5 }
92
the.enemies:remove(self)
93
the.app.view:remove(self.thrust)
94
the.app.view:remove(self.shield)
100
the.bullets:remove(other)
103
chooseTarget = function (self)
105
x = math.random(the.bg.width),
106
y = math.random(the.bg.height)
b'\\ No newline at end of file'