Added encounters in locations (WIP - no fleet at the moment)
This commit is contained in:
parent
287805c8cc
commit
b1999822f0
|
@ -5,13 +5,17 @@ module SpaceTac {
|
|||
|
||||
// Router between game views
|
||||
export class GameUI extends Phaser.Game {
|
||||
// Currently playing universe
|
||||
universe: Game.Universe;
|
||||
// Current game session
|
||||
session: Game.GameSession;
|
||||
|
||||
// Current focused star system
|
||||
star: Game.Star;
|
||||
|
||||
constructor() {
|
||||
super(1280, 720, Phaser.AUTO, '-space-tac');
|
||||
|
||||
this.universe = null;
|
||||
this.session = new Game.GameSession();
|
||||
this.star = null;
|
||||
|
||||
this.state.add('boot', View.Boot);
|
||||
this.state.add('preload', View.Preload);
|
||||
|
@ -27,7 +31,7 @@ module SpaceTac {
|
|||
// Save current game in local browser storage
|
||||
saveGame(): boolean {
|
||||
if (typeof(Storage) !== "undefined") {
|
||||
localStorage.setItem("spacetac-savegame", this.universe.saveToString());
|
||||
localStorage.setItem("spacetac-savegame", this.session.saveToString());
|
||||
console.log("Game saved");
|
||||
return true;
|
||||
} else {
|
||||
|
@ -40,7 +44,7 @@ module SpaceTac {
|
|||
if (typeof(Storage) !== "undefined") {
|
||||
var loaded = localStorage.getItem("spacetac-savegame");
|
||||
if (loaded) {
|
||||
this.universe = Game.Universe.loadFromString(loaded);
|
||||
this.session = Game.GameSession.loadFromString(loaded);
|
||||
console.log("Game loaded");
|
||||
return true;
|
||||
} else {
|
||||
|
@ -51,5 +55,14 @@ module SpaceTac {
|
|||
console.error("localStorage not available");
|
||||
}
|
||||
}
|
||||
|
||||
// Get the focuses star system
|
||||
getFocusedStar(): Game.Star {
|
||||
if (this.star && this.star.universe === this.session.universe) {
|
||||
return this.star;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -234,7 +234,9 @@ module SpaceTac.Game {
|
|||
});
|
||||
|
||||
// Simulate game turn
|
||||
log.add(new ShipChangeEvent(this.playing_ship, this.playing_ship));
|
||||
if (this.playing_ship) {
|
||||
log.add(new ShipChangeEvent(this.playing_ship, this.playing_ship));
|
||||
}
|
||||
}
|
||||
|
||||
// Defines the initial ship positions for one fleet
|
||||
|
|
|
@ -31,6 +31,12 @@ module SpaceTac.Game {
|
|||
setLocation(location: StarLocation): void {
|
||||
this.location = location;
|
||||
this.player.setVisited(this.location.star);
|
||||
|
||||
// Check encounter
|
||||
var battle = this.location.enterLocation(this.player.fleet);
|
||||
if (battle) {
|
||||
this.player.setBattle(battle);
|
||||
}
|
||||
}
|
||||
|
||||
// Add a ship in this fleet
|
||||
|
|
58
src/app/game/GameSession.ts
Normal file
58
src/app/game/GameSession.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
/// <reference path="Serializable.ts"/>
|
||||
|
||||
module SpaceTac.Game {
|
||||
"use strict";
|
||||
|
||||
// A game session, binding a universe and a player
|
||||
export class GameSession extends Serializable {
|
||||
// Game universe
|
||||
universe: Universe;
|
||||
|
||||
// Current connected player
|
||||
player: Player;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.universe = new Universe();
|
||||
this.player = new Player(this.universe);
|
||||
}
|
||||
|
||||
// Load a game state from a string
|
||||
static loadFromString(serialized: string): GameSession {
|
||||
var serializer = new Serializer();
|
||||
return <GameSession>serializer.unserialize(serialized);
|
||||
}
|
||||
|
||||
// Serializes the game state to a string
|
||||
saveToString(): string {
|
||||
var serializer = new Serializer();
|
||||
return serializer.serialize(this);
|
||||
}
|
||||
|
||||
// Generate a real single player game
|
||||
startNewGame(): void {
|
||||
this.universe = new Universe();
|
||||
this.universe.generate();
|
||||
|
||||
var start_location = this.universe.stars[0].locations[0];
|
||||
start_location.encounter_gen = true;
|
||||
start_location.encounter = null;
|
||||
|
||||
this.player = new Game.Player(this.universe);
|
||||
this.player.fleet.setLocation(start_location);
|
||||
}
|
||||
|
||||
// Start a new "quick battle" game
|
||||
startQuickBattle(with_ai: boolean = false): void {
|
||||
var battle = Game.Battle.newQuickRandom(with_ai);
|
||||
this.player = battle.fleets[0].player;
|
||||
this.player.setBattle(battle);
|
||||
}
|
||||
|
||||
// Get currently played battle, null when none is in progress
|
||||
getBattle(): Battle {
|
||||
return this.player.getBattle();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,6 +5,9 @@ module SpaceTac.Game {
|
|||
|
||||
// One player (human or IA)
|
||||
export class Player extends Serializable {
|
||||
// Universe in which we are playing
|
||||
universe: Universe;
|
||||
|
||||
// Current fleet
|
||||
fleet: Fleet;
|
||||
|
||||
|
@ -15,9 +18,10 @@ module SpaceTac.Game {
|
|||
visited: Star[];
|
||||
|
||||
// Create a player, with an empty fleet
|
||||
constructor() {
|
||||
constructor(universe: Universe = new Universe()) {
|
||||
super();
|
||||
|
||||
this.universe = universe;
|
||||
this.fleet = new Fleet(this);
|
||||
this.ai = null;
|
||||
this.visited = [];
|
||||
|
@ -25,7 +29,7 @@ module SpaceTac.Game {
|
|||
|
||||
// Create a quick random player, with a fleet, for testing purposes
|
||||
static newQuickRandom(name: String): Player {
|
||||
var player = new Player();
|
||||
var player = new Player(new Universe());
|
||||
var ship: Ship;
|
||||
var ship_generator = new ShipGenerator();
|
||||
|
||||
|
@ -59,5 +63,13 @@ module SpaceTac.Game {
|
|||
this.visited.push(star);
|
||||
}
|
||||
}
|
||||
|
||||
// Get currently played battle, null when none is in progress
|
||||
getBattle(): Battle {
|
||||
return this.fleet.battle;
|
||||
}
|
||||
setBattle(battle: Battle): void {
|
||||
this.fleet.setBattle(battle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,6 +76,9 @@ module SpaceTac.Game {
|
|||
// List of points of interest
|
||||
locations: StarLocation[];
|
||||
|
||||
// Base level for encounters in this system
|
||||
level: number;
|
||||
|
||||
constructor(universe: Universe, x: number, y: number) {
|
||||
super();
|
||||
|
||||
|
@ -84,6 +87,7 @@ module SpaceTac.Game {
|
|||
this.y = y;
|
||||
this.radius = 0.1;
|
||||
this.locations = [];
|
||||
this.level = 1;
|
||||
}
|
||||
|
||||
// Get the distance to another star
|
||||
|
|
|
@ -26,6 +26,10 @@ module SpaceTac.Game {
|
|||
// Destination for jump, if its a WARP location
|
||||
jump_dest: StarLocation;
|
||||
|
||||
// Enemy encounter
|
||||
encounter: Fleet;
|
||||
encounter_gen: boolean;
|
||||
|
||||
constructor(star: Star, type: StarLocationType, x: number, y: number) {
|
||||
super();
|
||||
|
||||
|
@ -34,6 +38,9 @@ module SpaceTac.Game {
|
|||
this.x = x;
|
||||
this.y = y;
|
||||
this.jump_dest = null;
|
||||
|
||||
this.encounter = null;
|
||||
this.encounter_gen = false;
|
||||
}
|
||||
|
||||
// Set the jump destination of a WARP location
|
||||
|
@ -42,5 +49,33 @@ module SpaceTac.Game {
|
|||
this.jump_dest = jump_dest;
|
||||
}
|
||||
}
|
||||
|
||||
// Call this when first probing a location to generate the possible encounter
|
||||
// Returns the encountered fleet, null if no encounter happens
|
||||
tryGenerateEncounter(random: RandomGenerator = new RandomGenerator()): Fleet {
|
||||
if (!this.encounter_gen) {
|
||||
this.encounter_gen = true;
|
||||
|
||||
if (random.throw() < 0.8) {
|
||||
this.encounter = new Fleet();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
enterLocation(fleet: Fleet, random: RandomGenerator = new RandomGenerator()): Battle {
|
||||
var encounter = this.tryGenerateEncounter(random);
|
||||
if (encounter) {
|
||||
var battle = new Battle(fleet, encounter);
|
||||
battle.start();
|
||||
return battle;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,12 +5,6 @@ module SpaceTac.Game {
|
|||
|
||||
// Main game universe
|
||||
export class Universe extends Serializable {
|
||||
// Current connected player
|
||||
player: Player;
|
||||
|
||||
// Currently played battle
|
||||
battle: Battle;
|
||||
|
||||
// List of star systems
|
||||
stars: Star[];
|
||||
|
||||
|
@ -28,33 +22,6 @@ module SpaceTac.Game {
|
|||
this.radius = 50;
|
||||
}
|
||||
|
||||
// Load a game state from a string
|
||||
static loadFromString(serialized: string): Universe {
|
||||
var serializer = new Serializer();
|
||||
return <Universe>serializer.unserialize(serialized);
|
||||
}
|
||||
|
||||
// Generate a real single player game
|
||||
static newGame(): Universe {
|
||||
var universe = new Universe();
|
||||
universe.generate();
|
||||
universe.player = new Game.Player();
|
||||
universe.player.fleet.setLocation(universe.stars[0].locations[0]);
|
||||
return universe;
|
||||
}
|
||||
|
||||
// Start a new "quick battle" game
|
||||
startQuickBattle(with_ai: boolean = false): void {
|
||||
this.battle = Game.Battle.newQuickRandom(with_ai);
|
||||
this.player = this.battle.fleets[0].player;
|
||||
}
|
||||
|
||||
// Serializes the game state to a string
|
||||
saveToString(): string {
|
||||
var serializer = new Serializer();
|
||||
return serializer.serialize(this);
|
||||
}
|
||||
|
||||
// Generates a universe, with star systems and such
|
||||
generate(starcount: number = 50, random: RandomGenerator = new RandomGenerator()): void {
|
||||
this.stars = this.generateStars(starcount, random);
|
||||
|
|
44
src/app/game/specs/GameSession.spec.ts
Normal file
44
src/app/game/specs/GameSession.spec.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
/// <reference path="../../definitions/jasmine.d.ts"/>
|
||||
|
||||
module SpaceTac.Game.Specs {
|
||||
"use strict";
|
||||
|
||||
function applyGameSteps(session: GameSession): void {
|
||||
var battle = session.getBattle();
|
||||
battle.advanceToNextShip();
|
||||
// TODO Make some moves (IA?)
|
||||
battle.endBattle(battle.fleets[0]);
|
||||
}
|
||||
|
||||
describe("GameSession", () => {
|
||||
it("serializes to a string", () => {
|
||||
var session = new GameSession();
|
||||
session.startQuickBattle(false);
|
||||
|
||||
// Dump and reload
|
||||
var dumped = session.saveToString();
|
||||
var loaded_session = GameSession.loadFromString(dumped);
|
||||
|
||||
// Check equality
|
||||
expect(loaded_session).toEqual(session);
|
||||
|
||||
// Apply game steps
|
||||
applyGameSteps(session);
|
||||
applyGameSteps(loaded_session);
|
||||
|
||||
// Clean stored times as they might differ
|
||||
var clean = (session: GameSession) => {
|
||||
session.getBattle().fleets.forEach((fleet: Fleet) => {
|
||||
if (fleet.player.ai) {
|
||||
fleet.player.ai.started = 0;
|
||||
}
|
||||
});
|
||||
};
|
||||
clean(session);
|
||||
clean(loaded_session);
|
||||
|
||||
// Check equality after game steps
|
||||
expect(loaded_session).toEqual(session);
|
||||
});
|
||||
});
|
||||
}
|
|
@ -3,43 +3,7 @@
|
|||
module SpaceTac.Game.Specs {
|
||||
"use strict";
|
||||
|
||||
function applyGameSteps(universe: Universe): void {
|
||||
universe.battle.advanceToNextShip();
|
||||
// TODO Make some moves (IA?)
|
||||
universe.battle.endBattle(universe.battle.fleets[0]);
|
||||
}
|
||||
|
||||
describe("Universe", () => {
|
||||
it("serializes to a string", () => {
|
||||
var universe = new Universe();
|
||||
universe.startQuickBattle(false);
|
||||
|
||||
// Dump and reload
|
||||
var dumped = universe.saveToString();
|
||||
var loaded_universe = Universe.loadFromString(dumped);
|
||||
|
||||
// Check equality
|
||||
expect(loaded_universe).toEqual(universe);
|
||||
|
||||
// Apply game steps
|
||||
applyGameSteps(universe);
|
||||
applyGameSteps(loaded_universe);
|
||||
|
||||
// Clean stored times as they might differ
|
||||
var clean = (u: Universe) => {
|
||||
u.battle.fleets.forEach((fleet: Fleet) => {
|
||||
if (fleet.player.ai) {
|
||||
fleet.player.ai.started = 0;
|
||||
}
|
||||
});
|
||||
};
|
||||
clean(universe);
|
||||
clean(loaded_universe);
|
||||
|
||||
// Check equality after game steps
|
||||
expect(loaded_universe).toEqual(universe);
|
||||
});
|
||||
|
||||
it("generates star systems", () => {
|
||||
var universe = new Universe();
|
||||
var result = universe.generateStars(31);
|
||||
|
|
|
@ -29,7 +29,7 @@ module SpaceTac.View {
|
|||
onNewGame(): void {
|
||||
var gameui = <GameUI>this.game;
|
||||
|
||||
gameui.universe = Game.Universe.newGame();
|
||||
gameui.session.startNewGame();
|
||||
|
||||
this.game.state.start("router");
|
||||
}
|
||||
|
@ -38,8 +38,7 @@ module SpaceTac.View {
|
|||
onQuickBattle(): void {
|
||||
var gameui = <GameUI>this.game;
|
||||
|
||||
gameui.universe = new Game.Universe();
|
||||
gameui.universe.startQuickBattle(true);
|
||||
gameui.session.startQuickBattle(true);
|
||||
|
||||
this.game.state.start("router");
|
||||
}
|
||||
|
|
|
@ -4,17 +4,21 @@ module SpaceTac.View {
|
|||
// Router to other states
|
||||
export class Router extends Phaser.State {
|
||||
create() {
|
||||
var universe = (<GameUI>this.game).universe;
|
||||
var ui = <GameUI>this.game;
|
||||
var session = ui.session;
|
||||
|
||||
if (!universe) {
|
||||
if (!session) {
|
||||
// No universe, go back to main menu
|
||||
this.game.state.start("mainmenu", true, false);
|
||||
} else if (universe.battle) {
|
||||
} else if (session.getBattle()) {
|
||||
// A battle is raging, go to it
|
||||
this.game.state.start("battle", true, false, universe.player, universe.battle);
|
||||
this.game.state.start("battle", true, false, session.player, session.getBattle());
|
||||
} else if (ui.getFocusedStar()) {
|
||||
// Go to the focused star system
|
||||
this.game.state.start("starsystem", true, false, ui.star, session.player);
|
||||
} else {
|
||||
// Go to the universe map
|
||||
this.game.state.start("universe", true, false, universe, universe.player);
|
||||
this.game.state.start("universe", true, false, session.universe, session.player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,7 +85,12 @@ module SpaceTac.View {
|
|||
sprite.input.useHandCursor = true;
|
||||
sprite.onInputUp.addOnce(() => {
|
||||
this.player.fleet.setLocation(location);
|
||||
this.drawAll();
|
||||
|
||||
if (this.player.getBattle()) {
|
||||
this.game.state.start("router", true, false);
|
||||
} else {
|
||||
this.drawAll();
|
||||
}
|
||||
});
|
||||
sprite.scale.set(1.0 / this.scaling, 1.0 / this.scaling);
|
||||
sprite.anchor.set(0.5, 0.5);
|
||||
|
@ -95,13 +100,14 @@ module SpaceTac.View {
|
|||
|
||||
// Called when "Back" is clicked, go back to universe map
|
||||
onBackClicked(): void {
|
||||
this.game.state.start("universe", true, false, this.star.universe, this.player);
|
||||
(<GameUI>this.game).star = null;
|
||||
this.game.state.start("router", true, false);
|
||||
}
|
||||
|
||||
// Called when "jump" is clicked, initiate sector jump, and go back to universe map
|
||||
onJumpClicked(): void {
|
||||
this.player.fleet.jump();
|
||||
this.game.state.start("universe", true, false, this.star.universe, this.player);
|
||||
this.onBackClicked();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,7 +79,8 @@ module SpaceTac.View {
|
|||
sprite.anchor.set(0.5, 0.5);
|
||||
|
||||
sprite.onInputUp.add(() => {
|
||||
this.game.state.start("starsystem", true, false, star, this.player);
|
||||
(<GameUI>this.game).star = star;
|
||||
this.game.state.start("router", true, false);
|
||||
});
|
||||
sprite.input.useHandCursor = true;
|
||||
|
||||
|
|
Loading…
Reference in a new issue