<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
</body>
</html>
// Get Programming with JavaScript
// Listings 16.04 to 16.10
// The game controller
(function () {
// 16.05
var player;
var inPlay = false;
var init = function (mapData, playerName) {
var firstPlace = theCrypt.buildMap(mapData);
player = new theCrypt.Player(playerName, 50);
player.addItem("The Sword of Doom");
player.setPlace(firstPlace);
inPlay = true;
render();
return "";
};
// 16.06
var checkGameStatus = function () {
if (player.getData().health <= 0) {
inPlay = false;
renderMessage("Overcome by your wounds...");
renderMessage("...you fall to the ground.");
renderMessage("- Your adventure is over -");
}
};
// 16.07
var render = function () {
console.clear();
if (inPlay) {
theCrypt.placeView.render(player.getPlace());
theCrypt.playerView.render(player);
}
};
var renderMessage = function (message) {
theCrypt.messageView.render(message);
};
// 16.08
var get = function () {
if (inPlay) {
var place = player.getPlace();
var item = place.getLastItem();
if (item !== undefined) {
player.addItem(item);
render();
} else {
renderMessage("There is no item to get");
}
} else {
renderMessage("The game is over!");
}
return "";
};
// 16.09
var go = function (direction) {
if (inPlay) {
var place = player.getPlace();
var destination = place.getExit(direction);
var challenge = place.getChallenge(direction);
if (destination === undefined) {
renderMessage("There is no exit in that direction");
} else {
if ((challenge === undefined) || challenge.complete) {
player.setPlace(destination);
render();
} else {
if (challenge.damage) {
player.applyDamage(challenge.damage);
}
render();
renderMessage(challenge.message);
checkGameStatus();
}
}
} else {
renderMessage("The game is over!");
}
return "";
};
// 16.10
var use = function (item, direction) {
if (inPlay) {
var place = player.getPlace();
var challenge = place.getChallenge(direction);
if (challenge === undefined || challenge.complete === true) {
renderMessage("You don't need to use that there");
} else if (player.hasItem(item)) {
if (item === challenge.requires) {
renderMessage(challenge.success);
challenge.complete = true;
if (challenge.itemConsumed) {
player.removeItem(item);
}
} else {
renderMessage(challenge.failure);
}
} else {
renderMessage("You don't have that item");
}
} else {
renderMessage("The game is over!");
}
return "";
};
// 16.04
// Assign the public interface
window.game = {
get: get,
go: go,
use: use,
init: init
};
})();
/* Further Adventures
*
* The game controller uses the other
* modules created for The Crypt in
* Chapters 14 and 15.
*
* The controller is tested in
* listings 16.11 and 16.12.
*
* As you progress through the discussions
* in chapter 16, make sure you see
* how each function plays its part
* in making the game work.
*
*/
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |