PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/HeroDeo/WebContent/js/game/oasis_game.js

https://bitbucket.org/obelzile/herodeo
JavaScript | 497 lines | 385 code | 103 blank | 9 comment | 71 complexity | dc343bea8903d9abbe7ff505b39dd1ca MD5 | raw file
  1. var Oasis = function() {
  2. this.initialised = false;
  3. this.myuuid = "";
  4. this.myindex = -1;
  5. this.layout = null;
  6. this.turn = 0;
  7. this.playerindex = 0;
  8. this.gamestate = 1;
  9. this.board = null;
  10. this.target = null;
  11. this.spells = null;
  12. this.gamecontrols = null;
  13. this.units = null;
  14. this.drank = false;
  15. this.attacked = false;
  16. this.moving = false;
  17. this.moved = false;
  18. this.monstercontrol = null;
  19. this.treasures = [];
  20. Object.defineProperties(this, {
  21. "me" : {
  22. get : function()
  23. {
  24. return this.units.getUnit(this.myuuid);
  25. }
  26. },
  27. "merchant" : {
  28. get : function()
  29. {
  30. return Merchant;
  31. }
  32. }
  33. });
  34. };
  35. Oasis.prototype.init = function(data) {
  36. Logger.log(Logger.enum.notice,"init_game " + this.layout);
  37. this.myuuid = data.myuuid;
  38. this.layout = data.layout;
  39. this.myindex = data.myindex;
  40. this.gamestate = data.gamestate;
  41. this.turn = data.turn;
  42. this.playerindex = data.playerindex;
  43. this.target = new Target();
  44. this.spells = new Spells();
  45. this.moving = data.moving;
  46. this.moved = data.moved;
  47. this.movementSteps = [];
  48. Crafty.init(16*TILE_SIZE,16*TILE_SIZE);
  49. Crafty.canvas.init();
  50. Crafty.background("black");
  51. this.units = new Units();
  52. Merchant.init("#merchant");
  53. Merchant.onBuyItem = Oasis.prototype.onBuyItem;
  54. this.treasures = data.treasures;
  55. this.drank = data.drank;
  56. this.board = new Board();
  57. this.board.loadLayout(this.layout,function() {
  58. for(index in oasis.treasures) {
  59. oasis.board.addLayer(CellType.Chest,oasis.treasures[index]);
  60. }
  61. oasis.board.initialized = true;
  62. });
  63. //monsters init
  64. for(index in data.monsters) {
  65. var monster = new Monster(data.monsters[index]);
  66. this.units.addUnit(monster);
  67. }
  68. //players init
  69. if(data.players.length == 2) {
  70. playerInfoDivIndex = [0,3,1,2];
  71. } else if(data.players.length == 3) {
  72. playerInfoDivIndex = [0,1,3,2];
  73. } else {
  74. playerInfoDivIndex = [0,2,1,3];
  75. }
  76. for(var index=0;index<data.players.length;index++) {
  77. var playerInfoDiv = $($(".selection-border").get(playerInfoDivIndex[index]));
  78. var player = new Player(data.players[index],playerInfoDivIndex[index],playerInfoDiv);
  79. // this.players[index] = new Player(data.players[index],playerInfoDivIndex[index],playerInfoDiv);
  80. this.units.addUnit(player);
  81. }
  82. setPlayerInfoBorder(this.playerindex);
  83. /* for(var i=0;i<this.players.length;i++) {
  84. this.players[i].updateUI();
  85. }*/
  86. var players = oasis.units.getPlayers();
  87. for(var i=0;i<players.length;i++) {
  88. players[i].updateUI();
  89. }
  90. this.initialised = true;
  91. };
  92. Oasis.prototype.onBuyItem = function(shoppingList) {
  93. params = {};
  94. params.roomuuid = roomuuid;
  95. params.roompw = pw;
  96. params.shoppinglist = shoppingList;
  97. game.sendMessage("BuyItem",params);
  98. };
  99. Oasis.prototype.getOpponents = function() {
  100. var opponents = [];
  101. for(var i=0;this.units.count();i++) {
  102. var unit = this.units.get(i);
  103. if(unit.uuid != this.myuuid) {
  104. opponents.push(unit);
  105. }
  106. }
  107. return opponents;
  108. };
  109. Oasis.prototype.filterOccupiedCells = function(cells) {
  110. result = [];
  111. i=0;
  112. for(c in cells) {
  113. found = false;
  114. for(var i=0; i<this.units.count();i++) {
  115. if(cells[c].x == this.units.get(i).position.x && cells[c].y == this.units.get(i).position.y) {
  116. found = true;
  117. }
  118. }
  119. if(found == false) {
  120. result[i] = cells[c];
  121. i++;
  122. }
  123. }
  124. return result;
  125. };
  126. Oasis.prototype.run = function(data) {
  127. Logger.log(Logger.enum.notice,"run");
  128. this.updateScene();
  129. };
  130. Oasis.prototype.removeTreasure = function(data) {
  131. for(index in this.treasures) {
  132. if(this.treasures[index].x == data.position.x && this.treasures[index].y == data.position.y) {
  133. this.treasures.splice(index,1);
  134. }
  135. }
  136. oasis.board.removeLayer(CellType.Chest,data.position);
  137. this.gamecontrols.updateControls();
  138. };
  139. Oasis.prototype.updateTreasures = function(data) {
  140. for(index in data.treasures) {
  141. if(this.board.addCellType(CellType.Chest,data.treasures[index])) {
  142. this.treasures[treasures.length] = data.treasures[index];
  143. }
  144. }
  145. };
  146. Oasis.prototype.drinkFromWell = function() {
  147. params = {};
  148. params.roomuuid = roomuuid;
  149. params.roompw = pw;
  150. this.drank = true;
  151. game.sendMessage("DrinkFromWell",params);
  152. this.gamecontrols.updateControls();
  153. };
  154. Oasis.prototype.throwCoinInWell = function() {
  155. params = {};
  156. params.roomuuid = roomuuid;
  157. params.roompw = pw;
  158. game.sendMessage("ThrowCoinInWell",params);
  159. this.gamecontrols.updateControls();
  160. };
  161. Oasis.prototype.ramassermarqueur = function() {
  162. params = {};
  163. params.roomuuid = roomuuid;
  164. params.roompw = pw;
  165. game.sendMessage("RamasserMarqueur",params);
  166. this.gamecontrols.updateControls();
  167. };
  168. Oasis.prototype.visitemarchand = function() {
  169. params = {};
  170. params.roomuuid = roomuuid;
  171. params.roompw = pw;
  172. game.sendMessage("VisiteMarchand",params);
  173. this.gamecontrols.updateControls();
  174. };
  175. Oasis.prototype.initiateAttack = function(target) {
  176. if(this.attacked == false) {
  177. params = {};
  178. params.roomuuid = roomuuid;
  179. params.roompw = pw;
  180. params.target = target;
  181. this.attacked = false;
  182. game.sendMessage("InitiateAttack",params);
  183. this.gamecontrols.updateControls();
  184. }
  185. };
  186. Oasis.prototype.requestMoveState = function(value) {
  187. params = {};
  188. params.roomuuid = roomuuid;
  189. params.roompw = pw;
  190. if(value == true) {
  191. if(this.moving == false || this.moved == false) {
  192. game.sendMessage("RequestMovementState",params);
  193. }
  194. } else {
  195. if(this.moving == true) {
  196. game.sendMessage("FinishMovementState",params);
  197. }
  198. }
  199. };
  200. Oasis.prototype.requestMonsterAction = function() {
  201. params = {};
  202. params.roomuuid = roomuuid;
  203. params.roompw = pw;
  204. game.sendMessage("MonsterActionRequest",params);
  205. };
  206. Oasis.prototype.requestMonsterMovementState = function() {
  207. params = {};
  208. params.roomuuid = roomuuid;
  209. params.roompw = pw;
  210. game.sendMessage("RequestMonsterMovementState",params);
  211. };
  212. Oasis.prototype.requestMonsterAttackState = function() {
  213. params = {};
  214. params.roomuuid = roomuuid;
  215. params.roompw = pw;
  216. game.sendMessage("RequestMonsterAttackState",params);
  217. };
  218. Oasis.prototype.requestMonsterControlSkip = function() {
  219. params = {};
  220. params.roomuuid = roomuuid;
  221. params.roompw = pw;
  222. game.sendMessage("RequestMonsterControlSkip",params);
  223. };
  224. Oasis.prototype.finishMoveState = function() {
  225. params = {};
  226. params.roomuuid = roomuuid;
  227. params.roompw = pw;
  228. params.state = "finish";
  229. if(this.monstercontrol.monstermoving == true) {
  230. game.sendMessage(this.monstercontrol.finishCallback,params);
  231. }
  232. };
  233. Oasis.prototype.onMonsterMoveState = function(data) {
  234. if(this.monstercontrol == null) {
  235. this.monstercontrol = new MonsterControl(data);
  236. }
  237. var monster = oasis.units.getUnit(this.monstercontrol.monsteruuid);
  238. //monster doesn't exist client side
  239. if(monster == null) {
  240. this.monstercontrol = null;
  241. return;
  242. }
  243. if(data.moving == true) {
  244. monster.entity.attr({movable: true});
  245. monster.stepLeft = data.movement;
  246. } else {
  247. monster.entity.attr({movable: false});
  248. for(step in this.movementSteps) {
  249. this.movementSteps[step].destroy();
  250. }
  251. this.movementSteps = [];
  252. this.monstercontrol.monstermoved = true;
  253. this.monstercontrol.monstermoving = false;
  254. //this.monstercontrol = null;
  255. }
  256. this.gamecontrols.updateControls();
  257. };
  258. Oasis.prototype.onMonsterMoving = function(data) {
  259. params = {};
  260. params.roomuuid = roomuuid;
  261. params.position = data;
  262. game.sendMessage(oasis.monstercontrol.movementCallback,params);
  263. };
  264. Oasis.prototype.onMoveState = function(data) {
  265. this.moving = data.moving;
  266. this.moved = data.moved;
  267. var player = this.units.getUnit(this.myuuid);
  268. if(data.moving == true) {
  269. player.entity.attr({movable: true});
  270. player.stepLeft = data.stepleft;
  271. } else {
  272. player.entity.attr({movable: false});
  273. for(step in this.movementSteps) {
  274. this.movementSteps[step].destroy();
  275. }
  276. this.movementSteps = [];
  277. }
  278. this.gamecontrols.updateControls();
  279. };
  280. Oasis.prototype.endTurn = function() {
  281. params = {};
  282. params.roomuuid = roomuuid;
  283. params.roompw = pw;
  284. game.sendMessage("PlayerEndTurn",params);
  285. };
  286. Oasis.prototype.newPlayerTurn = function(data) {
  287. this.playerindex = data.playerindex;
  288. this.moving = false;
  289. this.moved = false;
  290. this.drinkwell = false;
  291. this.gamecontrols.updateControls();
  292. setPlayerInfoBorder(this.playerindex);
  293. this.units.getPlayer(this.playerindex).updateUI();
  294. };
  295. Oasis.prototype.turnBegin = function(playerIndex) {
  296. setSelectedPlayerInfo(playerIndex);
  297. this.players[this.playerindex].updateUI();
  298. //update player info
  299. };
  300. Oasis.prototype.attackTargetsUpdate = function(data) {
  301. this.units.getUnit(data.playeruuid).attackTargets = data.attacktargets;
  302. this.gamecontrols.updateControls();
  303. };
  304. Oasis.prototype.bonusPhase = function(data) {
  305. this.monstercontrol = null;
  306. this.moved = false;
  307. this.attacked = false;
  308. this.drank = false;
  309. this.moving = false;
  310. updateInfo();
  311. };
  312. Oasis.prototype.mainPhase = function(data) {
  313. updateInfo();
  314. };
  315. Oasis.prototype.removeUnit = function(data) {
  316. this.units.removeUnit(data.unit);
  317. };
  318. Oasis.prototype.updateUnit = function(data) {
  319. this.units.updateUnit(data.unit);
  320. updateInfo();
  321. };
  322. Oasis.prototype.updateUnits = function(data) {
  323. for(var i in data.units) {
  324. this.units.updateUnit(data.units[i]);
  325. }
  326. updateInfo();
  327. };
  328. Oasis.prototype.moveUnit = function(data) {
  329. var unit = this.units.getUnit(data.unit.uuid);
  330. if(unit != null) {
  331. var found = false;
  332. for(step in this.movementSteps) {
  333. if(this.movementSteps[step].attr("x")/TILE_SIZE == data.unit.position.x && this.movementSteps[step].attr("y")/TILE_SIZE == data.unit.position.y) {
  334. this.movementSteps[step].destroy();
  335. this.movementSteps.splice(step,1);
  336. found = true;
  337. }
  338. }
  339. if(found == false) {
  340. var color = "#0026FF";
  341. if(unit instanceof Player) {
  342. if(this.board.hasCellType(CellType.Trap,unit.position)) {
  343. color = "#FF0000";
  344. }
  345. }
  346. this.movementSteps[this.movementSteps.length] = Crafty.e("2D,Canvas,Color,Tint")
  347. .color(color)
  348. .attr({x:unit.position.x*TILE_SIZE,y:unit.position.y*TILE_SIZE,w:TILE_SIZE,h:TILE_SIZE,z: 15})
  349. .tint(color, 0.2);
  350. }
  351. var unit = this.units.updateUnit(data.unit);
  352. unit.updateUI();
  353. this.gamecontrols.updateControls();
  354. }
  355. };
  356. Oasis.prototype.setCurrentPlayerIndex = function(data) {
  357. this.playerindex = data.playerindex;
  358. if(data.refreshscene == true) {
  359. this.updateScene();
  360. }
  361. };
  362. Oasis.prototype.setGameState = function(data) {
  363. this.gamestate = data.gamestate;
  364. this.updateScene();
  365. };
  366. Oasis.prototype.updateScene = function() {
  367. switch(this.gamestate) {
  368. case 0:
  369. break;
  370. case 1: //hero selection
  371. if(this.myindex == this.playerindex) {
  372. Crafty.scene("heroselection");
  373. } else {
  374. Crafty.scene("herosselectionwait");
  375. }
  376. updateInfo();
  377. break;
  378. case 2: //gamebegin
  379. Crafty.scene("main");
  380. updateInfo();
  381. break;
  382. case 3: //gameover
  383. Crafty.scene("gameover");
  384. break;
  385. }
  386. };
  387. Crafty.scene("main",function() {
  388. Logger.log(Logger.enum.notice,"main scene " + oasis.layout);
  389. Crafty.background("black");
  390. oasis.board.generateEntities();
  391. for(var i=0; i<oasis.units.count();i++) {
  392. oasis.units.get(i).generateEntity();
  393. }
  394. });