-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweapon.lua
More file actions
284 lines (223 loc) · 6.91 KB
/
Copy pathweapon.lua
File metadata and controls
284 lines (223 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
-- This file is part of SpaceShooter.
--
-- SpaceShooter is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- SpaceShooter is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with SpaceShooter. If not, see <http://www.gnu.org/licenses/>.
-- Pivot a weapon within its range to point at a target
local function target_weapon (weapon, target)
local angle
local limit = 0.78 -- Pi / 4
angle = angle_between (weapon.owner, target)
angle = angle - weapon.owner.angle
angle = reduce_angle (angle)
if (angle > limit) then
angle = limit
elseif (angle < -limit) then
angle = -limit
end
weapon.angle = angle
end
Shot = class ()
Shot.__name = "Shot"
function Shot:__init(weapon)
self.dist = 0
self.accel = 0
self.min_speed = 0
self.velocity = {}
self.harms = {}
self.collects = {}
self.angle = weapon.owner.angle
self.velocity.angle = weapon.owner.angle + weapon.angle
local pos = weapon.owner.angle + weapon.mount_pos
self.x = weapon.owner.x + weapon.owner.rad * math.cos (pos)
self.y = weapon.owner.y + weapon.owner.rad * math.sin (pos)
self.update = function (self, dt)
update_pos (self, dt)
if (self.dist > shot_range) then
shots[self] = nil
else
wrap_edges (self)
end
end
self.hit = function (self)
shots[self] = nil
end
end
CannonShot = Shot:extends ()
CannonShot.__name = "CannonShot"
function CannonShot:__init (weapon)
CannonShot.super.__init(self, weapon)
self.rad = 8
self.bounds = {}
self.bounds.rad = self.rad
self.draw = function (self)
icon_draw (self, icons.shot)
end
end
function update_target (shot)
local best_dist
local options
local target
options = get_targets (shot)
for option in pairs(options) do
local dist = get_dist (shot, option)
if (not best_dist or dist < best_dist) then
best_dist = dist
target = option
end
end
return target
end
MissileShot = Shot:extends ()
MissileShot.__name = "MissileShot"
function MissileShot:__init (weapon)
MissileShot.super.__init(self, weapon)
self.velocity.speed = 200
self.accel = 600
self.max_speed = 150
self.guidance_dist = 50
self.weapon = weapon
self.rad = 12
self.bounds = {}
self.bounds.rad = 10
self.destroy = function (self, dt)
shots[self] = nil
make_explosion (self.x, self.y, self.rad)
end
self.hit_with = function (self, object)
self:destroy ()
end
self.update = function (self, dt)
if (self.max_dist and self.dist > self.max_dist) then
self:destroy ()
elseif (self.dist > self.guidance_dist) then
self.target = update_target (self)
if (not self.target) then
self:destroy ()
else
self.angle = angle_between (self, self.target)
accelerate (self, dt)
end
end
check_limits (self)
update_pos (self, dt)
end
self.draw = function (self)
icon_draw (self, self.icon)
end
end
PlayerMissileShot = MissileShot:extends ()
PlayerMissileShot.__name = "PlayerMissileShot"
function PlayerMissileShot:__init (weapon)
PlayerMissileShot.super.__init(self, weapon)
self.harms["enemies"] = true
self.icon = icons.player_missile
self.max_dist = 600
end
EnemyMissileShot = MissileShot:extends ()
EnemyMissileShot.__name = "EnemyMissileShot"
function EnemyMissileShot:__init (weapon)
EnemyMissileShot.super.__init(self, weapon)
self.harms["player"] = true
self.icon = icons.enemy_missile
self.max_dist = 600
end
PlayerCannonShot = CannonShot:extends ()
PlayerCannonShot.__name = "PlayerCannonShot"
function PlayerCannonShot:__init (weapon)
PlayerCannonShot.super.__init(self, weapon)
self.velocity.speed = 1000
self.harms["enemies"] = true
self.harms["enemymissiles"] = true
self.collects["powerups"] = true
end
EnemyCannonShot = CannonShot:extends ()
EnemyCannonShot.__name = "EnemyCannonShot"
function EnemyCannonShot:__init (weapon)
EnemyCannonShot.super.__init(self, weapon)
self.velocity.speed = 250
self.harms["player"] = true
self.harms["playermissiles"] = true
end
Weapon = class ()
Weapon.__name = "Weapon"
function Weapon:__init(owner, rate)
self.fire_rate = rate
self.next_fire = game_time + (1 / (rate * 2))
-- Ship that owns the weapon
self.owner = owner
-- Angle which the weapon points
self.angle = 0
-- Max angle that weapon can pivot to
self.angle_limit = 0
-- Angle at which the weapon is mounted on the ship (determines initial
-- shot position)
self.mount_pos = 0
self.fire = function ()
local rate = self.fire_rate * self.get_multiplier ()
-- Don't fire faster than max rate
if (game_time < self.next_fire) then
return
end
local shot = self.shoot (self)
shots[shot] = true
self.next_fire = game_time + (1 / rate)
end
end
Cannon = Weapon:extends ()
Cannon.__name = "Cannon"
function Cannon:__init(owner, rate)
Cannon.super.__init(self, owner, rate)
self.get_multiplier = function (self)
return owner.cannon_fire_mult
end
end
Missile = Weapon:extends ()
Missile.__name = "Missile"
function Missile:__init(owner, rate)
Missile.super.__init(self, owner, rate)
self.get_multiplier = function (self)
return owner.missile_fire_mult
end
end
PlayerCannon = Cannon:extends ()
PlayerCannon.__name = "PlayerCannon"
function PlayerCannon:__init(owner, pos, angle)
PlayerCannon.super.__init(self, owner, 5)
self.shoot = PlayerCannonShot
self.angle = angle
self.mount_pos = pos
end
PlayerMissile = Missile:extends ()
PlayerMissile.__name = "PlayerMissile"
function PlayerMissile:__init(owner, pos, angle)
PlayerMissile.super.__init(self, owner, 0.25)
self.shoot = PlayerMissileShot
self.angle = angle
self.mount_pos = pos
end
EnemyCannon = Cannon:extends ()
EnemyCannon.__name = "EnemyCannon"
function EnemyCannon:__init(owner)
EnemyCannon.super.__init(self, owner, 0.5)
self.shoot = EnemyCannonShot
self.angle_limit = 0.78 -- Pi / 4
self.update = function (self)
target_weapon (self, player)
end
end
EnemyMissile = Missile:extends ()
EnemyMissile.__name = "EnemyMissile"
function EnemyMissile:__init(owner)
EnemyMissile.super.__init(self, owner, 0.25)
self.shoot = EnemyMissileShot
end