Modify character with stage custom command

Share custom commands, script functions, actor implementations and other Naninovel plug-ins you've created.
Post Reply
User avatar
Elringus
Admin
Posts: 426
Joined: 11 May 2020 18:03

Modify character with stage custom command

Post by Elringus »

A custom command, that will override built-in @char providing additional stage parameter. The parameter allows specifying "stage" of the character on scene, that is x,y positions and scale:

Image

The command can be used as follows:

Code: Select all

; Place Kohaku at the upper-left stage
@char Kohaku stage:UpLeft

The VS Code extension will automatically pick-up available stage options and provide auto-complete:

Image

To add the command, copy-paste the code below to a new C# script.

Code: Select all

using System;
using System.Collections.Generic;
using Naninovel;
using Naninovel.Commands;

public enum Stage
{
    UpRight,
    UpCenter,
    UpLeft,
    
    Right,
    Center,
    Left,
    
    DownRight,
    DownCenter,
    DownLeft
}

public class StageParams
{
    public List<float?> Position { get; }
    public List<float?> Scale { get; }

    public StageParams (float x, float y, float scale)
    {
        Position = new List<float?> { x, y };
        Scale = new List<float?> { scale };
    }

    public static StageParams GetFor (string stageString)
    {
        if (!Enum.TryParse(stageString, out Stage stage))
            throw new ArgumentException($"Unknown stage: '{stageString}'.");

        switch (stage)
        {
            case Stage.UpRight:    return new StageParams(70, 30, 0.7f);
            case Stage.UpCenter:   return new StageParams(50, 30, 0.7f);
            case Stage.UpLeft:     return new StageParams(30, 30, 0.7f);
            
            case Stage.Right:      return new StageParams(80, 0, 1.0f);
            case Stage.Center:     return new StageParams(50, 0, 1.0f);
            case Stage.Left:       return new StageParams(20, 0, 1.0f);
            
            case Stage.DownRight:  return new StageParams(90, -30, 1.3f);
            case Stage.DownCenter: return new StageParams(50, -30, 1.3f);
            case Stage.DownLeft:   return new StageParams(10, -30, 1.3f);
            
            default: throw new ArgumentException($"Missing parameters for '{stage}' stage.");
        }
    }
}

[CommandAlias("char")]
public class ModifyCharWithStage : ModifyCharacter
{
    [ConstantContext(typeof(Stage))]
    public StringParameter Stage;

    public override UniTask ExecuteAsync (AsyncToken asyncToken = default)
    {
        if (Assigned(Stage))
        {
            var stageParams = StageParams.GetFor(Stage);
            ScenePosition = stageParams.Position;
            Scale = stageParams.Scale;
        }
        return base.ExecuteAsync(asyncToken);
    }
}

Thanks to Nai for the idea!

Post Reply