Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map state spawning and query #3

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 118 additions & 32 deletions cardinal/component/tilemap.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
)
1 change: 1 addition & 0 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
55 changes: 55 additions & 0 deletions cardinal/query/map_state.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 1 addition & 2 deletions cardinal/system/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package system

import (
"fmt"

"pkg.world.dev/world-engine/cardinal"

comp "oceanus-shard/component"
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cardinal/system/utils.go → cardinal/system/query_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading