rehorn / console.js

A Game Console for Browsers.

Home Page:http://gh.amio.us/console.js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Console.js

A Game Console for Browsers.

Check the live demo, or Usage / Advance Usage / API.

Usage

Include console.min.js in your page:

<script src="http://amio.github.io/console.js/lib/console.min.js"></script>

then:

new Console({
    "addbots": function (num) {
        // add some bots,
        // then tell player:
        return num + ' bots added.'
    }
});

Advance Usage

Init Console with options

var cnsl = new Console({}, {
    hotkey: 27, // <kbd>ESC</kbd> ('~' for default)
    welcome: 'Hello User.',
    caseSensitive: true,
    defaultHandler: function(){}
    onShow: function(){},
    onHide: function(){}
});
  • hotkey : {Number|boolean} The keyCode of hotkey. Hint: If you want to manually put up console(cnsl.toggle("on")), set to a falsy value. 192 by default, the "~".
  • welcome: {String} The welcome message. '' by default.
  • caseSensitive: {Boolean} If you want to. false by default.
  • defaultHandler: {Function} the default handler for any unspecified command. null by default.
  • onShow : {Function} On show callback. null by default.
  • onHide : {Function} On hide callback. null by default.

Alias

new Console({
    "add": "addbots",
    "addbots": function (num) {
        // add some bots,
        // then tell player:
        return num + ' bots added.'
    }
});

Late register command

.register(command, commandHandler)

var playerName = 'Player';

var cnsl = new Console();

cnsl.register('setname',function(name){
    playerName = name;
    return 'Player name is' + playerName + ' now.';
});

Late register command with extra (any) config

.register(command, commandHandler, commandConfig)

var playerName = 'Player';

var cnsl = new Console();

cnsl.register('setname',function(name){
    playerName = name;
    return 'Player name is' + playerName + ' now.';
}, {
    usage: 'SETNAME &lt;newname&gt; || NAME &lt;newname&gt;',
    desc: 'Change your name (works in network play too).'
})

.register('help', function () {
    var cmds = cnsl.commands;
    for (var name in cmds) {
        if (cmds.hasOwnProperty(name)) {
        cmds[name].desc && cnsl.log(' -', cmds[name].usage + ':', cmds[name].desc);
        }
    }
},{
    usage: 'HELP',
    desc: 'Show help messages.'
});

API

Create a Console

  • new Console() Create a console (with default options).
  • new Console(cmdObj) Create a console with command handlers.
  • new Console({}, optionObj) Create a console with options. (see Init console with options)

Instance Methods

.register(cmd, fn[, config])

  • .register(cmd, fn) Register a fn to cmd
  • .register(cmd, fn, config) Register a fn to cmd with a config object

.toggle([switch])

  • .toggle() Toggle the console
  • .toggle("on") Show the console
  • .toggle("off") Hide the console

.destroy()

  • .destroy() Suicide.

About

A Game Console for Browsers.

http://gh.amio.us/console.js/

License:MIT License