Line data Source code
1 : package query
2 :
3 : import (
4 : "fmt"
5 :
6 : "github.com/kijimaD/ruins/internal/consts"
7 : w "github.com/kijimaD/ruins/internal/world"
8 : "github.com/mlange-42/ark/ecs"
9 : )
10 :
11 : // AddCurrency はエンティティに所持金を追加する
12 : func AddCurrency(world w.World, entity ecs.Entity, amount consts.Currency) error {
13 1 : wallet := world.Components.Wallet.Get(entity)
14 1 : if wallet == nil {
15 1 : return fmt.Errorf("entity has no Wallet component")
16 1 : }
17 1 : wallet.Currency += amount
18 1 : return nil
19 : }
20 :
21 : // GetCurrency はエンティティの所持金を取得する
22 : func GetCurrency(world w.World, entity ecs.Entity) consts.Currency {
23 1 : wallet := world.Components.Wallet.Get(entity)
24 1 : if wallet == nil {
25 1 : return 0
26 1 : }
27 1 : return wallet.Currency
28 : }
29 :
30 : // HasCurrency は指定額以上の所持金を持っているか確認
31 : func HasCurrency(world w.World, entity ecs.Entity, amount consts.Currency) bool {
32 1 : return GetCurrency(world, entity) >= amount
33 1 : }
34 :
35 : // ConsumeCurrency はエンティティの所持金を消費する
36 : // 所持金が足りない場合はfalseを返す
37 : // TODO(kijima): 使いにくいのを直す
38 : func ConsumeCurrency(world w.World, entity ecs.Entity, amount consts.Currency) bool {
39 1 : if !HasCurrency(world, entity, amount) {
40 1 : return false
41 1 : }
42 1 : wallet := world.Components.Wallet.Get(entity)
43 1 : if wallet == nil {
44 1 : return false
45 1 : }
46 1 : wallet.Currency -= amount
47 1 : return true
48 : }
|