Added ability to travel, and jump between stars
This commit is contained in:
parent
b76a83c157
commit
4298222637
|
@ -70,5 +70,15 @@ module SpaceTac.Game {
|
|||
});
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
// Use the current warp location to make a jump to another star
|
||||
jump(): boolean {
|
||||
if (this.location && this.location.type === StarLocationType.WARP && this.location.jump_dest) {
|
||||
this.player.fleet.setLocation(this.player.fleet.location.jump_dest);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -110,7 +110,17 @@ module SpaceTac.Game {
|
|||
// Add warp locations around the star
|
||||
var links = this.getLinks();
|
||||
links.forEach((link: StarLink) => {
|
||||
result.push(this.generateOneLocation(StarLocationType.WARP, result, this.radius * 0.3, random));
|
||||
var warp = this.generateOneLocation(StarLocationType.WARP, result, this.radius * 0.3, random);
|
||||
|
||||
// If there is an unbound warp location on destination sector, bind with it
|
||||
var peer_star = link.getPeer(this);
|
||||
var peer_location = peer_star.findUnboundWarp();
|
||||
if (peer_location) {
|
||||
peer_location.setJumpDestination(warp);
|
||||
warp.setJumpDestination(peer_location);
|
||||
}
|
||||
|
||||
result.push(warp);
|
||||
});
|
||||
|
||||
// Add random locations
|
||||
|
@ -134,6 +144,17 @@ module SpaceTac.Game {
|
|||
return result;
|
||||
}
|
||||
|
||||
// Find an unbound warp location to bind, null if none found
|
||||
findUnboundWarp(): StarLocation {
|
||||
var result: StarLocation = null;
|
||||
this.locations.forEach((location: StarLocation) => {
|
||||
if (location.type === StarLocationType.WARP && !location.jump_dest) {
|
||||
result = location;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
private generateOneLocation(type: StarLocationType, others: StarLocation[], radius: number, random: RandomGenerator): StarLocation {
|
||||
var x = (random.throw(2) - 1) * radius;
|
||||
var y = (random.throw(2) - 1) * radius;
|
||||
|
|
|
@ -40,5 +40,16 @@ module SpaceTac.Game {
|
|||
var cc4 = ccw(this.first, this.second, other.second);
|
||||
return cc1 !== cc2 && cc3 !== cc4;
|
||||
}
|
||||
|
||||
// Get the other side of the link, for a given side
|
||||
getPeer(star: Star): Star {
|
||||
if (star === this.first) {
|
||||
return this.second;
|
||||
} else if (star === this.second) {
|
||||
return this.first;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,9 @@ module SpaceTac.Game {
|
|||
x: number;
|
||||
y: number;
|
||||
|
||||
// Destination for jump, if its a WARP location
|
||||
jump_dest: StarLocation;
|
||||
|
||||
constructor(star: Star, type: StarLocationType, x: number, y: number) {
|
||||
super();
|
||||
|
||||
|
@ -30,6 +33,14 @@ module SpaceTac.Game {
|
|||
this.type = type;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.jump_dest = null;
|
||||
}
|
||||
|
||||
// Set the jump destination of a WARP location
|
||||
setJumpDestination(jump_dest: StarLocation): void {
|
||||
if (this.type === StarLocationType.WARP) {
|
||||
this.jump_dest = jump_dest;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,5 +18,53 @@ module SpaceTac.Game.Specs {
|
|||
expect(result[0]).toEqual(new StarLink(universe.stars[0], universe.stars[1]));
|
||||
expect(result[1]).toEqual(new StarLink(universe.stars[0], universe.stars[3]));
|
||||
});
|
||||
|
||||
it("generates warp locations", () => {
|
||||
var universe = new Universe();
|
||||
universe.stars.push(new Star(universe, 0, 0));
|
||||
universe.stars.push(new Star(universe, 1, 0));
|
||||
universe.stars.push(new Star(universe, 1, 1));
|
||||
universe.addLink(universe.stars[0], universe.stars[1]);
|
||||
universe.addLink(universe.stars[0], universe.stars[2]);
|
||||
|
||||
var getWarps = (star: Star): StarLocation[] => {
|
||||
var result: StarLocation[] = [];
|
||||
star.locations.forEach((location: StarLocation) => {
|
||||
if (location.type === StarLocationType.WARP) {
|
||||
result.push(location);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
expect(getWarps(universe.stars[0]).length).toBe(0);
|
||||
expect(getWarps(universe.stars[1]).length).toBe(0);
|
||||
expect(getWarps(universe.stars[2]).length).toBe(0);
|
||||
|
||||
universe.stars[0].generate();
|
||||
|
||||
expect(getWarps(universe.stars[0]).length).toBe(2);
|
||||
expect(getWarps(universe.stars[1]).length).toBe(0);
|
||||
expect(getWarps(universe.stars[2]).length).toBe(0);
|
||||
|
||||
universe.stars[1].generate();
|
||||
|
||||
expect(getWarps(universe.stars[0]).length).toBe(2);
|
||||
expect(getWarps(universe.stars[1]).length).toBe(1);
|
||||
expect(getWarps(universe.stars[2]).length).toBe(0);
|
||||
|
||||
universe.stars[2].generate();
|
||||
|
||||
var warps = getWarps(universe.stars[0]);
|
||||
expect(warps.length).toBe(2);
|
||||
expect(warps[0].jump_dest).toBe(universe.stars[2].locations[1]);
|
||||
expect(warps[1].jump_dest).toBe(universe.stars[1].locations[1]);
|
||||
warps = getWarps(universe.stars[1]);
|
||||
expect(warps.length).toBe(1);
|
||||
expect(warps[0].jump_dest).toBe(universe.stars[0].locations[2]);
|
||||
warps = getWarps(universe.stars[2]);
|
||||
expect(warps.length).toBe(1);
|
||||
expect(warps[0].jump_dest).toBe(universe.stars[0].locations[1]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -27,5 +27,16 @@ module SpaceTac.Game.Specs {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("gets the peer of a given sector", () => {
|
||||
var star1 = new Star(null, 0, 0);
|
||||
var star2 = new Star(null, 0, 1);
|
||||
var star3 = new Star(null, 0, 1);
|
||||
var link1 = new StarLink(star1, star2);
|
||||
|
||||
expect(link1.getPeer(star1)).toBe(star2);
|
||||
expect(link1.getPeer(star2)).toBe(star1);
|
||||
expect(link1.getPeer(star3)).toBeNull();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -58,6 +58,7 @@ module SpaceTac.View {
|
|||
this.loadImage("map/planet-icon.png");
|
||||
this.loadImage("map/warp-icon.png");
|
||||
this.loadImage("map/button-back.png");
|
||||
this.loadImage("map/button-jump.png");
|
||||
|
||||
// Load sounds
|
||||
this.loadSound("battle/ship-change.wav");
|
||||
|
|
|
@ -16,6 +16,9 @@ module SpaceTac.View {
|
|||
// Scaling used to transform game coordinates in screen ones
|
||||
private scaling: number;
|
||||
|
||||
// Buttons
|
||||
private button_back: Phaser.Button;
|
||||
private button_jump: Phaser.Button;
|
||||
|
||||
// Init the view, binding it to a universe
|
||||
init(star: Game.Star, player: Game.Player) {
|
||||
|
@ -31,17 +34,14 @@ module SpaceTac.View {
|
|||
this.locations.position.set(this.star.radius * this.scaling, this.star.radius * this.scaling);
|
||||
this.locations.scale.set(this.scaling);
|
||||
|
||||
this.drawLocations();
|
||||
// Buttons
|
||||
this.button_back = this.add.button(0, 0, "map-button-back", this.onBackClicked, this);
|
||||
this.button_back.input.useHandCursor = true;
|
||||
this.button_jump = this.add.button(150, 0, "map-button-jump", this.onJumpClicked, this);
|
||||
this.button_jump.input.useHandCursor = true;
|
||||
this.button_jump.visible = false;
|
||||
|
||||
// Draw fleet location
|
||||
var location = this.player.fleet.location;
|
||||
var fleet = this.add.sprite(location.x, location.y, "map-fleet-icon", 0, this.locations);
|
||||
fleet.scale.set(1.0 / this.scaling, 1.0 / this.scaling);
|
||||
fleet.anchor.set(0.5, -0.5);
|
||||
this.game.tweens.create(fleet).to({angle: -360}, 5000, undefined, true, 0, -1);
|
||||
|
||||
// Back button
|
||||
this.add.button(0, 0, "map-button-back", this.onBackClicked, this).input.useHandCursor = true;
|
||||
this.drawAll();
|
||||
}
|
||||
|
||||
// Leaving the view, unbind and destroy
|
||||
|
@ -50,14 +50,37 @@ module SpaceTac.View {
|
|||
this.player = null;
|
||||
}
|
||||
|
||||
// Redraw the view
|
||||
drawAll(): void {
|
||||
this.locations.removeAll(true, true);
|
||||
|
||||
// Draw location icons
|
||||
this.drawLocations();
|
||||
|
||||
// Draw fleet
|
||||
var location = this.player.fleet.location;
|
||||
var fleet = this.add.sprite(location.x, location.y, "map-fleet-icon", 0, this.locations);
|
||||
fleet.scale.set(1.0 / this.scaling, 1.0 / this.scaling);
|
||||
fleet.anchor.set(0.5, -0.5);
|
||||
this.game.tweens.create(fleet).to({angle: -360}, 5000, undefined, true, 0, -1);
|
||||
|
||||
// Buttons
|
||||
this.button_jump.visible = this.player.fleet.location.jump_dest !== null;
|
||||
}
|
||||
|
||||
// Redraw the locations map
|
||||
drawLocations(): void {
|
||||
this.locations.removeAll(true, true);
|
||||
this.star.locations.forEach((location: Game.StarLocation) => {
|
||||
var key = "map-" + Game.StarLocationType[location.type].toLowerCase() + "-icon";
|
||||
var sprite = this.add.sprite(location.x, location.y, key, 0, this.locations);
|
||||
var sprite = this.add.button(location.x, location.y, key);
|
||||
sprite.input.useHandCursor = true;
|
||||
sprite.onInputUp.addOnce(() => {
|
||||
this.player.fleet.setLocation(location);
|
||||
this.drawAll();
|
||||
});
|
||||
sprite.scale.set(1.0 / this.scaling, 1.0 / this.scaling);
|
||||
sprite.anchor.set(0.5, 0.5);
|
||||
this.locations.addChild(sprite);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -65,5 +88,11 @@ module SpaceTac.View {
|
|||
onBackClicked(): void {
|
||||
this.game.state.start("universe", true, false, this.star.universe, this.player);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
BIN
src/assets/images/map/button-jump.png
Normal file
BIN
src/assets/images/map/button-jump.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.6 KiB |
Loading…
Reference in a new issue