diff --git a/cardinal/component/tilemap.go b/cardinal/component/tilemap.go index 6231add..89d306e 100644 --- a/cardinal/component/tilemap.go +++ b/cardinal/component/tilemap.go @@ -1,16 +1,33 @@ package component +type Tile struct { + ID int `json:"id"` + Tile TileType `json:"tile"` + Building BuildingType `json:"building"` +} + +type TileMap struct { + Tiles *[]Tile `json:"tiles"` + Width int `json:"width"` + Height int `json:"height"` +} + +func (TileMap) Name() string { + return "TileMap" +} + +const MapWidth = 9 +const MapHeight = 5 + type TileType string type BuildingType string -const MapWidth = 3 -const MapHeight = 3 - const ( - Generic TileType = "Generic" - Wood TileType = "Wood" - Water TileType = "Water" - Stone TileType = "Stone" + Generic TileType = "Generic" + Wood TileType = "Wood" + Stone TileType = "Stone" + Water TileType = "Water" + Coastline TileType = "Coastline" ) const ( @@ -24,33 +41,102 @@ const ( UnitLimitHouse BuildingType = "UnitLimitHouse" ) -type Tile struct { - Tile TileType `json:"tile"` - Building BuildingType `json:"building"` -} - -func GetDefaultTiles() *map[int]Tile { - tiles := map[int]Tile{ - 0: {Tile: Generic, Building: Warehouse}, - 1: {Tile: Stone, Building: Quarry}, - 2: {Tile: Wood, Building: FishermanHut}, - 3: {Tile: Water, Building: UnitLimitHouse}, - 4: {Tile: Generic, Building: Shipyard}, - 5: {Tile: Generic, Building: None}, - 6: {Tile: Water, Building: UnitLimitHouse}, - 7: {Tile: Generic, Building: Shipyard}, - 8: {Tile: Generic, Building: None}, +func GetDefaultTiles() *[]Tile { + tiles := []Tile{ + {ID: Tile0, Tile: Coastline, Building: None}, + {ID: Tile1, Tile: Coastline, Building: None}, + {ID: Tile2, Tile: Coastline, Building: None}, + {ID: Tile3, Tile: Coastline, Building: None}, + {ID: Tile4, Tile: Coastline, Building: None}, + {ID: Tile5, Tile: Generic, Building: None}, + {ID: Tile6, Tile: Generic, Building: None}, + {ID: Tile7, Tile: Generic, Building: None}, + {ID: Tile8, Tile: Generic, Building: None}, + {ID: Tile9, Tile: Generic, Building: None}, + {ID: Tile10, Tile: Generic, Building: None}, + {ID: Tile11, Tile: Generic, Building: None}, + {ID: Tile12, Tile: Generic, Building: None}, + {ID: Tile13, Tile: Generic, Building: None}, + {ID: Tile14, Tile: Generic, Building: None}, + {ID: Tile15, Tile: Generic, Building: None}, + {ID: Tile16, Tile: Generic, Building: None}, + {ID: Tile17, Tile: Generic, Building: None}, + {ID: Tile18, Tile: Generic, Building: None}, + {ID: Tile19, Tile: Generic, Building: None}, + {ID: Tile20, Tile: Generic, Building: None}, + {ID: Tile21, Tile: Generic, Building: None}, + {ID: Tile22, Tile: Generic, Building: Main}, + {ID: Tile23, Tile: Generic, Building: None}, + {ID: Tile24, Tile: Stone, Building: None}, + {ID: Tile25, Tile: Generic, Building: None}, + {ID: Tile26, Tile: Generic, Building: None}, + {ID: Tile27, Tile: Generic, Building: None}, + {ID: Tile28, Tile: Generic, Building: None}, + {ID: Tile29, Tile: Generic, Building: None}, + {ID: Tile30, Tile: Generic, Building: None}, + {ID: Tile31, Tile: Generic, Building: None}, + {ID: Tile32, Tile: Generic, Building: None}, + {ID: Tile33, Tile: Generic, Building: None}, + {ID: Tile34, Tile: Generic, Building: None}, + {ID: Tile35, Tile: Generic, Building: None}, + {ID: Tile36, Tile: Generic, Building: None}, + {ID: Tile37, Tile: Generic, Building: None}, + {ID: Tile38, Tile: Generic, Building: None}, + {ID: Tile39, Tile: Generic, Building: None}, + {ID: Tile40, Tile: Generic, Building: None}, + {ID: Tile41, Tile: Generic, Building: None}, + {ID: Tile42, Tile: Generic, Building: None}, + {ID: Tile43, Tile: Generic, Building: None}, + {ID: Tile44, Tile: Wood, Building: None}, } return &tiles } -type TileMap struct { - Tiles *map[int]Tile `json:"tiles"` - Width int `json:"width"` - Height int `json:"height"` -} - -func (TileMap) Name() string { - return "TileMap" -} +const ( + Tile0 = 0 + Tile1 = 1 + Tile2 = 2 + Tile3 = 3 + Tile4 = 4 + Tile5 = 5 + Tile6 = 6 + Tile7 = 7 + Tile8 = 8 + Tile9 = 9 + Tile10 = 10 + Tile11 = 11 + Tile12 = 12 + Tile13 = 13 + Tile14 = 14 + Tile15 = 15 + Tile16 = 16 + Tile17 = 17 + Tile18 = 18 + Tile19 = 19 + Tile20 = 20 + Tile21 = 21 + Tile22 = 22 + Tile23 = 23 + Tile24 = 24 + Tile25 = 25 + Tile26 = 26 + Tile27 = 27 + Tile28 = 28 + Tile29 = 29 + Tile30 = 30 + Tile31 = 31 + Tile32 = 32 + Tile33 = 33 + Tile34 = 34 + Tile35 = 35 + Tile36 = 36 + Tile37 = 37 + Tile38 = 38 + Tile39 = 39 + Tile40 = 40 + Tile41 = 41 + Tile42 = 42 + Tile43 = 43 + Tile44 = 44 +) diff --git a/cardinal/main.go b/cardinal/main.go index b3ea0c1..00bd0d2 100644 --- a/cardinal/main.go +++ b/cardinal/main.go @@ -45,6 +45,7 @@ func MustInitWorld(w *cardinal.World) { // NOTE: You must register your queries here for it to be accessible. Must( cardinal.RegisterQuery[query.PlayerHealthRequest, query.PlayerHealthResponse](w, "player-health", query.PlayerHealth), + cardinal.RegisterQuery[query.MapStateRequest, query.MapStateResponse](w, "player-map", query.PlayerMap), ) // Each system executes deterministically in the order they are added. diff --git a/cardinal/query/map_state.go b/cardinal/query/map_state.go new file mode 100644 index 0000000..977358e --- /dev/null +++ b/cardinal/query/map_state.go @@ -0,0 +1,55 @@ +package query + +import ( + "fmt" + comp "oceanus-shard/component" + "pkg.world.dev/world-engine/cardinal" + "pkg.world.dev/world-engine/cardinal/search/filter" + "pkg.world.dev/world-engine/cardinal/types" +) + +type MapStateRequest struct { + Nickname string +} + +type MapStateResponse struct { + Tiles *[]comp.Tile `json:"tiles"` + Width int `json:"width"` + Height int `json:"height"` +} + +func PlayerMap(world cardinal.WorldContext, req *MapStateRequest) (*MapStateResponse, error) { + var playerMap *comp.TileMap + var err error + searchErr := cardinal.NewSearch().Entity( + filter.Contains(filter.Component[comp.Player](), filter.Component[comp.TileMap]())). + Each(world, func(id types.EntityID) bool { + var player *comp.Player + player, err = cardinal.GetComponent[comp.Player](world, id) + if err != nil { + return false + } + + if player.Nickname == req.Nickname { + playerMap, err = cardinal.GetComponent[comp.TileMap](world, id) + if err != nil { + return false + } + return false + } + + return true + }) + if searchErr != nil { + return nil, searchErr + } + if err != nil { + return nil, err + } + + if playerMap == nil { + return nil, fmt.Errorf("player %s does not exist", req.Nickname) + } + + return &MapStateResponse{Tiles: playerMap.Tiles, Width: playerMap.Width, Height: playerMap.Height}, nil +} diff --git a/cardinal/system/attack.go b/cardinal/system/attack.go index 1d113a3..673f4d0 100644 --- a/cardinal/system/attack.go +++ b/cardinal/system/attack.go @@ -2,7 +2,6 @@ package system import ( "fmt" - "pkg.world.dev/world-engine/cardinal" comp "oceanus-shard/component" @@ -17,7 +16,7 @@ func AttackSystem(world cardinal.WorldContext) error { return cardinal.EachMessage[msg.AttackPlayerMsg, msg.AttackPlayerMsgReply]( world, func(attack cardinal.TxData[msg.AttackPlayerMsg]) (msg.AttackPlayerMsgReply, error) { - playerID, playerHealth, err := queryTargetPlayer(world, attack.Msg.TargetNickname) + playerID, playerHealth, err := QueryTargetPlayer(world, attack.Msg.TargetNickname) if err != nil { return msg.AttackPlayerMsgReply{}, fmt.Errorf("failed to inflict damage: %w", err) } diff --git a/cardinal/system/utils.go b/cardinal/system/query_utils.go similarity index 90% rename from cardinal/system/utils.go rename to cardinal/system/query_utils.go index 267140c..3210aaf 100644 --- a/cardinal/system/utils.go +++ b/cardinal/system/query_utils.go @@ -8,8 +8,8 @@ import ( "pkg.world.dev/world-engine/cardinal/types" ) -// queryTargetPlayer queries for the target player's entity ID and health component. -func queryTargetPlayer(world cardinal.WorldContext, targetNickname string) (types.EntityID, *comp.Health, error) { +// QueryTargetPlayer queries for the target player's entity ID and health component. +func QueryTargetPlayer(world cardinal.WorldContext, targetNickname string) (types.EntityID, *comp.Health, error) { var playerID types.EntityID var playerHealth *comp.Health var err error