AnimationLayer.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /// <reference path="../Elements.ts" />
  2. // parseInt(Elements.getStyle(".anims-orcNotStand").style.width.slice(0, -2))
  3. class AnimationLayer {
  4. private name = "unknown";
  5. private layer = 0;
  6. private cStep = 0;
  7. private totalSteps = 9;
  8. private frameSize = 200;
  9. private heightMult = 1; // used to create shadows
  10. private feetHeight = 0; // used to move shadows up and down
  11. private filters = ["hue-rotate(0deg)", "brightness(1)"];
  12. private el = document.createElement("a");
  13. constructor (filename : string, layer? : number) {
  14. this.name = filename;
  15. this.layer = layer;
  16. this.el.classList.add("sceneAnimation");
  17. if (layer != undefined) {
  18. this.el.style.zIndex = layer.toString();
  19. }
  20. let style = Elements.getStyle(".anims-" + filename);
  21. if (style == undefined || style.width == undefined) {
  22. console.error("Animation " + filename + " not found.");
  23. } else {
  24. let height = (parseInt(style.height.slice(0,-2)));
  25. this.frameSize = height;
  26. this.totalSteps = (parseInt(style.width.slice(0,-2)) / height);
  27. }
  28. this.el.classList.add("anims-" + filename);
  29. this.updateSize();
  30. }
  31. public updateSize () {
  32. this.el.style.transform = "scale(" + (Elements.AnimationHandler.spriteSize / this.frameSize) + "," + this.heightMult * (Elements.AnimationHandler.spriteSize / this.frameSize) + ")";
  33. this.el.style.width = this.frameSize + "px";
  34. this.el.style.height = this.frameSize + "px";
  35. this.el.style.marginLeft = "-" + this.frameSize/2 + "px";
  36. this.el.style.marginTop = "-" + ((this.frameSize / 2)) + "px";
  37. if (this.heightMult != 1) {
  38. let realHeight = this.heightMult * (Elements.AnimationHandler.spriteSize / this.frameSize) * this.frameSize;
  39. this.el.style.top = "100%";
  40. this.el.style.marginTop = (- (this.feetHeight - (this.feetHeight * this.heightMult))) + "px";
  41. //this.el.style.top = "100%";
  42. //this.el.style.marginTop = "-" + (realHeight + this.feetHeight);
  43. console.log("finalHeight: ", realHeight, "feetHeight:", this.feetHeight, "target:" , 130, this.el.style.marginTop);
  44. // Originally top: 50%;
  45. }
  46. }
  47. public addShadow (feetHeight : number) {
  48. let shadow = Elements.AnimationHandler.addAnimation(this.name, this.layer - 1);
  49. shadow.heightMult = 0.15;
  50. shadow.feetHeight = feetHeight;
  51. shadow.updateSize();
  52. shadow.brightify(0);
  53. }
  54. public updateSprite (percentage : number) {
  55. this.cStep = Math.floor(percentage * this.totalSteps);
  56. this.el.style.backgroundPositionX = "-" + (this.cStep * this.frameSize) + "px";
  57. }
  58. /**
  59. * Turn refers to how much we turn the hue wheel, in degres.
  60. * Anything that's colorizable begins at red, so:
  61. * RED = 0
  62. * YELLOW = 60
  63. * GEREN = 120
  64. * BLUE = 240
  65. * RED = 360
  66. *
  67. * So pink is between RED and BLUE. 300 can be pink.
  68. * @param degrees
  69. */
  70. public colorize (degrees : number) {
  71. this.filters[0] = "hue-rotate(" + degrees + "deg)";
  72. this.updateFilter();
  73. }
  74. public brightify (percent : number) {
  75. this.filters[1] = "brightness(" + percent + ")";
  76. this.updateFilter();
  77. }
  78. public updateFilter () {
  79. this.el.style.filter = this.filters.join(" ");
  80. }
  81. public getElement () {
  82. return this.el;
  83. }
  84. }
  85. /**
  86. Planning...
  87. - Player Spritesheets need to have certain important markers down, set as connect points.
  88. ----- Top of Head coordinates (used for "stunned" effects, whatever)
  89. ----- Crotch coordinates
  90. ----- Facing Direction
  91. ----- Mouth Coordinates
  92. These are, of course, connection points.
  93. **/