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

Added tilemap spawning #1

Merged
merged 3 commits into from
Jun 13, 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
56 changes: 56 additions & 0 deletions cardinal/component/tilemap.go
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 32 additions & 1 deletion cardinal/system/player_spawner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions cardinal/system/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading