Line data Source code
1 : package mapplanner
2 :
3 : import (
4 : "fmt"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : )
8 :
9 : // AutoTileIndex は16タイルオートタイルのインデックス(0-15)
10 : // 4方向の隣接情報をビットマスクで表現
11 : type AutoTileIndex int
12 :
13 : // 16タイル標準パターン定数
14 : // ビットマスク:上(1) 右(2) 下(4) 左(8)
15 : // 各ビットは「その方向に同じタイルがある」ことを示す
16 : const (
17 : AutoTileIsolated AutoTileIndex = 0 // 0000: 全方向が異なる(孤立)
18 : AutoTileUp AutoTileIndex = 1 // 0001: 上だけ同じ
19 : AutoTileRight AutoTileIndex = 2 // 0010: 右だけ同じ
20 : AutoTileUpRight AutoTileIndex = 3 // 0011: 上右が同じ
21 : AutoTileDown AutoTileIndex = 4 // 0100: 下だけ同じ
22 : AutoTileVertical AutoTileIndex = 5 // 0101: 上下が同じ
23 : AutoTileDownRight AutoTileIndex = 6 // 0110: 下右が同じ
24 : AutoTileUpDownRight AutoTileIndex = 7 // 0111: 上下右が同じ
25 : AutoTileLeft AutoTileIndex = 8 // 1000: 左だけ同じ
26 : AutoTileUpLeft AutoTileIndex = 9 // 1001: 上左が同じ
27 : AutoTileHorizontal AutoTileIndex = 10 // 1010: 左右が同じ
28 : AutoTileUpLeftRight AutoTileIndex = 11 // 1011: 上左右が同じ
29 : AutoTileDownLeft AutoTileIndex = 12 // 1100: 下左が同じ
30 : AutoTileUpDownLeft AutoTileIndex = 13 // 1101: 上下左が同じ
31 : AutoTileDownLeftRight AutoTileIndex = 14 // 1110: 下左右が同じ
32 : AutoTileCenter AutoTileIndex = 15 // 1111: 全方向に同じタイル
33 : )
34 :
35 : // String はAutoTileIndexの文字列表現を返す
36 : func (ati AutoTileIndex) String() string {
37 1 : switch ati {
38 : case AutoTileIsolated:
39 1 : return "Isolated"
40 : case AutoTileUp:
41 1 : return "Up"
42 : case AutoTileRight:
43 1 : return "Right"
44 : case AutoTileUpRight:
45 1 : return "UpRight"
46 : case AutoTileDown:
47 1 : return "Down"
48 : case AutoTileVertical:
49 1 : return "Vertical"
50 : case AutoTileDownRight:
51 1 : return "DownRight"
52 : case AutoTileUpDownRight:
53 1 : return "UpDownRight"
54 : case AutoTileLeft:
55 1 : return "Left"
56 : case AutoTileUpLeft:
57 1 : return "UpLeft"
58 : case AutoTileHorizontal:
59 1 : return "Horizontal"
60 : case AutoTileUpLeftRight:
61 1 : return "UpLeftRight"
62 : case AutoTileDownLeft:
63 1 : return "DownLeft"
64 : case AutoTileUpDownLeft:
65 1 : return "UpDownLeft"
66 : case AutoTileDownLeftRight:
67 1 : return "DownLeftRight"
68 : case AutoTileCenter:
69 1 : return "Center"
70 : default:
71 0 : panic(fmt.Sprintf("invalid AutoTileIndex value: %d", int(ati)))
72 : }
73 : }
74 :
75 : // AutoTileBits は4方向の接続の有無からオートタイルのビットマスクを組む。上1・右2・下4・左8。
76 : // プランナ格子を見る CalculateAutoTileIndex と、生成後の ECS 実体を見る overworld の継ぎ目再計算が
77 : // この規則を1箇所から共有し、両者のビット割り当てがずれないようにする。純関数なので単体で検証できる。
78 : func AutoTileBits(up, right, down, left bool) AutoTileIndex {
79 1 : bitmask := 0
80 1 : if up {
81 1 : bitmask |= 1
82 1 : }
83 1 : if right {
84 1 : bitmask |= 2
85 1 : }
86 1 : if down {
87 1 : bitmask |= 4
88 1 : }
89 1 : if left {
90 1 : bitmask |= 8
91 1 : }
92 1 : return AutoTileIndex(bitmask)
93 : }
94 :
95 : // CalculateAutoTileIndex は4方向の隣接情報からオートタイルインデックスを計算
96 : // 同じタイル名のタイルとのみ接続する
97 : func (mp *MetaPlan) CalculateAutoTileIndex(idx gc.TileIdx, tileType string) AutoTileIndex {
98 : // 4方向の隣接チェック - 同じタイル名の場合のみ接続
99 1 : up := mp.UpTile(idx).Id == tileType
100 1 : right := mp.RightTile(idx).Id == tileType
101 1 : down := mp.DownTile(idx).Id == tileType
102 1 : left := mp.LeftTile(idx).Id == tileType
103 1 : return AutoTileBits(up, right, down, left)
104 1 : }
105 :
106 : // IsValidIndex はインデックスが有効範囲内かチェック
107 : func (mp *MetaPlan) IsValidIndex(idx gc.TileIdx) bool {
108 1 : return idx >= 0 && int(idx) < len(mp.Tiles)
109 1 : }
|