Skip to content

Commit

Permalink
Added tilemap spawning (#1)
Browse files Browse the repository at this point in the history
* Added tilemap spawning

* Changed Tiles type to map

* Reverted lint-go.yml changes
  • Loading branch information
k-karuna authored Jun 13, 2024
1 parent 1725418 commit 9f1084b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
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

0 comments on commit 9f1084b

Please sign in to comment.