"Space Exploration" (in Snowman 1.3)
This <em>Space Exploration</em> example borrows heavily from games like <em>FTL</em> (2012) where randomly generated obstacles prevent the player from her goal.
<h3>Narrative Premise</h3>
Your ship has taken damage while in a hazardous area of space. With the navigation system broken, you know that it will take 15 hyperspace jumps to make it out of the area and back into safety.
<h3>Game Rules</h3>
Each hyperspace jump takes 1 unit of fuel.
Because the navigation system is broken, each jump will land you in a random system of 1-4 planets. Those marked in RED are high-risk, high-reward. Those marked in GREEN are low-risk, low-reward. (The chance of RED or GREEN is 50%.)
Starting with 20 health and 2 units of fuel for your ship, explore space, planets you find, and try to make your way across the hazardous area of space.
[[Programming the rules]]<script>
// Borrow technique from SugarCube to make a global "variables"
story.state.variables = {};
var _vars = story.state.variables;
_vars.health = 20;
_vars.fuel = 3;
_vars.system = [];
_vars.numberOfJumpsLeft = 15;
// Create global functions
story.state.functions = {};
var _functions = story.state.functions;
_functions.redOutcome = function() {
var _vars = story.state.variables;
var _percentage = _.random(1, 10);
var response = "";
if( _percentage >= 6) {
var _foundHealth = _.random( 1, 5);
var _foundFuel = _.random( 1, 3);
response = "The hostile environment damaged the ship, but extra fuel was found. -" + _foundHealth + " to health and +" + _foundFuel + " to fuel.";
_vars.health -= _foundHealth;
_vars.fuel += _foundFuel;
} else {
if( _percentage <= 3) {
var _foundHealth = _.random(2, 7);
response = "A hostile ship attacked. -" + _foundHealth + " to health";
_vars.health -= _foundHealth;
} else {
response = "Nothing happened."
}
}
return response;
};
_functions.greenOutcome = function() {
var _vars = story.state.variables;
var _percentage = _.random(1, 10);
var response = "";
if( _percentage == 1) {
var _foundFuel = _.random( 1, 2);
response = "Fuel was found in some wreckage. + " + _foundFuel + "to fuel";
_vars.fuel += _foundFuel;
} else {
if( _percentage >= 6) {
var _foundHealth = _.random(1, 3);
response = "During a brief pause, the ship was able to be repaired. +" + _foundHealth + " to health";
_vars.health += _foundHealth;
} else {
response = "Nothing happened."
}
}
return response;
};
</script>
[[Explore Space 1]]<div class="gameScreen">
<% var _vars = story.state.variables; %>
<% _vars.fuel-- %>
<% _vars.numberOfJumpsLeft-- %>
[[ Hyperjump |Explore Space 2]]
<div id="HUD">
<%= window.story.render("HUD") %>
</div>
<%= window.story.render("Generate System") %>
<div id="display"></div>
<%= window.story.render("Display System") %>
<%= window.story.render("Check Status") %>
</div><div class="gameScreen">
<% var _vars = story.state.variables; %>
<% _vars.fuel-- %>
<% _vars.numberOfJumpsLeft-- %>
[[ Hyperjump |Explore Space 1]]
<div id="HUD">
<%= window.story.render("HUD") %>
</div>
<%= window.story.render("Generate System") %>
<div id="display"></div>
<%= window.story.render("Display System") %>
<%= window.story.render("Check Status") %>
</div><script>
var _vars = story.state.variables;
var planets =_.random(1, 4);
_vars.system = new Array(planets);
for(var i = 0; i < _vars.system.length; i++) {
_vars.system[i] = _.sample(["RED", "GREEN"]);
}
</script>
<script>
var _vars = story.state.variables;
// Wipe out current contents
$("#display").html("");
for(var i = 0; i < _vars.system.length; i++) {
if(_vars.system[i] == "RED") {
var link = $("<a href='#'>RED</a>")
.click(function(e) {
$( this )
.replaceWith( window.story.render("Show Outcome - Red") );
return false;
});
$("#display").append( link );
$("#display").append( "<br>" );
}
if(_vars.system[i] == "GREEN") {
var link = $("<a href='#'>GREEN</a>")
.click(function(e) {
$( this )
.replaceWith( window.story.render("Show Outcome - Green") );
return false;
});
$("#display").append( link );
$("#display").append( "<br>" );
}
}
</script><%= story.state.functions.redOutcome() %>
<% $("#HUD").html(window.story.render("HUD")) %>
<%= window.story.render("Check Status") %>
<%= story.state.functions.greenOutcome() %>
<% $("#HUD").html(window.story.render("HUD")) %>
<%= window.story.render("Check Status") %>Health: <%= story.state.variables.health %> <br>
Fuel: <%= story.state.variables.fuel %> <br>
Number of Jumps Left: <%= story.state.variables.numberOfJumpsLeft %> <br>The ship exploded in flight.
<h3>Game Over</h3>Without fuel, the ship tumbled and spun in the endless black.
<h3>Game Over</h3>After 15 hyperjumps, the ship left the hazardous area and called for help.
<h3>Success!</h3><script>
var _vars = story.state.variables;
var status = "";
if(_vars.health <= 0) {
status = window.story.render("Destroyed");
}
if(_vars.fuel <= 0) {
status = window.story.render("Lost in space");
}
if(_vars.numberOfJumpsLeft <= 0) {
status = window.story.render("Safe");
}
if(status != "") {
$(".gameScreen").html(status);
}
</script>