Line data Source code
1 : package mapplanner
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : "github.com/kijimaD/ruins/internal/raw"
6 : "github.com/kijimaD/ruins/internal/rawschema"
7 : )
8 :
9 : // EnvironmentPlanner は環境情報を計算するプランナー
10 : type EnvironmentPlanner struct{}
11 :
12 : // PlanMeta はタイルごとの環境を計算して MetaPlan.Environments に入れる。
13 : // タイルの行から決まる値はそのまま使い、水の影響は周りのタイルから導く
14 : func (p EnvironmentPlanner) PlanMeta(mp *MetaPlan) error {
15 1 : mp.Environments = make([]gc.TileEnvironment, len(mp.Tiles))
16 1 : for i := range mp.Tiles {
17 1 : mp.Environments[i] = raw.TileEnvironmentOf(mp.Tiles[i])
18 1 : mp.Environments[i].Water = p.calcWater(mp, i)
19 1 : }
20 :
21 1 : return nil
22 : }
23 :
24 : // calcWater は水の影響を計算する。水のタイルの上なら水中、隣が水のタイルなら水辺、どちらでもなければゼロ値
25 : func (p EnvironmentPlanner) calcWater(mp *MetaPlan, idx int) rawschema.WaterExposure {
26 1 : if isWaterTile(mp.Tiles[idx]) {
27 1 : return rawschema.Submerged
28 1 : }
29 :
30 : // 隣接タイルに水があるかチェック
31 1 : width := int(mp.Level.TileWidth)
32 1 : height := int(mp.Level.TileHeight)
33 1 : x, y := idx%width, idx/width
34 1 :
35 1 : adjacentIndices := []int{}
36 1 : if x > 0 {
37 1 : adjacentIndices = append(adjacentIndices, idx-1)
38 1 : }
39 1 : if x < width-1 {
40 1 : adjacentIndices = append(adjacentIndices, idx+1)
41 1 : }
42 1 : if y > 0 {
43 1 : adjacentIndices = append(adjacentIndices, idx-width)
44 1 : }
45 1 : if y < height-1 {
46 1 : adjacentIndices = append(adjacentIndices, idx+width)
47 1 : }
48 :
49 1 : for _, adjIdx := range adjacentIndices {
50 1 : if isWaterTile(mp.Tiles[adjIdx]) {
51 1 : return rawschema.Nearby
52 1 : }
53 : }
54 :
55 1 : return ""
56 : }
57 :
58 : // isWaterTile はタイルが水のタイルかを返す
59 : func isWaterTile(tile rawschema.Tile) bool {
60 1 : return tile.Water != nil && *tile.Water
61 1 : }
|