Skip to content

Commit

Permalink
Add new level cost. Add customer spawn requirement. Add cross level p…
Browse files Browse the repository at this point in the history
…rogress saving.
  • Loading branch information
Golen87 committed Aug 19, 2024
1 parent d7675a0 commit 785deae
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 41 deletions.
14 changes: 9 additions & 5 deletions src/components/Employee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ export class Employee extends Button {
}

update(time: number, delta: number) {
const factor = this.doingCuteThing
? 0.1
: this.hasBeenPurchased
? 0.02
: 0;
const factor = this.doingCuteThing ? 0.1 : this.hasBeenPurchased ? 0.02 : 0;
const squish = 1.0 + factor * Math.sin((6 * time) / 1000);
this.spriteCont.setScale(1.0, squish - 0.2 * this.holdSmooth);
}
Expand Down Expand Up @@ -114,6 +110,14 @@ export class Employee extends Button {
}
}

// Only used when loading levels
forceUpgrade(id: EmployeeId) {
this.hasBeenPurchased = true;
this.setAlpha(1.0);
this.employeeId = id;
this.sprite.setTexture(this.spriteKey);
}

/* Getters */

get employeeType(): EmployeeType {
Expand Down
2 changes: 1 addition & 1 deletion src/components/EmployeeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const EmployeeData: { [key in EmployeeId]: EmployeeInterface } = {
spriteKey: "worker",
walkSpeed: 1,
workSpeed: 1,
cost: 1000,
cost: 300,
upgradeTo: EmployeeId.RaccoonTier2,
},
[EmployeeId.RaccoonTier2]: {
Expand Down
3 changes: 3 additions & 0 deletions src/components/Levels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Level {
height: number;
cellSize: number;
grid: number[][];
upgradeCost?: number;
}

export enum BlockType {
Expand Down Expand Up @@ -42,6 +43,7 @@ export const LevelData: Level[] = [
[X, _, 5, 5, _, 6, _, _],
[X, X, X, X, X, X, X, X],
],
upgradeCost: 1000,
},
{
id: LevelId.Level2,
Expand All @@ -58,6 +60,7 @@ export const LevelData: Level[] = [
[X, 9, 9, 9, _, _, 6, _, _],
[X, X, X, X, X, X, X, X, X],
],
upgradeCost: 2000,
},
{
id: LevelId.Level3,
Expand Down
10 changes: 10 additions & 0 deletions src/components/Station.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ export class Station extends Button {
}
}

// Only used when loading levels
forceUpgrade(id: StationId) {
this.hasBeenPurchased = true;
this.setAlpha(1.0);
this.stationId = id;
this.sprite.setTexture(this.spriteKey);
this.spriteCont.y = this.spriteOffset;
this.sprite.setScale(this.spriteSize / this.sprite.width);
}

applyItem(id: number, sp: string) {
this.appliedItems.push(id);
let st = new Phaser.GameObjects.Sprite(
Expand Down
24 changes: 20 additions & 4 deletions src/components/UI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GameScene } from "@/scenes/GameScene";
import { Timer } from "./Timer";
import { TextButton } from "./TextButton";
import { Level } from "./Levels";

export class UI extends Phaser.GameObjects.Container {
public scene: GameScene;
Expand Down Expand Up @@ -72,21 +73,23 @@ export class UI extends Phaser.GameObjects.Container {
this.moneyText.setOrigin(0.5);
this.panel.add(this.moneyText);

this.nextButton = new TextButton(scene, 0, 300, 300, 80, "Start day");
this.nextButton = new TextButton(scene, 0, 600, 300, 80, "Start day");
this.panel.add(this.nextButton);
this.nextButton.on("click", () => {
this.emit("nextDay");
});

this.newLocationButton = new TextButton(scene, 0, 440, 240, 80, "Move");
this.newLocationButton = new TextButton(scene, 0, 400, 300, 200, "...");
this.panel.add(this.newLocationButton);
this.newLocationButton.on("click", () => {
this.emit("nextLevel");
});
this.newLocationButton.setVisible(false);
}

update(time: number, delta: number) {}
update(time: number, delta: number) {
this.nextButton.update(time, delta);
this.newLocationButton.update(time, delta);
}

setDay(day: number) {
this.dayText.setText(`Day ${day}`);
Expand All @@ -96,8 +99,21 @@ export class UI extends Phaser.GameObjects.Container {
this.dayProgressTimer.redraw(time);
}

setLevel(level: Level) {
this.newLocationButton.setData("cost", level.upgradeCost);
this.newLocationButton.setVisible(level.upgradeCost !== undefined);
this.newLocationButton.setText(
`Upgrade\n shop\n $${level.upgradeCost}`
);
}

setMoney(money: number) {
this.moneyText.setText(`Money: $${money}`);

const upgradeCost = this.newLocationButton.getData("cost");
const canUpgrade = money >= upgradeCost;
this.newLocationButton.setAlpha(canUpgrade ? 1 : 0.5);
this.newLocationButton.enabled = canUpgrade;
}

setShoppingMode(isShopping: boolean) {
Expand Down
84 changes: 53 additions & 31 deletions src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { Customer } from "@/components/Customer";
import { CustomerId } from "@/components/CustomerData";
import { Station } from "@/components/Station";
import { UI } from "@/components/UI";
import { StationId, StationType } from "@/components/StationData";
import { StationData, StationId, StationType } from "@/components/StationData";
import { Inventory } from "@/components/Inventory";
import { SimpleButton } from "@/components/elements/SimpleButton";
import { ToggleButton } from "@/components/elements/ToggleButton";
import { ItemButton } from "@/components/ItemButton";
import { ItemHandler } from "@/components/ItemHandler";
import { UpgradeOverlay } from "@/components/UpgradeOverlay";
import { SummaryOverlay } from "@/components/SummaryOverlay";
import { EmployeeId } from "@/components/EmployeeData";
import { EmployeeData, EmployeeId } from "@/components/EmployeeData";
import { BlockType, LevelData, LevelId } from "@/components/Levels";
import { Effect } from "@/components/Effect";
import { TextEffect } from "@/components/TextEffect";
Expand Down Expand Up @@ -59,7 +59,7 @@ export class GameScene extends BaseScene {
public timeOfDay: number = 0;
public customerSpawnTimer: Phaser.Time.TimerEvent;
public customerSpawnPool: CustomerId[] = [];
public money: number = 0;
public money: number = 500;
public dailyStats: {
money: number;
happyCustomers: number;
Expand Down Expand Up @@ -89,6 +89,7 @@ export class GameScene extends BaseScene {
stations: [
StationId.WaitingSeatTier1,
StationId.HornAndNailsTier1,
StationId.ScalePolishTier1,
StationId.CashRegister,
],
employees: [EmployeeId.RaccoonTier1],
Expand All @@ -107,6 +108,18 @@ export class GameScene extends BaseScene {

this.ui = new UI(this);
this.ui.setDepth(1000);
this.ui.on("nextDay", () => {
this.startDay();
});
this.ui.on("nextLevel", () => {
const upgradeCost = LevelData[this.level].upgradeCost ?? 0;
if (this.money >= upgradeCost) {
this.money -= upgradeCost;
this.ui.setMoney(this.money);
this.intermission.fadeToIntermission(Mode.NextLevelCutscene);
}
});

this.iHandler = new ItemHandler(this);

this.intermission = new Intermission(this);
Expand Down Expand Up @@ -152,31 +165,21 @@ export class GameScene extends BaseScene {
"blankspr"
);

//UI
this.ui.setMoney(this.money);
this.ui.setDay(this.day);
this.ui.on("nextDay", () => {
this.startDay();
});
this.ui.on("nextLevel", () => {
this.intermission.fadeToIntermission(Mode.NextLevelCutscene);
});

this.upgradeOverlay = new UpgradeOverlay(this);
this.upgradeOverlay.setDepth(1010);
this.upgradeOverlay.on("upgradeStation", (station: Station) => {
this.money -= station.upgradeCost;
this.ui.setMoney(this.money);
station.upgrade();
this.upgradeOverlay.close();
// this.upgradeOverlay.selectStation(station);
this.upgradeOverlay.selectStation(station);
this.updateSavedPurchases();
});
this.upgradeOverlay.on("upgradeEmployee", (employee: Employee) => {
this.money -= employee.upgradeCost;
this.ui.setMoney(this.money);
employee.upgrade();
// this.upgradeOverlay.selectEmployee(employee);
this.upgradeOverlay.close();
this.upgradeOverlay.selectEmployee(employee);
this.updateSavedPurchases();
});
this.upgradeOverlay.on("close", () => {
this.sortDepth();
Expand Down Expand Up @@ -312,20 +315,30 @@ export class GameScene extends BaseScene {

// Load saved purchases
this.savedPurchases.stations.forEach((id) => {
const station = this.stations.find((s) => s.stationId === id);
const station = this.stations.find(
(s) => !s.hasBeenPurchased && s.stationType === StationData[id].type
);
if (station) {
station.upgrade();
station.forceUpgrade(id);
}
});
this.savedPurchases.employees.forEach((id) => {
const employee = this.employees.find((e) => e.employeeId === id);
const employee = this.employees.find(
(e) => !e.hasBeenPurchased && e.employeeType === EmployeeData[id].type
);
if (employee) {
employee.upgrade();
employee.forceUpgrade(id);
}
});

// Generate navmesh
this.navmesh = GenerateNavMesh(this.board, LevelData[id])
this.navmesh = GenerateNavMesh(this.board, LevelData[id]);

this.ui.setLevel(level);
this.ui.setMoney(this.money);
this.ui.setDay(this.day);

this.setState(GameState.Shopping);
}

// Start a new day
Expand Down Expand Up @@ -383,7 +396,7 @@ export class GameScene extends BaseScene {
// Attempt to spawn customer and reset timer
attemptSpawnCustomer() {
// Delay to next customer spawn
let delay = 1000;
let delay = 2000;

// Randomly select customer type
const id = Phaser.Math.RND.pick(this.customerSpawnPool);
Expand Down Expand Up @@ -413,16 +426,25 @@ export class GameScene extends BaseScene {
updateSpawnPool() {
this.customerSpawnPool = [];

const anyTier2Stations = this.stations.some(
const tier2StationCount = this.stations.filter(
(s) => s.stationTier >= 2 && s.hasBeenPurchased
);
const anyTier3Stations = this.stations.some(
(s) => s.stationTier >= 3 && s.hasBeenPurchased
);
).length;
const tier3StationCount = this.stations.filter(
(s) => s.stationTier >= 2 && s.hasBeenPurchased
).length;

this.customerSpawnPool.push(CustomerId.Small);
if (anyTier2Stations) this.customerSpawnPool.push(CustomerId.Medium);
if (anyTier3Stations) this.customerSpawnPool.push(CustomerId.Large);
if (tier2StationCount >= 2) this.customerSpawnPool.push(CustomerId.Medium);
if (tier3StationCount >= 2) this.customerSpawnPool.push(CustomerId.Large);
}

updateSavedPurchases() {
this.savedPurchases.stations = this.stations
.filter((s) => s.hasBeenPurchased)
.map((s) => s.stationId);
this.savedPurchases.employees = this.employees
.filter((e) => e.hasBeenPurchased)
.map((e) => e.employeeId);
}

// Add new station
Expand Down

0 comments on commit 785deae

Please sign in to comment.