FogOfWarViewer.js

  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /*jslint node: true, vars: true */
  7. /*global gEngine, Scene, GameObjectset, TextureObject, Camera, vec2,
  8. Renderable, FontRenderable, SpriteRenderable, LightRenderable,
  9. GameObject, Hero, Minion, Dye, Light */
  10. /* find out more about jslint: http://www.jslint.com/help.html */
  11. "use strict"; // Operate in Strict mode such that variables must be declared before used!
  12. /**
  13. * Subclass of FogOfWar. Object tracks the movement of target object and radius for Field of View.
  14. * @name FogOfWarViewer
  15. * @constructor
  16. */
  17. function FogOfWarViewer(){
  18. this.mTarget = null;
  19. this.mXPos = null;
  20. this.mYPos = null;
  21. this.mChanged = true;
  22. this.mRadius = null;
  23. }
  24. /**
  25. * Sets the target and radius for an object to have a Field of View.
  26. * @param {object} t - Already created object for the Fog Of War to track.
  27. * @param {number} r - radius in World Coordinates
  28. */
  29. FogOfWarViewer.prototype.setTarget = function(t, r) {
  30. this.mTarget = t;
  31. this.mXPos = t.getXform().getXPos();
  32. this.mYPos = t.getXform().getYPos();
  33. this.mRadius = r;
  34. };
  35. /**
  36. * Removes the target object from FogOfWarViewer object.
  37. */
  38. FogOfWarViewer.prototype.removeTarget = function () {
  39. this.mTarget = null;
  40. this.mXPos = null;
  41. this.mYPos = null;
  42. this.mRadius = null;
  43. };
  44. /**
  45. * Checks to see if the target object has changed positions.
  46. * @returns {Boolean} - true if target object has changed X or Y location in World Coordinates.
  47. */
  48. FogOfWarViewer.prototype.checkMove = function(){
  49. var targetX = this.mTarget.getXform();
  50. if(!this.mChanged){
  51. if(targetX.getXPos() === this.mXPos && targetX.getYPos() === this.mYPos){
  52. return false;
  53. }else{
  54. return true;
  55. }
  56. }else{
  57. this.mChanged = false;
  58. return true;
  59. }
  60. };
  61. /**
  62. * Changes the size of radius for a Field of View.
  63. * @param {number} r - New radius in World Coordinates.
  64. */
  65. FogOfWarViewer.prototype.setRadius = function (r){
  66. this.mRadius = r;
  67. this.mChanged = true;
  68. };
  69. /**
  70. * Returns the value of radius for Field of View for this object.
  71. * @returns {Number} - Radius in World Coordinates.
  72. */
  73. FogOfWarViewer.prototype.getRadius = function (){
  74. return this.mRadius;
  75. };
  76. /**
  77. * Returns the X location in World Coordinates.
  78. * @returns {Number} - X location in World Coordinates.
  79. */
  80. FogOfWarViewer.prototype.getX = function(){ return this.mXPos; };
  81. /**
  82. * Returns the Y location in World Coordinates.
  83. * @returns {Number} - Y location in World Coordinates.
  84. */
  85. FogOfWarViewer.prototype.getY = function(){ return this.mYPos; };
  86. /**
  87. * Returns the target object.
  88. * @returns {Object} - Target object for this FogOfWarViewer.
  89. */
  90. FogOfWarViewer.prototype.getTarget = function () { return this.mTarget; };
  91. /**
  92. * Updates the previous X and Y location.
  93. */
  94. FogOfWarViewer.prototype.update = function (){
  95. if(this.mTarget !== null){
  96. this.mXPos = this.mTarget.getXform().getXPos();
  97. this.mYPos = this.mTarget.getXform().getYPos();
  98. }
  99. };