Projectile = Entity:createSubClass();

function Projectile:init()
   self:setForceUpdate(true);
   self:setBlockable(true);
   self.lifeTime = 32;

   self.isBullet = true;
   self.editorProperties = {
      faction = "Neutral";
   }

   self:setLayer(3);

end

function Projectile:update()
   
   self.lifeTime = self.lifeTime - 1;

   if self.lifeTime < 0 then
      self:die();
   end

end

function Projectile:collided(otherEnt)

   local ent = entitiesByName[otherEnt];
   if ent then
      if ent.isActor and ent.editorProperties and not (ent.editorProperties.faction == self.editorProperties.faction) then

         -- We hit an enemy. Let their collision code deal with damage taken.

         local explosion = Explosion:create();
         explosion:setPos(self:getPos());

         self:die();
         
      end
   end

end

function Projectile:collidedWithWorld()

   local explosion = Explosion:create();
   explosion:setLayer(self:getLayer());
   explosion:setPos(self:getPos());

   self:die();

end


Bullet = Projectile:createSubClass();
registerSpawn("Bullet");

function Bullet:init()

   Projectile.init(self);

   self:setTexture("oldsprites.tga");

   self:setSpriteScale(2);

   setSpriteFromDef(self, 10, 4);

end

function shootBullet(entSrc, targetX, targetY)

   local x, y = entSrc:getPos();

   local shotSpeed = 32;
   local shotOffset = 32;

   local newShot = Bullet:create();

   local mx, my = targetX, targetY;
   local mdx = mx - x;
   local mdy = my - y;
   local mdist = math.sqrt(mdx * mdx + mdy * mdy);
   mdx = mdx / mdist;
   mdy = mdy / mdist;

   x = x + mdx;
   y = y + mdy;
   newShot:setPos(x + mdx * shotOffset, y + mdy * shotOffset);
   newShot:setVel(mdx * (shotSpeed), mdy * (shotSpeed));
   newShot:setSpriteAngle(90 + math.atan2(mdx, mdy) * -(180 / 3.14));

   local newLight = Light_Basic:create();
   newLight:setLinkTarget(newShot:getName());

   if entSrc.editorProperties then
      newShot.editorProperties.faction = entSrc.editorProperties.faction;
   end

end



