diff --git a/assets/Data/GameSettings.json b/assets/Data/GameSettings.json index fee30f7..bb8cb64 100644 --- a/assets/Data/GameSettings.json +++ b/assets/Data/GameSettings.json @@ -1,18 +1,24 @@ { "playerSettings": { - "defaultHP": 0, + "defaultHP": 50, "requiredXP": [ - 0, - 0, - 0, - 0 + 5, + 5, + 5, + 10, + 10, + 10, + 20, + 20, + 20, + 20 ], - "collisionDelay": 0, + "collisionDelay": 0.5, "testSettings": { "test": 0 } }, "weaponSettings": { - "strikeDelay": 0 + "strikeDelay": 2 } } \ No newline at end of file diff --git a/assets/Scripts/Game/Data/GameSettings.ts b/assets/Scripts/Game/Data/GameSettings.ts index d542616..4da1209 100644 --- a/assets/Scripts/Game/Data/GameSettings.ts +++ b/assets/Scripts/Game/Data/GameSettings.ts @@ -7,13 +7,8 @@ export class PlayerSettings { public defaultHP = 0; public requiredXP: number[] = []; public collisionDelay = 0; - public testSettings = new TestSettings(); } export class WeaponSettings { public strikeDelay = 0; } - -export class TestSettings { - public test = 0; -} diff --git a/assets/Scripts/Game/GameBootstrapper.ts b/assets/Scripts/Game/GameBootstrapper.ts index d91cf35..12b0ded 100644 --- a/assets/Scripts/Game/GameBootstrapper.ts +++ b/assets/Scripts/Game/GameBootstrapper.ts @@ -7,6 +7,8 @@ import { EnemyManager } from "./Enemy/EnemyManager"; import { KeyboardInput } from "./Input/KeyboardInput"; import { MultiInput } from "./Input/MultiInput"; import { VirtualJoystic } from "./Input/VirtualJoystic"; +import { GameModalLauncher } from "./ModalWIndows/GameModalLauncher"; +import { Pauser } from "./Pauser"; import { Player } from "./Player/Player"; import { GameUI } from "./UI/GameUI"; import { Upgrader } from "./Upgrades/Upgrader"; @@ -20,48 +22,38 @@ export class GameBootstrapper extends Component { @property(Player) private player: Player; @property(Weapon) private weapon: Weapon; @property(EnemyManager) private enemyManager: EnemyManager; - @property(CCFloat) private strikeDelay = 0; - @property(CCFloat) private collisionDelay = 0; @property(Camera) private camera: Camera; @property(GameUI) private gameUI: GameUI; - @property(Number) private requiredLevelXps: number[] = []; @property(ModalWindowManager) private modalWindowManager: ModalWindowManager; @property(JsonAsset) private settingsAsset: JsonAsset; - @property(GameSettings) private settingsPref: GameSettings = new GameSettings(); private playerCollisionSystem: PlayerCollisionSystem; - private upgrader: Upgrader; - private isPaused = false; + private gamePauser: Pauser = new Pauser(); public start(): void { const gameSettings: GameSettings = this.settingsAsset.json; - console.log("Collision delay: " + gameSettings.playerSettings.collisionDelay); - console.log(JSON.stringify(new GameSettings())); this.virtualJoystic.init(); - this.weapon.init(this.strikeDelay); + this.weapon.init(gameSettings.weaponSettings.strikeDelay); const wasd = new KeyboardInput(KeyCode.KEY_W, KeyCode.KEY_S, KeyCode.KEY_A, KeyCode.KEY_D); const arrowKeys = new KeyboardInput(KeyCode.ARROW_UP, KeyCode.ARROW_DOWN, KeyCode.ARROW_LEFT, KeyCode.ARROW_RIGHT); const dualInput: MultiInput = new MultiInput([this.virtualJoystic, wasd, arrowKeys]); - this.player.init(dualInput, this.weapon, 50, this.requiredLevelXps); + this.player.init(dualInput, this.weapon, gameSettings.playerSettings.defaultHP, gameSettings.playerSettings.requiredXP); - this.playerCollisionSystem = new PlayerCollisionSystem(this.player, this.collisionDelay); + this.playerCollisionSystem = new PlayerCollisionSystem(this.player, gameSettings.playerSettings.collisionDelay); new WeaponCollisionSystem(this.weapon); - this.upgrader = new Upgrader(this.player); + const upgrader = new Upgrader(this.player); + new GameModalLauncher(this.modalWindowManager, this.player, this.gamePauser, upgrader); this.enemyManager.init(this.player.node); this.gameUI.init(this.player); - - this.showModal(); - - //console.log("DEfault hp: " + gameSettings.playerSettings.defaultHP); } public update(deltaTime: number): void { - if (this.isPaused) return; + if (this.gamePauser.IsPaused) return; this.player.gameTick(deltaTime); this.playerCollisionSystem.gameTick(deltaTime); @@ -69,11 +61,4 @@ export class GameBootstrapper extends Component { this.camera.node.worldPosition = this.player.node.worldPosition; } - - private async showModal(): Promise { - this.isPaused = true; - const result: UpgradeType = await this.modalWindowManager.showModal("LevelUpModalWindow", "test params"); - this.isPaused = false; - console.log("Result: " + result); - } } diff --git a/assets/Scripts/Game/ModalWIndows.meta b/assets/Scripts/Game/ModalWIndows.meta new file mode 100644 index 0000000..b1141d4 --- /dev/null +++ b/assets/Scripts/Game/ModalWIndows.meta @@ -0,0 +1,12 @@ +{ + "ver": "1.1.0", + "importer": "directory", + "imported": true, + "uuid": "00b7fdba-e250-4a79-be07-e87bdfc07929", + "files": [], + "subMetas": {}, + "userData": { + "compressionType": {}, + "isRemoteBundle": {} + } +} diff --git a/assets/Scripts/Game/ModalWIndows/GameModalLauncher.ts b/assets/Scripts/Game/ModalWIndows/GameModalLauncher.ts new file mode 100644 index 0000000..de51b54 --- /dev/null +++ b/assets/Scripts/Game/ModalWIndows/GameModalLauncher.ts @@ -0,0 +1,27 @@ +import { ModalWindowManager } from "../../Services/ModalWindowSystem/ModalWindowManager"; +import { Pauser } from "../Pauser"; +import { Player } from "../Player/Player"; +import { Upgrader } from "../Upgrades/Upgrader"; +import { UpgradeType } from "../Upgrades/UpgradeType"; +import { GameModalWindowTypes } from "./GameModalWindowTypes"; + +export class GameModalLauncher { + public constructor( + private modalWindowManager: ModalWindowManager, + private player: Player, + private gamePauser: Pauser, + private upgrader: Upgrader + ) { + this.player.Level.LevelUpEvent.on(this.showLevelUpModal, this); + } + + private async showLevelUpModal(): Promise { + this.gamePauser.pause(); + const skillToUpgrade: UpgradeType = await this.modalWindowManager.showModal( + GameModalWindowTypes.LevelUpModal, + Array.from(this.upgrader.getAvailableUpgrades()) + ); + this.gamePauser.resume(); + this.upgrader.upgradeSkill(skillToUpgrade); + } +} diff --git a/tests/virt.test.ts.meta b/assets/Scripts/Game/ModalWIndows/GameModalLauncher.ts.meta similarity index 70% rename from tests/virt.test.ts.meta rename to assets/Scripts/Game/ModalWIndows/GameModalLauncher.ts.meta index 6aa230b..714b12d 100644 --- a/tests/virt.test.ts.meta +++ b/assets/Scripts/Game/ModalWIndows/GameModalLauncher.ts.meta @@ -2,7 +2,7 @@ "ver": "4.0.23", "importer": "typescript", "imported": true, - "uuid": "62fcbd59-7ce1-4582-91c1-e502763b791b", + "uuid": "19da79a2-30b2-4d73-83fb-b3ef17bde4d7", "files": [], "subMetas": {}, "userData": {} diff --git a/assets/Scripts/Game/ModalWIndows/GameModalWindowTypes.ts b/assets/Scripts/Game/ModalWIndows/GameModalWindowTypes.ts new file mode 100644 index 0000000..ddb3eed --- /dev/null +++ b/assets/Scripts/Game/ModalWIndows/GameModalWindowTypes.ts @@ -0,0 +1,3 @@ +export enum GameModalWindowTypes { + LevelUpModal = "LevelUpModalWindow" +} diff --git a/assets/Scripts/Game/ModalWIndows/GameModalWindowTypes.ts.meta b/assets/Scripts/Game/ModalWIndows/GameModalWindowTypes.ts.meta new file mode 100644 index 0000000..bba4b45 --- /dev/null +++ b/assets/Scripts/Game/ModalWIndows/GameModalWindowTypes.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "72fb4349-7b73-41e1-ad7a-4d50abcfc1fa", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/Game/Pauser.ts b/assets/Scripts/Game/Pauser.ts new file mode 100644 index 0000000..01b9c69 --- /dev/null +++ b/assets/Scripts/Game/Pauser.ts @@ -0,0 +1,15 @@ +export class Pauser { + private isPaused = false; + + public get IsPaused(): boolean { + return this.isPaused; + } + + public pause(): void { + this.isPaused = true; + } + + public resume(): void { + this.isPaused = false; + } +} diff --git a/assets/Scripts/Game/Pauser.ts.meta b/assets/Scripts/Game/Pauser.ts.meta new file mode 100644 index 0000000..f4176c4 --- /dev/null +++ b/assets/Scripts/Game/Pauser.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "d20eb678-aaf9-47e6-bd12-43fa37d3c9ba", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/Game/UI/LevelUpWindow/LevelUpModalWindow.ts b/assets/Scripts/Game/UI/LevelUpWindow/LevelUpModalWindow.ts index 67bad46..b4d1e78 100644 --- a/assets/Scripts/Game/UI/LevelUpWindow/LevelUpModalWindow.ts +++ b/assets/Scripts/Game/UI/LevelUpWindow/LevelUpModalWindow.ts @@ -1,5 +1,6 @@ import { instantiate, Node, Prefab, Vec3, _decorator } from "cc"; import { ModalWindow } from "../../../Services/ModalWindowSystem/ModalWindow"; +import { shuffle } from "../../../Services/Utils/ArrayUtils"; import { delay } from "../../../Services/Utils/AsyncUtils"; import { UpgradeType } from "../../Upgrades/UpgradeType"; import { LevelUpSkill } from "./LevelUpSkill"; @@ -7,19 +8,25 @@ import { LevelUpSkill } from "./LevelUpSkill"; const { ccclass, property } = _decorator; @ccclass("LevelUpModalWindow") -export class LevelUpModalWindow extends ModalWindow { +export class LevelUpModalWindow extends ModalWindow { @property(Prefab) private skillPrefab: Prefab; @property(Node) private skillParent: Node; - protected async setup(params: string): Promise { + private maxUpgradesToPick = 3; + + protected async setup(availableUpgrades: UpgradeType[]): Promise { + const shuffledAvailableUpgrades = shuffle(availableUpgrades); + if (this.maxUpgradesToPick < shuffledAvailableUpgrades.length) { + shuffledAvailableUpgrades.length = this.maxUpgradesToPick; + } const xPositions: number[] = [-180, 0, 180]; await delay(300); - for (let i = 0; i < 3; i++) { + for (let i = 0; i < shuffledAvailableUpgrades.length; i++) { await delay(500); const skill: LevelUpSkill = instantiate(this.skillPrefab).getComponent(LevelUpSkill); skill.node.setParent(this.skillParent); skill.node.setPosition(new Vec3(xPositions[i])); - skill.init(params); + skill.init(shuffledAvailableUpgrades[i]); skill.ChooseSkillEvent.on(this.chooseSkill, this); } } diff --git a/assets/Scripts/Game/UI/LevelUpWindow/LevelUpSkill.ts b/assets/Scripts/Game/UI/LevelUpWindow/LevelUpSkill.ts index 0b8a2cd..f341e0a 100644 --- a/assets/Scripts/Game/UI/LevelUpWindow/LevelUpSkill.ts +++ b/assets/Scripts/Game/UI/LevelUpWindow/LevelUpSkill.ts @@ -1,14 +1,15 @@ import { Component, Label, NodeEventType, _decorator } from "cc"; import { ISignal } from "../../../Services/EventSystem/ISignal"; import { Signal } from "../../../Services/EventSystem/Signal"; +import { UpgradeType } from "../../Upgrades/UpgradeType"; const { ccclass, property } = _decorator; @ccclass("LevelUpSkill") export class LevelUpSkill extends Component { @property(Label) private skillTitle: Label; private chooseSkillEvent: Signal = new Signal(); - public init(skillTitle: string): void { - this.skillTitle.string = skillTitle; + public init(skillType: UpgradeType): void { + this.skillTitle.string = `Skill ${skillType}`; this.node.on(NodeEventType.MOUSE_DOWN, this.chooseSkill, this); } diff --git a/assets/Scripts/Game/Upgrades/Upgrader.ts b/assets/Scripts/Game/Upgrades/Upgrader.ts index e8affbc..9b24e3f 100644 --- a/assets/Scripts/Game/Upgrades/Upgrader.ts +++ b/assets/Scripts/Game/Upgrades/Upgrader.ts @@ -16,9 +16,22 @@ export class Upgrader { public upgradeSkill(type: UpgradeType): void { if (!this.typeToAction.has(type)) throw new Error("Upgrade does not have " + type); + if (this.isMaxLevel(type)) throw new Error("Upgrade is already at max level" + type); + this.typeToAction.get(type)(); } + public getAvailableUpgrades(): Set { + const availableUpgrades: Set = new Set(); + for (const key of this.typeToAction.keys()) { + if (!this.isMaxLevel(key)) { + availableUpgrades.add(key); + } + } + + return availableUpgrades; + } + private setTypeMaps(upgradeType: UpgradeType, action: () => void, maxLevel: number): void { this.typeToAction.set(upgradeType, action); this.typeToLevel.set(upgradeType, 0); @@ -32,4 +45,8 @@ export class Upgrader { private upgradeWeaponDamage(): void { this.player.Weapon.upgradeWeaponDamage(); } + + private isMaxLevel(type: UpgradeType): boolean { + return this.typeToMaxLevel.get(type) <= this.typeToLevel.get(type); + } } diff --git a/assets/Scripts/Services/Utils/ArrayUtils.ts b/assets/Scripts/Services/Utils/ArrayUtils.ts new file mode 100644 index 0000000..2d49372 --- /dev/null +++ b/assets/Scripts/Services/Utils/ArrayUtils.ts @@ -0,0 +1,10 @@ +export function shuffle(array: T[]): T[] { + const shuffledArray: T[] = [...array]; + + for (let i = shuffledArray.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]]; + } + + return shuffledArray; +} diff --git a/assets/Scripts/Services/Utils/ArrayUtils.ts.meta b/assets/Scripts/Services/Utils/ArrayUtils.ts.meta new file mode 100644 index 0000000..197cba6 --- /dev/null +++ b/assets/Scripts/Services/Utils/ArrayUtils.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "4cd531d9-5419-440d-880c-36ce0fe32955", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/package.json b/package.json index 35e07b1..a87ba9d 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "prettier": "^2.7.1", "eslint": "^8.25.0", "jest": "^29.2.2", - "lodash": "^4.17.21", - "ts-jest": "^29.0.3" + "ts-jest": "^29.0.3", + "lodash": "^4.17.21" } } \ No newline at end of file diff --git a/tests/utils/arrayUtils.test.ts b/tests/utils/arrayUtils.test.ts new file mode 100644 index 0000000..6f45c65 --- /dev/null +++ b/tests/utils/arrayUtils.test.ts @@ -0,0 +1,13 @@ +import { shuffle } from "../../assets/Scripts/Services/Utils/ArrayUtils"; + +test("shuffle shuffles the array", () => { + const array: number[] = [0, 1, 2, 3, 4, 5, 6]; + const shuffledArray: number[] = shuffle(array); + + let positionsShuffled = 0; + for (let i = 0; i < shuffledArray.length; i++) { + if (shuffledArray[i] != i) positionsShuffled++; + } + + expect(positionsShuffled).toBeGreaterThan(3); +}); diff --git a/tests/virt.test.ts b/tests/virt.test.ts deleted file mode 100644 index 60d5d77..0000000 --- a/tests/virt.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -test("try ", () => { - const x = 200; - const y = -100; - - const atan = Math.atan(Math.abs(y) / Math.abs(x)); - - console.log(Math.sin(atan) * y); - console.log(Math.cos(atan) * x); - - expect(true).toBeTruthy(); -}); - -export {};