From 9f1084b2294253c6a7b6d396c1d9abd48f95da07 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 13 Jun 2024 16:14:50 +0300 Subject: [PATCH] Added tilemap spawning (#1) * Added tilemap spawning * Changed Tiles type to map * Reverted lint-go.yml changes --- cardinal/component/tilemap.go | 56 +++++++++++++++++++++++++++++++ cardinal/main.go | 1 + cardinal/system/player_spawner.go | 33 +++++++++++++++++- cardinal/system/utils.go | 4 +-- 4 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 cardinal/component/tilemap.go diff --git a/cardinal/component/tilemap.go b/cardinal/component/tilemap.go new file mode 100644 index 0000000..6231add --- /dev/null +++ b/cardinal/component/tilemap.go @@ -0,0 +1,56 @@ +package component + +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" +) + +const ( + None BuildingType = "None" + Main BuildingType = "Main" + Woodcutter BuildingType = "Woodcutter" + Quarry BuildingType = "Quarry" + FishermanHut BuildingType = "FishermanHut" + Shipyard BuildingType = "Shipyard" + Warehouse BuildingType = "Warehouse" + 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}, + } + + 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" +} diff --git a/cardinal/main.go b/cardinal/main.go index 0b54bab..b3ea0c1 100644 --- a/cardinal/main.go +++ b/cardinal/main.go @@ -31,6 +31,7 @@ func MustInitWorld(w *cardinal.World) { Must( cardinal.RegisterComponent[component.Player](w), cardinal.RegisterComponent[component.Health](w), + cardinal.RegisterComponent[component.TileMap](w), ) // Register messages (user action) diff --git a/cardinal/system/player_spawner.go b/cardinal/system/player_spawner.go index 03a4ba5..b2a92b7 100644 --- a/cardinal/system/player_spawner.go +++ b/cardinal/system/player_spawner.go @@ -2,6 +2,8 @@ package system import ( "fmt" + "pkg.world.dev/world-engine/cardinal/search/filter" + "pkg.world.dev/world-engine/cardinal/types" "pkg.world.dev/world-engine/cardinal" @@ -19,9 +21,38 @@ func PlayerSpawnerSystem(world cardinal.WorldContext) error { return cardinal.EachMessage[msg.CreatePlayerMsg, msg.CreatePlayerResult]( world, func(create cardinal.TxData[msg.CreatePlayerMsg]) (msg.CreatePlayerResult, error) { + var playerExist = false + var err error + err = cardinal.NewSearch().Entity( + filter.Contains(filter.Component[comp.Player]())). + Each(world, func(id types.EntityID) bool { + var player *comp.Player + player, err = cardinal.GetComponent[comp.Player](world, id) + if err != nil { + return false + } + + // Terminates the search if the player is found + if player.Nickname == create.Msg.Nickname { + playerExist = true + return false + } + + // Continue searching if the player is not the target player + return true + }) + if err != nil { + return msg.CreatePlayerResult{}, fmt.Errorf("error creating player: %w", err) + } + + if playerExist { + return msg.CreatePlayerResult{Success: false}, + fmt.Errorf("error creating player, player with nickname %s already exist", create.Msg.Nickname) + } + id, err := cardinal.Create(world, comp.Player{Nickname: create.Msg.Nickname}, - comp.Health{HP: InitialHP}, + comp.TileMap{Tiles: comp.GetDefaultTiles(), Width: comp.MapWidth, Height: comp.MapHeight}, ) if err != nil { return msg.CreatePlayerResult{}, fmt.Errorf("error creating player: %w", err) diff --git a/cardinal/system/utils.go b/cardinal/system/utils.go index d9fbc32..267140c 100644 --- a/cardinal/system/utils.go +++ b/cardinal/system/utils.go @@ -2,12 +2,10 @@ package system 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" - - comp "oceanus-shard/component" ) // queryTargetPlayer queries for the target player's entity ID and health component.