Sign in to follow this  
slayemin

Unity Website for code library?

Recommended Posts

I've developed a class called "Range" which keeps track of a number between a min and max number. It's useful for keeping track of any data which could be represented by a progress bar (hitpoints, mana, cooldown time, etc) and generating a random number within the range. I think other people would find it useful within their projects, so I'd like to share it. Is there a section on this site for freely sharing reusable code? In case there isn't, I'm copy/pasting it below. You're free to use it in any way with no strings attached and no guarantee of fitness/correctness.

Note: My code sample is relatively trivial and generic enough to be usable in any game/app. It's 95% tested, but not very rigorously as it should be.

[b]Range.cs - C# XNA Version:[/b]
[CODE]
using System;
using Microsoft.Xna.Framework;
namespace EricsLib
{
/// <summary>
/// The range class gives you a number which is between a min and max number.
/// Usages: -Keeping track of hitpoints or similar metrics
/// -keeping track of ability cool down timers
/// -Generating random numbers within a range
/// -Storing direction angles or other circular data (use looping values)
/// </summary>
public class Range
{
float m_max, m_min; //the thresholds for the meter value
float m_regen; //how many points you want to regenerate/degenerate every second
float m_current; //the current value of the meter
bool m_loopValues; //if this is 'true', then the current value will loop between min and max values instead of being capped.
public Range()
{
m_max = 100;
m_min = 0;
m_current = 100;
m_regen = 0;
m_loopValues = false;
}
/// <summary>
/// Creates a range with the current value at max value and min at zero
/// </summary>
/// <param name="max">the highest range value allowed</param>
public Range(float max)
{
m_max = max;
m_current = max;
m_min = 0.0f;
m_loopValues = false;
}
/// <summary>
/// creates a range value with the current value defaulted to the max value
/// </summary>
/// <param name="min">lowest range value</param>
/// <param name="max">highest range value</param>
public Range(float min, float max)
{
m_max = max;
m_min = min;
m_current = max;
m_loopValues = false;
}
/// <summary>
/// Creates a range
/// </summary>
/// <param name="min">lowest range value</param>
/// <param name="max">highest range value</param>
/// <param name="current">current range value</param>
public Range(float min, float max, float current)
{
m_max = max;
m_min = min;
m_current = current;
m_loopValues = false;
}
/// <summary>
/// creates a range value with regeneration/decay value set so the current value increases/decreases over time
/// </summary>
/// <param name="min">lowest range value</param>
/// <param name="max">highest range value</param>
/// <param name="current">current value in range</param>
/// <param name="regen">the change in current value over time</param>
public Range(float min, float max, float current, float regen)
{
m_max = max;
m_min = min;
m_current = current;
m_regen = regen;
m_loopValues = false;
}
public Range(Range Copy)
{
if (Copy == null)
return;
m_max = Copy.m_max;
m_min = Copy.m_min;
m_current = Copy.m_current;
m_regen = Copy.m_regen;
m_loopValues = Copy.m_loopValues;
}
/// <summary>
/// The current value will always be between min and max values
/// </summary>
public float Current
{
get
{
return m_current; //E: I'm assuming this gets truncated.
}
set
{
//do some quick bounds checking
if (value > m_max) //value is greater than specified max value
{
if (m_loopValues)
{
//the remaining looped value should be seamlessly added to the minimum value if it overflows the max
m_current = m_min + (value % m_max); //tested
}
else
{
m_current = m_max;
}
}
else if (value < m_min) //value is less than specified minimum value
{
if (m_loopValues)
{
m_current = m_min + (value % (m_max - m_min)); //tested
}
else
{
m_current = m_min;
}
}
else
m_current = value;
}
}
/// <summary>
/// Sets the current value to a random number between the min and max range values
/// </summary>
/// <param name="SetCurrent">Flag on whether you want to set the current value to the randomly generated number</param>
/// <returns>the value of current</returns>
public float Random(bool SetCurrent)
{
double d = Calc.random.NextDouble();
float diff = m_max - m_min;
diff *= (float)d;
if (SetCurrent == true)
{
m_current = m_max - diff;
}
return m_max - diff;
}
/// <summary>
/// Returns a random float which lies between min and max value of the range.
/// </summary>
/// <returns></returns>
public float Random()
{
return Random(false);
}
public float Max
{
get
{
return m_max;
}
set
{
m_max = value;
}
}
public float Min
{
get
{
return m_min;
}
set
{
m_min = value;
}
}
public float Half
{
get
{
return (m_min + m_max) / 2.0f;
}
}
/// <summary>
/// Per second regeneration/decay value for the current value of the meter
/// </summary>
public float RegenRate
{
get
{
return m_regen;
}
set
{
m_regen = value;
}
}
public void Update(GameTime gameTime)
{
//are we regenerating/decaying over time?
if (m_regen != 0)
{
Current += m_regen * (gameTime.ElapsedGameTime.Milliseconds);
}
}
public void SetToMax()
{
m_current = m_max;
}
public void SetToMin()
{
m_current = m_min;
}
public bool IsEmpty()
{
return (m_current <= m_min);
}
/// <summary>
/// Set/Get the current value as a percentage of the min and max values
/// </summary>
public float Percent
{
get
{
return ((m_current - m_min) / (m_max - m_min)) * 100;
}
set
{
m_current = m_min + ((value / 100) * (m_max - m_min));
}
}
/// <summary>
/// if this is 'true', then the current value will loop between min and max values instead of being capped.
/// Default: false
/// </summary>
public bool LoopValues
{
get
{
return m_loopValues;
}
set
{
m_loopValues = value;
}
}
/// <summary>
/// Tells you if the current range value is at the maximum value
/// </summary>
/// <returns></returns>
public bool IsMax()
{
return m_current == m_max;
}
/// <summary>
/// Tells you if the current range value is at the minimum value
/// </summary>
/// <returns></returns>
public bool IsMin()
{
return m_current == m_min;
}
}
}
[/CODE]

[b]Range.cs - C# Unity3D version:[/b]
[CODE]
using System;
using UnityEngine;
namespace EricsLib
{
/// <summary>
/// The range class gives you a number which is between a min and max number.
/// Usages: -Keeping track of hitpoints or similar metrics
/// -keeping track of ability cool down timers
/// -Generating random numbers within a range
/// -Storing direction angles or other circular data (use looping values)
/// </summary>
public class Range
{
float m_max, m_min; //the thresholds for the meter value
float m_regen; //how many points you want to regenerate/degenerate every second
float m_current; //the current value of the meter
bool m_loopValues; //if this is 'true', then the current value will loop between min and max values instead of being capped.
public Range()
{
m_max = 100;
m_min = 0;
m_current = 100;
m_regen = 0;
m_loopValues = false;
}
/// <summary>
/// Creates a range with the current value at max value and min at zero
/// </summary>
/// <param name="max">the highest range value allowed</param>
public Range(float max)
{
m_max = max;
m_current = max;
m_min = 0.0f;
m_loopValues = false;
}
/// <summary>
/// creates a range value with the current value defaulted to the max value
/// </summary>
/// <param name="min">lowest range value</param>
/// <param name="max">highest range value</param>
public Range(float min, float max)
{
m_max = max;
m_min = min;
m_current = max;
m_loopValues = false;
}
/// <summary>
/// Creates a range
/// </summary>
/// <param name="min">lowest range value</param>
/// <param name="max">highest range value</param>
/// <param name="current">current range value</param>
public Range(float min, float max, float current)
{
m_max = max;
m_min = min;
m_current = current;
m_loopValues = false;
}
/// <summary>
/// creates a range value with regeneration/decay value set so the current value increases/decreases over time
/// </summary>
/// <param name="min">lowest range value</param>
/// <param name="max">highest range value</param>
/// <param name="current">current value in range</param>
/// <param name="regen">the change in current value over time</param>
public Range(float min, float max, float current, float regen)
{
m_max = max;
m_min = min;
m_current = current;
m_regen = regen;
m_loopValues = false;
}
public Range(Range Copy)
{
if (Copy == null)
return;
m_max = Copy.m_max;
m_min = Copy.m_min;
m_current = Copy.m_current;
m_regen = Copy.m_regen;
m_loopValues = Copy.m_loopValues;
}
/// <summary>
/// The current value will always be between min and max values
/// </summary>
public float Current
{
get
{
return m_current; //E: I'm assuming this gets truncated.
}
set
{
//do some quick bounds checking
if (value > m_max) //value is greater than specified max value
{
if (m_loopValues)
{
//the remaining looped value should be seamlessly added to the minimum value if it overflows the max
m_current = m_min + (value % m_max); //tested
}
else
{
m_current = m_max;
}
}
else if (value < m_min) //value is less than specified minimum value
{
if (m_loopValues)
{
m_current = m_min + (value % (m_max - m_min)); //tested
}
else
{
m_current = m_min;
}
}
else
m_current = value;
}
}
/// <summary>
/// Sets the current value to a random number between the min and max range values
/// </summary>
/// <param name="SetCurrent">Flag on whether you want to set the current value to the randomly generated number</param>
/// <returns>the value of current</returns>
public float Random(bool SetCurrent)
{
double d = Calc.random.NextDouble();
float diff = m_max - m_min;
diff *= (float)d;
if (SetCurrent == true)
{
m_current = m_max - diff;
}
return m_max - diff;
}
/// <summary>
/// Returns a random float which lies between min and max value of the range.
/// </summary>
/// <returns></returns>
public float Random()
{
return Random(false);
}
public float Max
{
get
{
return m_max;
}
set
{
m_max = value;
}
}
public float Min
{
get
{
return m_min;
}
set
{
m_min = value;
}
}
public float Half
{
get
{
return (m_min + m_max) / 2.0f;
}
}
/// <summary>
/// Per second regeneration/decay value for the current value of the meter
/// </summary>
public float RegenRate
{
get
{
return m_regen;
}
set
{
m_regen = value;
}
}
public void Update()
{
//are we regenerating/decaying over time?
if (m_regen != 0)
{
Current += m_regen * Time.deltaTime;
}
}
public void SetToMax()
{
m_current = m_max;
}
public void SetToMin()
{
m_current = m_min;
}
public bool IsEmpty()
{
return (m_current <= m_min);
}
/// <summary>
/// Set/Get the current value as a percentage of the min and max values
/// </summary>
public float Percent
{
get
{
return ((m_current - m_min) / (m_max - m_min)) * 100;
}
set
{
m_current = m_min + ((value / 100) * (m_max - m_min));
}
}
/// <summary>
/// if this is 'true', then the current value will loop between min and max values instead of being capped.
/// Default: false
/// </summary>
public bool LoopValues
{
get
{
return m_loopValues;
}
set
{
m_loopValues = value;
}
}
/// <summary>
/// Tells you if the current range value is at the maximum value
/// </summary>
/// <returns></returns>
public bool IsMax()
{
return m_current == m_max;
}
/// <summary>
/// Tells you if the current range value is at the minimum value
/// </summary>
/// <returns></returns>
public bool IsMin()
{
return m_current == m_min;
}
}
}
[/CODE]

Share this post


Link to post
Share on other sites
If you want to share code publicly, the proper place to do it is on something like github or bitbucket. And instead of saying "You're free to use it in any way with no strings attached and no guarantee of fitness/correctness", it would be better include a proper source code license file with your code.

Also, if you want your code to be "tested", you should actually write tests for it. If you don't know how, do some research on unit testing and NUnit. You can include the test code with your code above. Otherwise, I not consider this to be "tested" code, now matter how many manual tests you performed.

Hope that helps.

Share this post


Link to post
Share on other sites
[quote name='thok' timestamp='1348610184' post='4983759']
And instead of saying "You're free to use it in any way with no strings attached and no guarantee of fitness/correctness", it would be better include a proper source code license file with your code.[/quote]
Agreed.

[b]@slayemin[/b]: Here's some licenses that you might want:

Suggestion for "[i]free to do whatever[/i]": [url="http://en.wikipedia.org/wiki/MIT_License"]MIT license[/url]
Suggestion for "[i]free to do whatever but give me credit and share changes[/i]": [url="http://www.gnu.org/copyleft/lesser.html"]LGPL[/url]

Suggestion for "[i]I don't care [/i][i]at all[/i][i], just use the code if you want![/i]": [url="http://en.wikipedia.org/wiki/Public_domain"]Public domain[/url] ((this isn't actually a license) [size=2]This only works for countries that have public domain, and you can just ignore the countries that don't - people in those countries will use it anyway and it becomes their problem[/size])

For art, music, photographs, and other non-code works, see [url="http://creativecommons.org/"]Creative Commons[/url]. Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628354
    • Total Posts
      2982237
  • Similar Content

    • By koto
      Hello,

      I'm developing a roguelike game. Currently it's in a playable state and I think it's a high time to show it and hopefully get some feedback.

      The game has following features:
      - turn based
      - procedurally generated levels
      - tons of loot
      - spells system
      - trading system
      - some quests
      - some limited crafting
      - door/key/lever mechanics
      - quite a lot of different (look/behaviour) monsters

      Trailer:
      Demo: http://kotogames.com/wp-content/uploads/2017/11/OuaD_demo.zip
      Regards,
      Tom
       
    • By Afambus
      Hello,
      I have a full experienced team that looking for a composer and another 3d designer, im expecting as much from you there are other people in this server that can help you we have a determined team, we have done many things in our game there is more information in the discord about everyone and there skills there region and about the game, if you join the discord pm asap
      https://discord.gg/b2teN3m
    • By BAG Labs
      Mobile SoS

      Platform: Android
      Genre: Board
      Link: Google Play
       
      This games sharpen memory and test your strategies to place S-O-S pattern within time limit and serve 3 difficulties as Easy, Normal, and Hard.

      Goals of the game is to put S-O-S words in patterns (Horizontal, Vertical, and Diagonal) alternately with enemy.
       
      Features:
      Single Player Multiplayer Achievements Leaderboards  
       
      Screenshot:

       

       

       
      Link: Google Play
       
      Please help us improve this game with review
    • By abarnes
      Hello!
      I am a game development student in my second year of a three year program and I would like to start building my portfolio. I was thinking of creating some games to show what I can do to potential employers since I wont have any work related experience when I graduate. But as I'm sure you all know there are tons of ways to approach developing/designing a game and I'm curious if anyone had any insight as to any "standards" that come with this? Is it okay to use game engines like Unity, Unreal, Game Maker etc? Or would it be better to make a game from scratch to better show case your skills? Any and all advice will be greatly appreciated!
    • By Hilster
      Hello 2D Artists,
      I've started making a 2D Puzzle Adventure game for mobile and I'm looking for someone who would want in on creating assets for the game. The core of the programming is pretty much complete, you can walk within the grid laid out and push boxes, when there is an object on top of a pressure pad it will activate the linked objects or if there is one object with multiple linked pressure pads it requires you to activate all points for the object to become active. 

      The level iteration for the game is quick and simple, a Photoshop file that is made of individual pixels that represents objects is put into the game and it creates the level out of those pixels with the assigned objects.
      The objects that need sprites created so far is the character, box, pressure pad, door, trap door, the walls, the stairs and the tiled background.
      I intend to add more objects so the amount I'd like to add will be extended.
      My motivations for posting here is to have something that looks nice to be able to display on my portfolio, so if you're looking for a working game that you can place your art into and improve the look of your portfolio then we're in business.
      Please reply with a few past examples of your art below and I'll be in touch!
  • Popular Now