PageRenderTime 282ms CodeModel.GetById 60ms app.highlight 124ms RepoModel.GetById 56ms app.codeStats 33ms

/js/BubbleDots/BubbleDotsServerGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 170 lines | 82 code | 25 blank | 63 comment | 4 complexity | c91e3bad0753dff1127038a9e28c22bb MD5 | raw file
  1/**
  2 File:
  3 DemoServerGame
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 BubbleDots
  8 Abstract:
  9 This is a concrete server instance of our game
 10 Basic Usage:
 11 DemoServerGame = new BubbleDots.DemoServerGame();
 12 DemoServerGame.start();
 13 DemoServerGame.explodeEveryone();
 14 Version:
 15 1.0
 16 */
 17(function () {
 18    require("../model/ImprovedNoise.js");
 19    require("./lib/color.js");
 20    require("./lib/Tween.js");
 21
 22    BubbleDots.DemoServerGame = function () {
 23        BubbleDots.DemoServerGame.superclass.constructor.call(this);
 24
 25        this.setGameDuration(BubbleDots.Constants.GAME_DURATION);
 26        this.setupCollisionManager();
 27        this.setupRandomField();
 28
 29        return this;
 30    };
 31
 32    BubbleDots.DemoServerGame.prototype = {
 33        collisionManager: null,
 34
 35        /**
 36         * Map RealtimeMultiplayerGame.Constants.CMDS to functions
 37         * If ServerNetChannel does not contain a function, it will check to see if it is a special function which the delegate wants to catch
 38         * If it is set, it will call that CMD on its delegate
 39         */
 40        setupCmdMap: function () {
 41            BubbleDots.DemoServerGame.superclass.setupCmdMap.call(this);
 42            this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_UPDATE] = this.shouldUpdatePlayer;
 43        },
 44
 45        /**
 46         * Sets up the collision manager
 47         */
 48        setupCollisionManager: function () {
 49            // Collision simulation
 50            this.collisionManager = new RealtimeMultiplayerGame.modules.circlecollision.CircleManager();
 51            this.collisionManager.setBounds(0, 0, BubbleDots.Constants.GAME_WIDTH, BubbleDots.Constants.GAME_HEIGHT);
 52            this.collisionManager.setNumberOfCollisionPasses(2);
 53            this.collisionManager.setNumberOfTargetingPasses(0);
 54            this.collisionManager.setCallback(this.onCollisionManagerCollision, this);
 55        },
 56
 57        /**
 58         * Called when the collision manager detects a collision
 59         */
 60        onCollisionManagerCollision: function (ci, cj, v) {
 61            ci.delegate.onCollision(ci.delegate, cj.delegate, v.clone());
 62            cj.delegate.onCollision(ci.delegate, cj.delegate, v.clone());
 63        },
 64
 65        /**
 66         * Randomly places some CircleEntities into game
 67         */
 68        setupRandomField: function () {
 69            //RealtimeMultiplayerGame.model.noise(10, 10, i/total)
 70            var total = BubbleDots.Constants.MAX_CIRCLES;
 71            for (var i = 0; i < total; i++) {
 72                var radius = BubbleDots.Constants.ENTITY_DEFAULT_RADIUS;
 73                var entity = this.createEntity(BubbleDots.CircleEntity, radius, this.getNextEntityID(), RealtimeMultiplayerGame.Constants.SERVER_SETTING.CLIENT_ID);
 74
 75                // Randomly make the object 'food' or 'poison'
 76                if (i % 5 === 0) {
 77                    entity.addTraitAndExecute(new BubbleDots.traits.PoisonTrait());
 78                } else {
 79                    entity.addTraitAndExecute(new BubbleDots.traits.FoodTrait());
 80                }
 81
 82//				entity.addTraitAndExecute( new BubbleDots.traits.PerlinNoiseTrait() );
 83            }
 84        },
 85
 86        /**
 87         * Helper method to create a single CircleEntity
 88         * @param {Number} aRadius
 89         * @param {Number} anEntityid
 90         * @param {Number} aClientid
 91         */
 92        createEntity: function (aBubbleDotEntityConstructor, aRadius, anEntityid, aClientid) {
 93            // Create the GameEntity
 94            var circleEntity = new aBubbleDotEntityConstructor(anEntityid, aClientid);
 95            circleEntity.position.set(Math.random() * BubbleDots.Constants.GAME_WIDTH * 20, Math.random() * BubbleDots.Constants.GAME_HEIGHT);
 96
 97            // Create a randomly sized circle, that will represent this entity in the collision manager
 98            var collisionCircle = new RealtimeMultiplayerGame.modules.circlecollision.PackedCircle();
 99            circleEntity.setCollisionCircle(collisionCircle);
100            circleEntity.setRadius(aRadius);
101
102            // Place the circle and collision circle into corresponding containers
103            this.collisionManager.addCircle(circleEntity.getCollisionCircle());
104            this.fieldController.addEntity(circleEntity);
105
106            return circleEntity;
107        },
108
109        /**
110         * @inheritDoc
111         */
112        tick: function () {
113            this.collisionManager.handleCollisions();
114            BubbleDots.lib.TWEEN.update();
115
116//			var boundaryRule = RealtimeMultiplayerGame.modules.circlecollision.CircleManager.prototype.BOUNDARY_CONSTRAIN_Y;
117//			var that = this;
118//			this.fieldController.getPlayers().forEach(function(key, value) {
119//				this.collisionManager.handleBoundaryForCircle( value.getCollisionCircle(), boundaryRule );
120//			}, this);
121
122            // Note we call superclass's implementation after we're done
123            BubbleDots.DemoServerGame.superclass.tick.call(this);
124        },
125
126        /**
127         * @inheritDoc
128         */
129        shouldAddPlayer: function (aClientid, data) {
130            var center = new RealtimeMultiplayerGame.model.Point(BubbleDots.Constants.GAME_WIDTH / 2, BubbleDots.Constants.GAME_HEIGHT / 2);
131            var playerEntity = this.createEntity(BubbleDots.PlayerEntity, BubbleDots.Constants.ENTITY_DEFAULT_RADIUS, this.getNextEntityID(), aClientid);
132            playerEntity.position = center.clone();
133            playerEntity.getCollisionCircle().setPosition(center.clone());
134            playerEntity.setInput(new RealtimeMultiplayerGame.Input.Keyboard());
135            playerEntity.setColor("4");
136
137            playerEntity.addTraitAndExecute(new BubbleDots.traits.GravityTrait());
138
139            // Set the boundary trait and the rule it will use
140            var boundaryTrait = new BubbleDots.traits.BoundaryTrait(this.collisionManager);
141            boundaryTrait.setBoundaryRule(RealtimeMultiplayerGame.modules.circlecollision.CircleManager.prototype.BOUNDARY_CONSTRAIN_Y);
142            playerEntity.addTraitAndExecute(boundaryTrait);
143
144            this.fieldController.addPlayer(playerEntity);
145        },
146
147        /**
148         * @inheritDoc
149         */
150        shouldUpdatePlayer: function (aClientid, data) {
151            var entity = this.fieldController.getEntityWithid(data.payload.entityid);
152            entity.input.deconstructInputBitmask(data.payload.input);
153        },
154
155        /**
156         * @inheritDoc
157         */
158        shouldRemovePlayer: function (aClientid) {
159            var entity = this.fieldController.getPlayerWithid(aClientid);
160            if (entity) {
161                this.collisionManager.removeCircle(entity.getCollisionCircle());
162            }
163
164            BubbleDots.DemoServerGame.superclass.shouldRemovePlayer.call(this, aClientid);
165        }
166    };
167
168    // extend RealtimeMultiplayerGame.AbstractServerGame
169    RealtimeMultiplayerGame.extend(BubbleDots.DemoServerGame, RealtimeMultiplayerGame.AbstractServerGame, null);
170})();