Class AbstractScript<T>

Type Parameters:
T - The required host type.
All Implemented Interfaces:
ScriptInstance
Direct Known Subclasses:
EntityScript, EnvironmentScript, GameScript

public abstract class AbstractScript<T> extends Object implements ScriptInstance

Base implementation that provides typed access to a script host, context, and global state.

Override the protected lifecycle and input callbacks needed by the script. Resources registered through context() are automatically released after detached() completes.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final ScriptGlobals
    Game-wide values shared between scripts.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    attach(ScriptContext<?> context)
    Attaches this instance to a host context.
    protected void
    Called after the context is installed and the script becomes active.
    protected final ICamera
    Returns the active camera from the game world.
    protected final ScriptContext<T>
    Returns the current attachment context.
    final void
    Releases resources acquired by this attachment.
    protected void
    Called before context-owned resources are released.
    protected final Environment
    Returns the host's current environment, or null for game scripts without one.
    protected final ScriptGlobals
    Returns game-wide values shared between scripts.
    protected final T
    Returns the current script host.
    protected final ScriptInput
    Returns the managed input helper for key/mouse bindings and state queries.
    protected void
    Called when a key is pressed.
    protected void
    Called when a key is released.
    protected void
    Called when a key is typed.
    protected void
    Called when a mouse button is clicked (pressed and released).
    protected void
    Called when the mouse is moved.
    protected void
    Called when a mouse button is pressed.
    protected void
    Called when a mouse button is released.
    protected void
    Called when the mouse wheel is rotated.
    protected void
    Called during the render pass for custom graphics rendering.
    void
    Renders script-owned content.
    protected final <E extends IEntity>
    E
    spawn(E entity, double x, double y)
    Spawns the given entity at the specified coordinates.
    protected final <E extends IEntity>
    E
    spawn(E entity, Point2D location)
    Spawns the given entity at the specified location.
    protected final <E extends IEntity>
    E
    spawn(Class<E> entityType, double x, double y)
    Spawns an entity of the given type at the specified coordinates.
    protected final <E extends IEntity>
    E
    spawn(Class<E> entityType, Point2D location)
    Spawns an entity of the given type at the specified location.
    protected final Creature
    spawnCreature(String spritePrefix, double x, double y)
    Spawns a creature with the given sprite prefix at the specified coordinates.
    protected final Creature
    spawnCreature(String spritePrefix, Point2D location)
    Spawns a creature with the given sprite prefix at the specified location.
    protected final ScriptedSpawner
    Returns a fluent spawner for creating entities in the current environment.
    protected final Prop
    spawnProp(String spriteSheet, double x, double y)
    Spawns a prop with the given spritesheet at the specified coordinates.
    protected final Prop
    spawnProp(String spriteSheet, Point2D location)
    Spawns a prop with the given spritesheet at the specified location.
    protected final ScriptUiOverlay
    ui()
    Returns the scripted UI overlay service owned by this context.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface ScriptInstance

    update
  • Field Details

    • globals

      protected final ScriptGlobals globals
      Game-wide values shared between scripts.
  • Constructor Details

    • AbstractScript

      public AbstractScript()
  • Method Details

    • attach

      public final void attach(ScriptContext<?> context) throws Exception
      Description copied from interface: ScriptInstance
      Attaches this instance to a host context.
      Specified by:
      attach in interface ScriptInstance
      Parameters:
      context - The context for this attachment.
      Throws:
      Exception - if initialization fails.
    • detach

      public final void detach() throws Exception
      Description copied from interface: ScriptInstance
      Releases resources acquired by this attachment.
      Specified by:
      detach in interface ScriptInstance
      Throws:
      Exception - if cleanup fails.
    • render

      public void render(Graphics2D g) throws Exception
      Description copied from interface: ScriptInstance
      Renders script-owned content.
      Specified by:
      render in interface ScriptInstance
      Parameters:
      g - The current graphics context.
      Throws:
      Exception - if script rendering fails.
    • onRender

      protected void onRender(Graphics2D g) throws Exception
      Called during the render pass for custom graphics rendering.
      Parameters:
      g - The current graphics context.
      Throws:
      Exception - if rendering fails.
    • onKeyPressed

      protected void onKeyPressed(KeyEvent event) throws Exception
      Called when a key is pressed.
      Parameters:
      event - The keyboard event.
      Throws:
      Exception - if handling fails.
    • onKeyReleased

      protected void onKeyReleased(KeyEvent event) throws Exception
      Called when a key is released.
      Parameters:
      event - The keyboard event.
      Throws:
      Exception - if handling fails.
    • onKeyTyped

      protected void onKeyTyped(KeyEvent event) throws Exception
      Called when a key is typed.
      Parameters:
      event - The keyboard event.
      Throws:
      Exception - if handling fails.
    • onMouseClicked

      protected void onMouseClicked(MouseEvent event) throws Exception
      Called when a mouse button is clicked (pressed and released).
      Parameters:
      event - The mouse event.
      Throws:
      Exception - if handling fails.
    • onMousePressed

      protected void onMousePressed(MouseEvent event) throws Exception
      Called when a mouse button is pressed.
      Parameters:
      event - The mouse event.
      Throws:
      Exception - if handling fails.
    • onMouseReleased

      protected void onMouseReleased(MouseEvent event) throws Exception
      Called when a mouse button is released.
      Parameters:
      event - The mouse event.
      Throws:
      Exception - if handling fails.
    • onMouseMoved

      protected void onMouseMoved(MouseEvent event) throws Exception
      Called when the mouse is moved.
      Parameters:
      event - The mouse event.
      Throws:
      Exception - if handling fails.
    • onMouseWheel

      protected void onMouseWheel(MouseWheelEvent event) throws Exception
      Called when the mouse wheel is rotated.
      Parameters:
      event - The wheel event.
      Throws:
      Exception - if handling fails.
    • context

      protected final ScriptContext<T> context()
      Returns the current attachment context.
      Returns:
      The current context.
      Throws:
      IllegalStateException - if the script is not attached.
    • host

      protected final T host()
      Returns the current script host.
      Returns:
      The typed host.
    • environment

      protected final Environment environment()
      Returns the host's current environment, or null for game scripts without one.
      Returns:
      The current environment, or null when unavailable.
    • globals

      protected final ScriptGlobals globals()
      Returns game-wide values shared between scripts.
      Returns:
      The shared global registry.
    • input

      protected final ScriptInput input()
      Returns the managed input helper for key/mouse bindings and state queries.
      Returns:
      The input helper owned by this attachment.
    • ui

      protected final ScriptUiOverlay ui()
      Returns the scripted UI overlay service owned by this context.
      Returns:
      The overlay owned by this attachment.
    • camera

      protected final ICamera camera()
      Returns the active camera from the game world.
      Returns:
      The active camera, or null when unavailable.
    • spawner

      protected final ScriptedSpawner spawner()
      Returns a fluent spawner for creating entities in the current environment.
      Returns:
      A spawner bound to the current environment.
    • spawnCreature

      protected final Creature spawnCreature(String spritePrefix, double x, double y)
      Spawns a creature with the given sprite prefix at the specified coordinates.
      Parameters:
      spritePrefix - The animation sprite prefix.
      x - The map x-coordinate.
      y - The map y-coordinate.
      Returns:
      The spawned creature.
    • spawnCreature

      protected final Creature spawnCreature(String spritePrefix, Point2D location)
      Spawns a creature with the given sprite prefix at the specified location.
      Parameters:
      spritePrefix - The animation sprite prefix.
      location - The location in map coordinates.
      Returns:
      The spawned creature.
    • spawnProp

      protected final Prop spawnProp(String spriteSheet, double x, double y)
      Spawns a prop with the given spritesheet at the specified coordinates.
      Parameters:
      spriteSheet - The spritesheet name.
      x - The map x-coordinate.
      y - The map y-coordinate.
      Returns:
      The spawned prop.
    • spawnProp

      protected final Prop spawnProp(String spriteSheet, Point2D location)
      Spawns a prop with the given spritesheet at the specified location.
      Parameters:
      spriteSheet - The spritesheet name.
      location - The location in map coordinates.
      Returns:
      The spawned prop.
    • spawn

      protected final <E extends IEntity> E spawn(Class<E> entityType, double x, double y)
      Spawns an entity of the given type at the specified coordinates.
      Type Parameters:
      E - The entity type.
      Parameters:
      entityType - The concrete entity type.
      x - The map x-coordinate.
      y - The map y-coordinate.
      Returns:
      The spawned entity.
    • spawn

      protected final <E extends IEntity> E spawn(Class<E> entityType, Point2D location)
      Spawns an entity of the given type at the specified location.
      Type Parameters:
      E - The entity type.
      Parameters:
      entityType - The concrete entity type.
      location - The location in map coordinates.
      Returns:
      The spawned entity.
    • spawn

      protected final <E extends IEntity> E spawn(E entity, double x, double y)
      Spawns the given entity at the specified coordinates.
      Type Parameters:
      E - The entity type.
      Parameters:
      entity - The entity to add.
      x - The map x-coordinate.
      y - The map y-coordinate.
      Returns:
      The supplied entity.
    • spawn

      protected final <E extends IEntity> E spawn(E entity, Point2D location)
      Spawns the given entity at the specified location.
      Type Parameters:
      E - The entity type.
      Parameters:
      entity - The entity to add.
      location - The location in map coordinates.
      Returns:
      The supplied entity.
    • attached

      protected void attached() throws Exception
      Called after the context is installed and the script becomes active.
      Throws:
      Exception - if initialization fails.
    • detached

      protected void detached() throws Exception
      Called before context-owned resources are released.
      Throws:
      Exception - if cleanup fails.