So I created a base class called AncientWisdom... and sub classes based on 15 different AncientWisdoms with more specific detail. My problem is that on angular I output each of the PermanentUpgrade... classes out and when I do an end value that triggers the > maxUnlocks logic it will update all subclasses that are inheriting from AncientWisdom to that value as well as throw a model value being updated unexpectedly. I've tried moving the if statement even down to the class level and still getting the error.
I will probably end up just going instead with only the base class and setting a few values, but I'm very confused on why my classes are sharing variables like that without being static.
export class AncientWisdom {
startValue: number;
endValue: number;
name: string;
maxUnlocks: number;
constructor() {
this.endValue = 0;
this.startValue = 0;
this.maxUnlocks = -1;
}
calculateCost(): number {
if (this.endValue > this.maxUnlocks) {
this.endValue = this.maxUnlocks;
}
const currentCost = this.startValue * (this.startValue + 1);
const desiredCost = this.endValue * (this.endValue + 1);
const cost = (desiredCost - currentCost) * 400 / 2;
return cost > 0 ? cost : 0;
}
}
import { AncientWisdom } from "./ancientWisdom.model";
export class PermanentUpgradeEnergy extends AncientWisdom {
constructor() {
super();
this.name = 'Increased Energy Points';
this.maxUnlocks = 2;
}
calculateCost(): number {
const currentCost = this.startValue * (this.startValue + 1);
const desiredCost = this.endValue * (this.endValue + 1);
const cost = (desiredCost - currentCost) * 400 / 2;
return cost > 0 ? cost : 0;
}
}
import { AncientWisdom } from "./ancientWisdom.model";
export class PermanentUpgradeLessHP extends AncientWisdom {
constructor() {
super();
this.name = 'Reduced Boss HP';
this.maxUnlocks = 10;
}
calculateCost(): number {
const currentCost = this.startValue * (this.startValue + 1);
const desiredCost = this.endValue * (this.endValue + 1);
const cost = (desiredCost - currentCost) * 200 / 2;
return cost > 0 ? cost : 0;
}
}
export class AncientWisdomsComponent implements OnInit {
ancientWisdoms: AncientWisdom[] = [];
constructor() { }
ngOnInit() {
this.ancientWisdoms = [
new PermanentUpgradeMoreXP,
new PermanentUpgradeMoreGold,
new PermanentUpgradeMoreDrops,
new PermanentUpgradeMoreMovementSpeed,
new PermanentUpgradeLessHP,
new PermanentUpgradeEnergy,
new PermanentUpgradeMoreEnemies,
new PermanentUpgradeLongerBuffs,
new PermanentUpgradeMoreMercenaries
];
}
}
<h1 class="mt-5">Ancient Wisdoms</h1>
<form>
<div *ngFor="let ancientWisdom of ancientWisdoms" class="form-group row">
<div class="col-3">
<label class="form-label">{{ancientWisdom.name}}</label>
</div>
<div class="col">
<input type="number" class="form-control" name="startValue" [(ngModel)]="ancientWisdom.startValue" />
</div>
<div class="col">
<input type="number" class="form-control" name="endValue" [(ngModel)]="ancientWisdom.endValue" />
</div>
<div class="col">
{{ancientWisdom.calculateCost()}}
</div>
</div>
</form>