From 0f45244a8ea1be7d16d02d77079a845cdeab9589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Wed, 14 Jan 2015 01:00:00 +0100 Subject: [PATCH] Added the first weapon (Gatling Gun), using LootGenerator, LootTemplate and the slots system --- src/scripts/game/LootGenerator.ts | 14 ++++++-- src/scripts/game/LootTemplate.ts | 1 - src/scripts/game/Player.ts | 21 +++++++++--- src/scripts/game/Ship.ts | 9 +++++- src/scripts/game/ShipGenerator.ts | 39 +++++++++++++++++++++++ src/scripts/game/Slot.ts | 14 +++++++- src/scripts/game/equipments/GatlingGun.ts | 16 ++++++++++ 7 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 src/scripts/game/ShipGenerator.ts create mode 100644 src/scripts/game/equipments/GatlingGun.ts diff --git a/src/scripts/game/LootGenerator.ts b/src/scripts/game/LootGenerator.ts index 5722fcc..2a4e9e9 100644 --- a/src/scripts/game/LootGenerator.ts +++ b/src/scripts/game/LootGenerator.ts @@ -11,16 +11,24 @@ module SpaceTac.Game { // Construct a basic loot generator // The list of templates will be automatically populated - constructor() { + constructor(random: RandomGenerator = null) { this.templates = []; - this.random = new RandomGenerator(); + this.random = random || new RandomGenerator(); this.populate(); } // Fill the list of templates populate(): void { - + var templates: LootTemplate[] = []; + for (var template_name in SpaceTac.Game.Equipments) { + if (template_name) { + var template_class = SpaceTac.Game.Equipments[template_name]; + var template: LootTemplate = new template_class(); + templates.push(template); + } + } + this.templates = templates; } // Generate a random equipment diff --git a/src/scripts/game/LootTemplate.ts b/src/scripts/game/LootTemplate.ts index 19c2f7e..fb3e1fb 100644 --- a/src/scripts/game/LootTemplate.ts +++ b/src/scripts/game/LootTemplate.ts @@ -93,7 +93,6 @@ module SpaceTac.Game { random = random || new RandomGenerator(); var random_range = this.getPowerRangeForLevel(level); - console.log(level, random_range); if (random_range) { var power = random.throw() * (random_range.max - random_range.min) + random_range.min; return this.generateFixed(power); diff --git a/src/scripts/game/Player.ts b/src/scripts/game/Player.ts index 4ddcf7a..7edf8d3 100644 --- a/src/scripts/game/Player.ts +++ b/src/scripts/game/Player.ts @@ -14,11 +14,24 @@ module SpaceTac.Game { // Create a quick random player, with a fleet, for testing purposes static newQuickRandom(name: String): Player { var player = new Player(); + var ship: Ship; + var ship_generator = new ShipGenerator(); - new Ship(player.fleet, name + "'s First"); - new Ship(player.fleet, name + "'s Second"); - new Ship(player.fleet, name + "'s Third"); - new Ship(player.fleet, name + "'s Fourth"); + ship = ship_generator.generate(1); + ship.name = name + "'s First"; + player.fleet.addShip(ship); + + ship = ship_generator.generate(1); + ship.name = name + "'s Second"; + player.fleet.addShip(ship); + + ship = ship_generator.generate(1); + ship.name = name + "'s Third"; + player.fleet.addShip(ship); + + ship = ship_generator.generate(1); + ship.name = name + "'s Fourth"; + player.fleet.addShip(ship); return player; } diff --git a/src/scripts/game/Ship.ts b/src/scripts/game/Ship.ts index 284fa25..05afde0 100644 --- a/src/scripts/game/Ship.ts +++ b/src/scripts/game/Ship.ts @@ -47,7 +47,7 @@ module SpaceTac.Game { slots: Slot[]; // Create a new ship inside a fleet - constructor(fleet: Fleet, name: string) { + constructor(fleet: Fleet = null, name: string = null) { this.fleet = fleet; this.name = name; this.initiative_level = 1; @@ -131,5 +131,12 @@ module SpaceTac.Game { this.setArenaPosition(this.arena_x + dx, this.arena_y + dy); this.useActionPoints(cost); } + + // Add an empty equipment slot of the given type + addSlot(type: SlotType): Slot { + var result = new Slot(this, type); + this.slots.push(result); + return result; + } } } diff --git a/src/scripts/game/ShipGenerator.ts b/src/scripts/game/ShipGenerator.ts new file mode 100644 index 0000000..b223d9f --- /dev/null +++ b/src/scripts/game/ShipGenerator.ts @@ -0,0 +1,39 @@ +module SpaceTac.Game { + "use strict"; + + // Generator of random ship + export class ShipGenerator { + // Random number generator used + random: RandomGenerator; + + // Create a default ship generator + constructor(random: RandomGenerator = null) { + this.random = random || new RandomGenerator(); + } + + // Generate a ship of a given level + // The ship will not be named, nor will be a member of any fleet + generate(level: number): Ship { + var result = new Ship(); + var loot = new LootGenerator(this.random); + + // Set basic info + result.level = level; + + // Add equipment slots + result.addSlot(SlotType.Armor); + result.addSlot(SlotType.Engine); + result.addSlot(SlotType.Power); + result.addSlot(SlotType.Shield); + result.addSlot(SlotType.Weapon); + + // Fill equipment slots + result.slots.forEach((slot: Slot) => { + var equipment = loot.generate(new IntegerRange(level, level), slot.type); + slot.attach(equipment); + }); + + return result; + } + } +} diff --git a/src/scripts/game/Slot.ts b/src/scripts/game/Slot.ts index ff6799d..2462c28 100644 --- a/src/scripts/game/Slot.ts +++ b/src/scripts/game/Slot.ts @@ -11,8 +11,20 @@ module SpaceTac.Game { // Type of slot type: SlotType; - // Currently attached equipment + // Currently attached equipment, null if none attached: Equipment; + + // Create an empty slot for a ship + constructor(ship: Ship, type: SlotType) { + this.ship = ship; + this.type = type; + this.attached = null; + } + + // Attach an equipment in this slot + attach(equipment: Equipment): void { + this.attached = equipment; + } } } diff --git a/src/scripts/game/equipments/GatlingGun.ts b/src/scripts/game/equipments/GatlingGun.ts new file mode 100644 index 0000000..afa8adb --- /dev/null +++ b/src/scripts/game/equipments/GatlingGun.ts @@ -0,0 +1,16 @@ +/// + +module SpaceTac.Game.Equipments { + "use strict"; + + // Equipment: Gatling Gun + export class GatlingGun extends LootTemplate { + constructor() { + super(SlotType.Weapon, "Gatling Gun"); + + this.distance = new Range(20, 30); + this.ap_usage = new Range(3, 4); + this.min_level = new IntegerRange(1, 3); + } + } +}