房屋系统是农场模块的核心组件之一,代表玩家在农场中的主要建筑。房屋等级影响土地产出、特殊土地数量和其他游戏机制,是玩家进度的重要指标。
房屋共有12个等级,从1级(初始)到12级(最高)。
| 等级 | 产出加成 | 特殊土地上限 | 降级天数 | 升级材料 |
|---|---|---|---|---|
| 1 | 0% | 0 | 不降级 | 木材x20 |
| 2 | 5% | 0 | 7天 | 木材x30, 石材x10 |
| 3 | 10% | 0 | 7天 | 木材x40, 石材x20 |
| 4 | 15% | 0 | 7天 | 木材x50, 石材x30, 钢材x5 |
| 5 | 20% | 0 | 7天 | 木材x60, 石材x40, 钢材x10 |
| 6 | 25% | 0 | 7天 | 木材x70, 石材x50, 钢材x15 |
| 7 | 30% | 2 | 7天 | 木材x80, 石材x60, 钢材x20, 钻石x5 |
| 8 | 35% | 4 | 7天 | 木材x90, 石材x70, 钢材x25, 钻石x10 |
| 9 | 40% | 6 | 7天 | 木材x100, 石材x80, 钢材x30, 钻石x15 |
| 10 | 45% | 8 | 7天 | 木材x120, 石材x90, 钢材x35, 钻石x20 |
| 11 | 50% | 10 | 7天 | 木材x140, 石材x100, 钢材x40, 钻石x25 |
| 12 | 60% | 12 | 不降级 | 木材x160, 石材x120, 钢材x50, 钻石x30 |
房屋等级提供全局产出加成,影响所有土地的作物产量:
最终产量 = 基础产量 × (1 + 土地加成) × (1 + 房屋加成) × (1 - 灾害减产)
房屋等级决定玩家可以拥有的特殊土地(金/蓝/紫)数量:
为了鼓励玩家持续活跃,房屋设有降级机制:
| 字段名 | 类型 | 说明 |
|---|---|---|
| id | bigint | 主键ID |
| user_id | bigint | 用户ID |
| house_level | tinyint | 房屋等级 |
| last_upgrade_time | timestamp | 最后升级时间 |
| created_at | timestamp | 创建时间 |
| updated_at | timestamp | 更新时间 |
| 字段名 | 类型 | 说明 |
|---|---|---|
| id | bigint | 主键ID |
| level | tinyint | 等级 |
| output_bonus | decimal | 产出加成 |
| special_land_limit | tinyint | 特殊土地上限 |
| upgrade_materials | json | 升级所需材料 |
| downgrade_days | int | 降级天数 |
| created_at | timestamp | 创建时间 |
| updated_at | timestamp | 更新时间 |
{
"materials": [
{"item_id": 1001, "amount": 20}, // 木材x20
{"item_id": 1002, "amount": 10} // 石材x10
]
}
iduser_id(确保每个用户只有一条农场记录)house_level(加速查询特定等级的用户)last_upgrade_time(用于降级检查)/**
* 为新用户创建农场记录
*
* @param int $userId 用户ID
* @return FarmUser 创建的农场对象
*/
public function createFarmUser(int $userId): FarmUser
/**
* 升级用户房屋
*
* @param int $userId 用户ID
* @return bool 升级是否成功
* @throws InsufficientMaterialsException 材料不足异常
* @throws MaxLevelReachedException 已达最高等级异常
*/
public function upgradeHouse(int $userId): bool
/**
* 获取用户房屋信息
*
* @param int $userId 用户ID
* @return array 房屋信息
*/
public function getHouseInfo(int $userId): array
/**
* 检查并处理房屋降级
* 此方法应由定时任务调用
*
* @return int 处理的降级数量
*/
public function processHouseDowngrade(): int
/**
* 计算房屋的产出加成
*
* @param int $houseLevel 房屋等级
* @return float 产出加成系数
*/
public function calculateOutputBonus(int $houseLevel): float
/**
* 计算特殊土地上限
*
* @param int $houseLevel 房屋等级
* @return int 特殊土地上限
*/
public function getSpecialLandLimit(int $houseLevel): int
/**
* 验证用户是否拥有足够的升级材料
*
* @param int $userId 用户ID
* @param int $targetLevel 目标等级
* @return bool 是否拥有足够材料
*/
public function hasEnoughUpgradeMaterials(int $userId, int $targetLevel): bool
/**
* 检查用户房屋是否需要降级
*
* @param FarmUser $farmUser 用户农场对象
* @return bool 是否需要降级
*/
public function shouldDowngrade(FarmUser $farmUser): bool
前端应展示房屋的以下信息:
前端应提供以下房屋操作:
房屋升级时需要消耗对应的材料物品:
/**
* 升级用户房屋
*/
public function upgradeHouse(int $userId): bool
{
return DB::transaction(function () use ($userId) {
// 获取用户农场信息
$farmUser = $this->farmUserRepository->findByUserId($userId);
if (!$farmUser) {
throw new FarmUserNotFoundException("用户农场信息不存在");
}
$currentLevel = $farmUser->house_level;
$nextLevel = $currentLevel + 1;
// 验证是否已达最高等级
if ($currentLevel >= 12) {
throw new MaxLevelReachedException("房屋已达最高等级");
}
// 获取升级所需材料
$houseConfig = $this->houseConfigRepository->findByLevel($nextLevel);
$materials = json_decode($houseConfig->upgrade_materials, true)['materials'];
// 检查用户是否拥有足够的材料
foreach ($materials as $material) {
$hasItem = $this->itemService->checkUserItem(
$userId,
$material['item_id'],
$material['amount']
);
if (!$hasItem) {
throw new InsufficientMaterialsException("材料不足");
}
}
// 消耗材料
foreach ($materials as $material) {
$this->itemService->consumeUserItem(
$userId,
$material['item_id'],
$material['amount'],
"升级房屋"
);
}
// 更新房屋等级和最后升级时间
$this->farmUserRepository->update($farmUser->id, [
'house_level' => $nextLevel,
'last_upgrade_time' => now()
]);
// 触发房屋升级事件
event(new HouseUpgradedEvent($userId, $currentLevel, $nextLevel));
return true;
});
}
/**
* 获取房屋升级所需材料
*/
public function getHouseUpgradeMaterials(int $userId): array
{
// 获取用户当前房屋等级
$farmUser = $this->farmUserRepository->findByUserId($userId);
if (!$farmUser) {
throw new FarmUserNotFoundException("用户农场信息不存在");
}
$currentLevel = $farmUser->house_level;
$nextLevel = $currentLevel + 1;
// 验证是否已达最高等级
if ($currentLevel >= 12) {
return [
'can_upgrade' => false,
'reason' => '房屋已达最高等级',
'materials' => []
];
}
// 获取下一级房屋配置
$houseConfig = $this->houseConfigRepository->findByLevel($nextLevel);
$materials = json_decode($houseConfig->upgrade_materials, true)['materials'];
// 检查用户拥有的材料数量
$materialStatus = [];
foreach ($materials as $material) {
$itemInfo = $this->itemService->getItemInfo($material['item_id']);
$userAmount = $this->itemService->getUserItemAmount($userId, $material['item_id']);
$materialStatus[] = [
'item_id' => $material['item_id'],
'name' => $itemInfo['name'],
'icon' => $itemInfo['icon'],
'required' => $material['amount'],
'owned' => $userAmount,
'sufficient' => $userAmount >= $material['amount']
];
}
return [
'can_upgrade' => true,
'current_level' => $currentLevel,
'next_level' => $nextLevel,
'materials' => $materialStatus
];
}
未来可以添加房屋外观系统,允许玩家自定义房屋样式,提供视觉效果和可能的额外属性加成。
随着游戏发展,可以为房屋添加更多功能:
可以为不同等级的房屋添加特殊效果: