Slash-The-Hordes/assets/Scripts/Game/Game.ts

244 lines
11 KiB
TypeScript
Raw Normal View History

2023-01-02 10:14:44 +00:00
import { Canvas, Component, KeyCode, Vec2, _decorator, Node } from "cc";
2023-01-02 09:56:41 +00:00
import { AppRoot } from "../AppRoot/AppRoot";
import { requireAppRootAsync } from "../AppRoot/AppRootUtils";
2022-12-07 09:47:46 +00:00
import { delay } from "../Services/Utils/AsyncUtils";
2022-12-20 09:00:47 +00:00
import { GameAudioAdapter } from "./Audio/GameAudioAdapter";
2022-12-13 09:21:44 +00:00
import { Background } from "./Background/Background";
2022-12-23 10:36:12 +00:00
import { MagnetCollisionSystem } from "./Collision/MagnetCollisionSystem";
2022-11-08 18:45:57 +00:00
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
2022-11-30 07:21:22 +00:00
import { PlayerProjectileCollisionSystem } from "./Collision/PlayerProjectileCollisionSystem";
2022-11-08 18:45:57 +00:00
import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem";
2022-12-13 14:56:13 +00:00
import { GameSettings, PlayerSettings } from "./Data/GameSettings";
2022-12-13 15:58:55 +00:00
import { TranslationData } from "./Data/TranslationData";
2022-12-13 10:58:40 +00:00
import { UserData } from "./Data/UserData";
2022-11-16 11:26:20 +00:00
import { KeyboardInput } from "./Input/KeyboardInput";
2022-11-14 15:35:47 +00:00
import { MultiInput } from "./Input/MultiInput";
2022-11-08 18:45:57 +00:00
import { VirtualJoystic } from "./Input/VirtualJoystic";
2022-12-23 10:36:12 +00:00
import { ItemAttractor } from "./Items/ItemAttractor";
2022-12-20 10:27:51 +00:00
import { ItemManager } from "./Items/ItemManager";
2022-11-25 11:00:09 +00:00
import { GameModalLauncher } from "./ModalWIndows/GameModalLauncher";
import { Pauser } from "./Pauser";
2022-12-19 11:00:55 +00:00
import { TestValues } from "./TestGameRunner";
2022-11-16 13:04:23 +00:00
import { GameUI } from "./UI/GameUI";
2023-01-02 12:51:49 +00:00
import { EnemyDeathEffectSpawner } from "./Unit/Enemy/EnemyDeathEffectSpawner/EnemyDeathEffectSpawner";
2022-11-28 11:19:04 +00:00
import { EnemyManager } from "./Unit/Enemy/EnemyManager";
2022-12-21 13:56:23 +00:00
import { EnemyProjectileLauncher } from "./Unit/Enemy/ProjectileLauncher.cs/EnemyProjectileLauncher";
2022-12-13 10:58:40 +00:00
import { MetaUpgrades } from "./Unit/MetaUpgrades/MetaUpgrades";
import { Player, PlayerData } from "./Unit/Player/Player";
2022-11-30 10:44:20 +00:00
import { HaloProjectileLauncher } from "./Unit/Player/ProjectileLauncher/HaloProjectileLauncher";
2022-12-21 07:51:39 +00:00
import { ProjectileData } from "./Unit/Player/ProjectileLauncher/ProjectileData";
2022-12-21 13:56:23 +00:00
import { ProjectileLauncher } from "./Unit/Player/ProjectileLauncher/ProjectileLauncher";
2022-12-01 11:16:52 +00:00
import { WaveProjectileLauncher } from "./Unit/Player/ProjectileLauncher/WaveProjectileLauncher";
2022-11-24 12:17:05 +00:00
import { Upgrader } from "./Upgrades/Upgrader";
2022-12-13 10:58:40 +00:00
import { MetaUpgradeType } from "./Upgrades/UpgradeType";
2022-11-28 10:49:16 +00:00
2022-11-08 18:45:57 +00:00
const { ccclass, property } = _decorator;
2022-12-07 09:47:46 +00:00
@ccclass("Game")
export class Game extends Component {
2022-12-21 13:08:49 +00:00
private static instance: Game;
2022-11-08 18:45:57 +00:00
@property(VirtualJoystic) private virtualJoystic: VirtualJoystic;
@property(Player) private player: Player;
2022-11-30 10:44:20 +00:00
@property(ProjectileLauncher) private haloProjectileLauncherComponent: ProjectileLauncher;
2022-12-01 11:16:52 +00:00
@property(ProjectileLauncher) private horizontalProjectileLauncherComponent: ProjectileLauncher;
@property(ProjectileLauncher) private diagonalProjectileLauncherComponent: ProjectileLauncher;
2022-12-26 17:49:48 +00:00
@property(ProjectileLauncher) private enemyAxeProjectileLauncherComponent: ProjectileLauncher;
@property(ProjectileLauncher) private enemyMagicOrbProjectileLauncherComponent: ProjectileLauncher;
2022-11-16 11:26:20 +00:00
@property(EnemyManager) private enemyManager: EnemyManager;
2023-01-02 12:51:49 +00:00
@property(EnemyDeathEffectSpawner) private deathEffectSpawner: EnemyDeathEffectSpawner;
2022-12-20 10:27:51 +00:00
@property(ItemManager) private itemManager: ItemManager;
2022-11-16 13:04:23 +00:00
@property(GameUI) private gameUI: GameUI;
2023-01-02 09:56:41 +00:00
@property(Canvas) private gameCanvas: Canvas;
2022-12-13 09:21:44 +00:00
@property(Background) private background: Background;
2022-12-20 09:00:47 +00:00
@property(GameAudioAdapter) private gameAudioAdapter: GameAudioAdapter;
2023-01-02 10:14:44 +00:00
@property(Node) private blackScreen: Node;
2022-11-17 17:28:39 +00:00
2022-11-08 18:45:57 +00:00
private playerCollisionSystem: PlayerCollisionSystem;
2022-11-30 10:44:20 +00:00
private haloProjectileLauncher: HaloProjectileLauncher;
2022-12-01 11:16:52 +00:00
private horizontalProjectileLauncher: WaveProjectileLauncher;
private diagonalProjectileLauncher: WaveProjectileLauncher;
2022-11-08 18:45:57 +00:00
2022-12-26 17:49:48 +00:00
private enemyAxeProjectileLauncher: EnemyProjectileLauncher;
private enemyMagicOrbProjectileLauncher: EnemyProjectileLauncher;
2022-12-21 10:30:21 +00:00
2022-12-23 10:36:12 +00:00
private itemAttractor: ItemAttractor;
2022-11-25 11:00:09 +00:00
private gamePauser: Pauser = new Pauser();
2022-12-21 13:08:49 +00:00
private gameResult: GameResult;
2022-11-23 08:01:01 +00:00
2022-12-13 17:21:04 +00:00
private timeAlive = 0;
2022-12-07 09:47:46 +00:00
public static get Instance(): Game {
return this.instance;
}
2023-01-02 10:14:44 +00:00
public start(): void {
2022-12-07 09:47:46 +00:00
this.gamePauser.pause();
2023-01-02 09:56:41 +00:00
Game.instance = this;
2022-12-07 09:47:46 +00:00
}
2023-01-05 08:53:16 +00:00
public async play(userData: UserData, settings: GameSettings, translationData: TranslationData, testValues?: TestValues): Promise<GameResult> {
await this.setup(userData, settings, translationData, testValues);
this.gamePauser.resume();
this.blackScreen.active = false;
AppRoot.Instance.ScreenFader.playClose();
while (!this.gameResult.hasExitManually && this.player.Health.IsAlive) await delay(100);
this.gamePauser.pause();
Game.instance = null;
this.gameResult.score = this.timeAlive;
if (!this.gameResult.hasExitManually) {
await delay(2000);
}
return this.gameResult;
}
public exitGame(): void {
this.gameResult.hasExitManually = true;
}
public update(deltaTime: number): void {
if (this.gamePauser.IsPaused) return;
this.player.gameTick(deltaTime);
this.playerCollisionSystem.gameTick(deltaTime);
this.enemyManager.gameTick(deltaTime);
this.haloProjectileLauncher.gameTick(deltaTime);
this.horizontalProjectileLauncher.gameTick(deltaTime);
this.diagonalProjectileLauncher.gameTick(deltaTime);
this.enemyAxeProjectileLauncher.gameTick(deltaTime);
this.enemyMagicOrbProjectileLauncher.gameTick(deltaTime);
this.itemAttractor.gameTick(deltaTime);
this.background.gameTick();
this.timeAlive += deltaTime;
this.gameUI.updateTimeAlive(this.timeAlive);
AppRoot.Instance.MainCamera.node.setWorldPosition(this.player.node.worldPosition);
this.gameUI.node.setWorldPosition(this.player.node.worldPosition);
}
private async setup(userData: UserData, settings: GameSettings, translationData: TranslationData, testValues: TestValues): Promise<void> {
2023-01-02 10:14:44 +00:00
await requireAppRootAsync();
2023-01-02 09:56:41 +00:00
this.gameCanvas.cameraComponent = AppRoot.Instance.MainCamera;
2022-12-21 13:08:49 +00:00
this.gameResult = new GameResult();
2022-12-13 10:58:40 +00:00
const metaUpgrades = new MetaUpgrades(userData.game.metaUpgrades, settings.metaUpgrades);
2022-11-24 12:17:05 +00:00
2022-11-08 18:45:57 +00:00
this.virtualJoystic.init();
2022-11-29 14:55:47 +00:00
2022-11-14 15:35:47 +00:00
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);
2022-12-07 09:47:46 +00:00
const multiInput: MultiInput = new MultiInput([this.virtualJoystic, wasd, arrowKeys]);
2022-12-13 10:58:40 +00:00
2022-12-13 14:56:13 +00:00
this.player.init(multiInput, this.createPlayerData(settings.player, metaUpgrades));
2022-12-20 10:27:51 +00:00
this.enemyManager.init(this.player.node, settings.enemyManager);
2023-01-02 12:51:49 +00:00
this.deathEffectSpawner.init(this.enemyManager);
2022-11-14 15:35:47 +00:00
2022-12-20 10:27:51 +00:00
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, settings.player.collisionDelay, this.itemManager);
2022-11-29 14:55:47 +00:00
new WeaponCollisionSystem(this.player.Weapon);
2022-11-16 11:26:20 +00:00
2022-12-13 10:58:40 +00:00
const projectileData = new ProjectileData();
projectileData.damage = 1 + metaUpgrades.getUpgradeValue(MetaUpgradeType.OverallDamage);
projectileData.pierces = 1 + metaUpgrades.getUpgradeValue(MetaUpgradeType.ProjectilePiercing);
2022-11-30 10:44:20 +00:00
this.haloProjectileLauncher = new HaloProjectileLauncher(
this.haloProjectileLauncherComponent,
this.player.node,
2022-12-13 10:58:40 +00:00
settings.player.haloLauncher,
projectileData
2022-11-30 10:44:20 +00:00
);
2022-12-01 11:16:52 +00:00
this.horizontalProjectileLauncher = new WaveProjectileLauncher(
this.horizontalProjectileLauncherComponent,
2022-11-30 10:44:20 +00:00
this.player.node,
2022-12-01 11:16:52 +00:00
[new Vec2(-1, 0), new Vec2(1, 0)],
2022-12-13 10:58:40 +00:00
settings.player.horizontalLauncher,
projectileData
2022-11-30 10:44:20 +00:00
);
2022-11-30 08:37:09 +00:00
2022-12-01 11:16:52 +00:00
this.diagonalProjectileLauncher = new WaveProjectileLauncher(
this.diagonalProjectileLauncherComponent,
this.player.node,
[new Vec2(-0.5, -0.5), new Vec2(0.5, -0.5)],
2022-12-13 10:58:40 +00:00
settings.player.diagonalLauncher,
projectileData
2022-12-01 11:16:52 +00:00
);
2022-12-26 17:49:48 +00:00
this.enemyAxeProjectileLauncher = new EnemyProjectileLauncher(
this.enemyAxeProjectileLauncherComponent,
2022-12-21 10:30:21 +00:00
this.player.node,
this.enemyManager,
2022-12-26 17:49:48 +00:00
settings.enemyManager.axeLauncher
);
this.enemyMagicOrbProjectileLauncher = new EnemyProjectileLauncher(
this.enemyMagicOrbProjectileLauncherComponent,
this.player.node,
this.enemyManager,
settings.enemyManager.magicOrbLauncher
2022-12-21 10:30:21 +00:00
);
2022-12-01 11:16:52 +00:00
new PlayerProjectileCollisionSystem([this.haloProjectileLauncher, this.horizontalProjectileLauncher, this.diagonalProjectileLauncher]);
2022-11-30 15:04:48 +00:00
2022-12-23 10:36:12 +00:00
this.itemAttractor = new ItemAttractor(this.player.node, 100);
new MagnetCollisionSystem(this.player.Magnet, this.itemAttractor);
2022-12-12 13:41:08 +00:00
const upgrader = new Upgrader(
this.player,
this.horizontalProjectileLauncher,
this.haloProjectileLauncher,
this.diagonalProjectileLauncher,
settings.upgrades
);
2023-01-02 09:56:41 +00:00
const modalLauncher = new GameModalLauncher(AppRoot.Instance.ModalWindowManager, this.player, this.gamePauser, upgrader, translationData);
2022-11-30 07:21:22 +00:00
2023-01-02 07:57:59 +00:00
this.itemManager.init(this.enemyManager, this.player, this.gameResult, modalLauncher, settings.items);
2022-12-21 13:08:49 +00:00
this.gameUI.init(this.player, modalLauncher);
2022-12-13 09:21:44 +00:00
this.background.init(this.player.node);
2022-12-19 11:00:55 +00:00
if (testValues) {
this.timeAlive += testValues.startTime;
this.player.Level.addXp(testValues.startXP);
}
2022-12-22 11:08:00 +00:00
this.gameAudioAdapter.init(
this.player,
this.enemyManager,
this.itemManager,
this.horizontalProjectileLauncher,
this.diagonalProjectileLauncher,
this.haloProjectileLauncher
);
2022-11-08 18:45:57 +00:00
}
2022-12-13 14:56:13 +00:00
private createPlayerData(settings: PlayerSettings, metaUpgrades: MetaUpgrades): PlayerData {
const playerData: PlayerData = Object.assign(new PlayerData(), settings);
2022-12-16 12:52:54 +00:00
playerData.maxHp = metaUpgrades.getUpgradeValue(MetaUpgradeType.Health) + settings.defaultHP;
2022-12-13 14:56:13 +00:00
playerData.requiredXP = settings.requiredXP;
playerData.speed = metaUpgrades.getUpgradeValue(MetaUpgradeType.MovementSpeed) + settings.speed;
playerData.regenerationDelay = settings.regenerationDelay;
playerData.xpMultiplier = metaUpgrades.getUpgradeValue(MetaUpgradeType.XPGatherer) + 1;
playerData.goldMultiplier = metaUpgrades.getUpgradeValue(MetaUpgradeType.GoldGatherer) + 1;
playerData.damage = metaUpgrades.getUpgradeValue(MetaUpgradeType.OverallDamage) + settings.weapon.damage;
playerData.strikeDelay = settings.weapon.strikeDelay;
2022-12-23 10:36:12 +00:00
playerData.magnetDuration = settings.magnetDuration;
2022-12-13 14:56:13 +00:00
return playerData;
}
2022-11-08 18:45:57 +00:00
}
2022-12-13 17:25:19 +00:00
export class GameResult {
2022-12-21 13:08:49 +00:00
public hasExitManually = false;
2022-12-13 17:25:19 +00:00
public goldCoins = 0;
public score = 0;
}