Skip to content

Commit

Permalink
Added tilemap spawning
Browse files Browse the repository at this point in the history
  • Loading branch information
k-karuna committed Jun 13, 2024
1 parent 1725418 commit 05631cc
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 3 deletions.
52 changes: 52 additions & 0 deletions cardinal/component/tilemap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package component

type TileType string
type BuildingType string

const (
Generic TileType = "Generic"

Check failure on line 7 in cardinal/component/tilemap.go

View workflow job for this annotation

GitHub Actions / Go

SA9004: only the first constant in this group has an explicit type (staticcheck)
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)

Check failure on line 32 in cardinal/component/tilemap.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 8, in <argument> detected (gomnd)

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}

Check failure on line 36 in cardinal/component/tilemap.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 2, in <assign> detected (gomnd)
tiles[3] = Tile{Tile: Water, Building: UnitLimitHouse, X: 3, Y: 0}

Check failure on line 37 in cardinal/component/tilemap.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 3, in <assign> detected (gomnd)
tiles[4] = Tile{Tile: Generic, Building: Shipyard, X: 4, Y: 0}

Check failure on line 38 in cardinal/component/tilemap.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 4, in <assign> detected (gomnd)
tiles[5] = Tile{Tile: Generic, Building: None, X: 5, Y: 0}

Check failure on line 39 in cardinal/component/tilemap.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 5, in <assign> detected (gomnd)
tiles[6] = Tile{Tile: Generic, Building: Woodcutter, X: 6, Y: 0}

Check failure on line 40 in cardinal/component/tilemap.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 6, in <assign> detected (gomnd)
tiles[7] = Tile{Tile: Generic, Building: Main, X: 7, Y: 0}

Check failure on line 41 in cardinal/component/tilemap.go

View workflow job for this annotation

GitHub Actions / Go

mnd: Magic number: 7, in <assign> detected (gomnd)

return &tiles
}

type TileMap struct {
Tiles *[]Tile `json:"tiles"`
}

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
2 changes: 2 additions & 0 deletions cardinal/system/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 21 in cardinal/system/attack.go

View workflow job for this annotation

GitHub Actions / Go

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
playerID, playerHealth, err := queryTargetPlayer(world, attack.Msg.TargetNickname)
if err != nil {
return msg.AttackPlayerMsgReply{}, fmt.Errorf("failed to inflict damage: %w", err)
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()},
)
if err != nil {
return msg.CreatePlayerResult{}, fmt.Errorf("error creating player: %w", err)
Expand Down
39 changes: 37 additions & 2 deletions cardinal/system/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {

Check failure on line 53 in cardinal/system/utils.go

View workflow job for this annotation

GitHub Actions / Go

line is 143 characters (lll)
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
}

0 comments on commit 05631cc

Please sign in to comment.