Skip to content

Commit

Permalink
Fixed builds on incorrect tile types; Fixed Effect capacity and amoun…
Browse files Browse the repository at this point in the history
…t fields; (#13)
  • Loading branch information
k-karuna authored Jul 4, 2024
1 parent 56c4cc3 commit 2dd4b8c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
13 changes: 8 additions & 5 deletions cardinal/component/building.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ var BuildingConfigs = map[BuildingType]BuildingConstants{
{Type: Fish, Amount: constants.ShipyardResourcesFishAmount},
},
Effect: &Effect{
Type: Raft,
Amount: constants.ShipyardEffectRaftAmount,
Type: Raft,
Capacity: constants.ShipyardEffectRaftCapacity,
},
TileType: CoastlineTile,
},
Expand Down Expand Up @@ -149,9 +149,12 @@ func GetBuilding(buildingType BuildingType) (Building, error) {
}, nil
case Shipyard:
return Building{
Level: 1,
Type: buildingType,
Effect: config.Effect,
Level: 1,
Type: buildingType,
Effect: &Effect{
Type: config.Effect.Type,
Amount: 1,
},
}, nil
case Warehouse:
return Building{
Expand Down
2 changes: 1 addition & 1 deletion cardinal/component/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func GetAllEffectTypes() []EffectType {

type Effect struct {
Type EffectType `json:"type"`
Amount int `json:"amount"`
Amount int `json:"amount,omitempty"`
Capacity int `json:"capacity,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion cardinal/constants/buildings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
ShipyardResourcesWoodAmount = 20
ShipyardResourcesStoneAmount = 30
ShipyardResourcesFishAmount = 10
ShipyardEffectRaftAmount = 1
ShipyardEffectRaftCapacity = 1
)

const (
Expand Down
1 change: 1 addition & 0 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func MustInitWorld(w *cardinal.World) {
cardinal.RegisterComponent[component.Building](w),
cardinal.RegisterComponent[component.PlayerResources](w),
cardinal.RegisterComponent[component.TileMap](w),
cardinal.RegisterComponent[component.Effect](w),
)

// Register messages (user action)
Expand Down
2 changes: 1 addition & 1 deletion cardinal/msg/delete_building.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package msg

type DeleteBuildingMsg struct {
TileIndex int `json:"tileID"`
TileIndex int `json:"tileIndex"`
}

type DeleteBuildingResult struct {
Expand Down
19 changes: 10 additions & 9 deletions cardinal/system/create_building.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func CreateBuildingSystem(world cardinal.WorldContext) error {
return msg.CreateBuildingResult{Success: false}, fmt.Errorf("index of tiles out of range")
}

if tiles[request.Msg.TileIndex].Tile != comp.BuildingConfigs[building.Type].TileType {
return msg.CreateBuildingResult{Success: false},
fmt.Errorf("failed to create building, this building doesn't fit this tiletype")
}

if err := SubtractResourcesToBuild(world, building, request.Tx.PersonaTag); err != nil {
return msg.CreateBuildingResult{Success: false}, err
}
Expand All @@ -48,7 +53,7 @@ func CreateBuildingSystem(world cardinal.WorldContext) error {
return msg.CreateBuildingResult{Success: false}, fmt.Errorf("failed to create building: %w", err)
}

playerEntityID, _ := cardinal.Create(world,
buildingEntityID, _ := cardinal.Create(world,
player,
comp.Building{
Level: building.Level,
Expand All @@ -65,17 +70,13 @@ func CreateBuildingSystem(world cardinal.WorldContext) error {
Type: building.Farming.Type,
Speed: building.Farming.Speed,
}
_ = cardinal.AddComponentTo[comp.Farming](world, playerEntityID)
_ = cardinal.SetComponent(world, playerEntityID, farmingComponent)
_ = cardinal.AddComponentTo[comp.Farming](world, buildingEntityID)
_ = cardinal.SetComponent(world, buildingEntityID, farmingComponent)
}

if building.Effect != nil {
effectComponent := &comp.Effect{
Type: building.Effect.Type,
Amount: building.Effect.Amount,
}
_ = cardinal.AddComponentTo[comp.Effect](world, playerEntityID)
_ = cardinal.SetComponent(world, playerEntityID, effectComponent)
_ = cardinal.AddComponentTo[comp.Effect](world, buildingEntityID)
_ = cardinal.SetComponent(world, buildingEntityID, building.Effect)
}

return msg.CreateBuildingResult{Success: true}, nil
Expand Down

0 comments on commit 2dd4b8c

Please sign in to comment.