From 4b631142f7b3a8cfd99fff947715c9b593778df9 Mon Sep 17 00:00:00 2001 From: thefish Date: Mon, 11 Nov 2019 03:13:37 +0300 Subject: [PATCH] prefabs broken --- cmd/game/main.go | 2 +- engine/gamemap/mapgens/default.go | 37 ++++++++++++++++++++++--------- engine/gamemap/room.go | 16 +++++++++---- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/cmd/game/main.go b/cmd/game/main.go index 33c79b5..dd278c1 100644 --- a/cmd/game/main.go +++ b/cmd/game/main.go @@ -69,7 +69,7 @@ func main() { go decodeInput(mainCtx, mw.GetLayer("base")) //fixme set up (load / generate) level - level, rooms := mapgens.DefaultGen(gamemap.NewLevel(mainCtx, "test", 1)) + level, rooms := mapgens.DefaultGen(mainCtx, gamemap.NewLevel(mainCtx, "test", 1)) State.Level = level sidebarWidth := 0 diff --git a/engine/gamemap/mapgens/default.go b/engine/gamemap/mapgens/default.go index c1bf985..0f548b5 100644 --- a/engine/gamemap/mapgens/default.go +++ b/engine/gamemap/mapgens/default.go @@ -1,6 +1,7 @@ package mapgens import ( + "fmt" "lab.zaar.be/thefish/alchemyst-go/engine/gamemap" "lab.zaar.be/thefish/alchemyst-go/engine/types" "lab.zaar.be/thefish/alchemyst-go/util" @@ -37,10 +38,15 @@ var fges = map[int]types.RectFill{ }, } -func DefaultGen(l *gamemap.Level) (*gamemap.Level, []*gamemap.Room) { +func DefaultGen(ctx util.ClientCtx,l *gamemap.Level) (*gamemap.Level, []*gamemap.Room) { rng := util.NewRNG() + //load prefabs + //pfLoader := gamemap.NewPrefabLoader(ctx) + //pfRooms := pfLoader.PrefabRoomsList() + + //fill with walls for i := 0; i < l.W; i ++ { for j := 0; j < l.H; j++ { @@ -51,20 +57,27 @@ func DefaultGen(l *gamemap.Level) (*gamemap.Level, []*gamemap.Room) { rooms := make([]*gamemap.Room, 0) for i := 0; i < maxrooms; i++ { - var failed = false + failed := false var fillage types.RectFill fillage = fges[rng.GetWeightedEntity(map[int]int{1: 10, 2: 1})] - newRoom := gamemap.NewRandomRectRoom( - rng, - rng.Range(minRoomSize, maxRoomSize), - rng.Range(minRoomSize, maxRoomSize), - fillage, - ) + var newRoom *gamemap.Room + //if rng.Range(0, 5) > 3 { + newRoom = gamemap.NewRandomRectRoom( + rng, + rng.Range(minRoomSize, maxRoomSize), + rng.Range(minRoomSize, maxRoomSize), + fillage, + ) + //} else { + // prefab + // newRoom = &pfRooms[0] + //} where := types.Coords{ rng.Range(1, l.W -2 - newRoom.W), rng.Range(1, l.H - 2 - newRoom.H), } + newRoom.MoveToCoords(where) for _, otherRoom := range rooms { if otherRoom.Intersects(newRoom.Rect) { @@ -77,7 +90,6 @@ func DefaultGen(l *gamemap.Level) (*gamemap.Level, []*gamemap.Room) { rooms = append(rooms, newRoom) } - newRoom.BlitToLevel(l, where) //addStairs(rooms) //itemize(rooms) } @@ -87,7 +99,12 @@ func DefaultGen(l *gamemap.Level) (*gamemap.Level, []*gamemap.Room) { //refine it to minimum spanning tree //connect accordingly - + for _, room := range rooms { + err := room.BlitToLevel(l) + if err != nil { + fmt.Printf("err: %v", err) + } + } for idx, room := range rooms { if idx > 0 { diff --git a/engine/gamemap/room.go b/engine/gamemap/room.go index ed6ff4d..d086db5 100644 --- a/engine/gamemap/room.go +++ b/engine/gamemap/room.go @@ -2,6 +2,7 @@ package gamemap import ( "errors" + "fmt" "lab.zaar.be/thefish/alchemyst-go/engine/items" "lab.zaar.be/thefish/alchemyst-go/engine/mob" "lab.zaar.be/thefish/alchemyst-go/engine/types" @@ -29,14 +30,15 @@ func (r *Room) Put (x, y int, tileFunc interface{}) { } } -func (room *Room) BlitToLevel(l *Level, where types.Coords) error { +func (room *Room) BlitToLevel(l *Level) error { //copy tiles like this: //https://stackoverflow.com/questions/21011023/copy-pointer-values-a-b-in-golang for j := 0; j < room.H; j++ { for i := 0; i < room.W; i++ { - underlyingTile := l.GetTileByXY(i+where.X, j+where.Y) + mapCoords := types.Coords{room.X + i, room.Y + j} + underlyingTile := l.GetTile(mapCoords) tileFunc := room.Geometry[i+j*room.W] @@ -46,11 +48,17 @@ func (room *Room) BlitToLevel(l *Level, where types.Coords) error { //check underlying tile if underlyingTile == nil || underlyingTile.Name != "Wall" { + fmt.Println("Invalid blit!") return invalidBlit } - l.Put(i+where.X, j+where.Y, tileFunc) + l.Put(mapCoords.X, mapCoords.Y, tileFunc) } } + + return nil +} + +func (room *Room) MoveToCoords(where types.Coords) *Room { //update room coords? room.X = where.X room.Y = where.Y @@ -62,7 +70,7 @@ func (room *Room) BlitToLevel(l *Level, where types.Coords) error { room.Connectors[i].X += where.X room.Connectors[i].Y += where.Y } - return nil + return room } func NewRandomRectRoom(rng *util.RNG, w, h int, fillage types.RectFill) *Room {