fysplane/machinegun.lua

77 lines
1.7 KiB
Lua
Raw Normal View History

-- A generic machine gun which shoots all kinds of projectiles
Class = require 'hump/class'
require 'entities/vickers77'
require 'entities/tinyshot'
2014-10-19 03:19:01 +00:00
require 'entities/bigball'
GUNS = {
chaingun = {
interval = 0.01,
2014-10-19 00:08:25 +00:00
projectile = TinyShot,
speed = 50000,
2014-10-19 01:35:07 +00:00
damage = 5
},
2014-10-19 03:19:01 +00:00
bigball = {
interval = 0.2,
2014-10-19 03:19:01 +00:00
projectile = BigBall,
speed = 2000,
2014-10-19 03:19:01 +00:00
damage = 0
},
vickers77 = {
interval = 0.1,
2014-10-19 00:08:25 +00:00
projectile = Vickers77,
speed = 10000,
2014-10-19 01:35:07 +00:00
damage = 25
}
}
MachineGun = Class{
init = function(self, plane, type)
self:setType(type)
-- Plane this gun is attached to
self.plane = plane
self.shooting = false
end;
update = function(self, dt)
2014-10-19 03:19:01 +00:00
if self.shooting and self.since_last_shot >= self.gun['interval'] then
self:fire()
self.since_last_shot = 0
else
self.since_last_shot = self.since_last_shot + dt
end
end;
setType = function(self, type)
self.gun = GUNS[type]
self.since_last_shot = 0
end;
fire = function(self)
local gunPos = self.plane:getGunPosition()
local level = self.plane.level
local shot = self.gun['projectile'](gunPos[1], gunPos[2], level)
2014-10-18 23:15:52 +00:00
shot:setOwner(self.plane);
local angle = self.plane.body:getAngle()
shot.body:setAngle(angle)
2014-10-19 01:12:52 +00:00
local speeds = rad_dist_to_xy(angle, self.gun['speed'])
2014-10-19 01:12:52 +00:00
shot.body:setLinearVelocity(speeds[1], speeds[2])
end;
startShooting = function(self)
self.shooting = true
end;
stopShooting = function(self)
self.shooting = false
self.since_last_shot = 0
end;
}