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 : "github.com/kijimaD/ruins/internal/rawschema"
7 : )
8 :
9 : // RectRoomPlanner は長方形の部屋を作成する
10 : type RectRoomPlanner struct {
11 : Row rawschema.RectRoomStart
12 : }
13 :
14 : // PlanInitial は初期プランを行う
15 : func (b RectRoomPlanner) PlanInitial(planData *MetaPlan) error {
16 1 : b.PlanRooms(planData)
17 1 : return nil
18 1 : }
19 :
20 : // PlanRooms は部屋をプランする
21 : // 上端(y=0)と下端(y=height)に必ず各1つの部屋を配置し、上下端への接続を保証する
22 : func (b RectRoomPlanner) PlanRooms(planData *MetaPlan) {
23 1 : width := int(planData.Level.TileWidth)
24 1 : height := int(planData.Level.TileHeight)
25 1 : rooms := make([]gc.Rect, 0, 16)
26 1 :
27 : // 上端に必ず1つの部屋を配置(y=0から開始)
28 1 : topRoom := b.createRoom(planData, width, height, 0)
29 1 : rooms = append(rooms, topRoom)
30 1 :
31 : // 下端に必ず1つの部屋を配置(Y2=height-1に密着)
32 1 : bottomW := b.Row.RoomSize.Roll(planData.RNG)
33 1 : bottomH := b.Row.RoomSize.Roll(planData.RNG)
34 1 : bottomX := planData.RNG.IntN(width)
35 1 : rooms = append(rooms, gc.Rect{Min: consts.Coord[consts.Tile]{X: consts.Tile(bottomX), Y: consts.Tile(max(height-bottomH, 0))}, Max: consts.Coord[consts.Tile]{X: consts.Tile(min(bottomX+bottomW, width) - 1), Y: consts.Tile(height - 1)}})
36 1 :
37 : // 残りの部屋はランダム配置
38 1 : maxRooms := b.Row.Rooms.Roll(planData.RNG)
39 1 : for range maxRooms {
40 1 : y := planData.RNG.IntN(height)
41 1 : rooms = append(rooms, b.createRoom(planData, width, height, y))
42 1 : }
43 :
44 1 : planData.Rooms = rooms
45 : }
46 :
47 : // createRoom は指定のY座標から部屋を生成する
48 : func (b RectRoomPlanner) createRoom(planData *MetaPlan, width, height, y int) gc.Rect {
49 1 : x := planData.RNG.IntN(width)
50 1 : w := b.Row.RoomSize.Roll(planData.RNG)
51 1 : h := b.Row.RoomSize.Roll(planData.RNG)
52 1 : return gc.Rect{Min: consts.Coord[consts.Tile]{X: consts.Tile(x), Y: consts.Tile(max(y, 0))}, Max: consts.Coord[consts.Tile]{X: consts.Tile(min(x+w, width) - 1), Y: consts.Tile(min(y+h, height) - 1)}}
53 1 :
54 : }
|