Line data Source code
1 : package mapplanner
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : "github.com/kijimaD/ruins/internal/consts"
6 : )
7 :
8 : // DoorPlanner は部屋の入口にランダムにドアを配置するプランナー。
9 : // 床タイルの左右または上下が壁であるパターンを検出してドアを置く
10 : type DoorPlanner struct {
11 : DoorChance float64 // ドア生成確率(0.0〜1.0)
12 : }
13 :
14 : // PlanMeta は全タイルを走査してドアを配置する
15 : func (p DoorPlanner) PlanMeta(mp *MetaPlan) error {
16 1 : width := int(mp.Level.TileWidth)
17 1 : height := int(mp.Level.TileHeight)
18 1 :
19 1 : for y := 1; y < height-1; y++ {
20 1 : for x := 1; x < width-1; x++ {
21 1 : idx := y*width + x
22 1 : if mp.Tiles[idx].BlockPass {
23 1 : continue
24 : }
25 :
26 1 : if !isDoorPattern(mp, x, y, width) {
27 1 : continue
28 : }
29 :
30 1 : if mp.RNG.Float64() >= p.DoorChance {
31 1 : continue
32 : }
33 :
34 1 : mp.Doors = append(mp.Doors, DoorSpec{
35 1 : Coord: consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(y)},
36 1 : Orientation: doorOrientation(mp, x, y, width),
37 1 : })
38 : }
39 : }
40 1 : return nil
41 : }
42 :
43 : // isDoorPattern は床タイルがドア配置可能なパターンかを判定する。
44 : // 左右が壁かつ上下が床、または上下が壁かつ左右が床の場合にtrueを返す
45 : func isDoorPattern(mp *MetaPlan, x, y, width int) bool {
46 1 : idx := y*width + x
47 1 : left := mp.Tiles[idx-1]
48 1 : right := mp.Tiles[idx+1]
49 1 : top := mp.Tiles[idx-width]
50 1 : bottom := mp.Tiles[idx+width]
51 1 :
52 : // 左右が壁、上下が床 → 縦向きドア
53 1 : if left.BlockPass && right.BlockPass && !top.BlockPass && !bottom.BlockPass {
54 1 : return true
55 1 : }
56 : // 上下が壁、左右が床 → 横向きドア
57 1 : if top.BlockPass && bottom.BlockPass && !left.BlockPass && !right.BlockPass {
58 1 : return true
59 1 : }
60 :
61 1 : return false
62 : }
63 :
64 : // doorOrientation はドア位置の隣接タイルから向きを判定する
65 : func doorOrientation(mp *MetaPlan, x, y, width int) gc.DoorOrientation {
66 1 : idx := y*width + x
67 1 : if mp.Tiles[idx-1].BlockPass && mp.Tiles[idx+1].BlockPass {
68 1 : return gc.DoorOrientationVertical
69 1 : }
70 1 : return gc.DoorOrientationHorizontal
71 : }
|