1
0
Fork 0

Join remote sessions in spectator mode (not fully working yet)

This commit is contained in:
Michaël Lemaire 2017-06-08 23:58:23 +02:00
parent c21d1d587b
commit ea13e25c5e
9 changed files with 75 additions and 15 deletions

View file

@ -33,6 +33,8 @@ module TS.SpaceTac {
this.session = new GameSession();
if (!headless) {
this.state.onStateChange.add((state: string) => console.log(`View change: ${state}`));
this.state.add('boot', UI.Boot);
this.state.add('preload', UI.Preload);
this.state.add('mainmenu', UI.MainMenu);

View file

@ -17,6 +17,12 @@ module TS.SpaceTac {
// Starting location
start_location: StarLocation
// Indicator that the session is the primary one
primary = true
// Indicator of spectator mode
spectator = false
constructor() {
this.id = RandomGenerator.global.id(20);
this.universe = new Universe();

View file

@ -129,11 +129,13 @@ module TS.SpaceTac.UI {
*/
autoSave(): void {
let session = this.gameui.session;
let connection = this.getConnection();
connection.publish(session, session.getDescription())
.then(() => this.messages.addMessage("Auto-saved to cloud"))
.catch(console.error)
//.catch(() => this.messages.addMessage("Error saving game to cloud"));
if (session.primary) {
let connection = this.getConnection();
connection.publish(session, session.getDescription())
.then(() => this.messages.addMessage("Auto-saved to cloud"))
.catch(console.error)
//.catch(() => this.messages.addMessage("Error saving game to cloud"));
}
}
/**

View file

@ -227,6 +227,10 @@ module TS.SpaceTac.UI {
// Enable or disable the global player interaction
// Disable interaction when it is the AI turn, or when the current ship can't play
setInteractionEnabled(enabled: boolean): void {
if (this.session.spectator) {
enabled = false;
}
this.action_bar.setInteractive(enabled);
this.exitTargettingMode();
this.interacting = enabled;

View file

@ -43,5 +43,12 @@ module TS.SpaceTac.UI {
getContent(): string {
return this.content.text;
}
/**
* Set the current text content
*/
setContent(content: string): void {
this.content.text = content.slice(0, this.maxlength);
}
}
}

View file

@ -11,6 +11,9 @@ module TS.SpaceTac.UI {
// Interacting player
player = new Player()
// Interaction enabled or not
interactive = true
// Layers
layer_universe: Phaser.Group
layer_overlay: Phaser.Group
@ -213,7 +216,7 @@ module TS.SpaceTac.UI {
*/
doJump(): void {
let location = this.player.fleet.location;
if (location && location.type == StarLocationType.WARP && location.jump_dest) {
if (this.interactive && location && location.type == StarLocationType.WARP && location.jump_dest) {
let dest_location = location.jump_dest;
let dest_star = dest_location.star;
this.player_fleet.moveToLocation(dest_location, 3, duration => {
@ -233,7 +236,7 @@ module TS.SpaceTac.UI {
*/
openShop(): void {
let location = this.player.fleet.location;
if (location && location.shop) {
if (this.interactive && location && location.shop) {
this.character_sheet.setShop(location.shop);
this.character_sheet.show(this.player.fleet.ships[0]);
}
@ -243,7 +246,7 @@ module TS.SpaceTac.UI {
* Move the fleet to another location
*/
moveToLocation(dest: StarLocation): void {
if (dest != this.player.fleet.location) {
if (this.interactive && dest != this.player.fleet.location) {
this.setInteractionEnabled(false);
this.player_fleet.moveToLocation(dest, 1, null, () => this.updateInfo(dest.star));
}
@ -253,6 +256,7 @@ module TS.SpaceTac.UI {
* Set the interactive state
*/
setInteractionEnabled(enabled: boolean) {
this.interactive = enabled && !this.session.spectator;
this.actions.setVisible(enabled && this.zoom == 2, 300);
this.animations.setVisible(this.zoom_in, enabled && this.zoom < 2, 300);
this.animations.setVisible(this.zoom_out, enabled && this.zoom > 0, 300);

View file

@ -0,0 +1,29 @@
/// <reference path="../TestGame.ts" />
/// <reference path="MainMenu.ts" />
module TS.SpaceTac.UI.Specs {
describe("LoadDialog", () => {
let testgame = setupSingleView(testgame => [new MainMenu(), []]);
it("joins remote sessions as spectator", function (done) {
let view = <MainMenu>testgame.ui.state.getCurrentState();
let session = new GameSession();
expect(session.primary).toBe(true);
expect(session.spectator).toBe(false);
view.getConnection().publish(session, "Test").then(token => {
let dialog = new LoadDialog(view);
dialog.token_input.setContent(token);
spyOn(view.gameui, "setSession").and.callFake((joined: GameSession) => {
expect(joined.id).toEqual(session.id);
expect(joined.primary).toBe(false);
expect(joined.spectator).toBe(true);
done();
});
dialog.join();
});
});
});
}

View file

@ -30,7 +30,7 @@ module TS.SpaceTac.UI {
/**
* Refresh available save games
*/
private refreshSaves(): void {
refreshSaves(): void {
let connection = this.view.getConnection();
// TODO include local save
@ -45,7 +45,7 @@ module TS.SpaceTac.UI {
/**
* Set the current selected save game
*/
private setCurrentSave(position: number): void {
setCurrentSave(position: number): void {
if (this.saves.length == 0) {
this.save_name.setContent("No save game found");
} else {
@ -59,19 +59,23 @@ module TS.SpaceTac.UI {
/**
* Change the selected save
*/
private paginateSave(offset: number) {
paginateSave(offset: number) {
this.setCurrentSave(this.save_selected + offset);
}
/**
* Join an online game
*/
private join(): void {
join(): void {
let token = this.token_input.getContent();
let connection = this.view.getConnection();
connection.loadByToken(token).then(session => {
if (session) {
// For now, we will only spectate
session.primary = false;
session.spectator = true;
this.view.gameui.setSession(session);
}
});
@ -80,7 +84,7 @@ module TS.SpaceTac.UI {
/**
* Load selected save game
*/
private load(): void {
load(): void {
if (this.save_selected >= 0 && this.saves.length > this.save_selected) {
let connection = this.view.getConnection();
let [saveid, saveinfo] = this.saves[this.save_selected];

View file

@ -42,8 +42,10 @@ module TS.SpaceTac.UI {
pageMenu() {
this.pageCommon();
this.addButton(this.width / 2, 600, () => this.pageInvite(), "options-button");
this.addText(this.width / 2, 600, "Invite a friend", "#5398e9", 36, true, true);
if (this.view.session.primary) {
this.addButton(this.width / 2, 600, () => this.pageInvite(), "options-button");
this.addText(this.width / 2, 600, "Invite a friend", "#5398e9", 36, true, true);
}
this.addButton(this.width / 2, 800, () => this.view.gameui.quitGame(), "options-button");
this.addText(this.width / 2, 800, "Quit to menu", "#5398e9", 36, true, true);