Skip to content

Commit

Permalink
Merge pull request #8 from 01010111/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
01010111 committed Sep 17, 2019
2 parents 9c86c6b + 82c2bb5 commit 8ae4242
Show file tree
Hide file tree
Showing 19 changed files with 949 additions and 678 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Then you'll be set!
## Usage
This library is split into two distinct sections, Extensions, and Utilities.

Check the WIKI for specific usage instructions!
Check the [API](http://01010111.com/zerolib/) for specific usage instructions!

### .extensions
Extensions are a neat language feature of Haxe. They can add functionality to preexisting classes.
Expand All @@ -32,5 +32,4 @@ Learn more about using Extensions [here](https://github.com/01010111/zerolib/wik
### .utilities
A handful of utilities like vectors, a pseudo-random number generator, a tiny ECS implementation, and more!

**[See example usage in the Wiki](https://github.com/01010111/zerolib/wiki)**
**[Browse the API](http://01010111.com/zerolib/)**
2 changes: 0 additions & 2 deletions src/Main.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import zero.utilities.Vec2;

class Main {
public static function main() {
trace('howdy world');
Expand Down
64 changes: 35 additions & 29 deletions zero/extensions/ArrayExt.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ using Math;

/**
* A collection of extension methods for Arrays (some of these are only usable on arrays containing specific types)
*
* **Usage:**
*
* - use this extension by adding this where you normally import modules: `using zero.extensions.ArrayExt;`
* - now you can use any of these functions on different arrays: `[0, 1, 2, 3].get_random(); // 2`
* - or use all of the extensions in this library by adding: `using zero.extensions.Tools;`
*/
class ArrayExt
{

/**
* Converts an array of strings to an array of itegers
* @param array input array
* @return Array<Int>
* @param array input array
* @return Array<Int>
*/
public static inline function strings_to_ints(array:Array<String>):Array<Int> return [for (s in array) Std.parseInt(s)];

/**
* Checks whether or not an array contains a value or object
* @param array input array
* @param value value to check
* @return Bool
* @param array input array
* @param value value to check
* @return Bool
*/
public static inline function contains(array:Array<Dynamic>, value:Dynamic):Bool return array.indexOf(value) >= 0;

Expand All @@ -35,15 +41,15 @@ class ArrayExt

/**
* Returns a random element from an array
* @param array input array
* @return Dynamic
* @param array input array
* @return Dynamic
*/
public static inline function get_random(array:Array<Dynamic>):Dynamic return array[array.length.get_random().to_int()];

/**
* shuffles an array in place and returns it
* @param array input array
* @return Array<T>
* @param array input array
* @return Array<T>
*/
public static function shuffle<T>(array:Array<T>):Array<T>
{
Expand All @@ -60,9 +66,9 @@ class ArrayExt

/**
* Merges a second array (of the same type) into the first array
* @param a1 first array
* @param a2 second array
* @return Array<T>
* @param a1 first array
* @param a2 second array
* @return Array<T>
*/
public static function merge<T>(a1:Array<T>, a2:Array<T>):Array<T>
{
Expand Down Expand Up @@ -97,9 +103,9 @@ class ArrayExt
/**
* Uses a flood-fill algorithm to change equal values contiguous to the input coordinates to a new value
* @param array input array
* @param x x coordinate
* @param y y coordinate
* @param value new value
* @param x x coordinate
* @param y y coordinate
* @param value new value
*/
public static function flood_fill_2D(array:Array<Array<Dynamic>>, x:Int, y:Int, value:Dynamic)
{
Expand All @@ -121,9 +127,9 @@ class ArrayExt

/**
* Uses a flood-fill algorithm to change equal values contiguous to the input position to a new value
* @param array input array
* @param pos index position
* @param value new value
* @param array input array
* @param pos index position
* @param value new value
*/
public static function flood_fill_1D(array:Array<Dynamic>, pos:Int, value:Dynamic)
{
Expand All @@ -143,10 +149,10 @@ class ArrayExt

/**
* Uses a flood-fill type algorithm to generate a heat map from the coordinates
* @param array input array
* @param x x coordinate
* @param y y coordinate
* @param max_value max heat value, -1 will find the max value based on the minimum result
* @param array input array
* @param x x coordinate
* @param y y coordinate
* @param max_value max heat value, -1 will find the max value based on the minimum result
* @return Array<Array<Int>>
*/
public static function heat_map(array:Array<Array<Dynamic>>, x:Int, y:Int, max_value:Int = -1):Array<Array<Int>>
Expand Down Expand Up @@ -178,9 +184,9 @@ class ArrayExt

/**
* get a value from a 2D array with coordinates
* @param array input array
* @param x x coordinate
* @param y y coordinate
* @param array input array
* @param x x coordinate
* @param y y coordinate
* @return Dynamic
*/
public static function get_xy(array:Array<Array<Dynamic>>, x:Int, y:Int):Dynamic
Expand All @@ -192,10 +198,10 @@ class ArrayExt

/**
* set a value in a 2D array
* @param array input array
* @param x x coordinate
* @param y y coordinate
* @param value input value
* @param array input array
* @param x x coordinate
* @param y y coordinate
* @param value input value
*/
public static function set_xy(array:Array<Array<Dynamic>>, x:Int, y:Int, value:Dynamic)
{
Expand Down
33 changes: 33 additions & 0 deletions zero/extensions/EnumExt.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package zero.extensions;

using zero.extensions.EnumExt;
using zero.extensions.ArrayExt;

/**
* A collection of extension methods for Enums
*
* **Usage:**
*
* - use this extension by adding this where you normally import modules: `using zero.extensions.EnumExt;`
* - now you can use any of these functions on different arrays: `MyEnum.get_random();`
* - or use all of the extensions in this library by adding: `using zero.extensions.Tools;`
*/

class EnumExt
{

/**
* Get an array of all enums, shortcut for Type.allEnums()
* @param e input
* @return Array<T> return [for (e in Type.allEnums(e)) e]
*/
public static inline function all<T>(e:Enum<T>):Array<T> return [for (e in Type.allEnums(e)) e];

/**
* Returns a random enum value from input
* @param e input
* @return T return e.all().get_random()
*/
public static inline function get_random<T>(e:Enum<T>):T return e.all().get_random();

}
Loading

0 comments on commit 8ae4242

Please sign in to comment.