Skip to content

@lands.io/mod-sdk / BaseAction

Class: BaseAction\<T>

Abstract base class for creating custom actions. Extend this class to create new action types.

Example

class MyCustomAction extends BaseAction {
  readonly id = 'my-action';
  readonly label = 'My Action';

  canActivate(context: ActionContext): boolean {
    return context.player.troops() > 1000;
  }

  chooseTarget(context: ActionContext): ActionTarget | null {
    // AI targeting logic
    return null;
  }

  createExecution(context: ActionContext, target: ActionTarget): Execution {
    // Create and return an execution
  }
}

Type parameters

Name Type
T extends ActionTarget = ActionTarget

Implements

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new BaseAction\<T>(): BaseAction\<T>

Type parameters

Name Type
T extends ActionTarget = ActionTarget

Returns

BaseAction\<T>

Properties

id

Readonly Abstract id: string

Implementation of

Action.id


label

Readonly Abstract label: string

Implementation of

Action.label

Methods

canActivate

canActivate(context): boolean

Parameters

Name Type
context ActionContext

Returns

boolean

Implementation of

Action.canActivate


chooseTarget

chooseTarget(context): null | T

Parameters

Name Type
context ActionContext

Returns

null | T

Implementation of

Action.chooseTarget


createExecution

createExecution(context, target): Execution

Parameters

Name Type
context ActionContext
target T

Returns

Execution

Implementation of

Action.createExecution


getCost

getCost(context): number

Parameters

Name Type
context ActionContext

Returns

number

Implementation of

Action.getCost


Source Code

View full implementation
/**
 * Abstract base class for creating custom actions.
 * Extend this class to create new action types.
 *
 * @example
 * ```typescript
 * class MyCustomAction extends BaseAction {
 *   readonly id = 'my-action';
 *   readonly label = 'My Action';
 *
 *   canActivate(context: ActionContext): boolean {
 *     return context.player.troops() > 1000;
 *   }
 *
 *   chooseTarget(context: ActionContext): ActionTarget | null {
 *     // AI targeting logic
 *     return null;
 *   }
 *
 *   createExecution(context: ActionContext, target: ActionTarget): Execution {
 *     // Create and return an execution
 *   }
 * }
 * ```
 */
export abstract class BaseAction<T extends ActionTarget = ActionTarget>
  implements Action<T>
{
  abstract readonly id: string;
  abstract readonly label: string;

  abstract canActivate(context: ActionContext): boolean;
  abstract chooseTarget(context: ActionContext): T | null;
  abstract createExecution(context: ActionContext, target: T): Execution;

  getCost?(context: ActionContext): number;
}