From 05631cc5400289d597860b54c6306508477a4d9c Mon Sep 17 00:00:00 2001 From: Konst Date: Thu, 13 Jun 2024 14:45:25 +0300 Subject: [PATCH 1/3] Added tilemap spawning --- cardinal/component/tilemap.go | 52 +++++++++++++++++++++++++++++++ cardinal/main.go | 1 + cardinal/system/attack.go | 2 ++ cardinal/system/player_spawner.go | 33 +++++++++++++++++++- cardinal/system/utils.go | 39 +++++++++++++++++++++-- 5 files changed, 124 insertions(+), 3 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..ba27d0d --- /dev/null +++ b/cardinal/component/tilemap.go @@ -0,0 +1,52 @@ +package component + +type TileType string +type BuildingType string + +const ( + Generic TileType = "Generic" + Wood = "Wood" + Water = "Water" + Stone = "Stone" +) + +const ( + None BuildingType = "None" + Main = "Main" + Woodcutter = "Woodcutter" + Quarry = "Quarry" + FishermanHut = "FishermanHut" + Shipyard = "Shipyard" + Warehouse = "Warehouse" + UnitLimitHouse = "UnitLimitHouse" +) + +type Tile struct { + Tile TileType `json:"tile"` + Building BuildingType `json:"building"` + X int `json:"x"` + Y int `json:"y"` +} + +func GetDefaultTiles() *[]Tile { + tiles := make([]Tile, 8) + + tiles[0] = Tile{Tile: Generic, Building: Warehouse, X: 0, Y: 0} + tiles[1] = Tile{Tile: Stone, Building: Quarry, X: 1, Y: 0} + tiles[2] = Tile{Tile: Wood, Building: FishermanHut, X: 2, Y: 0} + tiles[3] = Tile{Tile: Water, Building: UnitLimitHouse, X: 3, Y: 0} + tiles[4] = Tile{Tile: Generic, Building: Shipyard, X: 4, Y: 0} + tiles[5] = Tile{Tile: Generic, Building: None, X: 5, Y: 0} + tiles[6] = Tile{Tile: Generic, Building: Woodcutter, X: 6, Y: 0} + tiles[7] = Tile{Tile: Generic, Building: Main, X: 7, Y: 0} + + return &tiles +} + +type TileMap struct { + Tiles *[]Tile `json:"tiles"` +} + +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/attack.go b/cardinal/system/attack.go index 1d113a3..fe11d75 100644 --- a/cardinal/system/attack.go +++ b/cardinal/system/attack.go @@ -17,6 +17,8 @@ func AttackSystem(world cardinal.WorldContext) error { return cardinal.EachMessage[msg.AttackPlayerMsg, msg.AttackPlayerMsgReply]( world, func(attack cardinal.TxData[msg.AttackPlayerMsg]) (msg.AttackPlayerMsgReply, error) { + personaTag := attack.Tx.PersonaTag + fmt.Println(personaTag) 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/player_spawner.go b/cardinal/system/player_spawner.go index 03a4ba5..01dabee 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()}, ) 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..07afe9a 100644 --- a/cardinal/system/utils.go +++ b/cardinal/system/utils.go @@ -2,12 +2,12 @@ package system import ( "fmt" + comp "oceanus-shard/component" + "pkg.world.dev/world-engine/cardinal/persona/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. @@ -49,3 +49,38 @@ func queryTargetPlayer(world cardinal.WorldContext, targetNickname string) (type return playerID, playerHealth, err } + +func querySignerComponentByPersona(world cardinal.WorldContext, targetPersonaName string) (types.EntityID, *component.SignerComponent, error) { + var signerEntityID types.EntityID + var signerEntity *component.SignerComponent + var err error + searchErr := cardinal.NewSearch().Entity( + filter.Exact(filter.Component[component.SignerComponent]())).Each(world, + func(id types.EntityID) bool { + signerEntity, err = cardinal.GetComponent[component.SignerComponent](world, id) + + if err != nil { + return false + } + + // Terminates the search if the player is found + if signerEntity.PersonaTag == targetPersonaName { + signerEntityID = id + return false + } + + return true + }) + + if searchErr != nil { + return 0, nil, err + } + if err != nil { + return 0, nil, err + } + if signerEntityID == 0 { + return 0, nil, fmt.Errorf("signer component %q does not exist", targetPersonaName) + } + + return signerEntityID, signerEntity, err +} From e39eddcb68965456849fe777cfa70f6b459ca730 Mon Sep 17 00:00:00 2001 From: Konst Date: Thu, 13 Jun 2024 15:55:48 +0300 Subject: [PATCH 2/3] Changed Tiles type to map --- .github/workflows/lint-go.yml | 2 +- cardinal/component/tilemap.go | 52 +++++++++++++++++-------------- cardinal/system/attack.go | 2 -- cardinal/system/player_spawner.go | 2 +- cardinal/system/utils.go | 37 ---------------------- 5 files changed, 30 insertions(+), 65 deletions(-) diff --git a/.github/workflows/lint-go.yml b/.github/workflows/lint-go.yml index 9432d45..3f1cb09 100644 --- a/.github/workflows/lint-go.yml +++ b/.github/workflows/lint-go.yml @@ -26,6 +26,6 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v4 with: - version: v1.56.2 + version: v1.56.2 ./... args: --timeout=10m -v working-directory: ./cardinal \ No newline at end of file diff --git a/cardinal/component/tilemap.go b/cardinal/component/tilemap.go index ba27d0d..6231add 100644 --- a/cardinal/component/tilemap.go +++ b/cardinal/component/tilemap.go @@ -3,48 +3,52 @@ package component type TileType string type BuildingType string +const MapWidth = 3 +const MapHeight = 3 + const ( Generic TileType = "Generic" - Wood = "Wood" - Water = "Water" - Stone = "Stone" + Wood TileType = "Wood" + Water TileType = "Water" + Stone TileType = "Stone" ) const ( None BuildingType = "None" - Main = "Main" - Woodcutter = "Woodcutter" - Quarry = "Quarry" - FishermanHut = "FishermanHut" - Shipyard = "Shipyard" - Warehouse = "Warehouse" - UnitLimitHouse = "UnitLimitHouse" + 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"` - X int `json:"x"` - Y int `json:"y"` } -func GetDefaultTiles() *[]Tile { - tiles := make([]Tile, 8) - - tiles[0] = Tile{Tile: Generic, Building: Warehouse, X: 0, Y: 0} - tiles[1] = Tile{Tile: Stone, Building: Quarry, X: 1, Y: 0} - tiles[2] = Tile{Tile: Wood, Building: FishermanHut, X: 2, Y: 0} - tiles[3] = Tile{Tile: Water, Building: UnitLimitHouse, X: 3, Y: 0} - tiles[4] = Tile{Tile: Generic, Building: Shipyard, X: 4, Y: 0} - tiles[5] = Tile{Tile: Generic, Building: None, X: 5, Y: 0} - tiles[6] = Tile{Tile: Generic, Building: Woodcutter, X: 6, Y: 0} - tiles[7] = Tile{Tile: Generic, Building: Main, X: 7, Y: 0} +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 *[]Tile `json:"tiles"` + Tiles *map[int]Tile `json:"tiles"` + Width int `json:"width"` + Height int `json:"height"` } func (TileMap) Name() string { diff --git a/cardinal/system/attack.go b/cardinal/system/attack.go index fe11d75..1d113a3 100644 --- a/cardinal/system/attack.go +++ b/cardinal/system/attack.go @@ -17,8 +17,6 @@ func AttackSystem(world cardinal.WorldContext) error { return cardinal.EachMessage[msg.AttackPlayerMsg, msg.AttackPlayerMsgReply]( world, func(attack cardinal.TxData[msg.AttackPlayerMsg]) (msg.AttackPlayerMsgReply, error) { - personaTag := attack.Tx.PersonaTag - fmt.Println(personaTag) 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/player_spawner.go b/cardinal/system/player_spawner.go index 01dabee..b2a92b7 100644 --- a/cardinal/system/player_spawner.go +++ b/cardinal/system/player_spawner.go @@ -52,7 +52,7 @@ func PlayerSpawnerSystem(world cardinal.WorldContext) error { id, err := cardinal.Create(world, comp.Player{Nickname: create.Msg.Nickname}, - comp.TileMap{Tiles: comp.GetDefaultTiles()}, + 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 07afe9a..267140c 100644 --- a/cardinal/system/utils.go +++ b/cardinal/system/utils.go @@ -3,8 +3,6 @@ package system import ( "fmt" comp "oceanus-shard/component" - "pkg.world.dev/world-engine/cardinal/persona/component" - "pkg.world.dev/world-engine/cardinal" "pkg.world.dev/world-engine/cardinal/search/filter" "pkg.world.dev/world-engine/cardinal/types" @@ -49,38 +47,3 @@ func queryTargetPlayer(world cardinal.WorldContext, targetNickname string) (type return playerID, playerHealth, err } - -func querySignerComponentByPersona(world cardinal.WorldContext, targetPersonaName string) (types.EntityID, *component.SignerComponent, error) { - var signerEntityID types.EntityID - var signerEntity *component.SignerComponent - var err error - searchErr := cardinal.NewSearch().Entity( - filter.Exact(filter.Component[component.SignerComponent]())).Each(world, - func(id types.EntityID) bool { - signerEntity, err = cardinal.GetComponent[component.SignerComponent](world, id) - - if err != nil { - return false - } - - // Terminates the search if the player is found - if signerEntity.PersonaTag == targetPersonaName { - signerEntityID = id - return false - } - - return true - }) - - if searchErr != nil { - return 0, nil, err - } - if err != nil { - return 0, nil, err - } - if signerEntityID == 0 { - return 0, nil, fmt.Errorf("signer component %q does not exist", targetPersonaName) - } - - return signerEntityID, signerEntity, err -} From 79faa120a71532a6361784f54f20fb022d60304a Mon Sep 17 00:00:00 2001 From: Konst Date: Thu, 13 Jun 2024 16:00:08 +0300 Subject: [PATCH 3/3] Reverted lint-go.yml changes --- .github/workflows/lint-go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint-go.yml b/.github/workflows/lint-go.yml index 3f1cb09..9432d45 100644 --- a/.github/workflows/lint-go.yml +++ b/.github/workflows/lint-go.yml @@ -26,6 +26,6 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v4 with: - version: v1.56.2 ./... + version: v1.56.2 args: --timeout=10m -v working-directory: ./cardinal \ No newline at end of file