game runner

This commit is contained in:
Martin 2022-12-07 10:47:46 +01:00
parent 669ed8cd81
commit 9884bc4090
24 changed files with 292 additions and 37 deletions

View File

@ -10,12 +10,15 @@
},
{
"__type__": "cc.Scene",
"_name": "scene-2d",
"_name": "Menu",
"_objFlags": 0,
"_parent": null,
"_children": [
{
"__id__": 2
},
{
"__id__": 10
}
],
"_active": true,
@ -23,13 +26,13 @@
"_prefab": null,
"autoReleaseAssets": false,
"_globals": {
"__id__": 9
"__id__": 12
},
"_id": "1a3bccb5-bbb7-4058-846c-ed41b52415b0"
},
{
"__type__": "cc.Node",
"_name": "Canvas",
"_name": "Menu",
"_objFlags": 0,
"_parent": {
"__id__": 1
@ -41,14 +44,14 @@
],
"_active": true,
"_components": [
{
"__id__": 6
},
{
"__id__": 7
},
{
"__id__": 8
},
{
"__id__": 9
}
],
"_prefab": null,
@ -95,6 +98,9 @@
},
{
"__id__": 5
},
{
"__id__": 6
}
],
"_prefab": null,
@ -185,6 +191,17 @@
"_atlas": null,
"_id": "72StvrPGhHK4bp9R7RA2NO"
},
{
"__type__": "22c61zFd7hNRaAttffaS6ep",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 3
},
"_enabled": true,
"__prefab": null,
"_id": "0e1btqtltATbmaReZXZW4r"
},
{
"__type__": "cc.UITransform",
"_name": "",
@ -247,21 +264,75 @@
"_id": "2aCNE8+JdDk581XA25TXI9"
},
{
"__type__": "cc.SceneGlobals",
"ambient": {
"__type__": "cc.Node",
"_name": "AppRoot",
"_objFlags": 0,
"_parent": {
"__id__": 1
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 11
}
],
"_prefab": null,
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 1073741824,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": "58b/Etqn1D0LBB1esUnUos"
},
{
"__type__": "4943dSvt9FBvLizJ1HhFOzg",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 10
},
"shadows": {
"__id__": 11
},
"_skybox": {
"__id__": 12
},
"fog": {
"_enabled": true,
"__prefab": null,
"_id": "eeXuVZFQ1KNpRron3jnCKj"
},
{
"__type__": "cc.SceneGlobals",
"ambient": {
"__id__": 13
},
"octree": {
"shadows": {
"__id__": 14
},
"_skybox": {
"__id__": 15
},
"fog": {
"__id__": 16
},
"octree": {
"__id__": 17
}
},
{

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "e26cf04f-eda1-4f45-b452-329628f16eea",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,34 @@
import { _decorator, Component, Node, director, AudioSource } from "cc";
import { SaveSystem } from "./SaveSystem";
const { ccclass, property } = _decorator;
@ccclass("AppRoot")
export class AppRoot extends Component {
@property(AudioSource) private soundSource: AudioSource;
@property(AudioSource) private musicSource: AudioSource;
private static instance: AppRoot;
private saveSystem: SaveSystem;
public static get Instance(): AppRoot {
return this.instance;
}
public get SaveSystem(): SaveSystem {
return this.saveSystem;
}
public start(): void {
if (AppRoot.Instance == null) {
AppRoot.instance = this;
director.addPersistRootNode(this.node);
this.init();
} else {
this.destroy();
}
}
private init(): void {
this.saveSystem = new SaveSystem();
}
}

View File

@ -2,7 +2,7 @@
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ee9d41d4-91f4-4d8f-a4ef-7e0df147fb35",
"uuid": "4943d4af-b7d1-41bc-b8b3-2751e114ece0",
"files": [],
"subMetas": {},
"userData": {}

View File

@ -0,0 +1,21 @@
import { sys } from "cc";
import { UserData } from "../Game/Data/UserData";
export class SaveSystem {
private userDataIdentifier = "user-data";
public save(userData: UserData): void {
sys.localStorage.setItem(this.userDataIdentifier, JSON.stringify(userData));
}
public load(): UserData {
const data: string = sys.localStorage.getItem(this.userDataIdentifier);
if (!data) return new UserData();
try {
return <UserData>JSON.parse(data);
} catch (error) {
return new UserData();
}
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ed50cd4b-21fa-4acc-9fee-92abaf3e2134",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,17 @@
export class UserData {
public soundVolume = 0;
public musicVolume = 0;
public game = new GameData();
}
export class GameData {
public goldCoins = 0;
public metaUpgrades = new MetaUpgradesData();
}
export class MetaUpgradesData {
public maxHpLevel = 0;
public overallDamageLevel = 0;
public projectilePiercingLevel = 0;
public movementSpeedLevel = 0;
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "7431ff7c-480c-4474-9434-654eda0a1a08",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -1,5 +1,6 @@
import { Camera, Component, JsonAsset, KeyCode, Vec2, _decorator } from "cc";
import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowManager";
import { delay } from "../Services/Utils/AsyncUtils";
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
import { PlayerProjectileCollisionSystem } from "./Collision/PlayerProjectileCollisionSystem";
import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem";
@ -19,8 +20,8 @@ import { Upgrader } from "./Upgrades/Upgrader";
const { ccclass, property } = _decorator;
@ccclass("GameBootstrapper")
export class GameBootstrapper extends Component {
@ccclass("Game")
export class Game extends Component {
@property(VirtualJoystic) private virtualJoystic: VirtualJoystic;
@property(Player) private player: Player;
@property(ProjectileLauncher) private haloProjectileLauncherComponent: ProjectileLauncher;
@ -39,15 +40,26 @@ export class GameBootstrapper extends Component {
private gamePauser: Pauser = new Pauser();
private static instance: Game;
public static get Instance(): Game {
return this.instance;
}
public start(): void {
Game.instance = this;
this.gamePauser.pause();
}
public async playGame(): Promise<number> {
const settings: GameSettings = <GameSettings>this.settingsAsset.json;
this.virtualJoystic.init();
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, settings.player);
const multiInput: MultiInput = new MultiInput([this.virtualJoystic, wasd, arrowKeys]);
this.player.init(multiInput, settings.player);
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, settings.player.collisionDelay);
new WeaponCollisionSystem(this.player.Weapon);
@ -80,6 +92,12 @@ export class GameBootstrapper extends Component {
new GameModalLauncher(this.modalWindowManager, this.player, this.gamePauser, upgrader);
this.gameUI.init(this.player);
this.gamePauser.resume();
await delay(10000);
this.gamePauser.pause();
Game.instance = null;
return 1;
}
public update(deltaTime: number): void {

View File

@ -13,12 +13,14 @@ export class Enemy extends Component {
private movementType: EnemyMovementType;
private health: UnitHealth = new UnitHealth(1);
private deathEvent: Signal<Enemy> = new Signal<Enemy>();
private speed: number;
private speedX: number;
private speedY: number;
public setup(position: Vec3, movementType: EnemyMovementType): void {
this.movementType = movementType;
this.health = new UnitHealth(1);
this.speed = randomRange(40, 90);
this.speedX = randomRange(40, 90);
this.speedY = randomRange(40, 90);
this.node.setWorldPosition(position);
this.node.active = true;
}
@ -52,8 +54,8 @@ export class Enemy extends Component {
public moveBy(move: Vec3, deltaTime: number): void {
const newPosition: Vec3 = this.node.worldPosition;
newPosition.x += move.x * this.speed * deltaTime;
newPosition.y += move.y * this.speed * deltaTime;
newPosition.x += move.x * this.speedX * deltaTime;
newPosition.y += move.y * this.speedY * deltaTime;
this.node.setWorldPosition(newPosition);
}

View File

@ -11,7 +11,7 @@ export class WaveEnemyMover extends EnemyMover {
let direction: Vec3 = new Vec3();
// if the enemy is added soon enough, move as a single group towards one direction
if (Vec3.distance(this.lastTargetPosition, this.targetNode.worldPosition) < 10) {
if (Vec3.equals(this.lastTargetPosition, this.targetNode.worldPosition)) {
direction = this.lastDirection;
} else {
direction = Vec3.subtract(direction, this.targetNode.worldPosition, enemy.node.worldPosition);

View File

@ -15,7 +15,7 @@ export class IndividualEnemySpawner {
if (this.spawnTimer.tryFinishPeriod()) {
const posX: number = randomRange(300, 600) * randomPositiveOrNegative();
const posY: number = randomRange(300, 600) * randomPositiveOrNegative();
this.enemySpawner.spawnNewEnemy(posX, posY, EnemyMovementType.Launch);
this.enemySpawner.spawnNewEnemy(posX, posY, EnemyMovementType.Follow);
}
}
}

View File

@ -48,8 +48,8 @@ export class WaveEnemySpawner {
private trySpawnNewGroup(): void {
if (this.spawnTimer.tryFinishPeriod()) {
const defaultPosX: number = 200 * randomPositiveOrNegative();
const defaultPosY: number = 200 * randomPositiveOrNegative();
const defaultPosX: number = (500 + randomRange(0, 100)) * randomPositiveOrNegative();
const defaultPosY: number = randomRange(0, 500) * randomPositiveOrNegative();
const enemies: Enemy[] = [];
const side: number = Math.ceil(Math.sqrt(this.enemiesPerWave));

View File

@ -9,5 +9,6 @@ export enum UpgradeType {
export enum MetaUpgradeType {
MaxHp,
OverallDamage,
ProjectilePiercing
ProjectilePiercing,
MovementSpeed
}

12
assets/Scripts/Menu.meta Normal file
View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "4caa94da-a5e7-4535-b418-750cb2cb8d55",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,28 @@
import { director } from "cc";
import { AppRoot } from "../AppRoot/AppRoot";
import { UserData } from "../Game/Data/UserData";
import { Game } from "../Game/Game";
import { delay } from "../Services/Utils/AsyncUtils";
export class GameRunner {
private static instance: GameRunner = new GameRunner();
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
public static get Instance(): GameRunner {
return this.instance;
}
public async playGame(): Promise<void> {
director.loadScene("Game");
const userData: UserData = AppRoot.Instance.SaveSystem.load();
while (Game.Instance == null) await delay(10);
const result: number = await Game.Instance.playGame();
userData.game.goldCoins += result;
AppRoot.Instance.SaveSystem.save(userData);
console.log("Gold coins: " + result);
console.log("All gold coins: " + userData.game.goldCoins);
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "c3869838-2d3a-4a22-a2f4-77d11a8226d5",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,10 @@
import { _decorator, Component, Node } from "cc";
import { GameRunner } from "./GameRunner";
const { ccclass, property } = _decorator;
@ccclass("Menu")
export class Menu extends Component {
public start(): void {
GameRunner.Instance.playGame();
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "22c61cc5-77b8-4d45-a02d-b5f7da4ba7a9",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -1,7 +0,0 @@
import { Collider2D, IPhysics2DContact } from "cc";
export type ContactParams = {
selfCollider: Collider2D;
otherCollider: Collider2D;
contact: IPhysics2DContact | null;
};