mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2024-12-24 10:48:46 +00:00
Pauser, game modal launcher
This commit is contained in:
parent
43953f18a9
commit
4ca44647d6
@ -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
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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 = <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<void> {
|
||||
this.isPaused = true;
|
||||
const result: UpgradeType = await this.modalWindowManager.showModal<string, UpgradeType>("LevelUpModalWindow", "test params");
|
||||
this.isPaused = false;
|
||||
console.log("Result: " + result);
|
||||
}
|
||||
}
|
||||
|
12
assets/Scripts/Game/ModalWIndows.meta
Normal file
12
assets/Scripts/Game/ModalWIndows.meta
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "00b7fdba-e250-4a79-be07-e87bdfc07929",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
27
assets/Scripts/Game/ModalWIndows/GameModalLauncher.ts
Normal file
27
assets/Scripts/Game/ModalWIndows/GameModalLauncher.ts
Normal file
@ -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<void> {
|
||||
this.gamePauser.pause();
|
||||
const skillToUpgrade: UpgradeType = await this.modalWindowManager.showModal<UpgradeType[], UpgradeType>(
|
||||
GameModalWindowTypes.LevelUpModal,
|
||||
Array.from(this.upgrader.getAvailableUpgrades())
|
||||
);
|
||||
this.gamePauser.resume();
|
||||
this.upgrader.upgradeSkill(skillToUpgrade);
|
||||
}
|
||||
}
|
@ -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": {}
|
3
assets/Scripts/Game/ModalWIndows/GameModalWindowTypes.ts
Normal file
3
assets/Scripts/Game/ModalWIndows/GameModalWindowTypes.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export enum GameModalWindowTypes {
|
||||
LevelUpModal = "LevelUpModalWindow"
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "72fb4349-7b73-41e1-ad7a-4d50abcfc1fa",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
15
assets/Scripts/Game/Pauser.ts
Normal file
15
assets/Scripts/Game/Pauser.ts
Normal file
@ -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;
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Pauser.ts.meta
Normal file
9
assets/Scripts/Game/Pauser.ts.meta
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "d20eb678-aaf9-47e6-bd12-43fa37d3c9ba",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@ -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<string, UpgradeType> {
|
||||
export class LevelUpModalWindow extends ModalWindow<UpgradeType[], UpgradeType> {
|
||||
@property(Prefab) private skillPrefab: Prefab;
|
||||
@property(Node) private skillParent: Node;
|
||||
|
||||
protected async setup(params: string): Promise<void> {
|
||||
private maxUpgradesToPick = 3;
|
||||
|
||||
protected async setup(availableUpgrades: UpgradeType[]): Promise<void> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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<LevelUpSkill> = new Signal<LevelUpSkill>();
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -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<UpgradeType> {
|
||||
const availableUpgrades: Set<UpgradeType> = new Set<UpgradeType>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
10
assets/Scripts/Services/Utils/ArrayUtils.ts
Normal file
10
assets/Scripts/Services/Utils/ArrayUtils.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export function shuffle<T>(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;
|
||||
}
|
9
assets/Scripts/Services/Utils/ArrayUtils.ts.meta
Normal file
9
assets/Scripts/Services/Utils/ArrayUtils.ts.meta
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "4cd531d9-5419-440d-880c-36ce0fe32955",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@ -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"
|
||||
}
|
||||
}
|
13
tests/utils/arrayUtils.test.ts
Normal file
13
tests/utils/arrayUtils.test.ts
Normal file
@ -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);
|
||||
});
|
@ -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 {};
|
Loading…
Reference in New Issue
Block a user