Line data Source code
1 : package query
2 :
3 : import (
4 : "fmt"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : w "github.com/kijimaD/ruins/internal/world"
8 : "github.com/mlange-42/ark/ecs"
9 : )
10 :
11 : // IsWeapon はエンティティが武器かを返す。近接または射撃のいずれかを持てば武器とみなす。
12 : func IsWeapon(world w.World, entity ecs.Entity) bool {
13 1 : return world.Components.Melee.Has(entity) || world.Components.Fire.Has(entity)
14 1 : }
15 :
16 : // GetWeapons は武器一覧を取得する(スロット1〜5)
17 : // 必ず長さ5のスライスを返す
18 : func GetWeapons(world w.World, owner ecs.Entity) []*ecs.Entity {
19 1 : weapons := make([]*ecs.Entity, 5)
20 1 :
21 1 : weaponsQuery := ecs.NewFilter1[gc.LocationEquipped](world.ECS).Query()
22 1 : for weaponsQuery.Next() {
23 1 : entity := weaponsQuery.Entity()
24 1 : if !IsWeapon(world, entity) {
25 1 : continue
26 : }
27 1 : equipped := world.Components.LocationEquipped.Get(entity)
28 1 : if owner == equipped.Owner {
29 1 : if equipped.EquipmentSlot >= gc.SlotWeapon1 && equipped.EquipmentSlot <= gc.SlotWeapon5 {
30 1 : index := int(equipped.EquipmentSlot) - int(gc.SlotWeapon1)
31 1 : weapons[index] = &entity
32 1 : }
33 : }
34 : }
35 :
36 1 : return weapons
37 : }
38 :
39 : // GetArmorEquipments は防具一覧を取得する(HEAD, TORSO, ARMS, HANDS, LEGS, FEET, JEWELRY)
40 : // 必ず長さ7のスライスを返す
41 : func GetArmorEquipments(world w.World, owner ecs.Entity) []*ecs.Entity {
42 1 : entities := make([]*ecs.Entity, 7)
43 1 :
44 1 : armorQuery := ecs.NewFilter2[gc.LocationEquipped, gc.Wearable](world.ECS).Query()
45 1 : for armorQuery.Next() {
46 1 : entity := armorQuery.Entity()
47 1 : equipped := world.Components.LocationEquipped.Get(entity)
48 1 : if owner == equipped.Owner {
49 1 : switch equipped.EquipmentSlot {
50 : case gc.SlotHead:
51 1 : entities[0] = &entity
52 : case gc.SlotTorso:
53 1 : entities[1] = &entity
54 : case gc.SlotArms:
55 1 : entities[2] = &entity
56 : case gc.SlotHands:
57 1 : entities[3] = &entity
58 : case gc.SlotLegs:
59 1 : entities[4] = &entity
60 : case gc.SlotFeet:
61 1 : entities[5] = &entity
62 : case gc.SlotJewelry:
63 1 : entities[6] = &entity
64 : default:
65 1 : panic(fmt.Sprintf("invalid equipment slot: %v", equipped.EquipmentSlot))
66 : }
67 : }
68 : }
69 :
70 1 : return entities
71 : }
|