bzr branch
http://9ix.org/bzr/traderous
65
by Josh C
enemy is back |
1 |
-- player = 300 accel, 400 velLimit |
2 |
local accelLimit = 150 |
|
3 |
local velLimit = 200 |
|
7
by Josh C
enemy |
4 |
Enemy = Tile:extend { |
5 |
image = 'data/enemy.png', |
|
6 |
maxVelocity = {x=velLimit,y=velLimit}, |
|
7 |
minVelocity = {x=-velLimit, y=-velLimit}, |
|
65
by Josh C
enemy is back |
8 |
lastFired = 0, |
71
by Josh C
enemy "patrol path" (pick random destination) |
9 |
state = 'patrolling', |
65
by Josh C
enemy is back |
10 |
onNew = function (self) |
11 |
self.thrust = Tile:new{image = 'data/thrust.png'} |
|
66
by Josh C
enemy thrust |
12 |
--the.app.view:add(self.thrust) |
69
by Josh C
enemy shields and death |
13 |
self.shield = Shield:new{ship = self} |
71
by Josh C
enemy "patrol path" (pick random destination) |
14 |
|
15 |
self:chooseTarget() |
|
65
by Josh C
enemy is back |
16 |
end, |
17 |
onStartFrame = function (self) |
|
18 |
||
72
by Josh C
add enemy state switch |
19 |
local tvec = vector.new(self.target.x - self.x, |
71
by Josh C
enemy "patrol path" (pick random destination) |
20 |
self.target.y - self.y) |
65
by Josh C
enemy is back |
21 |
|
67
by Josh C
rotate enemy ship slower |
22 |
|
72
by Josh C
add enemy state switch |
23 |
local tAngle = math.atan2(tvec.y, tvec.x) |
24 |
local aDiff = self.rotation - tAngle |
|
67
by Josh C
rotate enemy ship slower |
25 |
|
26 |
while aDiff < (-math.pi) do |
|
27 |
--print(aDiff .. ' < -π') |
|
28 |
aDiff = aDiff + 2 * math.pi |
|
29 |
end |
|
30 |
while aDiff > math.pi do |
|
31 |
--print(aDiff .. ' > π') |
|
32 |
aDiff = aDiff - 2 * math.pi |
|
33 |
end |
|
34 |
||
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) |
|
39 |
else |
|
40 |
--print('not rotating') |
|
41 |
self.velocity.rotation = 0 |
|
42 |
end |
|
65
by Josh C
enemy is back |
43 |
|
72
by Josh C
add enemy state switch |
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 then |
|
54 |
self.state = 'combat' |
|
55 |
self.target = the.player |
|
56 |
end |
|
57 |
elseif pdist2 > (2 * 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 |
|
65
by Josh C
enemy is back |
67 |
self.acceleration = vector.new(300, 0) |
68 |
self.acceleration:rotate_inplace(self.rotation) |
|
69 |
self.thrust.visible = true |
|
70 |
else |
|
71 |
self.acceleration = {x=0, y=0} |
|
72 |
self.thrust.visible = false |
|
73 |
end |
|
74 |
||
72
by Josh C
add enemy state switch |
75 |
if tdist2 < 400^2 then |
71
by Josh C
enemy "patrol path" (pick random destination) |
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 |
|
65
by Josh C
enemy is back |
91 |
end |
92 |
||
93 |
end, |
|
7
by Josh C
enemy |
94 |
onUpdate = function(self) |
65
by Josh C
enemy is back |
95 |
self.thrust.x = self.x - self.width |
96 |
self.thrust.y = self.y |
|
97 |
self.thrust.rotation = self.rotation |
|
98 |
end, |
|
69
by Josh C
enemy shields and death |
99 |
onCollide = function (self, other) |
100 |
if other:instanceOf(Bullet) and other.source ~= self then |
|
101 |
if self.shield.strength == 0 then |
|
102 |
-- die |
|
103 |
the.app.view:add( Boom:new { |
|
104 |
x = self.x, |
|
105 |
y = self.y, |
|
106 |
velocity = { rotation = 5 } |
|
107 |
}) |
|
108 |
||
109 |
self:die() |
|
110 |
self.thrust:die() |
|
111 |
self.shield:die() |
|
112 |
the.enemies:remove(self) |
|
113 |
the.app.view:remove(self.thrust) |
|
114 |
the.app.view:remove(self.shield) |
|
115 |
else |
|
116 |
self.shield:onHit() |
|
117 |
end |
|
118 |
||
119 |
other:die() |
|
120 |
the.bullets:remove(other) |
|
121 |
end |
|
71
by Josh C
enemy "patrol path" (pick random destination) |
122 |
end, |
123 |
chooseTarget = function (self) |
|
124 |
self.target = { |
|
125 |
x = math.random(the.bg.width), |
|
126 |
y = math.random(the.bg.height) |
|
127 |
} |
|
128 |
end |
|
7
by Josh C
enemy |
129 |
} |