About VagabondEx (twitter: @LateTide)

Entries

 
Ludum Dare 27
 
Ludum Dare 24

VagabondEx's Trophies

VagabondEx's Archive

Simple Game Manager

Posted by (twitter: @LateTide)
Thursday, August 22nd, 2013 12:48 pm

Just created a small script for helping me with the game – admittedly not the most sophisticated tool around, but it might help me to create content faster.

I can’t attach it to the blog (js files are not allowed) but I am obliged to show it, so here we go:

// Base namespace
var ben = {

};

// checks that the object is not null, and has a function with the given name
ben.hasFunction = function(obj, functionName) {
    var hasF = obj && obj[functionName] && typeof obj[functionName] === "function";
    if(!hasF) {
        console.log(obj + ' doesn\'t have \''+ functionName +'\' function');
    }
    return hasF;
};

// Orchestrates the player turns and the game updates
ben.doUpdate = function() {
    if(!ben.hasFunction(this, "update")) {
        return;
    }
    if(!ben.hasFunction(this, 'isSynchronous')) {
        return;
    }
    if(this.currentTurn >= this.maxTurns) {
        this.stop();
        return;
    }
    if(this.isSynchronous()) {
        if(!this.isReady()) {
            return;
        }
        var ndx;
        this.setReady(false);
        console.log('Processing turn '+this.getCurrentTurn()); 
        for(ndx in this.players) {
            var currentPlayer = this.players[ndx];
            if(!ben.hasFunction(currentPlayer, 'doTurn')) {
                continue;
            }
            currentPlayer.doTurn(this.context);
            this.context.currentPlayer = ndx;
            this.update(this.context);
        }
        this.increaseTurn();
        this.setReady(true);
    } else {
        this.stop();
        return;
        // TODO: Work on this when needed
    }
};

// Starts the update thread
ben.doStart = function() {
    if(!this ) {
        return;
    }
    var that = this;
    console.log('Starting');
    console.log(that);
    that._updateThread = window.setInterval(function() {that.doUpdate()}, that._updateDelay);
}

// Stops the update thread
ben.doStop = function() {
    console.log('Stopping');
    if(this._updateThread) {
        console.log('Has thread');
        window.clearInterval(this._updateThread);
        this._updateThread = null;
    }
}

// Creates the base Game object
// Params: 
//   gameName: The human readable name of the game
//   maxTurns: the number of total turns
//   playerList: an array of players
//   updateFunction: The update function to be called at an interval
//   synch: Synchronous flag. If true, the update waits for each players, otherwise the players input are processed in the regular updates
ben.Game = function(gameName, maxTurns, playerList, updateFunction, synch) {
    var game = {
        _updateDelay: 16, // ms, time between update calls
        _updateThread: null, // placeholder    
        _synchronous: synch, // Synchronous games wait for the players turn with an update
        _name: gameName, // the name of the game, for displaying
        players: playerList, // the players, incl ai
        maxTurns: maxTurns,
        currentTurn: 0,
        ready: true,
        context: {} // current game context
    };

    // to get the human readable name
    game.getName = function() {
        return game._name;
    };

    // to change the current players
    game.setPlayers = function(plList) {
        game.players = plList;
    }

    // to get the number of current players
    game.getPlayerCount = function() {
        return (players != null) ? players.length : 0;
    };

    // sets the ready flag to the given value
    game.setReady = function(rdy) {
        game.ready = rdy;
    };

    // returns the value of the ready flag
    game.isReady = function() {
        return game.ready;
    }

    // returns the value of the synchronous flag
    game.isSynchronous = function() {
        return game._synchronous;
    }    

    game.getCurrentTurn = function() {
        return game.currentTurn;
    }

    game.increaseTurn = function() {
        game.currentTurn += 1;
        return game.currentTurn;
    }

    // stores the update function
    if(updateFunction) {
        if(typeof updateFunction === "function") {
            game.update = updateFunction;
        } else {
            throw {message : "Passed updateFunction variable is not a function"};
        }
    };

    // this function is called with the update frequency
    game.doUpdate = ben.doUpdate;

    // calling this function starts the game
    game.start = ben.doStart;

    // calling this function stops the game
    game.stop = ben.doStop;

    return game;
};

Finally Done

Posted by (twitter: @LateTide)
Sunday, August 26th, 2012 3:33 pm

As I didn’t have much spare time to write this game, I didn’t want to waste it with writing posts – especially that I wasn’t absolutely sure that it will be done.

But it is now, and the entry is posted. I hope you will like it.

http://e-volve.appspot.com/

[cache: storing page]