Guest User

Untitled

a guest
Dec 12th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #
  2. # Choro - Tileset
  3. # Copyright (c) 2012 Nico Hämäläinen <nico@bender.fi>
  4. # MIT Licensed
  5. #
  6.  
  7. #
  8. # Events:
  9. # - tilesetImageLoaded
  10. # This event is emitted when the initial tileset graphic is loaded
  11. # and saved into the tileset instance.
  12. #
  13.  
  14. # Load needed modules
  15. EventEmitter = require('events').EventEmitter
  16. Canvas = require 'canvas'
  17. Util = require '../util'
  18. Rectangle = Util.math.Rectangle
  19.  
  20. # Create a new Tileset with given attributes
  21. Tileset = (@image, @tilesWide, @tilesHigh, @tileWidth, @tileHeight) ->
  22. # Make our tileset support events
  23. EventEmitter.call(this)
  24.  
  25. # If image param isn't an image
  26. if not (@image instanceof Canvas.Image)
  27. throw new Tileset.Error(100)
  28.  
  29. # If we're given silly sizes
  30. if @tilesWide <= 0 or @tilesHigh <= 0
  31. throw new Tileset.Error(101)
  32. if @tileWidth <= 0 or @tileHeight <= 0
  33. throw new Tileset.Error(102)
  34.  
  35. # If our image size doesn't match tile dimension and count
  36. realWidth = @tilesWide * @tileWidth
  37. realHeight = @tilesHigh * @tileHeight
  38. if @image.width < realWidth
  39. throw new Tileset.Error(103)
  40.  
  41. # Rectangle -data of all the tiles
  42. @tileRectangles = []
  43.  
  44. # Total count of tiles
  45. tileCount = @tilesWide * @tilesHigh
  46.  
  47. # Create tile rectangles for the whole tileset
  48. tileIndex = 0
  49. # Loop through the rows of the tileset
  50. y = 0
  51. while y < @tilesHigh
  52. # Loop through the columns of the current row
  53. x = 0
  54. while x < @tilesWide
  55. # Create a rectangle object for this tile position
  56. @tileRectangles[tileIndex] = new Rectangle(
  57. x * @tileWidth,
  58. y * @tileHeight,
  59. @tileWidth,
  60. @tileHeight
  61. )
  62. tileIndex++
  63. x++
  64. y++
  65.  
  66. this
  67.  
  68. # Set up event emitter
  69. Tileset.prototype = EventEmitter.prototype
  70.  
  71. # Load the tileset image from a URL and save it into the instance
  72. # emit an event to inform listeners of the image being succesfully
  73. # loaded into the Tileset.
  74. Tileset.prototype.loadImageFromURL = (imageURL) ->
  75. # Create the image object
  76. image = new Canvas.Image
  77. image.onload = () =>
  78. @image = image
  79. # Inform our listeners of this happening
  80. @emit('tilesetImageLoaded', image)
  81. image.src = imageURL
  82.  
  83. # Implement Tileset.Error
  84. Tileset.Error = Util.implementErrorClass('TilesetError', {
  85. 100: "Tileset image is not a valid Image or Canvas.Image object",
  86. 101: "Tileset width and height must be more than zero",
  87. 102: "Tileset tile sizes must be more than zero",
  88. 103: "Space required by tiles is larger than given image"
  89. })
  90.  
  91. # Exports
  92. module.exports = Tileset
Add Comment
Please, Sign In to add comment