Added effect generation from templates
This commit is contained in:
parent
5f1913da6d
commit
d1222ef26a
47
src/scripts/game/EffectTemplate.ts
Normal file
47
src/scripts/game/EffectTemplate.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
module SpaceTac.Game {
|
||||
"use strict";
|
||||
|
||||
// Modifier for a value of a BaseEffect subclass
|
||||
export class EffectTemplateModifier {
|
||||
// Value name
|
||||
name: string;
|
||||
|
||||
// Range of values (similar to ranges in LootTemplate)
|
||||
range: Range;
|
||||
|
||||
// Basic constructor
|
||||
constructor(name: string, range: Range) {
|
||||
this.name = name;
|
||||
this.range = range;
|
||||
}
|
||||
}
|
||||
|
||||
// Template used to generate a BaseEffect
|
||||
export class EffectTemplate {
|
||||
// Basic instance of the effect
|
||||
effect: BaseEffect;
|
||||
|
||||
// Effect value modifiers
|
||||
modifiers: EffectTemplateModifier[];
|
||||
|
||||
// Basic constructor
|
||||
constructor(effect: BaseEffect) {
|
||||
this.effect = effect;
|
||||
this.modifiers = [];
|
||||
}
|
||||
|
||||
// Add a value modifier for the effect
|
||||
addModifier(name: string, range: Range) {
|
||||
this.modifiers.push(new EffectTemplateModifier(name, range));
|
||||
}
|
||||
|
||||
// Generate an effect with a given power
|
||||
generateFixed(power: number): BaseEffect {
|
||||
var effect = Tools.copyObject(this.effect);
|
||||
this.modifiers.forEach((modifier: EffectTemplateModifier) => {
|
||||
effect[modifier.name] = modifier.range.getProportional(power);
|
||||
});
|
||||
return effect;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -23,6 +23,7 @@ module SpaceTac.Game {
|
|||
duration: IntegerRange;
|
||||
|
||||
// Effects
|
||||
permanent_effects: EffectTemplate[];
|
||||
|
||||
// Action Points usage
|
||||
ap_usage: Range;
|
||||
|
@ -39,6 +40,7 @@ module SpaceTac.Game {
|
|||
this.duration = new IntegerRange(0, 0);
|
||||
this.ap_usage = new Range(0, 0);
|
||||
this.min_level = new IntegerRange(0, 0);
|
||||
this.permanent_effects = [];
|
||||
}
|
||||
|
||||
// Generate a random equipment with this template
|
||||
|
@ -63,6 +65,10 @@ module SpaceTac.Game {
|
|||
|
||||
result.action = this.getActionForEquipment(result);
|
||||
|
||||
this.permanent_effects.forEach((eff_template: EffectTemplate) => {
|
||||
result.permanent_effects.push(eff_template.generateFixed(power));
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -103,6 +109,13 @@ module SpaceTac.Game {
|
|||
}
|
||||
}
|
||||
|
||||
// Convenience function to add a permanent attribute max effect on equipment
|
||||
addPermanentAttributeMaxEffect(code: AttributeCode, min: number, max: number = null): void {
|
||||
var template = new EffectTemplate(new AttributeMaxEffect(code, 0));
|
||||
template.addModifier("value", new Range(min, max));
|
||||
this.permanent_effects.push(template);
|
||||
}
|
||||
|
||||
// Method to reimplement to assign an action to a generated equipment
|
||||
protected getActionForEquipment(equipment: Equipment): BaseAction {
|
||||
return null;
|
||||
|
|
|
@ -10,10 +10,14 @@ module SpaceTac.Game {
|
|||
max: number;
|
||||
|
||||
// Create a range of values
|
||||
constructor(min: number, max: number) {
|
||||
constructor(min: number, max: number = null) {
|
||||
this.min = min;
|
||||
if (max === null) {
|
||||
this.max = this.min;
|
||||
} else {
|
||||
this.max = max;
|
||||
}
|
||||
}
|
||||
|
||||
// Get a proportional value (give 0.0-1.0 value to obtain a value in range)
|
||||
getProportional(cursor: number): number {
|
||||
|
|
|
@ -230,7 +230,6 @@ module SpaceTac.Game {
|
|||
Attribute.forEachCode((code: AttributeCode) => {
|
||||
old_attrs.setMaximum(code, new_attrs.getValue(code));
|
||||
});
|
||||
console.log(old_attrs, new_attrs);
|
||||
}
|
||||
|
||||
// Collect all effects to apply for updateAttributes
|
||||
|
|
|
@ -31,6 +31,10 @@ module SpaceTac.Game {
|
|||
// Attach an equipment in this slot
|
||||
attach(equipment: Equipment): void {
|
||||
this.attached = equipment;
|
||||
|
||||
if (this.ship) {
|
||||
this.ship.updateAttributes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
18
src/scripts/game/Tools.ts
Normal file
18
src/scripts/game/Tools.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
module SpaceTac.Game {
|
||||
"use strict";
|
||||
|
||||
// Generic tools functions
|
||||
export class Tools {
|
||||
static copyObject<T> (object: T): T {
|
||||
var objectCopy = <T>{};
|
||||
|
||||
for (var key in object) {
|
||||
if (object.hasOwnProperty(key)) {
|
||||
objectCopy[key] = object[key];
|
||||
}
|
||||
}
|
||||
|
||||
return objectCopy;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ module SpaceTac.Game.Equipments {
|
|||
super(SlotType.Power, "Basic Power Core");
|
||||
|
||||
this.min_level = new IntegerRange(1, 1);
|
||||
this.addPermanentAttributeMaxEffect(AttributeCode.AP, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
26
src/scripts/game/specs/EffectTemplate.spec.ts
Normal file
26
src/scripts/game/specs/EffectTemplate.spec.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
/// <reference path="../../definitions/jasmine.d.ts"/>
|
||||
|
||||
module SpaceTac.Game.Specs {
|
||||
"use strict";
|
||||
|
||||
describe("EffectTemplate", () => {
|
||||
it("interpolates between weak and strong effects", () => {
|
||||
var base_effect = new AttributeMaxEffect(AttributeCode.Hull, 6);
|
||||
var template = new EffectTemplate(base_effect);
|
||||
|
||||
template.addModifier("value", new Range(2, 8));
|
||||
|
||||
var effect = <AttributeMaxEffect>template.generateFixed(0.0);
|
||||
expect(effect.code).toEqual("attrmax");
|
||||
expect(effect.value).toEqual(2);
|
||||
|
||||
effect = <AttributeMaxEffect>template.generateFixed(1.0);
|
||||
expect(effect.code).toEqual("attrmax");
|
||||
expect(effect.value).toEqual(8);
|
||||
|
||||
effect = <AttributeMaxEffect>template.generateFixed(0.5);
|
||||
expect(effect.code).toEqual("attrmax");
|
||||
expect(effect.value).toEqual(5);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue