Skip to content

Create a "drawHealthbar" function #46

Description

@ArcadeMakerSources

Showing health points (or other variables) in a bar is a popular design pattern in games:

+--------------------------+
||||||||| 80 ||||||||      |
+--------------------------+

A built-in function for this would be really helpful.
The engine already has drawRect(x1, y1, x2, y2, [outline], [thickness]), drawText(x, y, text), getTextWidth(text) and getTextHeight(text) functions, so they can be used by our new function to draw the bar and the value in the center of the bar.
I want the function to have this signature:
drawHealthbar(x1, y1, x2, y2, fullCol, emptyCol, frameCol, value, min, max). The bar should be filled with a color that as value is going lower, the color becomes closer to emptyCol, and as value is going higher, the color becomes closer to fullCol, so if emptyCol is red and fullCol is green, then when value is 100 (supposing that min is 0 and max is 100), the bar color will be green, when value is 10 the bar color will be almost-red, when value is 50 the bar color will be orange, and so on.

To add this function, it should be declared in ArcadeMaker.Core/IGame.Funcs.cs, and look like this:

<summary>Draws a bar with the given value.</summary>
[EngineFunc(10)] // 10 parameters
[Param("x1", ParamType.Number, "The x value of the top left corner of the bar.")]
// rest of the parameters...
Exp.Void DrawHealthbar(Exp.Instance? _, IValue?[] args)
{
    
    IValue? x1 = args[0];
    // --- use the same approach to get x2, y1, and y2, and pass them as-they-are to the DrawRect() method ---

    // these arguments we need to get as primitives, bc they are needed for color calculation
    var fullCol = (uint)args[4].ThrowIfNull().Number;
    var emptyCol  = (uint)args[5].ThrowIfNull().Number;
    var frameCol = (uint)args[6].ThrowIfNull().Number;
    var value = (int)args[7].ThrowIfNull().Number;
    var min    = (int)args[8].ThrowIfNull().Number;
    var max   = (int)args[9].ThrowIfNull().Number;

    // draw the frame
    SetColor(_, [frameCol]);
    DrawRect(_, [x1, y1, x2, y2, true.ToExp(), 3d.ToExp());
    
     // --- fill the bar, write the value in the center ---

    return Exp.Void.Return;
}

And that's it!
Feel free to ask any question:)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions