PageRenderTime 230ms CodeModel.GetById 56ms app.highlight 46ms RepoModel.GetById 74ms app.codeStats 1ms

/js/BubbleDots/entities/PlayerEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 103 lines | 61 code | 13 blank | 29 comment | 9 complexity | dd8cf9cb54c71535ede2e481e3b1fa2c MD5 | raw file
  1/**
  2 File:
  3 BubbleDots.CircleEntity
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 BubbleDots
  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    var count = 0;
 17    BubbleDots.PlayerEntity = function (anEntityid, aClientid) {
 18        BubbleDots.PlayerEntity.superclass.constructor.call(this, anEntityid, aClientid);
 19        this.entityType = BubbleDots.Constants.ENTITY_TYPES.PLAYER_ENTITY;
 20        this.initThrust();
 21    };
 22
 23    BubbleDots.PlayerEntity.prototype = {
 24        _isThrusting: false,		// We need a better variable name.
 25        _thrustLevel: 0,
 26
 27        THRUST_DECREMENT: 0.001,		// How much to decrease thrust by
 28        THRUST_FORCE: 0.3,			// How much force to apply every tick when applying thrust
 29
 30        initThrust: function () {
 31            this._thrustLevel = 100.0;
 32            this._isThrusting = false;
 33        },
 34
 35        startThrust: function () {
 36            this._isThrusting = true;
 37//			this.velocity.y *= 0.5;
 38        },
 39
 40        applyThrust: function () {
 41            this._thrustLevel -= BubbleDots.PlayerEntity.prototype.THRUST_DECREMENT;
 42            if (this._thrustLevel > 0.0) {
 43                this.acceleration.y -= BubbleDots.PlayerEntity.prototype.THRUST_FORCE;
 44            }
 45        },
 46
 47        stopThrust: function () {
 48            this._isThrusting = false;
 49        },
 50
 51        /**
 52         * Update position of this entity - this is only called on the serverside
 53         * @param {Number} speedFactor    A number signifying how much faster or slower we are moving than the target framerate
 54         * @param {Number} gameClock    Current game time in seconds (zero based)
 55         * @param {Number} gameTick        Current game tick (incrimented each frame)
 56         */
 57        updatePosition: function (speedFactor, gameClock, gameTick) {
 58            this.handleInput(speedFactor);
 59            BubbleDots.PlayerEntity.superclass.updatePosition.call(this, speedFactor, gameClock, gameTick);
 60        },
 61
 62        handleInput: function (speedFactor) {
 63            var moveSpeed = 0.2;
 64
 65            if (this.input.isLeft()) this.acceleration.x -= moveSpeed;
 66            if (this.input.isRight()) this.acceleration.x += moveSpeed;
 67            if (this.input.isDown()) this.acceleration.y += moveSpeed;
 68
 69            // We're pressing up - apply thrust...
 70            // Call startThrust if we were not thrusting before
 71            if (this.input.isUp()) {
 72                if (!this._isThrusting) {
 73                    this.startThrust();
 74                }
 75
 76                this.applyThrust();
 77            } else if (this._isThrusting) {
 78                this.stopThrust();
 79            } else { // Default behavior - increase _thrustLevel
 80                this._thrustLevel += BubbleDots.PlayerEntity.prototype.THRUST_DECREMENT * 2;
 81                this._thrustLevel = Math.min(this._thrustLevel, 100);
 82            }
 83        },
 84
 85        ///// ACCESSORS
 86        /**
 87         * Set the CollisionCircle for this game entity.
 88         * @param aCollisionCircle
 89         */
 90        setCollisionCircle: function (aCollisionCircle) {
 91            BubbleDots.PlayerEntity.superclass.setCollisionCircle.call(this, aCollisionCircle);
 92            this.collisionCircle.collisionMask = 2;
 93            this.collisionCircle.collisionGroup = 1;
 94            this.collisionCircle.isFixed = true;
 95        },
 96        setInput: function (input) {
 97            this.input = input;
 98        }
 99    };
100
101    // extend RealtimeMultiplayerGame.model.GameEntity
102    RealtimeMultiplayerGame.extend(BubbleDots.PlayerEntity, BubbleDots.CircleEntity, null);
103})();