ig.

utils

Static utilities for general use.

Author:
  • Collin Hover - collinhover.com
Source:

<static> addType(classObject, entity, property, names, typeListName)

Adds space separated type flags by name to a property of an entity. In ImpactJS, there are 3 default types: A, B, and BOTH. This system allows you to build up to 32 types for much finer control over type, checkAgainst, and group. If you don't understand bitflags, a quick google search will be very helpful!
- currently, Impact++ uses around 10 types/flags (see example)
- to avoid poor performance, only use this when initializing an entity IMPORTANT: maxes out at 32 types/flags due to the way Javascript handles bitwise operations.

Parameters:
Name Type Argument Description
classObject Class class object to record types in.
entity ig.EntityExtended entity to add types to.
property String property name within entity to add types to.
names String space separated names to create type for.
typeListName String <optional>
list name to store types in, defaults to TYPE.
Source:
Example
// spawn an entity
var entity = ig.game.spawnEntity( ig.EntityExtended, 0, 0, { ...settings...} );
// add a type to our new entity
// this type system defaults to using TYPE as the namespace to record unique types
ig.utils.addType( ig.EntityExtended, entity, "type", "TYPE_NAME" );
// when we need to do dynamic checks for that type
// refer to it directly using the TYPE namespace
ig.EntityExtended.TYPE.TYPE_NAME
// and a check against the type of our new entity will return truthy
entity.type & ig.EntityExtended.TYPE.TYPE_NAME
// we can also add a GROUP type to our new entity
// note the last argument, we're using the GROUP namespace
// this is because group types should be different from TYPE types
ig.utils.addType( ig.EntityExtended, entity, "group", "GROUP_NAME", "GROUP" );
// refer to it directly using the GROUP namespace
ig.EntityExtended.GROUP.GROUP_NAME
// remember, types and groups can be added together to make combinations!
ig.utils.addType( ig.EntityExtended, entity, "type", "TYPE_1" );
ig.utils.addType( ig.EntityExtended, entity, "type", "TYPE_2" );
ig.utils.addType( ig.EntityExtended, entity, "type", "TYPE_3" );
// and we can still check for any one or more of those types easily
entity.type & ig.EntityExtended.TYPE.TYPE_1 === truthy
entity.type & ( ig.EntityExtended.TYPE.TYPE_1 | ig.EntityExtended.TYPE.TYPE_2 ) === truthy
entity.type & ig.EntityExtended.TYPE.TYPE_4 === falsy

<static> arrayCautiousAdd(target, element) → {Array}

Add element to target array only if not already in array. IMPORTANT: this modifies the array in place.

Parameters:
Name Type Description
target Array Target array
element * Single value to add
Source:
Returns:
{ Array } Array containing elements

<static> arrayCautiousAddMulti(target, elements) → {Array}

Add elements to target array only if not already in array. IMPORTANT: this modifies the array in place.

Parameters:
Name Type Description
target Array Target array
elements * Single object or array of values to add
Source:
Returns:
{ Array } Array containing elements

<static> arrayCautiousRemove(target, element) → {Array}

Removes element from target array. IMPORTANT: this modifies the array in place.

Parameters:
Name Type Description
target Array Target array
element * Single value to remove
Source:
Returns:
{ Array } Array containing elements

<static> arrayCautiousRemoveMulti(target, elements) → {Array}

Removes elements from target array. IMPORTANT: this modifies the array in place.

Parameters:
Name Type Description
target Array Target array
elements * Single object or array of values to remove
Source:
Returns:
{ Array } Array containing elements

<static> debounce(callback, delay) → {function}

Debounce a function to execute only once delay reached between subsequent calls, based on Ben Alman's jQuery Throttle / Debounce.

Parameters:
Name Type Argument Description
callback function Callback function
delay Number <optional>
Delay in ms
Source:
Returns:
{ function } Debounced function.

<static> forEach(array, callback, args)

Executes a callback on each item in an array, in the context of that item.

Parameters:
Name Type Description
array Array Array to iterate over
callback Callback Callback to call
args Array Arguments to pass
Source:

<static> getType(classObject, names, typeListName)

Gets a type flag by space separated names, and if does not exists, creates it. In ImpactJS, there are 3 default types: A, B, and BOTH. This system allows you to build up to 32 types for much finer control over type, checkAgainst, and group. If you don't understand bitflags, a quick google search will be very helpful!
- currently, Impact++ uses around 10 types/flags (see example)
- to avoid poor performance, only use this when initializing an entity IMPORTANT: maxes out at 32 types/flags due to the way Javascript handles bitwise operations.

Parameters:
Name Type Argument Description
classObject Class class / object to record types in.
names String space separated names to create type for.
typeListName String <optional>
list name to store types in, defaults to TYPE.
Source:
See:

<static> indexOfProperties(array, properties, values) → {Number}

Find the index of an object in an array matching all property values.

Parameters:
Name Type Description
array Array Array to search.
properties Array Property names.
values Array Values to match.
Source:
Returns:
{ Number } >= 0 if found, -1 if not

<static> indexOfProperty(array, property, value) → {Number}

Find the index of an object in an array with property = value.

Parameters:
Name Type Description
array Array Array to search
property String Property name
value * Value of property to match
Source:
Returns:
{ Number } >= 0 if found, -1 if not

<static> indexOfValue(array, value) → {Number}

Find the index of value in an array. Tip: in some cases, this may be faster than the native indexOf.

Parameters:
Name Type Description
array Array Array to search
value * Value of property to match
Source:
Returns:
{ Number } >= 0 if found, -1 if not

<static> isArray(target) → {Boolean}

Checks if target is array.

Parameters:
Name Type Description
target * target object
Source:
Returns:
{ Boolean } true if array, false if not

<static> isNumber(n) → {Boolean}

Checks if target is number.

Parameters:
Name Type Description
n Number number
Source:
Returns:
{ Boolean } true if number, false if not

<static> throttle(callback, delay, trailing) → {function}

Throttle a function to execute no more than once per delay (its like a cooldown), based on Ben Alman's jQuery Throttle / Debounce.

Parameters:
Name Type Argument Description
callback function Callback function
delay Number <optional>
Delay in ms
trailing Boolean <optional>
Whether to allow a trailing execution
Source:
See:
Returns:
{ function } Throttled function.

<static> toArray(target) → {Array}

Ensures an object is an array.

Parameters:
Name Type Description
target * target object
Source:
Returns:
{ Array } Array

<static> toNotArray(target, index) → {*}

Ensures an object is not an array.

Parameters:
Name Type Argument Default Description
target * target object
index Number <optional>
0 index of array to use if target is an array.
Source:
Returns:
{ * } item at index of array

<static> type(o) → {String}

Finds javascript type of object, slower than typeof or instanceof but sometimes necessary.

Parameters:
Name Type Description
o * target object
Source:
Returns:
{ String } Type of object