...

Package states

import "github.com/kijimaD/ruins/lib/states"
Overview
Index

Overview ▾

Package states はゲームステートの実装を提供する。

Actionベース入力処理の規約

Actionベースの入力処理を行うステートは、es.ActionHandler インターフェースを実装してください。 これはオプショナルなインターフェースで、すべてのステートが実装する必要はありません。

## 実装方法

1. es.ActionHandler インターフェースを実装

type YourState struct {
    es.BaseState[w.World]
}

// インターフェース実装の確認(コンパイル時チェック)
var _ es.State[w.World] = &YourState{}
var _ es.ActionHandler[w.World] = &YourState{}

2. HandleInput メソッドを実装(インターフェース要求)

func (st *YourState) HandleInput() (inputmapper.ActionID, bool) {
    if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
        return inputmapper.ActionMenuCancel, true
    }
    // 複数キー同時押しの判定もここで行う
    if inpututil.IsKeyJustPressed(ebiten.KeyW) && inpututil.IsKeyJustPressed(ebiten.KeyA) {
        return inputmapper.ActionMoveNorthWest, true
    }
    return "", false
}

3. DoAction メソッドを実装(インターフェース要求)

func (st *YourState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error) {
    switch action {
    case inputmapper.ActionMenuCancel:
        return es.Transition[w.World]{Type: es.TransPop}, nil
    case inputmapper.ActionMoveNorthWest:
        // 移動処理
        return es.Transition[w.World]{Type: es.TransNone}, nil
    default:
        return es.Transition[w.World]{Type: es.TransNone}, nil
    }
}

4. Update メソッドで規約パターンを使用

func (st *YourState) Update(world w.World) (es.Transition[w.World], error) {
    // キー入力をActionに変換
    if action, ok := st.HandleInput(); ok {
        if transition, err := st.DoAction(world, action); err != nil {
            return es.Transition[w.World]{}, err
        } else if transition.Type != es.TransNone {
            return transition, nil
        }
    }

    // その他の処理
    // ...

    return st.ConsumeTransition(), nil
}

## テスト実装パターン

func TestDoAction(t *testing.T) {
    state := &YourState{}
    world := testutil.InitTestWorld(t)

    // インターフェース実装の検証
    var _ es.ActionHandler[w.World] = state

    transition, err := state.DoAction(world, inputmapper.ActionMenuCancel)
    require.NoError(t, err)
    assert.Equal(t, es.TransPop, transition.Type)
}

## 参考実装

- DungeonState: 8方向移動とメニュー操作の実装例 - InventoryMenuState: メニュー操作の実装例

## メリット

- 入力処理とロジックを分離できる - テストからDoActionを直接呼び出せる - キー入力に依存しないテストが書ける - 複数キー同時押しの判定をhandleInputに集約できる

Index ▾

Constants
func ExecuteEnterAction(world w.World)
func ExecuteMoveAction(world w.World, direction gc.Direction)
func ExecuteWaitAction(world w.World)
func HandleWindowInput() (inputmapper.ActionID, bool)
func NewCraftMenuState() es.State[w.World]
func NewDebugMenuState() es.State[w.World]
func NewDungeonMenuState() es.State[w.World]
func NewDungeonState(depth int, opts ...DungeonStateOption) es.StateFactory[w.World]
func NewEquipMenuState() es.State[w.World]
func NewGameOverMessageState() es.State[w.World]
func NewGameStartMessageState() es.State[w.World]
func NewInteractionMenuState(world w.World) es.State[w.World]
func NewInventoryMenuState() es.State[w.World]
func NewLoadMenuState() es.State[w.World]
func NewMainMenuState() es.State[w.World]
func NewMessageState(messageData *messagedata.MessageData) es.State[w.World]
func NewSaveMenuState() es.State[w.World]
func NewShopMenuState() es.State[w.World]
func UpdateFocusIndex(action inputmapper.ActionID, focusIndex *int, itemCount int) bool
type CraftMenuState
    func (st *CraftMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)
    func (st *CraftMenuState) Draw(_ w.World, screen *ebiten.Image) error
    func (st *CraftMenuState) HandleInput() (inputmapper.ActionID, bool)
    func (st *CraftMenuState) OnPause(_ w.World) error
    func (st *CraftMenuState) OnResume(_ w.World) error
    func (st *CraftMenuState) OnStart(world w.World) error
    func (st *CraftMenuState) OnStop(_ w.World) error
    func (st CraftMenuState) String() string
    func (st *CraftMenuState) Update(world w.World) (es.Transition[w.World], error)
type DungeonState
    func (st *DungeonState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)
    func (st *DungeonState) Draw(world w.World, screen *ebiten.Image) error
    func (st *DungeonState) HandleInput() (inputmapper.ActionID, bool)
    func (st *DungeonState) OnPause(_ w.World) error
    func (st *DungeonState) OnResume(_ w.World) error
    func (st *DungeonState) OnStart(world w.World) error
    func (st *DungeonState) OnStop(world w.World) error
    func (st DungeonState) String() string
    func (st *DungeonState) Update(world w.World) (es.Transition[w.World], error)
type DungeonStateOption
    func WithBuilderType(builderType mapplanner.PlannerType) DungeonStateOption
    func WithSeed(seed uint64) DungeonStateOption
type EquipMenuState
    func (st *EquipMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)
    func (st *EquipMenuState) Draw(_ w.World, screen *ebiten.Image) error
    func (st *EquipMenuState) HandleInput() (inputmapper.ActionID, bool)
    func (st *EquipMenuState) OnPause(_ w.World) error
    func (st *EquipMenuState) OnResume(_ w.World) error
    func (st *EquipMenuState) OnStart(world w.World) error
    func (st *EquipMenuState) OnStop(_ w.World) error
    func (st EquipMenuState) String() string
    func (st *EquipMenuState) Update(world w.World) (es.Transition[w.World], error)
type InteractionAction
    func GetInteractionActions(world w.World) []InteractionAction
type InventoryMenuState
    func (st *InventoryMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)
    func (st *InventoryMenuState) Draw(_ w.World, screen *ebiten.Image) error
    func (st *InventoryMenuState) HandleInput() (inputmapper.ActionID, bool)
    func (st *InventoryMenuState) OnPause(_ w.World) error
    func (st *InventoryMenuState) OnResume(_ w.World) error
    func (st *InventoryMenuState) OnStart(world w.World) error
    func (st *InventoryMenuState) OnStop(_ w.World) error
    func (st InventoryMenuState) String() string
    func (st *InventoryMenuState) Update(world w.World) (es.Transition[w.World], error)
type MainMenuState
    func (st *MainMenuState) DoAction(_ w.World, action inputmapper.ActionID) (es.Transition[w.World], error)
    func (st *MainMenuState) Draw(world w.World, screen *ebiten.Image) error
    func (st *MainMenuState) HandleInput() (inputmapper.ActionID, bool)
    func (st *MainMenuState) OnPause(_ w.World) error
    func (st *MainMenuState) OnResume(_ w.World) error
    func (st *MainMenuState) OnStart(world w.World) error
    func (st *MainMenuState) OnStop(_ w.World) error
    func (st MainMenuState) String() string
    func (st *MainMenuState) Update(_ w.World) (es.Transition[w.World], error)
type MessageState
    func (st *MessageState) Draw(_ w.World, screen *ebiten.Image) error
    func (st *MessageState) OnPause(_ w.World) error
    func (st *MessageState) OnResume(_ w.World) error
    func (st *MessageState) OnStart(world w.World) error
    func (st *MessageState) OnStop(_ w.World) error
    func (st MessageState) String() string
    func (st *MessageState) Update(_ w.World) (es.Transition[w.World], error)
type PersistentMessageState
    func NewPersistentMessageState(messageData *messagedata.MessageData) *PersistentMessageState
    func (st *PersistentMessageState) OnResume(world w.World) error
    func (st PersistentMessageState) String() string
    func (st *PersistentMessageState) Update(_ w.World) (es.Transition[w.World], error)
type ShopMenuState
    func (st *ShopMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)
    func (st *ShopMenuState) Draw(_ w.World, screen *ebiten.Image) error
    func (st *ShopMenuState) HandleInput() (inputmapper.ActionID, bool)
    func (st *ShopMenuState) OnPause(_ w.World) error
    func (st *ShopMenuState) OnResume(_ w.World) error
    func (st *ShopMenuState) OnStart(world w.World) error
    func (st *ShopMenuState) OnStop(_ w.World) error
    func (st ShopMenuState) String() string
    func (st *ShopMenuState) Update(world w.World) (es.Transition[w.World], error)

Package files

action_handlers.go craft_menu.go doc.go dungeon.go equip_menu.go factories.go inventory_menu.go lib.go main_menu.go message.go persistent_message_state.go shop_menu.go window.go

Constants

共通の文字列定数

const (
    // UI表示用の定数
    TextNoDescription = "説明なし" // アイテムの説明がない場合の表示文字列
    TextClose         = "閉じる"  // メニューやウィンドウを閉じる際の表示文字列
)

func ExecuteEnterAction

func ExecuteEnterAction(world w.World)

ExecuteEnterAction はEnterキーによる状況に応じたアクションを実行する

func ExecuteMoveAction

func ExecuteMoveAction(world w.World, direction gc.Direction)

ExecuteMoveAction は移動アクションを実行する

func ExecuteWaitAction

func ExecuteWaitAction(world w.World)

ExecuteWaitAction は待機アクションを実行する

func HandleWindowInput

func HandleWindowInput() (inputmapper.ActionID, bool)

HandleWindowInput はウィンドウモード時のキー入力をActionに変換する

func NewCraftMenuState

func NewCraftMenuState() es.State[w.World]

NewCraftMenuState は新しいCraftMenuStateインスタンスを作成するファクトリー関数

func NewDebugMenuState

func NewDebugMenuState() es.State[w.World]

NewDebugMenuState は新しいDebugMenuStateインスタンスを作成するファクトリー関数

func NewDungeonMenuState

func NewDungeonMenuState() es.State[w.World]

NewDungeonMenuState は新しいDungeonMenuStateインスタンスを作成するファクトリー関数

func NewDungeonState

func NewDungeonState(depth int, opts ...DungeonStateOption) es.StateFactory[w.World]

NewDungeonState はDungeonStateインスタンスを作成するファクトリー関数 デフォルトではBuilderTypeはPlannerTypeRandomになる

func NewEquipMenuState

func NewEquipMenuState() es.State[w.World]

NewEquipMenuState は新しいEquipMenuStateインスタンスを作成するファクトリー関数

func NewGameOverMessageState

func NewGameOverMessageState() es.State[w.World]

NewGameOverMessageState はゲームオーバー用のMessageStateを作成するファクトリー関数

func NewGameStartMessageState

func NewGameStartMessageState() es.State[w.World]

NewGameStartMessageState はゲーム開始時の目的を説明するMessageStateを作成するファクトリー関数

func NewInteractionMenuState

func NewInteractionMenuState(world w.World) es.State[w.World]

NewInteractionMenuState はインタラクションメニューStateを作成する

func NewInventoryMenuState

func NewInventoryMenuState() es.State[w.World]

NewInventoryMenuState は新しいInventoryMenuStateインスタンスを作成するファクトリー関数

func NewLoadMenuState

func NewLoadMenuState() es.State[w.World]

NewLoadMenuState は新しいLoadMenuStateインスタンスを作成するファクトリー関数

func NewMainMenuState

func NewMainMenuState() es.State[w.World]

NewMainMenuState は新しいMainMenuStateインスタンスを作成するファクトリー関数

func NewMessageState

func NewMessageState(messageData *messagedata.MessageData) es.State[w.World]

NewMessageState はメッセージデータを受け取って新しいMessageStateを作成するファクトリー関数

func NewSaveMenuState

func NewSaveMenuState() es.State[w.World]

NewSaveMenuState は新しいSaveMenuStateインスタンスを作成するファクトリー関数

func NewShopMenuState

func NewShopMenuState() es.State[w.World]

NewShopMenuState は新しいShopMenuStateインスタンスを作成するファクトリー関数

func UpdateFocusIndex

func UpdateFocusIndex(action inputmapper.ActionID, focusIndex *int, itemCount int) bool

UpdateFocusIndex はナビゲーションアクションに応じてフォーカスインデックスを更新する

type CraftMenuState

CraftMenuState はクラフトメニューのゲームステート

type CraftMenuState struct {
    es.BaseState[w.World]
    // contains filtered or unexported fields
}

func (*CraftMenuState) DoAction

func (st *CraftMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)

DoAction はActionを実行する

func (*CraftMenuState) Draw

func (st *CraftMenuState) Draw(_ w.World, screen *ebiten.Image) error

Draw はゲームステートの描画処理を行う

func (*CraftMenuState) HandleInput

func (st *CraftMenuState) HandleInput() (inputmapper.ActionID, bool)

HandleInput はキー入力をActionに変換する

func (*CraftMenuState) OnPause

func (st *CraftMenuState) OnPause(_ w.World) error

OnPause はステートが一時停止される際に呼ばれる

func (*CraftMenuState) OnResume

func (st *CraftMenuState) OnResume(_ w.World) error

OnResume はステートが再開される際に呼ばれる

func (*CraftMenuState) OnStart

func (st *CraftMenuState) OnStart(world w.World) error

OnStart はステートが開始される際に呼ばれる

func (*CraftMenuState) OnStop

func (st *CraftMenuState) OnStop(_ w.World) error

OnStop はステートが停止される際に呼ばれる

func (CraftMenuState) String

func (st CraftMenuState) String() string

func (*CraftMenuState) Update

func (st *CraftMenuState) Update(world w.World) (es.Transition[w.World], error)

Update はゲームステートの更新処理を行う

type DungeonState

DungeonState はダンジョン探索中のゲームステート

type DungeonState struct {
    es.BaseState[w.World]
    Depth int
    // Seed はマップ生成用のシード値(0の場合はDungeonリソースのシード値を使用)
    Seed uint64
    // BuilderType は使用するマップビルダーのタイプ(BuilderTypeRandom の場合はランダム選択)
    BuilderType mapplanner.PlannerType
}

func (*DungeonState) DoAction

func (st *DungeonState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)

DoAction はActionを実行する

func (*DungeonState) Draw

func (st *DungeonState) Draw(world w.World, screen *ebiten.Image) error

Draw はゲームステートの描画処理を行う

func (*DungeonState) HandleInput

func (st *DungeonState) HandleInput() (inputmapper.ActionID, bool)

HandleInput はキー入力をActionに変換する

func (*DungeonState) OnPause

func (st *DungeonState) OnPause(_ w.World) error

OnPause はステートが一時停止される際に呼ばれる

func (*DungeonState) OnResume

func (st *DungeonState) OnResume(_ w.World) error

OnResume はステートが再開される際に呼ばれる

func (*DungeonState) OnStart

func (st *DungeonState) OnStart(world w.World) error

OnStart はステートが開始される際に呼ばれる

func (*DungeonState) OnStop

func (st *DungeonState) OnStop(world w.World) error

OnStop はステートが停止される際に呼ばれる

func (DungeonState) String

func (st DungeonState) String() string

func (*DungeonState) Update

func (st *DungeonState) Update(world w.World) (es.Transition[w.World], error)

Update はゲームステートの更新処理を行う

type DungeonStateOption

DungeonStateOption はDungeonStateのオプション設定関数

type DungeonStateOption func(*DungeonState)

func WithBuilderType

func WithBuilderType(builderType mapplanner.PlannerType) DungeonStateOption

WithBuilderType はマップビルダータイプを設定するオプション

func WithSeed

func WithSeed(seed uint64) DungeonStateOption

WithSeed はシード値を設定するオプション

type EquipMenuState

EquipMenuState は装備メニューのゲームステート

type EquipMenuState struct {
    es.BaseState[w.World]
    // contains filtered or unexported fields
}

func (*EquipMenuState) DoAction

func (st *EquipMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)

DoAction はActionを実行する

func (*EquipMenuState) Draw

func (st *EquipMenuState) Draw(_ w.World, screen *ebiten.Image) error

Draw はゲームステートの描画処理を行う

func (*EquipMenuState) HandleInput

func (st *EquipMenuState) HandleInput() (inputmapper.ActionID, bool)

HandleInput はキー入力をActionに変換する

func (*EquipMenuState) OnPause

func (st *EquipMenuState) OnPause(_ w.World) error

OnPause はステートが一時停止される際に呼ばれる

func (*EquipMenuState) OnResume

func (st *EquipMenuState) OnResume(_ w.World) error

OnResume はステートが再開される際に呼ばれる

func (*EquipMenuState) OnStart

func (st *EquipMenuState) OnStart(world w.World) error

OnStart はステートが開始される際に呼ばれる

func (*EquipMenuState) OnStop

func (st *EquipMenuState) OnStop(_ w.World) error

OnStop はステートが停止される際に呼ばれる

func (EquipMenuState) String

func (st EquipMenuState) String() string

func (*EquipMenuState) Update

func (st *EquipMenuState) Update(world w.World) (es.Transition[w.World], error)

Update はゲームステートの更新処理を行う

type InteractionAction

InteractionAction はインタラクション可能なアクション情報

type InteractionAction struct {
    Label    string                    // 表示ラベル(例:"開く(上)")
    Activity actions.ActivityInterface // 実行するアクティビティ
    Target   ecs.Entity                // ターゲットエンティティ
}

func GetInteractionActions

func GetInteractionActions(world w.World) []InteractionAction

GetInteractionActions はプレイヤー周辺の実行可能なアクションを取得する

type InventoryMenuState

InventoryMenuState はインベントリメニューのゲームステート

type InventoryMenuState struct {
    es.BaseState[w.World]
    // contains filtered or unexported fields
}

func (*InventoryMenuState) DoAction

func (st *InventoryMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)

DoAction はActionを実行する

func (*InventoryMenuState) Draw

func (st *InventoryMenuState) Draw(_ w.World, screen *ebiten.Image) error

Draw はゲームステートの描画処理を行う

func (*InventoryMenuState) HandleInput

func (st *InventoryMenuState) HandleInput() (inputmapper.ActionID, bool)

HandleInput はキー入力をActionに変換する

func (*InventoryMenuState) OnPause

func (st *InventoryMenuState) OnPause(_ w.World) error

OnPause はステートが一時停止される際に呼ばれる

func (*InventoryMenuState) OnResume

func (st *InventoryMenuState) OnResume(_ w.World) error

OnResume はステートが再開される際に呼ばれる

func (*InventoryMenuState) OnStart

func (st *InventoryMenuState) OnStart(world w.World) error

OnStart はステートが開始される際に呼ばれる

func (*InventoryMenuState) OnStop

func (st *InventoryMenuState) OnStop(_ w.World) error

OnStop はステートが停止される際に呼ばれる

func (InventoryMenuState) String

func (st InventoryMenuState) String() string

func (*InventoryMenuState) Update

func (st *InventoryMenuState) Update(world w.World) (es.Transition[w.World], error)

Update はゲームステートの更新処理を行う

MainMenuState は新しいメニューコンポーネントを使用するメインメニュー

type MainMenuState struct {
    es.BaseState[w.World]
    // contains filtered or unexported fields
}
func (st *MainMenuState) DoAction(_ w.World, action inputmapper.ActionID) (es.Transition[w.World], error)

DoAction はActionを実行する

func (st *MainMenuState) Draw(world w.World, screen *ebiten.Image) error

Draw はスクリーンに描画する

func (st *MainMenuState) HandleInput() (inputmapper.ActionID, bool)

HandleInput はキー入力をActionに変換する

func (st *MainMenuState) OnPause(_ w.World) error

OnPause はステートが一時停止される際に呼ばれる

func (st *MainMenuState) OnResume(_ w.World) error

OnResume はステートが再開される際に呼ばれる

func (st *MainMenuState) OnStart(world w.World) error

OnStart はステート開始時の処理を行う

func (st *MainMenuState) OnStop(_ w.World) error

OnStop はステートが停止される際に呼ばれる

func (st MainMenuState) String() string
func (st *MainMenuState) Update(_ w.World) (es.Transition[w.World], error)

Update はゲームステートの更新処理を行う

type MessageState

MessageState はメッセージを表示する専用ステート

type MessageState struct {
    es.BaseState[w.World]
    // contains filtered or unexported fields
}

func (*MessageState) Draw

func (st *MessageState) Draw(_ w.World, screen *ebiten.Image) error

Draw はゲームステートの描画処理を行う

func (*MessageState) OnPause

func (st *MessageState) OnPause(_ w.World) error

OnPause はステートが一時停止される際に呼ばれる

func (*MessageState) OnResume

func (st *MessageState) OnResume(_ w.World) error

OnResume はステートが再開される際に呼ばれる

func (*MessageState) OnStart

func (st *MessageState) OnStart(world w.World) error

OnStart はステートが開始される際に呼ばれる

func (*MessageState) OnStop

func (st *MessageState) OnStop(_ w.World) error

OnStop はステートが停止される際に呼ばれる

func (MessageState) String

func (st MessageState) String() string

func (*MessageState) Update

func (st *MessageState) Update(_ w.World) (es.Transition[w.World], error)

Update はゲームステートの更新処理を行う

type PersistentMessageState

PersistentMessageState は選択肢実行後もウィンドウを開いたままにするMessageStateラッパー

type PersistentMessageState struct {
    MessageState
}

func NewPersistentMessageState

func NewPersistentMessageState(messageData *messagedata.MessageData) *PersistentMessageState

NewPersistentMessageState は永続的なメッセージステートを作成する

func (*PersistentMessageState) OnResume

func (st *PersistentMessageState) OnResume(world w.World) error

OnResume はステートが再開される際に呼ばれる

func (PersistentMessageState) String

func (st PersistentMessageState) String() string

func (*PersistentMessageState) Update

func (st *PersistentMessageState) Update(_ w.World) (es.Transition[w.World], error)

Update はゲームステートの更新処理を行う

type ShopMenuState

ShopMenuState はショップメニューのゲームステート

type ShopMenuState struct {
    es.BaseState[w.World]
    // contains filtered or unexported fields
}

func (*ShopMenuState) DoAction

func (st *ShopMenuState) DoAction(world w.World, action inputmapper.ActionID) (es.Transition[w.World], error)

DoAction はActionを実行する

func (*ShopMenuState) Draw

func (st *ShopMenuState) Draw(_ w.World, screen *ebiten.Image) error

Draw はゲームステートの描画処理を行う

func (*ShopMenuState) HandleInput

func (st *ShopMenuState) HandleInput() (inputmapper.ActionID, bool)

HandleInput はキー入力をActionに変換する

func (*ShopMenuState) OnPause

func (st *ShopMenuState) OnPause(_ w.World) error

OnPause はステートが一時停止される際に呼ばれる

func (*ShopMenuState) OnResume

func (st *ShopMenuState) OnResume(_ w.World) error

OnResume はステートが再開される際に呼ばれる

func (*ShopMenuState) OnStart

func (st *ShopMenuState) OnStart(world w.World) error

OnStart はステートが開始される際に呼ばれる

func (*ShopMenuState) OnStop

func (st *ShopMenuState) OnStop(_ w.World) error

OnStop はステートが停止される際に呼ばれる

func (ShopMenuState) String

func (st ShopMenuState) String() string

func (*ShopMenuState) Update

func (st *ShopMenuState) Update(world w.World) (es.Transition[w.World], error)

Update はゲームステートの更新処理を行う