Skip to content

Create engine function for dynamically creating / exploring paths #29

Description

@ArcadeMakerSources

ArcadeMaker has a feature called "paths" - you can draw a path in the IDE, and make an object following it using startPath(...) or draw it using drawPath(...).

Image

But sometimes you want to get (in the code) the coordinates of a path, to check for intersection with it, for example. Currently, there's no function for getting the coordinates of a path.
And in other times you would want to dynamically construct a path in the code, and the engine lacks the right function for this too.

To implement a function that gets a path ID and returns an array of all of its coordinates, you should add to IGame.Funcs.cs a method like this:

/// <summary>
/// Gets an array of all the coordinates of the given path.
/// </summary>
/// <param name="expinst">The calling runtime instance (unused).</param>
/// <param name="args">(pathID).</param>
[EngineFunc(1)]
[Param("pathID", ParamType.Number, "The ID of the path to follow. Use 'Paths' enum for that, e.g. Paths.PthRoad.")]
Exp.Instance GetPathPoints(Exp.Instance? expinst, IValue?[] args)
{
    // get the path
    int id = (int)args[0].ThrowIfNull().Number;
    Resources.Path path = Paths.GetById(id);
    
    ExpSrc.General.PathPoint[] points = ...

    return new Exp.Instance(ClassDefSpan.ExpArrayDef, points);
}

Resources.Path has properties for StartPositionX, StartPositionY and Steps, where each step has properties for Width (the x diff from previous coordinate) and Height (the y diff from previous coordinate), and Speed. These properties can be used to construct an array of PathPoints (a class that contains fields for x, y, and speed, and is a valid Exp value).

To implement a function that gets an array of coordinates, creates a new path from them and returns its ID, you should add to IGame.Funcs.cs a method like this:

/// <summary>
/// Creates a path from the given PathPoint array, and returns its ID
/// </summary>
/// <param name="expinst">The calling runtime instance (unused).</param>
/// <param name="args">(points).</param>
[EngineFunc(1)]
[Param("points", ParamType.Array + " of " + nameof(ExpSrc.General.PathPoint), "An array containing the points to create a paht from.")]
IValue CreatePath(Exp.Instance? expinst, IValue?[] args)
{
    IValue[] arr = args[0].ThrowIfNull().Inst.ArrayValues ?? throw new EngineException("An array was expected");
    List<ExpSrc.General.PathPoint> points = [];
    foreach (var val in arr)
    {
        if (val is not ExpSrc.General.PathPoint pnt)
            throw new EngineException("Invalid argument - an array of PathPoint instances was expected.");
        else
            points.Add(pnt);
    }

    Resources.Path path = ...;
    
    Paths.Add(path);
    return path.ID.ToExp();
}

Feel free to ask any question :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions