Last active
December 15, 2015 10:39
-
-
Save error454/5247680 to your computer and use it in GitHub Desktop.
Simple value cycling with wrapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------------------------------------------------------------------------------- | |
-- Function......... : cycleValue | |
-- Author........... : Zachary Burke | |
-- Description...... : Increases or decreases a variable, handles wrapping the | |
-- value as well. | |
-- | |
-- Examples: | |
-- Say you have a variable nCounter where the max value is 5 and the minimum is 0 | |
-- | |
-- Increment | |
-- nCounter = 4 | |
-- nCounter = this.cycleValue ( nCounter, 0, 5, true, false ) | |
-- Before: 4 | |
-- After: 5 | |
-- Increment but don't wrap | |
-- nCounter = 5 | |
-- nCounter = this.cycleValue ( nCounter, 0, 5, true, false ) | |
-- Before: 5 | |
-- After: 5 | |
-- Decrement and don't wrap | |
-- nCounter = 4 | |
-- nCounter = this.cycleValue ( nCounter, 0, 5, false, false ) | |
-- Before: 4 | |
-- After: 3 | |
-- Increment with wrapping | |
-- nCounter = 5 | |
-- nCounter = this.cycleValue ( nCounter, 0, 5, true, true ) | |
-- Before: 5 | |
-- After: 0 | |
-------------------------------------------------------------------------------- | |
-------------------------------------------------------------------------------- | |
function MainMenuAI.cycleValue ( nValue, nMin, nMax, bIncrement, bWrap ) | |
-------------------------------------------------------------------------------- | |
local incrementAmount = bIncrement and 1 or -1 | |
nValue = nValue + incrementAmount | |
if(bWrap) | |
then | |
if(nValue > nMax) | |
then | |
return nMin | |
elseif(nValue < nMin) | |
then | |
return nMax | |
end | |
end | |
if(bIncrement) | |
then | |
return math.min ( nValue, nMax ) | |
else | |
return math.max ( nValue, nMin ) | |
end | |
-------------------------------------------------------------------------------- | |
end | |
-------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment