item.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. function Item(game) {
  2. this.id = 0;
  3. this.position = {x: 0, y: 0};
  4. this.width = 32;
  5. this.height = 32;
  6. this.color = "green";
  7. this.name = "Food";
  8. this.spriteCanvas = null;
  9. this.spriteContext = null;
  10. this.sprite = "food1";
  11. this.init = function(itemData) {
  12. for(var key in itemData){
  13. if(!this.hasOwnProperty(key)) { continue; }
  14. this[key] = itemData[key];
  15. }
  16. }
  17. this.update = function(delta) {
  18. }
  19. this.preRender = function(context, angle) {
  20. context.fillStyle = this.color;
  21. context.strokeStyle = this.color;
  22. context.lineWidth = 4;
  23. context.save();
  24. context.translate(0.5, 0.5);
  25. context.drawImage(system.assets.getSprite(this.sprite), 0, 0, 16, 16, 0, 0, 32, 32);
  26. /*context.beginPath();
  27. context.moveTo(0, 0);
  28. context.arc(0, 0, this.width / 4, 0, 2 * Math.PI);
  29. context.fill();
  30. context.stroke();*/
  31. context.restore();
  32. }
  33. this.draw = function(context) {
  34. if(this.spriteContext == null) {
  35. this.spriteCanvas = document.createElement("canvas");
  36. this.spriteCanvas.width = this.width;
  37. this.spriteCanvas.height = this.height;
  38. this.spriteContext = this.spriteCanvas.getContext("2d");
  39. this.preRender(this.spriteContext);
  40. }
  41. context.save();
  42. context.translate(this.position.x - (this.width / 2), this.position.y - (this.height / 2));
  43. context.drawImage(this.spriteCanvas, 0, 0);
  44. //bounds
  45. /*context.strokeStyle = "#FF7733";
  46. context.beginPath();
  47. context.moveTo(0, 0);
  48. context.lineTo(this.width, 0);
  49. context.lineTo(this.width, this.height);
  50. context.lineTo(0, this.height);
  51. context.lineTo(0, 0);
  52. context.stroke();*/
  53. context.restore();
  54. if(game.displayItemNames) {
  55. context.save();
  56. context.translate(this.position.x, this.position.y);
  57. context.font = "16px sans-serif";
  58. context.textAlign = "center";
  59. context.textBaseline = "middle";
  60. context.fillStyle = "#DDDDDD";
  61. context.strokeStyle = "#182208";
  62. context.lineWidth = 2;
  63. var textMetrics = context.measureText(this.name);
  64. var textWidth = textMetrics.width;
  65. context.strokeText(this.name, 0, -(this.height));
  66. context.fillText(this.name, 0, -(this.height));
  67. context.restore();
  68. }
  69. }
  70. };