This commit is contained in:
Martin 2022-11-16 14:04:23 +01:00
parent 0eb9cc907f
commit 3c39653e47
11 changed files with 790 additions and 170 deletions

File diff suppressed because it is too large Load Diff

View File

@ -65,7 +65,7 @@ export class PlayerCollisionSystem {
private resolveXpContact(xpCollider: Collider2D): void {
const xp: XP = xpCollider.node.getComponent(XP);
this.player.addXp(xp.Value);
this.player.Level.addXp(xp.Value);
xp.pickup();
console.log("Collided with xp: " + xp);

View File

@ -15,7 +15,7 @@ export class EnemyManager extends Component {
public init(targetNode: Node): void {
this.enemyMover = new EnemyMover(targetNode);
this.enemySpawner.init();
this.enemySpawner.init(targetNode);
this.enemySpawner.EnemyAddedEvent.on(this.onEnemyAdded, this);
this.xpSpawner.init();

View File

@ -1,4 +1,4 @@
import { Component, Prefab, randomRange, Vec3, _decorator } from "cc";
import { Component, Prefab, randomRange, Vec3, _decorator, Node } from "cc";
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
import { GameTimer } from "../../Services/GameTimer";
@ -15,7 +15,10 @@ export class EnemySpawner extends Component {
private enemyPool: ObjectPool<Enemy>;
private spawnTimer: GameTimer;
public init(): void {
private targetNode: Node;
public init(targetNode: Node): void {
this.targetNode = targetNode;
this.enemyPool = new ObjectPool(this.enemies[0], this.node, 5, "Enemy");
this.spawnTimer = new GameTimer(1);
}
@ -33,7 +36,10 @@ export class EnemySpawner extends Component {
private spawnNewEnemy(): void {
const enemy = this.enemyPool.borrow();
enemy.setup(new Vec3(randomRange(0, 300), randomRange(0, 800)));
const spawnPosition = new Vec3();
spawnPosition.x = this.targetNode.worldPosition.x + randomRange(-300, 300);
spawnPosition.y = this.targetNode.worldPosition.y + randomRange(-800, 800);
enemy.setup(spawnPosition);
enemy.DeathEvent.on(this.returnEnemyToPool, this);

View File

@ -1,4 +1,4 @@
import { Camera, CCFloat, Component, KeyCode, _decorator } from "cc";
import { Camera, CCFloat, CCInteger, Component, KeyCode, _decorator } from "cc";
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem";
import { EnemyManager } from "./Enemy/EnemyManager";
@ -6,6 +6,7 @@ import { KeyboardInput } from "./Input/KeyboardInput";
import { MultiInput } from "./Input/MultiInput";
import { VirtualJoystic } from "./Input/VirtualJoystic";
import { Player } from "./Player/Player";
import { GameUI } from "./UI/GameUI";
import { Weapon } from "./Weapon";
const { ccclass, property } = _decorator;
@ -18,6 +19,8 @@ export class GameBootstrapper extends Component {
@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[] = [];
private playerCollisionSystem: PlayerCollisionSystem;
public start(): void {
@ -26,12 +29,14 @@ export class GameBootstrapper extends Component {
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.player.init(dualInput, this.weapon, 50, this.requiredLevelXps);
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, this.collisionDelay);
new WeaponCollisionSystem(this.weapon);
this.enemyManager.init(this.player.node);
this.gameUI.init(this.player);
}
public update(deltaTime: number): void {

View File

@ -3,6 +3,7 @@ import { IInput } from "../Input/IInput";
import { Weapon } from "../Weapon";
import { PlayerUI } from "./PlayerUI/PlayerUI";
import { UnitHealth } from "./UnitHealth";
import { UnitLevel } from "./UnitLevel";
const { ccclass, property } = _decorator;
@ccclass("Player")
@ -14,13 +15,15 @@ export class Player extends Component {
private input: IInput;
private weapon: Weapon;
private health: UnitHealth;
private level: UnitLevel;
private xp: number;
public init(input: IInput, weapon: Weapon, maxHp: number): void {
public init(input: IInput, weapon: Weapon, maxHp: number, requiredLevelXps: number[]): void {
this.input = input;
this.weapon = weapon;
this.health = new UnitHealth(maxHp);
this.level = new UnitLevel(requiredLevelXps);
this.weapon.node.parent = this.node;
this.weapon.node.setPosition(new Vec3());
@ -32,12 +35,12 @@ export class Player extends Component {
return this.health;
}
public get Collider(): Collider2D {
return this.collider;
public get Level(): UnitLevel {
return this.level;
}
public addXp(points: number): void {
this.xp += points;
public get Collider(): Collider2D {
return this.collider;
}
public gameTick(deltaTime: number): void {

View File

@ -0,0 +1,46 @@
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
export class UnitLevel {
private xp = 0;
private requiredXPs: number[];
private currentLevel = 0;
private levelUpEvent: Signal<number> = new Signal<number>();
private xpAddedEvent: Signal<number> = new Signal<number>();
public constructor(requiredXPs: number[]) {
this.requiredXPs = requiredXPs;
}
public addXp(points: number): void {
this.xp += points;
this.xpAddedEvent.trigger(this.xp);
this.tryLevelUp();
}
public get XP(): number {
return this.xp;
}
public get RequiredXP(): number {
return this.requiredXPs[this.currentLevel];
}
public get LevelUpEvent(): ISignal<number> {
return this.levelUpEvent;
}
public get XpAddedEvent(): ISignal<number> {
return this.xpAddedEvent;
}
private tryLevelUp(): void {
if (this.requiredXPs.length <= this.currentLevel) return;
if (this.xp < this.requiredXPs[this.currentLevel]) return;
this.xp -= this.requiredXPs[this.currentLevel];
this.currentLevel++;
this.levelUpEvent.trigger(this.currentLevel);
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "5eeabc4f-21b1-4983-b935-e44c12051766",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "614dff3d-73df-4abb-8276-7931d2dc3596",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,22 @@
import { Component, ProgressBar, _decorator } from "cc";
import { Player } from "../Player/Player";
import { UnitLevel } from "../Player/UnitLevel";
const { ccclass, property } = _decorator;
@ccclass("GameUI")
export class GameUI extends Component {
@property(ProgressBar) private xpBar: ProgressBar;
private playerLevel: UnitLevel;
public init(player: Player): void {
this.playerLevel = player.Level;
this.playerLevel.XpAddedEvent.on(this.updateProgressBar, this);
this.playerLevel.LevelUpEvent.on(this.updateProgressBar, this);
this.xpBar.progress = 0;
}
private updateProgressBar(): void {
this.xpBar.progress = this.playerLevel.XP / this.playerLevel.RequiredXP;
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "2e393e26-6f0c-4667-ae26-c1d4e1026d08",
"files": [],
"subMetas": {},
"userData": {}
}