/js/DemoHelloWorld/DemoServerGame.js
JavaScript | 123 lines | 54 code | 15 blank | 54 comment | 2 complexity | 01ab85bb161787e11351007758e8e8c4 MD5 | raw file
1/** 2 File: 3 DemoServerGame 4 Created By: 5 Mario Gonzalez 6 Project: 7 DemoHelloWorld 8 Abstract: 9 This is a concrete server instance of our game 10 Basic Usage: 11 DemoServerGame = new DemoHelloWorld.DemoServerGame(); 12 DemoServerGame.start(); 13 DemoServerGame.explodeEveryone(); 14 Version: 15 1.0 16 */ 17(function () { 18 require("../model/ImprovedNoise.js"); 19 20 DemoHelloWorld.DemoServerGame = function () { 21 DemoHelloWorld.DemoServerGame.superclass.constructor.call(this); 22 this.setGameDuration(DemoHelloWorld.Constants.GAME_DURATION); 23 this.setupRandomField(); 24 return this; 25 }; 26 27 DemoHelloWorld.DemoServerGame.prototype = { 28 29 /** 30 * Map RealtimeMultiplayerGame.Constants.CMDS to functions 31 * If ServerNetChannel does not contain a function, it will check to see if it is a special function which the delegate wants to catch 32 * If it is set, it will call that CMD on its delegate 33 */ 34 setupCmdMap: function () { 35 DemoHelloWorld.DemoServerGame.superclass.setupCmdMap.call(this); 36 this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_UPDATE] = this.shouldUpdatePlayer; 37 }, 38 39 /** 40 * Randomly places some CircleEntities into game 41 */ 42 setupRandomField: function () { 43 //RealtimeMultiplayerGame.model.noise(10, 10, i/total) 44 var total = DemoHelloWorld.Constants.MAX_CIRCLES; 45 for (var i = 0; i < total; i++) { 46 var radius = DemoHelloWorld.Constants.ENTITY_DEFAULT_RADIUS + Math.random() * 5; 47 this.createCircleEntity(radius, this.getNextEntityID(), RealtimeMultiplayerGame.Constants.SERVER_SETTING.CLIENT_ID); 48 } 49 }, 50 51 /** 52 * Helper method to create a single CircleEntity 53 * @param {Number} aRadius 54 * @param {Number} anEntityid 55 * @param {Number} aClientid 56 */ 57 createCircleEntity: function (aRadius, anEntityid, aClientid) { 58 59 // Create the GameEntity 60 var circleEntity = new DemoHelloWorld.CircleEntity(anEntityid, aClientid); 61 circleEntity.entityType = DemoHelloWorld.Constants.ENTITY_TYPES.CIRCLE; 62 circleEntity.radius = aRadius; 63 circleEntity.position.set(Math.random() * DemoHelloWorld.Constants.GAME_WIDTH, Math.random() * DemoHelloWorld.Constants.GAME_HEIGHT); 64 65 // Place the circle and collision circle into corresponding containers 66 this.fieldController.addEntity(circleEntity); 67 return circleEntity; 68 }, 69 70 /** 71 * Updates the game 72 * Creates a WorldEntityDescription which it sends to NetChannel 73 */ 74 tick: function () { 75 // Do some fancy stuff for our own game 76 77 // Loop through each entity and move it to the left 78 this.fieldController.getEntities().forEach(function (key, entity) { 79 entity.position.x -= entity.speed; 80 if (entity.position.x < 0) { // reset 81 entity.position.x = DemoHelloWorld.Constants.GAME_WIDTH; 82 } 83 }, this); 84 85 // Note we call superclass's implementation after we're done 86 DemoHelloWorld.DemoServerGame.superclass.tick.call(this); 87 }, 88 89 /** 90 * @inheritDoc 91 */ 92 shouldAddPlayer: function (aClientid, data) { 93 // A player has joined the game - do something fancy 94 this.createCircleEntity(50, this.getNextEntityID(), aClientid); 95 }, 96 97 /** 98 * @inheritDoc 99 */ 100 shouldUpdatePlayer: function (aClientid, data) { 101 var entity = this.fieldController.getEntityWithid(data.payload.entityid); 102 entity.input.deconstructInputBitmask(data.payload.input); 103 }, 104 105 /** 106 * @inheritDoc 107 */ 108 shouldRemovePlayer: function (aClientid) { 109 DemoHelloWorld.DemoServerGame.superclass.shouldRemovePlayer.call(this, aClientid); 110 console.log("DEMO::REMOVEPLAYER"); 111 }, 112 113 /** 114 * @inheritDoc 115 */ 116 dealloc: function () { 117 DemoHelloWorld.DemoServerGame.superclass.dealloc.call(this); 118 } 119 }; 120 121 // extend RealtimeMultiplayerGame.AbstractServerGame 122 RealtimeMultiplayerGame.extend(DemoHelloWorld.DemoServerGame, RealtimeMultiplayerGame.AbstractServerGame, null); 123})()