PageRenderTime 132ms CodeModel.GetById 124ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/js/DemoCircles/DemoServerGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 147 lines | 77 code | 23 blank | 47 comment | 1 complexity | 989d11ebd09461ae669ac270ed5fb68f MD5 | raw file
  1/**
  2 File:
  3 DemoServerGame
  4 Created By:
  5 Mario Gonzalez
  6 Project:
  7 DemoApp
  8 Abstract:
  9 This is a concrete server instance of our game
 10 Basic Usage:
 11 DemoServerGame = new DemoApp.DemoServerGame();
 12 DemoServerGame.start();
 13 DemoServerGame.explodeEveryone();
 14 Version:
 15 1.0
 16 */
 17(function () {
 18    require("../model/ImprovedNoise.js");
 19
 20    DemoApp.DemoServerGame = function () {
 21        DemoApp.DemoServerGame.superclass.constructor.call(this);
 22
 23        this.setGameDuration(DemoApp.Constants.GAME_DURATION);
 24        this.setupCollisionManager();
 25        this.setupRandomField();
 26        return this;
 27    };
 28
 29    DemoApp.DemoServerGame.prototype = {
 30        collisionManager: null,
 31
 32        /**
 33         * Map RealtimeMultiplayerGame.Constants.CMDS to functions
 34         * If ServerNetChannel does not contain a function, it will check to see if it is a special function which the delegate wants to catch
 35         * If it is set, it will call that CMD on its delegate
 36         */
 37        setupCmdMap: function () {
 38            DemoApp.DemoServerGame.superclass.setupCmdMap.call(this);
 39            this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_UPDATE] = this.shouldUpdatePlayer;
 40        },
 41
 42        setupCollisionManager: function () {
 43            // Collision simulation
 44            this.collisionManager = new RealtimeMultiplayerGame.modules.circlecollision.CircleManager();
 45            this.collisionManager.setBounds(0, 0, DemoApp.Constants.GAME_WIDTH, DemoApp.Constants.GAME_HEIGHT);
 46            this.collisionManager.setNumberOfCollisionPasses(2);
 47            this.collisionManager.setNumberOfTargetingPasses(0);
 48            this.collisionManager.setCallback(this.onCollisionManagerCollision, this);
 49        },
 50
 51        /**
 52         * Called when the collision manager detects a collision
 53         */
 54        onCollisionManagerCollision: function (ci, cj, v) {
 55            ci.delegate.tempColor();
 56            cj.delegate.tempColor();
 57        },
 58
 59        /**
 60         * Randomly places some CircleEntities into game
 61         */
 62        setupRandomField: function () {
 63            //RealtimeMultiplayerGame.model.noise(10, 10, i/total)
 64            var total = DemoApp.Constants.MAX_CIRCLES;
 65            for (var i = 0; i < total; i++) {
 66                var radius = DemoApp.Constants.ENTITY_DEFAULT_RADIUS + Math.random() * 5;
 67                this.createCircleEntity(radius, this.getNextEntityID(), RealtimeMultiplayerGame.Constants.SERVER_SETTING.CLIENT_ID);
 68            }
 69        },
 70
 71        /**
 72         * Helper method to create a single CircleEntity
 73         * @param {Number} aRadius
 74         * @param {Number} anEntityid
 75         * @param {Number} aClientid
 76         */
 77        createCircleEntity: function (aRadius, anEntityid, aClientid) {
 78            // Create a randomly sized circle, that will represent this entity in the collision manager
 79            var collisionCircle = new RealtimeMultiplayerGame.modules.circlecollision.PackedCircle();
 80            collisionCircle.setRadius(aRadius);
 81
 82            // Create the GameEntity
 83            var circleEntity = new DemoApp.CircleEntity(anEntityid, aClientid);
 84            circleEntity.radius = aRadius;
 85            circleEntity.position.set(Math.random() * DemoApp.Constants.GAME_WIDTH, Math.random() * DemoApp.Constants.GAME_HEIGHT);
 86            circleEntity.setCollisionCircle(collisionCircle);
 87
 88            // Place the circle and collision circle into corresponding containers
 89            this.collisionManager.addCircle(circleEntity.getCollisionCircle());
 90            this.fieldController.addEntity(circleEntity);
 91
 92            circleEntity.entityType = DemoApp.Constants.ENTITY_TYPES.GENERIC_CIRCLE;
 93            return circleEntity;
 94        },
 95
 96        createPlayerEntity: function (anEntityid, aClientid) {
 97            // Create the GameEntity
 98            var playerEntity = new DemoApp.PlayerEntity(anEntityid, aClientid);
 99            playerEntity.position.set(Math.random() * DemoApp.Constants.GAME_WIDTH, Math.random() * DemoApp.Constants.GAME_HEIGHT);
100
101            var collisionCircle = new RealtimeMultiplayerGame.modules.circlecollision.PackedCircle();
102            collisionCircle.setRadius(playerEntity.radius);
103
104            playerEntity.setInput(new RealtimeMultiplayerGame.Input.Keyboard());
105            playerEntity.setCollisionCircle(collisionCircle);
106
107            // place player on field
108            this.collisionManager.addCircle(playerEntity.getCollisionCircle());
109            this.fieldController.addPlayer(playerEntity);
110
111            return playerEntity;
112        },
113
114        /**
115         * Updates the game
116         * Creates a WorldEntityDescription which it sends to NetChannel
117         */
118        tick: function () {
119            // Use both the BOUNDARY_WRAP_X flag, and the BOUNDARY_CONSTRAIN_Y flags as the rule
120            var boundsRule = RealtimeMultiplayerGame.modules.circlecollision.CircleManager.prototype.BOUNDARY_WRAP_X;
121            boundsRule |= RealtimeMultiplayerGame.modules.circlecollision.CircleManager.prototype.BOUNDARY_CONSTRAIN_Y;
122
123            this.collisionManager.handleBoundaryForAllCircles(boundsRule);
124            this.collisionManager.handleCollisions();
125
126            // Note we call superclass's implementation after we're done
127            DemoApp.DemoServerGame.superclass.tick.call(this);
128        },
129
130        shouldAddPlayer: function (aClientid, data) {
131            this.createPlayerEntity(this.getNextEntityID(), aClientid);
132        },
133
134        shouldUpdatePlayer: function (aClientid, data) {
135            var entity = this.fieldController.getEntityWithid(data.payload.entityid);
136            entity.input.deconstructInputBitmask(data.payload.input);
137        },
138
139        shouldRemovePlayer: function (aClientid) {
140            DemoApp.DemoServerGame.superclass.shouldRemovePlayer.call(this, aClientid);
141            console.log("DEMO::REMOVEPLAYER");
142        }
143    };
144
145    // extend RealtimeMultiplayerGame.AbstractServerGame
146    RealtimeMultiplayerGame.extend(DemoApp.DemoServerGame, RealtimeMultiplayerGame.AbstractServerGame, null);
147})();