Guest User

Untitled

a guest
Jul 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. MapVisual.prototype.recenter = function(me) {
  2.  
  3. var gridCoord = this.map.worldCoordsToGridCoords(me.x, me.y)
  4.  
  5. if (this.previousX !== gridCoord.x || this.previousY !== gridCoord.y) {
  6.  
  7. var dx = gridCoord.x - this.previousX
  8. var dy = gridCoord.y - this.previousY
  9.  
  10. // start
  11. var sx = gridCoord.x - 14
  12. var sy = gridCoord.y - 10
  13.  
  14. // end
  15. var ex = gridCoord.x + 14
  16. var ey = gridCoord.y + 10
  17.  
  18. // add floors and walls within the range of start to end (if they aren't in the cache)
  19. for (var i = sx; i < ex; i++) {
  20. for (var j = sy; j < ey; j++) {
  21. var wall = this.map.getWall(i, j)
  22. var floor = this.map.getFloor(i, j)
  23. var cacheKey = createKey(i, j)
  24.  
  25. if (wall.type !== WallType.Void && wall.type !== WallType.None && typeof this.wallCache[cacheKey] === 'undefined') {
  26. var index = indexify(i, j, this.map.walls)
  27. var frameName = getWallImageFrameName(wall.type, index)
  28. var sprite = new PIXI.Sprite.fromFrame(frameName)
  29. sprite.x = i * 16
  30. sprite.y = j * 16
  31. //sprite.anchor.y = 1/3
  32. this.wallLayer.addChild(sprite)
  33. this.wallCache[cacheKey] = sprite
  34. }
  35.  
  36. if (floor.type !== FloorType.Void && typeof this.floorCache[cacheKey] === 'undefined') {
  37. var index = indexify(i, j, this.map.floors)
  38. var frameName = getFloorImageFrameName(floor.type, index)
  39. var sprite = new PIXI.Sprite.fromFrame(frameName)
  40. sprite.x = i * 16
  41. sprite.y = j * 16
  42. this.groundLayer.addChild(sprite)
  43. this.floorCache[cacheKey] = sprite
  44. }
  45. }
  46. }
  47.  
  48.  
  49. // remove floors and walls that are no longer on screen
  50. if (dx < 0) {
  51. var rx = ex - dx
  52. for (var i = sy; i <= ey; i++) {
  53. var cacheKey = createKey(rx, i)
  54. if (typeof this.floorCache[cacheKey] !== 'undefined') {
  55. this.groundLayer.removeChild(this.floorCache[cacheKey])
  56. delete this.floorCache[cacheKey]
  57. }
  58. if (typeof this.wallCache[cacheKey] !== 'undefined') {
  59. this.wallLayer.removeChild(this.wallCache[cacheKey])
  60. delete this.wallCache[cacheKey]
  61. }
  62. }
  63. }
  64.  
  65. if (dx > 0) {
  66. var rx = sx - dx
  67. for (var i = sy; i <= ey; i++) {
  68. var cacheKey = createKey(rx, i)
  69. if (typeof this.floorCache[cacheKey] !== 'undefined') {
  70. this.groundLayer.removeChild(this.floorCache[cacheKey])
  71. delete this.floorCache[cacheKey]
  72. }
  73. if (typeof this.wallCache[cacheKey] !== 'undefined') {
  74. this.wallLayer.removeChild(this.wallCache[cacheKey])
  75. delete this.wallCache[cacheKey]
  76. }
  77. }
  78. }
  79.  
  80. if (dy < 0) {
  81. var ry = ey - dy
  82. for (var i = sx; i <= ex; i++) {
  83. var cacheKey = createKey(i, ry)
  84. if (typeof this.floorCache[cacheKey] !== 'undefined') {
  85. this.groundLayer.removeChild(this.floorCache[cacheKey])
  86. delete this.floorCache[cacheKey]
  87. }
  88. if (typeof this.wallCache[cacheKey] !== 'undefined') {
  89. this.wallLayer.removeChild(this.wallCache[cacheKey])
  90. delete this.wallCache[cacheKey]
  91. }
  92. }
  93. }
  94.  
  95. if (dy > 0) {
  96. var ry = sy - dy
  97. for (var i = sx; i <= ex; i++) {
  98. var cacheKey = createKey(i, ry)
  99. if (typeof this.floorCache[cacheKey] !== 'undefined') {
  100. this.groundLayer.removeChild(this.floorCache[cacheKey])
  101. delete this.floorCache[cacheKey]
  102. }
  103. if (typeof this.wallCache[cacheKey] !== 'undefined') {
  104. this.wallLayer.removeChild(this.wallCache[cacheKey])
  105. delete this.wallCache[cacheKey]
  106. }
  107. }
  108.  
  109.  
  110. }
  111. }
  112.  
  113. this.previousX = gridCoord.x
  114. this.previousY = gridCoord.y
  115.  
  116. }
Add Comment
Please, Sign In to add comment