1
0
Fork 0
spacetac/src/core/StarLocation.ts

142 lines
4.5 KiB
TypeScript
Raw Normal View History

2017-02-09 00:00:35 +00:00
module TS.SpaceTac {
2015-03-24 00:00:00 +00:00
export enum StarLocationType {
STAR,
WARP,
PLANET,
ASTEROID,
STATION
}
// Point of interest in a star system
export class StarLocation {
2015-03-24 00:00:00 +00:00
// Parent star system
star: Star;
// Type of location
type: StarLocationType;
// Location in the star system
x: number;
y: number;
// Absolute location in the universe
universe_x: number;
universe_y: number;
// Destination for jump, if its a WARP location
2017-03-09 17:11:00 +00:00
jump_dest: StarLocation | null;
// Enemy encounter
2017-03-14 22:28:07 +00:00
encounter: Fleet | null = null;
encounter_gen = false;
encounter_random = RandomGenerator.global;
2017-03-23 18:58:09 +00:00
// Shop to buy/sell equipment
shop: Shop | null = null;
2017-03-09 17:11:00 +00:00
constructor(star = new Star(), type: StarLocationType = StarLocationType.PLANET, x: number = 0, y: number = 0) {
this.star = star;
2015-03-24 00:00:00 +00:00
this.type = type;
this.x = x;
this.y = y;
2017-02-09 21:01:37 +00:00
this.universe_x = this.star.x + this.x;
this.universe_y = this.star.y + this.y;
this.jump_dest = null;
}
2017-03-23 18:58:09 +00:00
/**
* Add a shop in this location
*/
2017-05-10 15:29:10 +00:00
addShop(level = this.star.level) {
this.shop = new Shop(level);
2017-03-23 18:58:09 +00:00
}
/**
* Check if the location is clear of encounter
*/
isClear(): boolean {
return this.encounter_gen && this.encounter === null;
}
// Set the jump destination of a WARP location
setJumpDestination(jump_dest: StarLocation): void {
if (this.type === StarLocationType.WARP) {
this.jump_dest = jump_dest;
}
2015-03-24 00:00:00 +00:00
}
// Call this when first probing a location to generate the possible encounter
// Returns the encountered fleet, null if no encounter happens
2017-03-14 22:28:07 +00:00
tryGenerateEncounter(): Fleet | null {
if (!this.encounter_gen) {
this.encounter_gen = true;
2017-03-14 22:28:07 +00:00
if (this.encounter_random.random() < 0.8) {
2017-07-10 20:40:22 +00:00
this.setupEncounter();
}
}
return this.encounter;
}
// Call this when entering a location to generate the possible encounter
// *fleet* is the player fleet, entering the location
// Returns the engaged battle, null if no encounter happens
2017-03-14 22:28:07 +00:00
enterLocation(fleet: Fleet): Battle | null {
var encounter = this.tryGenerateEncounter();
if (encounter) {
var battle = new Battle(fleet, encounter);
battle.log.subscribe(event => {
if (event instanceof EndBattleEvent) {
if (!event.outcome.draw && event.outcome.winner !== encounter) {
// The encounter fleet lost, remove it
this.encounter = null;
}
}
});
battle.start();
return battle;
} else {
return null;
}
}
// Get the distance to another location
getDistanceTo(other: StarLocation): number {
var dx = this.x - other.x;
var dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* Clear an encounter, when the encountered fleet has been defeated
*/
clearEncounter() {
2017-03-23 18:58:09 +00:00
this.encounter_gen = true;
this.encounter = null;
}
2017-07-10 20:40:22 +00:00
/**
* Forces the setup of an encounter
*/
setupEncounter() {
this.encounter_gen = true;
let fleet_generator = new FleetGenerator(this.encounter_random);
let variations: [number, number][];
if (this.star.level == 1) {
variations = [[this.star.level, 2]];
} else if (this.star.level <= 3) {
variations = [[this.star.level, 2], [this.star.level - 1, 3]];
} else if (this.star.level <= 6) {
variations = [[this.star.level, 3], [this.star.level - 1, 4], [this.star.level + 1, 2]];
} else {
variations = [[this.star.level, 4], [this.star.level - 1, 5], [this.star.level + 1, 3], [this.star.level + 3, 2]];
2017-07-10 20:40:22 +00:00
}
let [level, enemies] = this.encounter_random.choice(variations);
this.encounter = fleet_generator.generate(level, new Player(this.star.universe, "Enemy"), enemies, true);
}
2015-03-24 00:00:00 +00:00
}
}