Data Realms Fan Forums
http://45.55.195.193/

Ammo refilling
http://45.55.195.193/viewtopic.php?f=75&t=26024
Page 1 of 1

Author:  Urch [ Sat Oct 29, 2011 5:36 pm ]
Post subject:  Ammo refilling

I need a script for a fully automatic weapon that will allow its ammo to refill over time. i'm not sure on the exact rates i'm going to want for this, so it'd be nice if the script was easily customizable. the weapon will be firing AEmitters.

Author:  Asklar [ Sun Oct 30, 2011 6:50 pm ]
Post subject:  Re: Ammo refilling

Code:
function Create(self)
self.CoolTimer = Timer()
self.TimeToBeginCooling = 1000
self.FillUpTimer = Timer()
self.HowMuchTimeBetweenFills = 250
end
function Update(self)

if self.CoolTimer:IsPastSimMS(self.TimeToBeginCooling) then
    if self.FillUpTimer:IsPastSimMS(self.HowMuchTimeBetweenFills) then
     self.RoundCount = self.RoundCount + 1
     self.FillUpTimer:Reset()
    end
elseif self:IsActivated() then
    self.CoolTimer:Reset()
end


Just written it up, I'm not sure if it will work, but I can't see why it shouldn't.
Easily customizable.

Author:  Asklar [ Sun Oct 30, 2011 7:26 pm ]
Post subject:  Re: Ammo refilling

Which is the origin of semi-colons?
What's their use?

Author:  Asklar [ Sun Oct 30, 2011 8:14 pm ]
Post subject:  Re: Ammo refilling

I shall not, I always knew my fate;
Damn semi colons.

Author:  CaveCricket48 [ Sun Oct 30, 2011 8:54 pm ]
Post subject:  Re: Ammo refilling

Zoink.

Edit: Fixed some stuffs.
Code:
function Create(self)

   self.maxAmmo = 0;
   self.ammoCounter = 0;

   if self.Magazine ~= nil then
      self.maxAmmo = 1 + self.Magazine.RoundCount;
      self.ammoCounter = self.maxAmmo;
   end

   self.fireTimer = Timer();
   self.rechargeTimer = Timer();

   self.fireDelay = 100; -- delay between shots (MS)
   self.rechargeDelay = 200;

   self.fireVel = 50;
   self.fireSpread = 5; -- Degrees!
   self.fireSpreadSharp = 10;

end

function Update(self)

   if self:IsActivated() then
      if self.ammoCounter > 1 then
         if self.fireTimer:IsPastSimMS(self.fireDelay) then
            self.fireTimer:Reset();
            self.rechargeTimer:Reset();
            self.ammoCounter = self.ammoCounter - 1;

            if self.HFlipped == false then
               self.negativeNum = 1;
            else
               self.negativeNum = -1;
            end

            local actor = MovableMan:GetMOFromID(self.RootID);

            if MovableMan:IsActor(actor) and ToActor(actor):GetController():IsState(Controller.AIM_SHARP) then
               local projectile = CreateAEmitter("Round name");
               projectile.Pos = self.MuzzlePos;
               projectile.Vel = self.Vel + Vector(self.fireVel*self.negativeNum,0):RadRotate(self.RotAngle):DegRotate((self.fireSpread/(-2))+(math.random()*self.fireSpreadSharp));
               MovableMan:AddParticle(projectile);
            else
               local projectile = CreateAEmitter("Round name");
               projectile.Pos = self.MuzzlePos;
               projectile.Vel = self.Vel + Vector(self.fireVel*self.negativeNum,0):RadRotate(self.RotAngle):DegRotate((self.fireSpread/(-2))+(math.random()*self.fireSpread));
               MovableMan:AddParticle(projectile);
            end

         end
      else
         self:Reload();
      end
   else
      if self.ammoCounter < self.maxAmmo and self.rechargeTimer:IsPastSimMS(self.rechargeDelay) then
         self.rechargeTimer:Reset();
         self.ammoCounter = self.ammoCounter + 1;
      end
   end

   if self.Magazine ~= nil then
      self.Magazine.RoundCount = self.ammoCounter;
   end

end

Author:  trotskygrad [ Sun Nov 20, 2011 1:33 am ]
Post subject:  Re: Ammo refilling

Asklar wrote:
I shall not, I always knew my fate;
Damn semi colons.


it's good practice if you transition to C++/Java

that being said I am guilty of not using semicolons in lualand

Author:  Abdul Alhazred [ Sun Nov 20, 2011 11:14 am ]
Post subject:  Re: Ammo refilling

Lets have a look at what PIL says about semicolons before jumping to conclusions:
Programming in Lua wrote:
Each piece of code that Lua executes, such as a file or a single line in interactive mode, is a chunk. More specifically, a chunk is simply a sequence of statements.

A semicolon may optionally follow any statement. Usually, I use semicolons only to separate two or more statements written in the same line, but this is just a convention. Line breaks play no role in Lua's syntax; for instance, the following four chunks are all valid and equivalent:
Code:
    a = 1
    b = a*2
   
    a = 1;
    b = a*2;
   
    a = 1 ; b = a*2
   
    a = 1   b = a*2    -- ugly, but valid

i.e ending a line with a semicolon is redundant.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/