RG Game library

Overview

RetroLib is a Lua library designed to facilitate the development of 2D games and applications in Retro Gadget. It provides functionality for graphics rendering, collision detection, easing animations, and various utility functions.

You can find a sample Gadget that uses the library in the tutorial section of the game.

Graphics

lib.CreateSprite( spritesheet SpriteSheet, spriteX, spriteY, position vec2, scale vec2, animationSpeed number, renderOrder number ) table

This function creates and return a sprite instance with the specified parameters and adds it to the rendering queue.

Sprites can be dynamically positioned, scaled, and animated.

Sprite variables:

  • spritesheet The spritesheet containing the sprite image.
  • spriteX, spriteY The coordinates of the sprite image within the spritesheet.
  • position The position of the sprite in 2D space.
  • rotation The angular rotation of the sprite in 2D space.
  • scale The scaling factor of the sprite.
  • color The base color of the sprite. By default is set to color.white
  • bgColor The base color of the background of the sprite. By default is set to color.clear
  • flipX Store the flip status of the sprite on the X axis
  • flipY Store the flip status of the sprite on the Y axis
  • frame Current frame of the current sprite animation
  • animations Table of sprites coordinates animation
  • animationSpeed The speed of animation playback
  • currentAnimation the index of the currently playing animation
  • collider table of the colliders added to the sprite
  • active determine if the sprite is rendered or not *renderOrder The order in which the sprite is rendered relative to other sprites.

Sprite methods:

sprite:FlipSpriteX( flip boolean )

Flips the sprite horizontally if flip value is true. Flip value is stored in the sprite.

sprite:FlipSpriteY( flip boolean )

Flips the sprite vertically if flip value is true. Flip value is stored in the sprite.

sprite:RotateSprite( angle )

Rotates the sprite by the specified angle (in radians).

sprite:ScaleSprite( scale vec2 )

Scales the sprite by the specified scale vector.

sprite:MoveX( xValue )

Moves the sprite horizontally by the specified xValue.

sprite:MoveY( yValue )

Moves the sprite vertically by the specified yValue.

sprite:Move( position vec2 )

Moves the sprite to the specified position.

sprite:SetRenderOrder( value number )

Sets the rendering order of the sprite.

sprite:StartAnimation( )

Starts the animation of the sprite.

sprite:StopAnimation( )

Stops the animation of the sprite.

sprite:CreateAnimation( animations )

Creates an animation for the sprite.

sprite:SetAnimation( animation number )

Sets the current animation of the sprite.

sprite:DrawAnimatedSprite( )

Draws the animated sprite.

sprite:DrawSprite( )

Draws the sprite without animation.

Collisions

CircleCollider:new( position vec2, radius number, tag string ) table

Creates and return a circle collider with the specified parameters.

CircleCollider variabiles:
tag can be used to check the collider
position the position of the collider
radius the radius of the circle collider
type the type of the collider

BoxCollider:new( position vec2, width number, height number, tag string ) table

Creates and return a box collider with the specified parameters.

BoxCollider variables:
tag can be used to check the collider
position the position of the collider
width the width of the box
height the height of the box
centre the centre of the box based on its size
type the type of the collider

Easings

lib.EasingTypes = {
  Linear 
  InQuad
  OutQuad
  InCubic
  OutCubic
  InQuart
  OutQuart 
  InQuint
  OutQuint
  InSine
  OutSine
  InExpo
  OutExpo
  InCirc
  OutCirc
  InElastic
  OutElastic
  InBack
  OutBack
  InBounce
  OutBounce
}

The easings need four parameters:

  • t = time
  • b = begin
  • c = change (ending - beginning)
  • d = duration

lib.EasingManager:new( ) table

Creates and return a new easing manager.

easingManager:new( ) table

Creates a new easing instance.

easing:EaseNumber( startValue, endValue, duration, easingType, onEaseValue, onEaseComplete )

Applies an easing function to interpolate between values.

easingManager:Update( )

Updates active easings.

Utils

lib.moveTableElement( table, fromIndex, toIndex )

Moves an element within a table.

lib.findIndex( table, element )

Finds the index of an element in a table.

Debug

Debugging RetroLib provides a debug mode that can be toggled on or off. When enabled, debug messages are printed to the console and colliders are drawn providing useful information for troubleshooting and monitoring the library's behaviour.

To enable debug mode, set lib.debugMode to true:

lib.debugMode = true