0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

React+Phaserのブラウザゲームに、既存セーブを壊さず経営システムを追加する

0
Posted at

公開済みのブラウザゲームへ「累計収益」と「店舗拡張」を追加しました。本記事では、React+TypeScriptで管理するUI状態とPhaserのゲーム状態を同期しながら、旧localStorageを壊さず拡張した実装を整理します。

店舗ランクをデータで定義する

条件や効果をJSXへ直接書かず、データとしてまとめます。

type ShopTier = {
  name: string;
  upgradeCost: number;
  requiredCompleted: number;
  requiredReputation: number;
  profitMultiplier: number;
  stockDiscount: number;
};

const shopTiers: ShopTier[] = [
  { name: "自宅ガレージ", upgradeCost: 0,
    requiredCompleted: 0, requiredReputation: 0,
    profitMultiplier: 1, stockDiscount: 0 },
  { name: "港町PC工房", upgradeCost: 60000,
    requiredCompleted: 3, requiredReputation: 20,
    profitMultiplier: 1.05, stockDiscount: .02 },
  { name: "PCショールーム", upgradeCost: 160000,
    requiredCompleted: 9, requiredReputation: 80,
    profitMultiplier: 1.12, stockDiscount: .05 },
];

UI、判定、利益計算が同じ定義を参照するため、表示と実際の効果がずれにくくなります。

旧セーブに存在しない項目を補う

セーブキーを変えると既存プレイヤーの進行が消えるので、同じキーを読み、追加項目だけデフォルト値で補いました。

function safeShopTier(value: number) {
  return Math.max(0, Math.min(shopTiers.length - 1, Math.floor(value || 0)));
}

function loadSave(): SaveData {
  const parsed = JSON.parse(localStorage.getItem(SAVE_KEY) ?? "null");

  return {
    ...parsed,
    shopTier: safeShopTier(Number(parsed?.shopTier) || 0),
    lifetimeRevenue: Math.max(0, Number(parsed?.lifetimeRevenue) || 0),
  };
}

値の欠落だけでなく、負数や将来の配列範囲外もここで正規化します。

利益計算を純粋関数へ分離する

画面表示と納品処理が別々の式を持つと、表示された利益と実際の加算額がずれます。そこで同じ関数を使います。

function shopDeliveryProfit(
  order: Order,
  build: WorkshopBuild,
  satisfaction: number,
  tier: number,
) {
  const base = Math.max(
    1000,
    order.budget - build.cost + satisfaction * 40,
  );
  const rate = shopTiers[safeShopTier(tier)].profitMultiplier;
  return Math.round(base * rate / 100) * 100;
}

店舗拡張は資金だけでなく、納品台数と評判も条件にしました。資金だけを条件にすると、単純な周回になりやすいためです。

ReactからPhaserへ状態を同期する

店舗ランクと累計収益を既存のプロフィール同期へ追加します。

useEffect(() => {
  window.pcWorkshopSetProfile?.({
    funds,
    reputation,
    completed,
    shopTier,
    lifetimeRevenue,
    // その他の状態
  });
}, [funds, reputation, completed, shopTier, lifetimeRevenue]);

Phaser側のrender_game_to_text()にも同じプロフィールを含めると、E2Eテストからゲーム内部状態を確認できます。

Playwrightで確認した項目

  • 新項目がない旧セーブから起動できる
  • 3回の店舗拡張が順番に成功する
  • 拡張費用が正しく差し引かれる
  • 最終店舗ランクがlocalStorageへ保存される
  • 1440pxと390pxで横スクロールが発生しない
  • 従来の13組み立て操作と納品が壊れていない
  • コンソールエラーがない

公開済みゲームへ機能を追加するときは、追加機能のテストだけでなく、旧セーブと既存操作の回帰テストが重要です。

実際のゲーム:
https://shop.nagasu-denki.jp/cat-pc-workshop/

#TypeScript #React #Phaser #Playwright #ゲーム制作

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?