PageRenderTime 31ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/js/model/GameEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 174 lines | 97 code | 19 blank | 58 comment | 11 complexity | 6894b853aef2818af4691cbc661dc2b0 MD5 | raw file
  1/**
  2 File:
  3 GameEntity.js
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 RealtimeMultiplayerNodeJS
  8 Abstract:
  9 This class is the base GameEntity class in RealtimeMultiplayerGame, it contains a position rotation, health
 10 Basic Usage:
 11
 12 var badGuy = new RealtimeMultiplayerGame.GameEntity();
 13 badGuy.position.x += 1;
 14 */
 15(function () {
 16    RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.model");
 17
 18    RealtimeMultiplayerGame.model.GameEntity = function (anEntityid, aClientid) {
 19        this.clientid = aClientid;
 20        this.entityid = anEntityid;
 21        this.traits = [];
 22        this.position = new RealtimeMultiplayerGame.model.Point(0, 0);
 23        return this;
 24    };
 25
 26    RealtimeMultiplayerGame.model.GameEntity.prototype = {
 27        // Connection info
 28        clientid: -1,														// Owner of this object
 29        entityid: -1,														// UUID for this entity
 30        entityType: -1,														// A special interger representing the entityType sent via along with other network info
 31        position: RealtimeMultiplayerGame.model.Point.prototype.ZERO,  		// Current position of this entity
 32        rotation: 0,
 33        traits: null,														// An array of our traits, in reverse added order
 34        view: null,
 35        lastReceivedEntityDescription: null,									// The last received entity description (set by renderAtTime)
 36
 37        /**
 38         * Update the view's position
 39         */
 40        updateView: function () {
 41            // OVERRIDE
 42        },
 43
 44        /**
 45         * Updates the position of this GameEntity based on it's movement properties (velocity, acceleration, damping)
 46         * @param {Number} speedFactor    A number signifying how much faster or slower we are moving than the target framerate
 47         * @param {Number} gameClock    Current game time in seconds (zero based)
 48         * @param {Number} gameTick        Current game tick (incrimented each frame)
 49         */
 50        updatePosition: function (speedFactor, gameClock, gameTick) {
 51            // OVERRIDE
 52        },
 53
 54        /**
 55         * Construct an entity description for this object, it is essentually a CSV so you have to know how to read it on the receiving end
 56         * @param wantsFullUpdate    If true, certain things that are only sent when changed are always sent
 57         */
 58        constructEntityDescription: function (gameTick, wantsFullUpdate) {
 59            // Note: "~~" is just a way to round the value without the Math.round function call
 60            var returnString = this.entityid;
 61            returnString += "," + this.clientid;
 62            returnString += "," + this.entityType;
 63            returnString += "," + ~~this.position.x;
 64            returnString += "," + ~~this.position.y;
 65
 66            return returnString;
 67        },
 68
 69        ////// TRAIT SUPPORT
 70        /**
 71         * Adds and attaches a trait (already created), to this entity.
 72         * The trait is only attached if we do not already have one of the same type attached, or don't care (aTrait.canStack = true)
 73         * @param {RealtimeMultiplayerGame.controller.traits.BaseTrait} aTrait A BaseTrait instance
 74         * @return {Boolean} Whether the trait was added
 75         */
 76        addTrait: function (aTrait) {
 77            // Check if we already have this trait, if we do - make sure the trait allows stacking
 78            var existingVersionOfTrait = this.getTraitWithName(aTrait.displayName);
 79            if (existingVersionOfTrait && !existingVersionOfTrait.canStack) {
 80                return false;
 81            }
 82
 83            // Remove existing version
 84            if (existingVersionOfTrait) {
 85                this.removeTraitWithName(aTrait.displayName);
 86            }
 87
 88
 89            this.traits.push(aTrait);
 90            aTrait.attach(this);
 91
 92            return aTrait;
 93        },
 94
 95        /**
 96         * Calls addTrait and executes it immediately
 97         * @param aTrait
 98         */
 99        addTraitAndExecute: function (aTrait) {
100            var wasAdded = this.addTrait(aTrait);
101            if (wasAdded) {
102                aTrait.execute();
103                return aTrait;
104            }
105
106            return null;
107        },
108
109        /**
110         * Removes a trait with a matching .displayName property
111         * @param aTraitName
112         */
113        removeTraitWithName: function (aTraitName) {
114            var len = this.traits.length;
115            var removedTraits = null;
116            for (var i = 0; i < len; ++i) {
117                if (this.traits[i].displayName === aTraitName) {
118                    removedTraits = this.traits.splice(i, 1);
119                    break;
120                }
121            }
122
123            // Detach removed traits
124            if (removedTraits) {
125                i = removedTraits.length;
126                while (i--) {
127                    removedTraits[i].detach();
128                }
129            }
130        },
131
132        /**
133         * Removes all traits contained in this entity
134         */
135        removeAllTraits: function () {
136            var i = this.traits.length;
137            while (i--) {
138                this.traits[i].detach();
139            }
140
141            this.traits = [];
142        },
143
144        ///// MEMORY
145        dealloc: function () {
146            this.position = null;
147            this.removeAllTraits();
148            this.traits = null;
149        },
150
151        ////// ACCESSORS
152        setView: function (aView) {
153            this.view = aView;
154        },
155        getView: function () {
156            return this.view;
157        },
158        /**
159         * Returns a trait with a matching .displayName property
160         * @param aTraitName
161         */
162        getTraitWithName: function (aTraitName) {
163            var len = this.traits.length;
164            var trait = null;
165            for (var i = 0; i < len; ++i) {
166                if (this.traits[i].displayName === aTraitName) {
167                    trait = this.traits[i];
168                    break;
169                }
170            }
171            return trait;
172        }
173    }
174})();