PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/js/core/AbstractGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 120 lines | 63 code | 16 blank | 41 comment | 2 complexity | 487d8607d418da8cca854a851d8c91b0 MD5 | raw file
  1/**
  2 File:
  3 AbstractGame.js
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 RealtimeMultiplayerNodeJS
  8 Abstract:
  9 This class is the base Game controller in RealtimeMultiplayerGame it provides things such as, keeping track of the current game clock, starting and stopping the game clock
 10 Basic Usage:
 11 [This class is not instantiated! - below is an example of using this class by extending it]
 12
 13 (function(){
 14		MyGameClass = function() {
 15			return this;
 16 		}
 17
 18		RealtimeMultiplayerGame.extend(MyGameClass, RealtimeMultiplayerGame.AbstractGame, null);
 19	};
 20 */
 21(function () {
 22    RealtimeMultiplayerGame.AbstractGame = function () {
 23        this.setupNetChannel();
 24        this.setupCmdMap();
 25        this.fieldController = new RealtimeMultiplayerGame.Controller.FieldController();
 26        return this;
 27    };
 28
 29    RealtimeMultiplayerGame.AbstractGame.prototype = {
 30        // Properties
 31        gameClockReal: 0,											// Actual time via "new Date().getTime();"
 32        gameClock: 0,											// Seconds since start
 33        gameTick: 0,											// Ticks since start
 34        isRunning: true,
 35        speedFactor: 1,											// Used to create Framerate Independent Motion (FRIM) - 1.0 means running at exactly the correct speed, 0.5 means half-framerate. (otherwise faster machines which can update themselves more accurately will have an advantage)
 36        intervalGameTick: null,											// Setinterval for gametick
 37        intervalFramerate: 60,											// Try to call our tick function this often, intervalFramerate, is used to determin how often to call settimeout - we can set to lower numbers for slower computers
 38        intervalTargetDelta: NaN,	// this.targetDelta, milliseconds between frames. Normally it is 16ms or 60FPS. The framerate the game is designed against - used to create framerate independent motion
 39        gameDuration: Number.MAX_VALUE,								// Gameduration
 40
 41        netChannel: null,											// ServerNetChannel / ClientNetChannel determined by subclass
 42        fieldController: null,											// FieldController
 43        cmdMap: {},
 44
 45        /**
 46         * Setup the ClientNetChannel or ServerNetChannel
 47         */
 48        setupNetChannel: function () {
 49        },
 50
 51        /**
 52         * setup the command mapping for the events recevied from netchannel
 53         */
 54        setupCmdMap: function () {
 55        },
 56
 57        // Methods
 58        tick: function () {
 59            // Store previous time and update current
 60            var oldTime = this.gameClockReal;
 61            this.gameClockReal = new Date().getTime();
 62
 63            // Our clock is zero based, so if for example it says 10,000 - that means the game started 10 seconds ago
 64            var delta = this.gameClockReal - oldTime;
 65            this.gameClock += delta;
 66            this.gameTick++;
 67
 68            // Framerate Independent Motion -
 69            // 1.0 means running at exactly the correct speed, 0.5 means half-framerate. (otherwise faster machines which can update themselves more accurately will have an advantage)
 70            this.speedFactor = delta / ( this.intervalTargetDelta );
 71            if (this.speedFactor <= 0) this.speedFactor = 1;
 72
 73            this.fieldController.tick(this.speedFactor, this.gameClockReal, this.gameTick);
 74        },
 75
 76
 77        /**
 78         * Start/Restart the game tick
 79         */
 80        startGameClock: function () {
 81            var that = this;
 82            this.gameClockReal = new Date().getTime();
 83            this.intervalTargetDelta = Math.floor(1000 / this.intervalFramerate);
 84            this.intervalGameTick = setInterval(function () {
 85                that.tick()
 86            }, this.intervalTargetDelta);
 87        },
 88
 89        /**
 90         * Stop the game tick
 91         */
 92        stopGameClock: function () {
 93            clearInterval(RealtimeMultiplayerGame.AbstractGame.prototype.intervalGameTick);
 94            clearTimeout(RealtimeMultiplayerGame.AbstractGame.prototype.intervalGameTick);
 95        },
 96
 97        setGameDuration: function () {
 98        },
 99
100        // Memory
101        dealloc: function () {
102            if (this.netChannel) this.netChannel.dealloc();
103            this.netChannel = null;
104
105            clearInterval(this.intervalGameTick);
106        },
107
108        log: function () {
109            // OVERRIDE or USE CONSOLE.LOG
110        },
111
112        ///// Accessors
113        getGameClock: function () {
114            return this.gameClock;
115        },
116        getGameTick: function () {
117            return this.gameTick;
118        }
119    }
120})();