PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 5ms RepoModel.GetById 36ms app.codeStats 0ms

/js/core/AbstractServerGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 95 lines | 41 code | 15 blank | 39 comment | 1 complexity | b3ddb6f3a07da4b958cc3cc77fe373bd MD5 | raw file
 1/**
 2 File:
 3 AbstractServerGame.js
 4 Created By:
 5 Mario Gonzalez
 6 Project:
 7 RealtimeMultiplayerNodeJS
 8 Abstract:
 9 This class is the base Game controller in RealtimeMultiplayerGame on the server side.
10 It provides things such as dropping players, and contains a ServerNetChannel
11 Basic Usage:
12 [This class is not instantiated! - below is an example of using this class by extending it]
13
14 (function(){
15		MyGameClass = function() {
16			return this;
17 		}
18
19		RealtimeMultiplayerGame.extend(MyGameClass, RealtimeMultiplayerGame.AbstractServerGame, null);
20	};
21 */
22(function () {
23
24    RealtimeMultiplayerGame.AbstractServerGame = function () {
25        this.intervalFramerate += 6;
26        RealtimeMultiplayerGame.AbstractServerGame.superclass.constructor.call(this);
27        return this;
28    };
29
30    RealtimeMultiplayerGame.AbstractServerGame.prototype = {
31        cmdMap: {},					// Map the CMD constants to functions
32        nextEntityID: 0,					// Incremented for everytime a new object is created
33
34        // Methods
35        setupNetChannel: function () {
36            this.netChannel = new RealtimeMultiplayerGame.network.ServerNetChannel(this);
37        },
38
39        /**
40         * Map RealtimeMultiplayerGame.Constants.CMDS to functions
41         * If ServerNetChannel does not contain a function, it will check to see if it is a special function which the delegate wants to catch
42         * If it is set, it will call that CMD on its delegate
43         */
44        setupCmdMap: function () {
45            RealtimeMultiplayerGame.AbstractServerGame.superclass.setupCmdMap.call(this);
46//			this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_UPDATE] = this.shouldUpdatePlayer;
47            // These are left in as an example
48//			this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_JOINED] = this.onPlayerJoined;
49//			this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_DISCONNECT] = this.onPlayerDisconnect;
50        },
51
52        /**
53         * Updates the gameworld
54         * Creates a WorldEntityDescription which it sends to NetChannel
55         */
56        tick: function () {
57            RealtimeMultiplayerGame.AbstractServerGame.superclass.tick.call(this);
58
59            // Allow all entities to update their position
60            this.fieldController.getEntities().forEach(function (key, entity) {
61                entity.updatePosition(this.speedFactor, this.gameClock, this.gameTick);
62            }, this);
63
64            // Create a new world-entity-description,
65            var worldEntityDescription = new RealtimeMultiplayerGame.model.WorldEntityDescription(this, this.fieldController.getEntities());
66            this.netChannel.tick(this.gameClock, worldEntityDescription);
67
68            if (this.gameClock > this.gameDuration) {
69                this.shouldEndGame();
70            }
71        },
72
73        shouldUpdatePlayer: function (client, data) {
74            console.log("(AbstractServerGame)::onPlayerUpdate");
75        },
76
77        shouldRemovePlayer: function (clientid) {
78            this.fieldController.removePlayer(clientid);
79        },
80
81        shouldEndGame: function () {
82            console.log("(AbstractServerGame)::shouldEndGame");
83        },
84
85
86        ///// Accessors
87        getNextEntityID: function () {
88            return ++this.nextEntityID;
89        }
90    }
91
92
93    // Extend RealtimeMultiplayerGame.AbstractGame
94    RealtimeMultiplayerGame.extend(RealtimeMultiplayerGame.AbstractServerGame, RealtimeMultiplayerGame.AbstractGame, null);
95})()