Added regeneration

This commit is contained in:
Martin 2022-11-28 14:34:34 +01:00
parent 692bb7c35c
commit a0bd11b61d
7 changed files with 61 additions and 7 deletions

View File

@ -13,16 +13,18 @@
20,
20
],
"regenerationDelay": 5,
"collisionDelay": 0.5
},
"weapon": {
"strikeDelay": 2
"strikeDelay": 2,
"damage": 1
},
"upgrades": {
"maxWeaponLengthUpgrades": 5,
"maxWeaponDamageUpgrades": 5,
"maxHorizontalProjectileUpgrades": 0,
"maxVerticalProjectileUpgrades": 0,
"maxRegenerationUpgrades": 0
"maxRegenerationUpgrades": 5
}
}

View File

@ -7,6 +7,7 @@ export class GameSettings {
export class PlayerSettings {
public defaultHP = 0;
public requiredXP: number[] = [];
public regenerationDelay = 0;
public collisionDelay = 0;
}

View File

@ -39,7 +39,7 @@ 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, settings.player.defaultHP, settings.player.requiredXP);
this.player.init(dualInput, this.weapon, settings.player);
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, settings.player.collisionDelay);
new WeaponCollisionSystem(this.weapon);

View File

@ -1,7 +1,9 @@
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { PlayerSettings } from "../../Data/GameSettings";
import { IInput } from "../../Input/IInput";
import { UnitHealth } from "../UnitHealth";
import { UnitLevel } from "../UnitLevel";
import { PlayerRegeneration } from "./PlayerRegeneration";
import { PlayerUI } from "./PlayerUI/PlayerUI";
import { Weapon } from "./Weapon/Weapon";
@ -17,12 +19,14 @@ export class Player extends Component {
private weapon: Weapon;
private health: UnitHealth;
private level: UnitLevel;
private regeneration: PlayerRegeneration;
public init(input: IInput, weapon: Weapon, maxHp: number, requiredLevelXps: number[]): void {
public init(input: IInput, weapon: Weapon, settings: PlayerSettings): void {
this.input = input;
this.weapon = weapon;
this.health = new UnitHealth(maxHp);
this.level = new UnitLevel(requiredLevelXps);
this.health = new UnitHealth(settings.defaultHP);
this.level = new UnitLevel(settings.requiredXP);
this.regeneration = new PlayerRegeneration(this.health, settings.regenerationDelay);
this.weapon.node.parent = this.node;
this.weapon.node.setPosition(new Vec3());
@ -42,6 +46,10 @@ export class Player extends Component {
return this.weapon;
}
public get Regeneration(): PlayerRegeneration {
return this.regeneration;
}
public get Collider(): Collider2D {
return this.collider;
}
@ -58,5 +66,6 @@ export class Player extends Component {
this.node.setWorldPosition(newPosition);
this.weapon.gameTick(deltaTime);
this.regeneration.gameTick(deltaTime);
}
}

View File

@ -0,0 +1,28 @@
import { GameTimer } from "../../../Services/GameTimer";
import { UnitHealth } from "../UnitHealth";
export class PlayerRegeneration {
private currentRegenerationAmount = 0;
private regenerationDelay: number;
private regenerationTimer: GameTimer = new GameTimer(0);
private health: UnitHealth;
public constructor(health: UnitHealth, regenerationDelay: number) {
this.health = health;
this.regenerationDelay = regenerationDelay;
}
public upgrade(): void {
this.currentRegenerationAmount++;
this.regenerationTimer = new GameTimer(this.regenerationDelay / this.currentRegenerationAmount);
}
public gameTick(deltaTime: number): void {
if (this.currentRegenerationAmount <= 0) return;
this.regenerationTimer.gameTick(deltaTime);
if (this.regenerationTimer.tryFinishPeriod()) {
this.health.heal(1);
}
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "d7c2f178-5239-4c80-b3f5-6b6a797bb71f",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -12,7 +12,8 @@ export class Upgrader {
this.player = player;
this.setTypeMaps(UpgradeType.WeaponLength, this.upgradeWeaponLength.bind(this), settings.maxWeaponLengthUpgrades);
this.setTypeMaps(UpgradeType.WeaponDamage, this.upgradeWeaponDamage, settings.maxWeaponDamageUpgrades);
this.setTypeMaps(UpgradeType.WeaponDamage, this.upgradeWeaponDamage.bind(this), settings.maxWeaponDamageUpgrades);
this.setTypeMaps(UpgradeType.Regeneration, this.upgradeRegeneration.bind(this), settings.maxRegenerationUpgrades);
}
public upgradeSkill(type: UpgradeType): void {
@ -49,6 +50,10 @@ export class Upgrader {
this.player.Weapon.upgradeWeaponDamage();
}
private upgradeRegeneration(): void {
this.player.Regeneration.upgrade();
}
private isMaxLevel(type: UpgradeType): boolean {
return this.typeToMaxLevel.get(type) <= this.typeToLevel.get(type);
}