PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 11ms RepoModel.GetById 29ms app.codeStats 1ms

/js/controller/FieldController.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 187 lines | 113 code | 31 blank | 43 comment | 14 complexity | a3c53f7029c434d9e64d8894e2583fe6 MD5 | raw file
  1(function () {
  2    RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.Controller");
  3
  4    RealtimeMultiplayerGame.Controller.FieldController = function () {
  5        this.entities = new SortedLookupTable();
  6        this.players = new SortedLookupTable();
  7    };
  8
  9    RealtimeMultiplayerGame.Controller.FieldController.prototype = {
 10        entities: null,					// A SortedLookupTable for all entities
 11        players: null,					// A SortedLookupTable for players only, stored using client.getClientid()
 12
 13        /**
 14         * Update all entities
 15         * @param {Number} speedFactor    A number signifying how much faster or slower we are moving than the target framerate
 16         * @param {Number} gameClock    Current game time in seconds (zero based)
 17         * @param {Number} gameTick        Current game tick (incrimented each frame)
 18         */
 19        tick: function (speedFactor, gameClock, gameTick) {
 20            // DO SOME STUFF
 21        },
 22
 23        /**
 24         * Internal function. Adds an entity to our collection, and adds it to the view if we have one
 25         * @param anEntity    An entity to add, should already be created and contain a unique entityid
 26         */
 27        addEntity: function (anEntity) {
 28            this.entities.setObjectForKey(anEntity, anEntity.entityid);
 29
 30            // If we have a view, then add the player to it
 31            if (this.view) {
 32                this.view.addEntity(anEntity.getView());
 33            }
 34
 35        },
 36
 37        /**
 38         * Updates the entity based on new information (called by AbstractClientGame::renderAtTime)
 39         * @param {int}        entityid    entityid we want to update
 40         * @param {RealtimeMultiplayerGame.model.Point}    newPosition    position
 41         * @param {Number}    newRotation    rotation
 42         * @param {Object}    newEntityDescription The full contents of the the snapshots newEntityDescription
 43         */
 44        updateEntity: function (entityid, newPosition, newRotation, newEntityDescription) {
 45            var entity = this.entities.objectForKey(entityid);
 46
 47            if (entity != null) {
 48                entity.position.x = newPosition.x;
 49                entity.position.y = newPosition.y;
 50                entity.rotation = newRotation;
 51                entity.lastReceivedEntityDescription = newEntityDescription;
 52            } else {
 53                console.log("(FieldController)::updateEntity - Error: Cannot find entity with entityid", entityid);
 54            }
 55        },
 56
 57///// Memory
 58
 59        addPlayer: function (aPlayerEntity) {
 60            this.addEntity(aPlayerEntity);
 61            this.players.setObjectForKey(aPlayerEntity, aPlayerEntity.clientid);
 62        },
 63
 64        /**
 65         * Remove a player.
 66         * Does player stuff, then calls removeEntity.
 67         * @param clientid    ConnectionID of the player who jumped out of the game
 68         */
 69        removePlayer: function (clientid) {
 70            var player = this.players.objectForKey(clientid);
 71            if (!player) {
 72                console.log("(FieldController), No 'Character' with clientid " + clientid + " ignoring...");
 73                return;
 74            }
 75
 76            this.removeEntity(player.entityid);
 77            this.players.remove(player.clientid);
 78        },
 79
 80
 81        /**
 82         * Removes an entity by it's ID
 83         * @param entityid
 84         */
 85        removeEntity: function (entityid) {
 86            var entity = this.entities.objectForKey(entityid);
 87
 88            if (this.view)
 89                this.view.removeEntity(entity.view);
 90
 91            entity.dealloc();
 92            this.entities.remove(entityid);
 93        },
 94
 95        /**
 96         * Checks an array of "active entities", against the existing ones.
 97         * It's used to remove entities that expired in between two updates
 98         * @param activeEntities
 99         */
100        removeExpiredEntities: function (activeEntities) {
101            var entityKeysArray = this.entities._keys;
102            var i = entityKeysArray.length;
103            var key;
104            var totalRemoved = 0;
105
106            while (i--) {
107                key = entityKeysArray[i];
108
109                // This entity is still active. Move along.
110                if (activeEntities[key])
111                    continue;
112
113                // This entity is not active, check if it belongs to the server
114                var entity = this.entities.objectForKey(key);
115                var isPlayer = this.players.objectForKey(entity.clientid) != null;
116
117
118                // Remove special way if player (which calls removeEntity on itself as well), or just remove it as an entity
119                if (isPlayer) {
120                    this.removePlayer(entity.clientid);
121                } else {
122                    this.removeEntity(entity.entityid);
123                }
124
125                totalRemoved++;
126            }
127
128        },
129
130        dealloc: function () {
131            this.players.forEach(function (key, entity) {
132                this.removePlayer(entity.clientid);
133            }, this);
134            this.players.dealloc();
135            this.players = null;
136
137            this.entities.forEach(function (key, entity) {
138                this.removeEntity(entity.entityid);
139            }, this);
140            this.entities.dealloc();
141            this.entities = null;
142
143
144            this.view = null;
145        },
146
147///// Accessors
148        // Will be called on client side
149        setView: function (aView) {
150            var theInterface = RealtimeMultiplayerGame.Controller.FieldControllerViewProtocol;
151            for (var member in theInterface) {
152                if ((typeof aView[member] != typeof theInterface[member])) {
153                    console.log("object failed to implement interface member " + member);
154                    return false;
155                }
156            }
157
158            // Checks passed
159            this.view = aView;
160        },
161        getView: function () {
162            return this.view
163        },
164        getEntities: function () {
165            return this.entities
166        },
167        getPlayers: function () {
168            return this.players;
169        },
170        getEntityWithid: function (anEntityid) {
171            return this.entities.objectForKey(anEntityid);
172        },
173        getPlayerWithid: function (aClientid) {
174            return this.players.objectForKey(aClientid);
175        }
176    };
177
178    /**
179     * Required methods for the FieldControllerView delegate
180     */
181    RealtimeMultiplayerGame.Controller.FieldControllerViewProtocol = {
182        addEntity: function (anEntityView) {
183        },
184        dealloc: function () {
185        }
186    }
187})();