Line data Source code
1 : package gameaction
2 :
3 : import (
4 : "errors"
5 : "fmt"
6 :
7 : gc "github.com/kijimaD/ruins/internal/components"
8 : "github.com/kijimaD/ruins/internal/rawschema"
9 : w "github.com/kijimaD/ruins/internal/world"
10 : "github.com/kijimaD/ruins/internal/world/lifecycle"
11 : "github.com/mlange-42/ark/ecs"
12 : )
13 :
14 : var (
15 : // errProfessionItem は職業の初期アイテムを生成できないときに返す
16 : errProfessionItem = errors.New("failed to generate profession starting item")
17 : // errProfessionEquipment は職業の初期装備を生成できないときに返す
18 : errProfessionEquipment = errors.New("failed to generate profession starting equipment")
19 : // errInvalidEquipmentSlot は職業の初期装備のスロット名が不正なときに返す
20 : errInvalidEquipmentSlot = errors.New("invalid equipment slot name")
21 : )
22 :
23 : // ApplyProfession はプレイヤーエンティティに職業の属性値・スキル・装備を適用する。
24 : // 職業選択時に使う。
25 : func ApplyProfession(world w.World, player ecs.Entity, prof rawschema.Profession) error {
26 : // 職業IDを保持する。再適用時は既存を更新する
27 1 : if err := gc.Upsert(world.ECS, world.Components.Profession, player, &gc.Profession{ID: prof.Id}); err != nil {
28 0 : return err
29 0 : }
30 :
31 : // 職業の属性値で上書き
32 1 : abils := world.Components.Abilities.Get(player)
33 1 : abils.Strength = gc.Ability{Base: prof.Abilities.Strength}
34 1 : abils.Sensation = gc.Ability{Base: prof.Abilities.Sensation}
35 1 : abils.Dexterity = gc.Ability{Base: prof.Abilities.Dexterity}
36 1 : abils.Agility = gc.Ability{Base: prof.Abilities.Agility}
37 1 : abils.Vitality = gc.Ability{Base: prof.Abilities.Vitality}
38 1 : abils.Defense = gc.Ability{Base: prof.Abilities.Defense}
39 1 :
40 : // 職業のスキル初期値を設定
41 1 : playerSkills := world.Components.Skills.Get(player)
42 1 : *playerSkills = *gc.NewSkills()
43 1 : if prof.Skills != nil {
44 1 : for _, ps := range *prof.Skills {
45 1 : playerSkills.Get(ps.Id).Value = ps.Value
46 1 : }
47 : }
48 : // 属性値変更後にHP/APを再計算
49 1 : _ = lifecycle.FullRecover(world, player)
50 1 :
51 : // 初期アイテムをバックパックに付与
52 1 : for _, profItem := range prof.Items {
53 1 : if _, err := lifecycle.SpawnBackpackItem(world, profItem.Name, profItem.Count); err != nil {
54 1 : return fmt.Errorf("%w: %s: %w", errProfessionItem, profItem.Name, err)
55 1 : }
56 : }
57 :
58 : // 初期装備を付与して装備する
59 1 : for _, equip := range prof.Equips {
60 1 : item, err := lifecycle.SpawnBackpackItem(world, equip.Name, 1)
61 1 : if err != nil {
62 1 : return fmt.Errorf("%w: %s: %w", errProfessionEquipment, equip.Name, err)
63 1 : }
64 1 : slot, ok := gc.ParseEquipmentSlot(equip.Slot)
65 1 : if !ok {
66 1 : return fmt.Errorf("%w: %s (item: %s)", errInvalidEquipmentSlot, equip.Slot, equip.Name)
67 1 : }
68 1 : lifecycle.MoveToEquip(world, item, player, slot)
69 : }
70 :
71 1 : return nil
72 : }
|