Line data Source code
1 : package query
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : w "github.com/kijimaD/ruins/internal/world"
6 : "github.com/mlange-42/ark/ecs"
7 : )
8 :
9 : // Relation はエンティティ間の派閥関係を表す
10 : type Relation string
11 :
12 : // 派閥間の関係性
13 : const (
14 : RelationHostile Relation = "hostile"
15 : RelationFriendly Relation = "friendly"
16 : RelationNeutral Relation = "neutral"
17 : )
18 :
19 : // IsEnemy はエンティティが敵性派閥かを返す
20 : func IsEnemy(world w.World, e ecs.Entity) bool {
21 1 : return world.Components.FactionEnemy.Has(e)
22 1 : }
23 :
24 : // IsAlly はエンティティが味方派閥かを返す
25 : func IsAlly(world w.World, e ecs.Entity) bool {
26 1 : return world.Components.FactionAlly.Has(e)
27 1 : }
28 :
29 : // IsNeutral はエンティティが中立派閥かを返す
30 : func IsNeutral(world w.World, e ecs.Entity) bool {
31 1 : return world.Components.FactionNeutral.Has(e)
32 1 : }
33 :
34 : // FactionRelation は2つのエンティティ間の派閥関係を返す。
35 : // FactionAlly同士はFriendly、FactionEnemy同士もFriendly、
36 : // FactionAllyとFactionEnemyはHostile、それ以外はNeutral
37 : func FactionRelation(world w.World, a, b ecs.Entity) Relation {
38 1 : aEnemy := IsEnemy(world, a)
39 1 : bEnemy := IsEnemy(world, b)
40 1 : aAlly := IsAlly(world, a)
41 1 : bAlly := IsAlly(world, b)
42 1 :
43 1 : if aEnemy && bAlly || aAlly && bEnemy {
44 1 : return RelationHostile
45 1 : }
46 1 : if aEnemy && bEnemy || aAlly && bAlly {
47 1 : return RelationFriendly
48 1 : }
49 1 : return RelationNeutral
50 : }
51 :
52 : // IsAreaSafe はアクターの周囲に敵対エンティティがいないかを返す。休息・睡眠・読書など継続
53 : // アクティビティが毎ターン安全を確かめるのに使う。座標を持たなければ判定できず危険とみなす。
54 : func IsAreaSafe(world w.World, actor ecs.Entity) bool {
55 1 : if !world.Components.GridElement.Has(actor) {
56 1 : return false
57 1 : }
58 1 : gridElement := world.Components.GridElement.Get(actor)
59 1 : actorX, actorY := int(gridElement.X), int(gridElement.Y)
60 1 :
61 1 : safeRadius := 1
62 1 : hasHostile := false
63 1 :
64 1 : areaQuery := ActiveFilter1[gc.GridElement](world).Query()
65 1 : for areaQuery.Next() {
66 1 : entity := areaQuery.Entity()
67 1 : if hasHostile {
68 1 : continue
69 : }
70 1 : if FactionRelation(world, actor, entity) != RelationHostile {
71 1 : continue
72 : }
73 1 : grid := world.Components.GridElement.Get(entity)
74 1 : dx, dy := int(grid.X)-actorX, int(grid.Y)-actorY
75 1 : if dx >= -safeRadius && dx <= safeRadius && dy >= -safeRadius && dy <= safeRadius {
76 1 : hasHostile = true
77 1 : }
78 : }
79 :
80 1 : return !hasHostile
81 : }
|