Line data Source code
1 : // Package world はゲームワールドの実装を提供する。
2 : package world
3 :
4 : import (
5 : "time"
6 :
7 : "github.com/hajimehoshi/ebiten/v2"
8 : gc "github.com/kijimaD/ruins/internal/components"
9 : "github.com/kijimaD/ruins/internal/config"
10 : "github.com/kijimaD/ruins/internal/gamelog"
11 : "github.com/kijimaD/ruins/internal/resources"
12 : "github.com/mlange-42/ark/ecs"
13 : )
14 :
15 : // Updater はロジック更新を行うシステム
16 : // Systemを再利用して状態を保持するのに使う
17 : type Updater interface {
18 : // String はシステム名を返す
19 : String() string
20 :
21 : // Update はゲームロジックの更新処理を行う
22 : Update(world World) error
23 : }
24 :
25 : // Renderer は描画を行うシステム
26 : type Renderer interface {
27 : // String はシステム名を返す
28 : String() string
29 :
30 : // Draw は描画処理を行う
31 : Draw(world World, screen *ebiten.Image) error
32 : }
33 :
34 : // World はゲーム全体に必要な情報を保持する
35 : type World struct {
36 : ECS *ecs.World
37 : Components *gc.Components
38 : Resources *resources.Resources
39 : Updaters map[string]Updater
40 : Renderers map[string]Renderer
41 : }
42 :
43 : // InitWorld は初期化する。config はシングルトンの初期化で読むので、構築前に渡す。
44 : func InitWorld(c *gc.Components, cfg *config.Config) (World, error) {
45 1 : arkWorld := ecs.NewWorld()
46 1 : if err := c.InitializeComponents(arkWorld); err != nil {
47 0 : return World{}, err
48 0 : }
49 :
50 1 : world := World{
51 1 : ECS: arkWorld,
52 1 : Components: c,
53 1 : Resources: resources.InitGameResources(),
54 1 : Updaters: make(map[string]Updater),
55 1 : Renderers: make(map[string]Renderer),
56 1 : }
57 1 : world.Resources.Config = cfg
58 1 :
59 1 : world.InitSingleton()
60 1 :
61 1 : return world, nil
62 : }
63 :
64 : // InitSingleton はシングルトンエンティティを新規作成してIDを保存する
65 : func (world World) InitSingleton() {
66 1 : singleton := world.ECS.NewEntity()
67 1 : world.Components.GameLog.Add(singleton, &gc.GameLog{
68 1 : Store: gamelog.NewSafeSlice(gamelog.GameLogMaxSize),
69 1 : })
70 1 : world.Components.Dungeon.Add(singleton, gc.NewDungeon())
71 1 : world.Components.TurnState.Add(singleton, gc.NewTurnState())
72 1 : world.Components.SpatialIndex.Add(singleton, gc.NewSpatialIndex())
73 1 : world.Components.WeaponSelection.Add(singleton, &gc.WeaponSelection{Slot: 1})
74 1 : world.Components.GameTime.Add(singleton, &gc.GameTime{})
75 1 : // 中立の曇りで穏やかに始め、InitialWeatherSpellTurns だけ続けてから WeatherSystem が遷移を始める。
76 : // turn0 で即ロールすると開始直後に荒天になりうるので固定長の立ち上がりにする
77 1 : world.Components.Weather.Add(singleton, &gc.Weather{Current: gc.WeatherCloudy, UntilTurn: gc.InitialWeatherSpellTurns})
78 1 : world.Components.PlayTime.Add(singleton, &gc.PlayTime{})
79 1 : world.Components.VisionState.Add(singleton, gc.NewVisionState())
80 1 : // config は構築時に渡されているので、設定言語をそのまま種にする。
81 1 : world.Components.UserSettings.Add(singleton, gc.NewUserSettings(world.Resources.Config.User.Language))
82 1 : world.Components.RunStats.Add(singleton, &gc.RunStats{})
83 1 : world.Resources.SingletonEntity = singleton
84 1 : }
85 :
86 : // ResetForNewGame は前のゲームの全実体を消し、シングルトンを作り直す。
87 : // 新しいゲームを始める経路が呼ぶ。ロードは save 側の ECS.Reset が同じ役目を担う
88 : func (world World) ResetForNewGame() {
89 1 : var clearEntities []ecs.Entity
90 1 : clearQuery := ecs.NewUnsafeFilter(world.ECS).Query()
91 1 : for clearQuery.Next() {
92 1 : clearEntities = append(clearEntities, clearQuery.Entity())
93 1 : }
94 1 : for _, e := range clearEntities {
95 1 : world.ECS.RemoveEntity(e)
96 1 : }
97 1 : world.InitSingleton()
98 1 : // 新規ランは計測基準を今に置く。InitSingleton の PlayTime は Total=0
99 1 : world.Components.PlayTime.Get(world.Resources.SingletonEntity).Start(0, time.Now())
100 : }
101 :
102 : // GetWorld は entities.World インターフェースを満たすためのメソッド
103 : func (world World) GetWorld() *ecs.World {
104 1 : return world.ECS
105 1 : }
|