diff --git a/src/scripts/game/EffectTemplate.ts b/src/scripts/game/EffectTemplate.ts new file mode 100644 index 0000000..79a532e --- /dev/null +++ b/src/scripts/game/EffectTemplate.ts @@ -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; + } + } +} diff --git a/src/scripts/game/LootTemplate.ts b/src/scripts/game/LootTemplate.ts index f68159c..94d18e9 100644 --- a/src/scripts/game/LootTemplate.ts +++ b/src/scripts/game/LootTemplate.ts @@ -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; diff --git a/src/scripts/game/Range.ts b/src/scripts/game/Range.ts index 4a86c1a..f024e70 100644 --- a/src/scripts/game/Range.ts +++ b/src/scripts/game/Range.ts @@ -10,9 +10,13 @@ module SpaceTac.Game { max: number; // Create a range of values - constructor(min: number, max: number) { + constructor(min: number, max: number = null) { this.min = min; - this.max = max; + 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) diff --git a/src/scripts/game/Ship.ts b/src/scripts/game/Ship.ts index f181328..8fc833c 100644 --- a/src/scripts/game/Ship.ts +++ b/src/scripts/game/Ship.ts @@ -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 diff --git a/src/scripts/game/Slot.ts b/src/scripts/game/Slot.ts index 7c53aa8..a504405 100644 --- a/src/scripts/game/Slot.ts +++ b/src/scripts/game/Slot.ts @@ -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(); + } } } diff --git a/src/scripts/game/Tools.ts b/src/scripts/game/Tools.ts new file mode 100644 index 0000000..51f97e0 --- /dev/null +++ b/src/scripts/game/Tools.ts @@ -0,0 +1,18 @@ +module SpaceTac.Game { + "use strict"; + + // Generic tools functions + export class Tools { + static copyObject (object: T): T { + var objectCopy = {}; + + for (var key in object) { + if (object.hasOwnProperty(key)) { + objectCopy[key] = object[key]; + } + } + + return objectCopy; + } + } +} diff --git a/src/scripts/game/equipments/BasicPowerCore.ts b/src/scripts/game/equipments/BasicPowerCore.ts index e5ca068..e64684d 100644 --- a/src/scripts/game/equipments/BasicPowerCore.ts +++ b/src/scripts/game/equipments/BasicPowerCore.ts @@ -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); } } } diff --git a/src/scripts/game/specs/EffectTemplate.spec.ts b/src/scripts/game/specs/EffectTemplate.spec.ts new file mode 100644 index 0000000..1f01ef0 --- /dev/null +++ b/src/scripts/game/specs/EffectTemplate.spec.ts @@ -0,0 +1,26 @@ +/// + +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 = template.generateFixed(0.0); + expect(effect.code).toEqual("attrmax"); + expect(effect.value).toEqual(2); + + effect = template.generateFixed(1.0); + expect(effect.code).toEqual("attrmax"); + expect(effect.value).toEqual(8); + + effect = template.generateFixed(0.5); + expect(effect.code).toEqual("attrmax"); + expect(effect.value).toEqual(5); + }); + }); +}