Skip to content

Commit

Permalink
Feat/buildings (#4)
Browse files Browse the repository at this point in the history
* Added building and resources entities

* Fixed incorrect building spawning

* Refactored building creation
  • Loading branch information
k-karuna authored Jun 21, 2024
1 parent 26a54ee commit ac81ed4
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 78 deletions.
116 changes: 116 additions & 0 deletions cardinal/component/building.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package component

type BuildingType string
type EffectsType string

const (
Main BuildingType = "Main"
Woodcutter BuildingType = "Woodcutter"
Quarry BuildingType = "Quarry"
FishermanHut BuildingType = "FishermanHut"
Shipyard BuildingType = "Shipyard"
Warehouse BuildingType = "Warehouse"
UnitLimitHouse BuildingType = "UnitLimitHouse"
)

const (
Ship EffectsType = "Ship"
)

type Building struct {
Level int `json:"level"`
Type BuildingType `json:"type"`
FarmingResource ResourceType `json:"farmingResource,omitempty"`
FarmingSpeed float32 `json:"farmingSpeed,omitempty"`
Effect EffectsType `json:"effect,omitempty"`
EffectAmount int `json:"effectAmount,omitempty"`
UnitLimit int `json:"unitLimit"`
StorageCapacity int `json:"storageCapacity"`
}

func (Building) Name() string {
return "Building"
}

const (
InitialBuildingLevel int = 1
MainBuildingUnitLimit int = 5
MainBuildingStorageCapacity int = 200
QuarryFarmingSpeed float32 = 10.0
QuarryUnitLimit int = 0
QuarryStorageCapacity int = 200
FishermanHutFarmingSpeed float32 = 8.0
FishermanHutUnitLimit int = 0
FishermanHutStorageCapacity int = 150
ShipyardUnitLimit int = 100
ShipyardStorageCapacity int = 300
)

func GetBuilding(buildingType BuildingType) Building {
switch buildingType {
case Main:
return Building{
Level: InitialBuildingLevel,
Type: Main,
UnitLimit: MainBuildingUnitLimit,
StorageCapacity: MainBuildingStorageCapacity,
}
case Quarry:
return Building{
Level: InitialBuildingLevel,
Type: Quarry,
FarmingResource: Stone,
FarmingSpeed: QuarryFarmingSpeed,
UnitLimit: QuarryUnitLimit,
StorageCapacity: QuarryStorageCapacity,
}
case FishermanHut:
return Building{
Level: InitialBuildingLevel,
Type: FishermanHut,
FarmingResource: Fish,
FarmingSpeed: FishermanHutFarmingSpeed,
UnitLimit: FishermanHutUnitLimit,
StorageCapacity: FishermanHutStorageCapacity,
}
case Shipyard:
return Building{
Level: InitialBuildingLevel,
Type: Shipyard,
UnitLimit: ShipyardUnitLimit,
StorageCapacity: ShipyardStorageCapacity,
}

// todo: refactor
case Woodcutter:
return Building{
Level: InitialBuildingLevel,
Type: Shipyard,
UnitLimit: ShipyardUnitLimit,
StorageCapacity: ShipyardStorageCapacity,
}
// todo: refactor
case UnitLimitHouse:
return Building{
Level: InitialBuildingLevel,
Type: Shipyard,
UnitLimit: ShipyardUnitLimit,
StorageCapacity: ShipyardStorageCapacity,
}
// todo: refactor
case Warehouse:
return Building{
Level: InitialBuildingLevel,
Type: Shipyard,
UnitLimit: ShipyardUnitLimit,
StorageCapacity: ShipyardStorageCapacity,
}
default:
return Building{
Level: 0,
Type: buildingType,
UnitLimit: 0,
StorageCapacity: 0,
}
}
}
20 changes: 20 additions & 0 deletions cardinal/component/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package component

type ResourceType string

const (
Wood ResourceType = "Wood"
Stone ResourceType = "Stone"
Fish ResourceType = "Fish"
Iron ResourceType = "Iron"
Cotton ResourceType = "Cotton"
)

type Resource struct {
Type ResourceType `json:"type"`
Amount int `json:"amount"`
}

func (Resource) Name() string {
return "Resource"
}
119 changes: 54 additions & 65 deletions cardinal/component/tilemap.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package component

type Tile struct {
ID int `json:"id"`
Tile TileType `json:"tile"`
Building BuildingType `json:"building"`
ID int `json:"id"`
Tile TileType `json:"tile"`
Building *Building `json:"building"`
}

type TileMap struct {
Expand All @@ -20,74 +20,63 @@ const MapWidth = 9
const MapHeight = 5

type TileType string
type BuildingType string

const (
Generic TileType = "Generic"
Wood TileType = "Wood"
Stone TileType = "Stone"
Water TileType = "Water"
Coastline TileType = "Coastline"
)

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"
GenericTile TileType = "Generic"
WoodTile TileType = "Wood"
StoneTile TileType = "Stone"
WaterTile TileType = "Water"
CoastlineTile TileType = "Coastline"
)

func GetDefaultTiles() *[]Tile {
var mainBuilding = GetBuilding(Main)
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},
{ID: Tile0, Tile: CoastlineTile, Building: nil},
{ID: Tile1, Tile: CoastlineTile, Building: nil},
{ID: Tile2, Tile: CoastlineTile, Building: nil},
{ID: Tile3, Tile: CoastlineTile, Building: nil},
{ID: Tile4, Tile: CoastlineTile, Building: nil},
{ID: Tile5, Tile: GenericTile, Building: nil},
{ID: Tile6, Tile: GenericTile, Building: nil},
{ID: Tile7, Tile: GenericTile, Building: nil},
{ID: Tile8, Tile: GenericTile, Building: nil},
{ID: Tile9, Tile: GenericTile, Building: nil},
{ID: Tile10, Tile: GenericTile, Building: nil},
{ID: Tile11, Tile: GenericTile, Building: nil},
{ID: Tile12, Tile: GenericTile, Building: nil},
{ID: Tile13, Tile: GenericTile, Building: nil},
{ID: Tile14, Tile: GenericTile, Building: nil},
{ID: Tile15, Tile: GenericTile, Building: nil},
{ID: Tile16, Tile: GenericTile, Building: nil},
{ID: Tile17, Tile: GenericTile, Building: nil},
{ID: Tile18, Tile: GenericTile, Building: nil},
{ID: Tile19, Tile: GenericTile, Building: nil},
{ID: Tile20, Tile: GenericTile, Building: nil},
{ID: Tile21, Tile: GenericTile, Building: nil},
{ID: Tile22, Tile: GenericTile, Building: &mainBuilding},
{ID: Tile23, Tile: GenericTile, Building: nil},
{ID: Tile24, Tile: StoneTile, Building: nil},
{ID: Tile25, Tile: GenericTile, Building: nil},
{ID: Tile26, Tile: GenericTile, Building: nil},
{ID: Tile27, Tile: GenericTile, Building: nil},
{ID: Tile28, Tile: GenericTile, Building: nil},
{ID: Tile29, Tile: GenericTile, Building: nil},
{ID: Tile30, Tile: GenericTile, Building: nil},
{ID: Tile31, Tile: GenericTile, Building: nil},
{ID: Tile32, Tile: GenericTile, Building: nil},
{ID: Tile33, Tile: GenericTile, Building: nil},
{ID: Tile34, Tile: GenericTile, Building: nil},
{ID: Tile35, Tile: GenericTile, Building: nil},
{ID: Tile36, Tile: GenericTile, Building: nil},
{ID: Tile37, Tile: GenericTile, Building: nil},
{ID: Tile38, Tile: GenericTile, Building: nil},
{ID: Tile39, Tile: GenericTile, Building: nil},
{ID: Tile40, Tile: GenericTile, Building: nil},
{ID: Tile41, Tile: GenericTile, Building: nil},
{ID: Tile42, Tile: GenericTile, Building: nil},
{ID: Tile43, Tile: GenericTile, Building: nil},
{ID: Tile44, Tile: WoodTile, Building: nil},
}

return &tiles
Expand Down
6 changes: 2 additions & 4 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func MustInitWorld(w *cardinal.World) {
Must(
cardinal.RegisterComponent[component.Player](w),
cardinal.RegisterComponent[component.Health](w),
cardinal.RegisterComponent[component.Resource](w),
cardinal.RegisterComponent[component.Building](w),
cardinal.RegisterComponent[component.TileMap](w),
)

Expand All @@ -57,10 +59,6 @@ func MustInitWorld(w *cardinal.World) {
system.RegenSystem,
system.PlayerSpawnerSystem,
))

Must(cardinal.RegisterInitSystems(w,
system.SpawnDefaultPlayersSystem,
))
}

func Must(err ...error) {
Expand Down
44 changes: 35 additions & 9 deletions cardinal/system/player_spawner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package system

import (
"fmt"

"pkg.world.dev/world-engine/cardinal/search/filter"
"pkg.world.dev/world-engine/cardinal/types"

Expand Down Expand Up @@ -42,28 +43,53 @@ func PlayerSpawnerSystem(world cardinal.WorldContext) error {
return true
})
if err != nil {
return msg.CreatePlayerResult{}, fmt.Errorf("error creating player: %w", err)
return msg.CreatePlayerResult{Success: false}, 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)
fmt.Errorf("error creating player, player with nickname %s already exists", create.Msg.Nickname)
}

id, err := cardinal.Create(world,
comp.Player{Nickname: create.Msg.Nickname},
comp.TileMap{Tiles: comp.GetDefaultTiles(), Width: comp.MapWidth, Height: comp.MapHeight},
var playerComponent = comp.Player{Nickname: create.Msg.Nickname}

mapID, err := cardinal.Create(world,
playerComponent,
comp.TileMap{
Tiles: comp.GetDefaultTiles(),
Width: comp.MapWidth,
Height: comp.MapHeight,
},
)

var building = comp.GetBuilding(comp.Main)
mainBuildingID, err := cardinal.Create(world,
playerComponent,
comp.Building{
Level: building.Level,
Type: building.Type,
FarmingResource: building.FarmingResource,
FarmingSpeed: building.FarmingSpeed,
Effect: building.Effect,
EffectAmount: building.EffectAmount,
UnitLimit: building.UnitLimit,
StorageCapacity: building.StorageCapacity,
},
comp.GetBuilding(comp.Main),
)

if err != nil {
return msg.CreatePlayerResult{}, fmt.Errorf("error creating player: %w", err)
return msg.CreatePlayerResult{Success: false}, fmt.Errorf("error creating player: %w", err)
}

err = world.EmitEvent(map[string]any{
"event": "new_player",
"id": id,
"event": "new_player",
"mapID": mapID,
"mainBuildingID": mainBuildingID,
})
world.CurrentTick()
if err != nil {
return msg.CreatePlayerResult{}, err
return msg.CreatePlayerResult{Success: false}, err
}
return msg.CreatePlayerResult{Success: true}, nil
})
Expand Down

0 comments on commit ac81ed4

Please sign in to comment.