1
1

AnimationHandler.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /// <reference path="../Elements.ts" />
  2. /// <reference path="../Classes/AnimationLayer.ts" />
  3. // Animations must have 200x200 frames, placed in a spritesheet with a single row
  4. // Use https://github.com/williammalone/HTML5-Photoshop-Sprite-Sheet-Creator
  5. // To make animations of varying Frames Per Second fit, we will have breakpoints in percentage that delimitate what is to be expected.
  6. // Example: at 25% animation length, that's the Back-Thrust moment, so all the animations should expect and behave as if that was happening right then.
  7. // 75% would be front-thrust, etc.
  8. // The idea is to have the base animation look "ok" without anything going on, but to also have things going on just fit mathmagically.
  9. // Support to animations that contain the WHOLE of the events should be possible as to make it so we can make special animations for special circumstances. e.g. only encounter with the ogre
  10. enum AnimationLayerLevel {
  11. BACKGROUND = 1,
  12. MIDDLE = 150,
  13. FRONT = 300
  14. }
  15. interface AnimationInstruction {
  16. name: string;
  17. layer?: AnimationLayerLevel | number;
  18. }
  19. module Elements.AnimationHandler {
  20. var container = document.getElementById("sceneAnimation");
  21. var background = document.createElement("a");
  22. var stop = true;
  23. export var spriteSize = 200;
  24. export var animations : Array<AnimationLayer> = [];
  25. export var animationSpeed = 1500; // how long to complete in ms
  26. export var lastTime = 0;
  27. export var percentage;
  28. export function clearAnimations () {
  29. while (container.firstChild != undefined) {
  30. container.removeChild(container.firstChild);
  31. }
  32. animations = [];
  33. }
  34. export function addAnimations (...names : Array<AnimationInstruction>) {
  35. fixVerticalPosition();
  36. for (let i = 0; i < names.length; i++) {
  37. addAnimation(names[i].name);
  38. }
  39. }
  40. export function addAnimation (name : string, layer = 1) {
  41. let newAnim = new AnimationLayer(name, layer);
  42. container.appendChild(newAnim.getElement());
  43. animations.push(newAnim);
  44. return newAnim;
  45. }
  46. export function setBackground (name : string) {
  47. background.className = "";
  48. background.classList.add("bg-" + name);
  49. background.classList.add("sceneAnimation");
  50. background.style.zIndex = "0";
  51. container.appendChild(background);
  52. }
  53. export function beginAnimate (targetAnimationSpeed? : number) {
  54. animationSpeed = targetAnimationSpeed == undefined ? 1500 : targetAnimationSpeed;
  55. lastTime = (new Date()).getTime();
  56. percentage = 0;
  57. container.style.display = "";
  58. fixVerticalPosition();
  59. stop = false;
  60. animate();
  61. }
  62. export function animate () {
  63. let time = (new Date()).getTime();
  64. let percGain = (time - lastTime) / animationSpeed;
  65. percentage += percGain;
  66. while (percentage > 1) {
  67. percentage -= 1;
  68. }
  69. for (let i = 0; i < animations.length; i++) {
  70. animations[i].updateSprite(percentage);
  71. }
  72. lastTime = time;
  73. if (!stop) {
  74. window.requestAnimationFrame(Elements.AnimationHandler.animate);
  75. }
  76. }
  77. export function fixVerticalPosition () {
  78. spriteSize = getSize(true);
  79. //container.style.marginBottom = (availHeight - 200) + "px";
  80. container.style.width = spriteSize + "px";
  81. container.style.marginLeft = "-" + (spriteSize / 2) + "px";
  82. container.style.height = spriteSize + "px";
  83. animations.forEach((anim) => {
  84. anim.updateSize();
  85. });
  86. if (background.parentElement == container) {
  87. background.style.transform = "";
  88. let height = background.offsetHeight;
  89. let width = background.offsetWidth;
  90. if (height != 0 && width != 0) {
  91. background.style.transform = "scale(" + (spriteSize / height) + ")";
  92. background.style.marginLeft = (-width / 2) + "px";
  93. background.style.marginTop = (-height / 2) + "px";
  94. }
  95. }
  96. }
  97. export function getSize (fixMin = true) {
  98. let tab = document.getElementById("currentTurn");
  99. //let tab1 = document.getElementById("roomName");
  100. //tab1.style.marginTop = "";
  101. let height = window.innerHeight - tab.clientHeight;
  102. return height;
  103. }
  104. export function stopAnimate () {
  105. stop = true;
  106. container.style.display = "none";
  107. clearAnimations();
  108. getSize(false);
  109. }
  110. stopAnimate();
  111. }
  112. window.addEventListener("resize", () => { Elements.AnimationHandler.fixVerticalPosition(); });