Line data Source code
1 : package components
2 :
3 : import (
4 : "image/color"
5 :
6 : "github.com/kijimaD/ruins/internal/consts"
7 : )
8 :
9 : // TileIdx はタイル番号
10 : type TileIdx int
11 :
12 : // LightInfo は視界内タイルの光源情報を保持する
13 : type LightInfo struct {
14 : Darkness float64
15 : Color color.RGBA
16 : Ambient float64 // タイルが受ける環境光の明るさ。屋根の下ほど日照を受けず暗い
17 : }
18 :
19 : // SeamlessBand はオーバーワールドのアクティブ帯の永続状態を保持する。
20 : // Active が true のときのみ有効。全フィールドがスカラーなので serde に乗る。
21 : // これによりロード後や遺跡遷移後に Band を再構築できる。
22 : type SeamlessBand struct {
23 : // Active はシームレスワールド中かを表す
24 : Active bool
25 : // RunSeed はチャンク決定的生成の元 seed
26 : RunSeed uint64
27 : // NorthIndex は北進したチャンク数。北へ進むほど増える
28 : NorthIndex consts.Chunk
29 : // ChunkW は1チャンクの幅
30 : ChunkW consts.Tile
31 : // ChunkH は帯の高さ
32 : ChunkH consts.Tile
33 : // Cols は帯の横のチャンク列数。有界
34 : Cols consts.Chunk
35 : // Rows は帯の縦チャンク行数。ゼロ値なら復元時に 1 へ正規化する
36 : Rows consts.Chunk
37 : }
38 :
39 : // BandOriginY は帯ローカル Y=0 すなわち北端が指す絶対タイル Y。北は -Y なので NorthIndex ぶん負へ伸びる。
40 : func (sb SeamlessBand) BandOriginY() consts.AbsTileY {
41 1 : return consts.BandOriginY(sb.NorthIndex, sb.ChunkH)
42 1 : }
43 :
44 : // LocalToAbsY は帯ローカル Y を絶対 Y に変換する。
45 : func (sb SeamlessBand) LocalToAbsY(localY consts.Tile) consts.AbsTileY {
46 1 : return consts.AbsTileY(localY) + sb.BandOriginY()
47 1 : }
48 :
49 : // AbsChunkRow は帯ローカルなタイル行 localY を絶対チャンク行へ移す。localY は帯ローカルで非負なので
50 : // 素の整数除算で足りる。北は -Y なので NorthIndex ぶん引く。探索フォグ・マクロ地図・全画面図の単一出どころ。
51 : func (sb SeamlessBand) AbsChunkRow(localY consts.Tile) consts.Chunk {
52 1 : return consts.Chunk(int(localY)/int(sb.ChunkH)) - sb.NorthIndex
53 1 : }
54 :
55 : // SpawnChunkY はプレイヤーが湧いた位置の絶対チャンク Y。奥行きの起点。プレイヤーは初期帯の中央行に
56 : // 湧くので Rows/2 で確定し、以後 NorthIndex がいくつでも変わらない不変値になる。
57 : func (sb SeamlessBand) SpawnChunkY() consts.Chunk {
58 1 : return sb.Rows / 2
59 1 : }
60 :
61 : // Dungeon は現在地を指すシングルトン。共存する複数ステージのうち、今どれが稼働中かを指す
62 : // identity だけを持つ。フィールド寸法・探索履歴・帯データなどステージ固有の状態は各ステージの
63 : // StageField が、時間や視界などグローバルな状態は専用シングルトンが持つ。
64 : type Dungeon struct {
65 : // CurrentStage は現在稼働しているステージのキー。往復の swap で切り替える。
66 : // 階層数は CurrentStage.Depth から、ダンジョン定義名は CurrentStage.Name から導出する。
67 : // オーバーワールドは深度0で NewOverworldStage() の固定名を持つ。フィールド寸法・探索履歴・帯データは
68 : // 各ステージの StageField が持ち、ここは identity だけを指す。
69 : CurrentStage StageKey
70 : }
71 :
72 : // NewDungeon は初期化されたDungeonを返す
73 : func NewDungeon() *Dungeon {
74 1 : return &Dungeon{}
75 1 : }
76 :
77 : // Level は現在の階層
78 : // タイル計算メソッドを提供する
79 : type Level struct {
80 : // 横のタイル数
81 : TileWidth consts.Tile
82 : // 縦のタイル数
83 : TileHeight consts.Tile
84 : }
85 :
86 : // CoordToIndex はタイル座標から、タイルスライスのインデックスを求める
87 : func (l *Level) CoordToIndex(pos consts.Coord[consts.Tile]) TileIdx {
88 1 : return TileIdx(int(pos.Y)*int(l.TileWidth) + int(pos.X))
89 1 : }
90 :
91 : // IndexToCoord はタイルスライスのインデックスからタイル座標を求める。CoordToIndex の逆操作
92 : func (l *Level) IndexToCoord(idx TileIdx) consts.Coord[consts.Tile] {
93 1 : x := consts.Tile(int(idx) % int(l.TileWidth))
94 1 : y := consts.Tile(int(idx) / int(l.TileWidth))
95 1 :
96 1 : return consts.Coord[consts.Tile]{X: x, Y: y}
97 1 : }
98 :
99 : // Width はステージ幅。横の全体ピクセル数
100 : func (l *Level) Width() consts.WorldPixel {
101 1 : return consts.WorldPixel(int(l.TileWidth) * int(consts.TileSize))
102 1 : }
103 :
104 : // Height はステージ縦。縦の全体ピクセル数
105 : func (l *Level) Height() consts.WorldPixel {
106 1 : return consts.WorldPixel(int(l.TileHeight) * int(consts.TileSize))
107 1 : }
|