Line data Source code
1 : package query
2 :
3 : import (
4 : "errors"
5 : "fmt"
6 :
7 : gc "github.com/kijimaD/ruins/internal/components"
8 : "github.com/kijimaD/ruins/internal/consts"
9 : "github.com/kijimaD/ruins/internal/geometry"
10 : w "github.com/kijimaD/ruins/internal/world"
11 : "github.com/mlange-42/ark/ecs"
12 : )
13 :
14 : // Player はプレイヤーエンティティをVisitする。
15 : // f はエンティティ生成などの構造変更を行うことがあるため、
16 : // クエリを閉じてから呼び出す。反復中はワールドがロックされる
17 : func Player(world w.World, f func(entity ecs.Entity)) {
18 1 : var players []ecs.Entity
19 1 : playerQuery := ecs.NewFilter2[gc.Player, gc.FactionAlly](world.ECS).Query()
20 1 : for playerQuery.Next() {
21 1 : players = append(players, playerQuery.Entity())
22 1 : }
23 1 : for _, entity := range players {
24 1 : f(entity)
25 1 : }
26 : }
27 :
28 : // プレイヤーのエンティティを1つに決められないときのエラー
29 : var (
30 : // ErrNoPlayer はプレイヤーのエンティティが無いときに返す
31 : ErrNoPlayer = errors.New("no player entity exists")
32 : // ErrMultiplePlayers はプレイヤーのエンティティが2つ以上あるときに返す
33 : ErrMultiplePlayers = errors.New("multiple player entities exist")
34 : )
35 :
36 : // GetPlayerEntity はプレイヤーエンティティを返す
37 : // プレイヤーが0個または2個以上の場合はエラーを返す
38 : func GetPlayerEntity(world w.World) (ecs.Entity, error) {
39 1 : var entities []ecs.Entity
40 1 : playerQuery := ecs.NewFilter1[gc.Player](world.ECS).Query()
41 1 : for playerQuery.Next() {
42 1 : entity := playerQuery.Entity()
43 1 : entities = append(entities, entity)
44 1 : }
45 :
46 1 : if len(entities) == 0 {
47 1 : return gc.InvalidEntity, ErrNoPlayer
48 1 : }
49 1 : if len(entities) > 1 {
50 1 : return gc.InvalidEntity, fmt.Errorf("%w: %d", ErrMultiplePlayers, len(entities))
51 1 : }
52 :
53 1 : return entities[0], nil
54 : }
55 :
56 : // AliveHas はエンティティが生存しコンポーネントを保持する場合のみ true を返す。
57 : // Ark は死亡エンティティへの Has でパニックするため、生存確認と組み合わせて安全に判定する
58 : func AliveHas[T any](world w.World, comp *ecs.Map[T], entity ecs.Entity) bool {
59 1 : return world.ECS.Alive(entity) && comp.Has(entity)
60 1 : }
61 :
62 : // IsPickable はフィールド上のアイテムだけを拾える物とみなす。prop や投影タイルは Item を持たず除外される
63 : func IsPickable(entity ecs.Entity, world w.World) bool {
64 1 : return world.Components.LocationOnField.Has(entity) &&
65 1 : world.Components.Item.Has(entity)
66 1 : }
67 :
68 : // PickablesAt は指定タイル上の拾得可能なエンティティを返す。拾得アクションの構築側が
69 : // 「どのタイルを拾うか」から「どのエンティティを拾うか」へ解決するのに使う。
70 : // タイル上のエンティティ列挙は共通の GetEntitiesAt に委ね、拾得可否だけを重ねる。
71 : func PickablesAt(world w.World, tile consts.Coord[consts.Tile]) []ecs.Entity {
72 1 : var result []ecs.Entity
73 1 : for _, entity := range GetEntitiesAt(world, tile.X, tile.Y) {
74 1 : if IsPickable(entity, world) {
75 1 : result = append(result, entity)
76 1 : }
77 : }
78 1 : return result
79 : }
80 :
81 : // IsInActivationRange はプレイヤーがトリガーの発動範囲内にいるかを判定する
82 : func IsInActivationRange(playerGrid, triggerGrid *gc.GridElement, activationRange gc.ActivationRange) bool {
83 1 : switch activationRange {
84 : case gc.ActivationRangeSameTile:
85 1 : return playerGrid.X == triggerGrid.X && playerGrid.Y == triggerGrid.Y
86 : case gc.ActivationRangeAdjacent:
87 1 : return geometry.IsAdjacent(playerGrid.Coord, triggerGrid.Coord)
88 : default:
89 1 : return false
90 : }
91 : }
92 :
93 : // GetEntitiesAt は指定座標にあるすべてのエンティティを返す
94 : func GetEntitiesAt(world w.World, x, y consts.Tile) []ecs.Entity {
95 1 : var entities []ecs.Entity
96 1 : // 座標が重なる退避中ステージのエンティティを座標検索に混ぜない
97 1 : entitiesQuery := ActiveFilter1[gc.GridElement](world).Query()
98 1 : for entitiesQuery.Next() {
99 1 : entity := entitiesQuery.Entity()
100 1 : grid := world.Components.GridElement.Get(entity)
101 1 : if grid.X == x && grid.Y == y {
102 1 : entities = append(entities, entity)
103 1 : }
104 : }
105 1 : return entities
106 : }
107 :
108 : // GetPlayerCamera はカメラを返す。存在しなければ nil を返す。
109 : // カメラはワールドに1つだけ存在する前提で、視点を読む側の定型をここへ集約する。
110 : func GetPlayerCamera(world w.World) *gc.Camera {
111 1 : var camera *gc.Camera
112 1 : cameraQuery := ecs.NewFilter1[gc.Camera](world.ECS).Query()
113 1 : for cameraQuery.Next() {
114 1 : camera = world.Components.Camera.Get(cameraQuery.Entity())
115 1 : }
116 1 : return camera
117 : }
|