Object Level Functions - Lojemiru/Loj-Hadron-Collider GitHub Wiki
If you're not already familiar with the Interface system, I highly recommend you read its page first, as Interfaces are necessary to use the rest of the Loj Hadron Collider and most of these functions.
Once you've defined an Interface to collide with, your main toolset for setting up objects and defining collision events will be lhc_activate, lhc_add, lhc_inherit_interface, lhc_cleanup, and lhc_move. For non-moving objects that you still want to use Interface-based collisions with, check out lhc_check_static.
- FAILURE TO USE lhc_cleanup() WILL RESULT IN MEMORY LEAKS -
Also provided are getters, setters, and adders for the Loj Hadron Collider's subpixel buffering system. Because the Loj Hadron Collider only moves in whole pixels, it stores any subpixel value passed into it to add on to subsequent movement when it can get a whole unit out of the subpixel buffer. You may want to draw offset at this position if you're upscaling the application surface, or may want to reset these when hitting certain objects for movement consistency.
lhc_activate
Activates the Loj Hadron Collider system for the calling instance. Must be called before any other LHC functions can be safely run. Should be used in a Create event.
Syntax:
lhc_activate();
Returns: N/A
lhc_add
Adds a collision event set to trigger on collisions with a given Interface.
The order that you define collisions using subsequent lhc_add calls will determine collision priority/order; the Loj Hadron Collider will check for and execute collisions in the order that you define them, from first to last.
Syntax:
lhc_add(interface, function);
Returns: N/A
Argument | Type | Description |
---|---|---|
interface | String | The interface to target. |
function | Method Variable | The function to run on collision. |
Example:
lhc_activate();
lhc_add("ISolid", function() {
// Check for horizontal collision.
if (lhc_collision_horizontal()) {
// Stop further x-axis movement this step...
lhc_stop_x();
// AND stop our x velocity.
xVel = 0;
}
// Check for vertical collision.
if (lhc_collision_vertical()) {
// Stop further y-axis movement this step...
lhc_stop_y();
// AND stop our y velocity.
yVel = 0;
}
});
lhc_add_offset_x
Adds to the current subpixel x-axis offset.
Syntax:
lhc_add_offset_x(x);
Returns: N/A
Argument | Type | Description |
---|---|---|
x | Real | The value to add to the subpixel x-axis offset. |
lhc_add_offset_y
Adds to the current subpixel y-axis offset.
Syntax:
lhc_add_offset_y(y);
Returns: N/A
Argument | Type | Description |
---|---|---|
y | Real | The value to add to the subpixel y-axis offset. |
lhc_check_static
Performs directionless Interface-based collision for the calling instance at its current position.
Note that this function should only be called in objects that do not use lhc_move but need to run Interface-based collisions, as lhc_move will perform a static check if it is called with both an xVel and yVel of 0.
Syntax:
lhc_check_static();
Returns: N/A
lhc_cleanup
Cleans up memory reserved by the Loj Hadron Collider for the calling Instance. Should be used in the Cleanup event; failing to do so will result in memory leaks.
Syntax:
lhc_cleanup();
Returns: N/A
lhc_get_offset_x
Gets the current subpixel x-axis offset.
Syntax:
lhc_get_offset_x();
Returns: Real
lhc_get_offset_y
Gets the current subpixel y-axis offset.
Syntax:
lhc_get_offset_y();
Returns: Real
lhc_get_vel_x
Returns the integer-rounded x-axis velocity for the previous movement step (current step if run in a collision event).
Syntax:
lhc_get_vel_x();
Returns: Integer
Example:
lhc_add("ISolid", function() {
if (lhc_collision_horizontal()) {
lhc_stop_x();
x -= lhc_get_vel_x();
xVel = -xVel;
}
});
lhc_get_vel_y
Returns the integer-rounded y-axis velocity for the previous movement step (current step if run in a collision event).
Syntax:
lhc_get_vel_y();
Returns: Integer
Example:
lhc_add("ISolid", function() {
if (lhc_collision_vertical()) {
lhc_stop_y();
y -= lhc_get_vel_y();
yVel = -yVel;
}
});
lhc_inherit_interface
Inherits the function names of the given Interface.
If you define function names for an Interface with lhc_create_interface, you should use this in the Create event of all objects assigned to that Interface to ensure compliance with expected function names. You also can and should use this to make child objects inherit their parents' Interfaces.
Syntax:
lhc_inherit_interface(interface);
Returns: N/A
Argument | Type | Description |
---|---|---|
interface | String | The name of the Interface to inherit from. |
lhc_move
Performs pixel-perfect movement and Interface-based collision for the calling instance. Should generally be used at the end of a Step event or in an End Step event.
Note: passing in both an xVel and yVel that do not result in movement during a step (i.e. value of 0 or while waiting for a value between 0 and 1 to accumulate enough to result in movement) will result in a call to lhc_check_static, so there's no need for messy else-if statements to run a static check when your object isn't moving :)
Syntax:
lhc_move(xVel, yVel, [line = false], [precise = false]);
Returns: N/A
Argument | Type | Description |
---|---|---|
xVel | Real | The x-axis velocity, in pixels, to move this step. |
yVel | Real | The y-axis velocity, in pixels, to move this step. |
[line] | Bool | Optional. Whether or not to use a single-pixel raycast for the initial collision check. |
[precise] | Bool | Optional. Whether or not to use precise hitboxes for the initial collision check. |
Example:
xVel = 5;
yVel = -2;
lhc_move(xVel, yVel);
lhc_remove
Removes the current collision event for the specified Interface.
Syntax:
lhc_remove(interface);
Returns: N/A
Argument | Type | Description |
---|---|---|
interface | String | The Interface to target. |
lhc_replace
Replaces the current collision event for the specified Interface.
Syntax:
lhc_replace(interface, function);
Returns: N/A
Argument | Type | Description |
---|---|---|
interface | String | The Interface to target. |
function | Method Variable | The function to run on collision. |
Example:
lhc_replace("ISolid", lhc_behavior_push);
lhc_set_offset_x
Sets the current subpixel x-axis offset.
Syntax:
lhc_set_offset_x(x);
Returns: N/A
Argument | Type | Description |
---|---|---|
x | Real | The value to set the subpixel x-axis offset to. |
lhc_set_offset_y
Sets the current subpixel y-axis offset.
Syntax:
lhc_set_offset_y(y);
Returns: N/A
Argument | Type | Description |
---|---|---|
y | Real | The value to set the subpixel y-axis offset to. |