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

/js/DemoCircles/CircleEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 134 lines | 79 code | 18 blank | 37 comment | 2 complexity | 02174bf2b0ae46d8c2992545b8721dbe MD5 | raw file
  1/**
  2 File:
  3 DemoApp.CircleEntity
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 DemoApp
  8 Abstract:
  9 This is the base entity for the demo game
 10 Basic Usage:
 11
 12 Version:
 13 1.0
 14 */
 15(function () {
 16
 17    var nOffset = Math.random() * 2000;
 18    DemoApp.CircleEntity = function (anEntityid, aClientid) {
 19        DemoApp.CircleEntity.superclass.constructor.call(this, anEntityid, aClientid);
 20
 21        this.setColor("FFFFFF");
 22        this.velocity = new RealtimeMultiplayerGame.model.Point(0, 0);
 23        this.acceleration = new RealtimeMultiplayerGame.model.Point(0, 0);
 24        return this;
 25    };
 26
 27    DemoApp.CircleEntity.prototype = {
 28        radius: DemoApp.Constants.ENTITY_DEFAULT_RADIUS,
 29        velocity: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
 30        acceleration: RealtimeMultiplayerGame.model.Point.prototype.ZERO,
 31        collisionCircle: null,										// An instance of RealtimeMultiplayerGame.modules.circlecollision.PackedCircle
 32        entityType: DemoApp.Constants.ENTITY_TYPES.GENERIC_CIRCLE,
 33
 34        /**
 35         * Update the entity's view - this is only called on the clientside
 36         */
 37        updateView: function () {
 38            if (!this.view) return;
 39            this.view.x = this.position.x - this.radius;
 40            this.view.y = this.position.y - this.radius;
 41
 42            var diameter = this.lastReceivedEntityDescription.radius * 2;
 43            this.view.setSize(diameter, diameter);
 44            this.view.setFillStyle("#" + this.lastReceivedEntityDescription.color);
 45        },
 46
 47        /**
 48         * Update position of this entity - this is only called on the serverside
 49         * @param {Number} speedFactor    A number signifying how much faster or slower we are moving than the target framerate
 50         * @param {Number} gameClock    Current game time in seconds (zero based)
 51         * @param {Number} gameTick        Current game tick (incrimented each frame)
 52         */
 53        updatePosition: function (speedFactor, gameClock, gameTick) {
 54
 55            // Modify velocity using perlin noise
 56            var theta = 0.008;
 57
 58            var noise = RealtimeMultiplayerGame.model.noise(nOffset + this.position.x * theta, nOffset + this.position.y * theta, gameTick * 0.003);
 59            var angle = noise * 12;
 60            var speed = 0.2;
 61            this.acceleration.x += Math.cos(angle) * speed - 0.3;
 62            this.acceleration.y -= Math.sin(angle) * speed;
 63
 64
 65            this.velocity.translatePoint(this.acceleration);
 66            this.velocity.limit(5);
 67            this.velocity.multiply(0.9);
 68            this.acceleration.set(0, 0);
 69            this.collisionCircle.position.translatePoint(this.velocity);
 70            this.position = this.collisionCircle.position.clone();
 71        },
 72
 73        tempColor: function () {
 74            var that = this;
 75
 76            clearTimeout(this.timeout);
 77            this.color = "FF0000";
 78            this.timeout = setTimeout(function () {
 79                that.setColor(that.originalColor);
 80            }, 50);
 81        },
 82
 83        /**
 84         * Deallocate memory
 85         */
 86        dealloc: function () {
 87            this.collisionCircle.dealloc();
 88            this.collisionCircle = null;
 89            DemoApp.CircleEntity.superclass.dealloc.call(this);
 90        },
 91
 92        constructEntityDescription: function () {
 93            return DemoApp.CircleEntity.superclass.constructEntityDescription.call(this) + ',' + this.radius + ',' + this.color;
 94        },
 95
 96        ///// ACCESSORS
 97        /**
 98         * Set the CollisionCircle for this game entity.
 99         * @param aCollisionCircle
100         */
101        setCollisionCircle: function (aCollisionCircle) {
102            this.collisionCircle = aCollisionCircle;
103            this.collisionCircle.setDelegate(this);
104            this.collisionCircle.setPosition(this.position.clone());
105            this.collisionCircle.setRadius(this.radius);
106            this.collisionCircle.collisionMask = 1;
107            this.collisionCircle.collisionGroup = 1;
108        },
109        getCollisionCircle: function () {
110            return this.collisionCircle
111        },
112
113        /**
114         * Set the color of this entity, a property originalColor is also stored
115         * @param aColor
116         */
117        setColor: function (aColor) {
118            if (!this.originalColor) {
119                this.originalColor = aColor;
120            }
121
122            this.color = aColor;
123        },
124        getColor: function () {
125            return this.color
126        },
127        getOriginalColor: function () {
128            return this.originalColor
129        }
130    };
131
132    // extend RealtimeMultiplayerGame.model.GameEntity
133    RealtimeMultiplayerGame.extend(DemoApp.CircleEntity, RealtimeMultiplayerGame.model.GameEntity, null);
134})();