1
1

AnimationHandler.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 = 0,
  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 stop = true;
  22. export var spriteSize = 200;
  23. export var animations : Array<AnimationLayer> = [];
  24. export var animationSpeed = 1500; // how long to complete in ms
  25. export var lastTime = 0;
  26. export var percentage;
  27. export function clearAnimations () {
  28. while (container.firstChild != undefined) {
  29. container.removeChild(container.firstChild);
  30. }
  31. animations = [];
  32. }
  33. export function addAnimations (...names : Array<AnimationInstruction>) {
  34. fixVerticalPosition();
  35. for (let i = 0; i < names.length; i++) {
  36. addAnimation(names[i].name);
  37. }
  38. }
  39. export function addAnimation (name : string, layer = 0) {
  40. let newAnim = new AnimationLayer(name, layer);
  41. container.appendChild(newAnim.getElement());
  42. animations.push(newAnim);
  43. return newAnim;
  44. }
  45. export function beginAnimate (targetAnimationSpeed? : number) {
  46. animationSpeed = targetAnimationSpeed == undefined ? 1500 : targetAnimationSpeed;
  47. lastTime = (new Date()).getTime();
  48. percentage = 0;
  49. fixVerticalPosition();
  50. container.style.display = "";
  51. stop = false;
  52. animate();
  53. }
  54. export function animate () {
  55. let time = (new Date()).getTime();
  56. let percGain = (time - lastTime) / animationSpeed;
  57. percentage += percGain;
  58. while (percentage > 1) {
  59. percentage -= 1;
  60. }
  61. for (let i = 0; i < animations.length; i++) {
  62. animations[i].updateSprite(percentage);
  63. }
  64. lastTime = time;
  65. if (!stop) {
  66. window.requestAnimationFrame(Elements.AnimationHandler.animate);
  67. }
  68. }
  69. function fixVerticalPosition () {
  70. spriteSize = getSize(true);
  71. //container.style.marginBottom = (availHeight - 200) + "px";
  72. container.style.width = spriteSize + "px";
  73. container.style.marginLeft = "-" + (spriteSize / 2) + "px";
  74. container.style.height = spriteSize + "px";
  75. animations.forEach((anim) => {
  76. anim.updateSize();
  77. })
  78. }
  79. export function getSize (fixMin = true) {
  80. let tab = document.getElementById("currentTurn");
  81. //let tab1 = document.getElementById("roomName");
  82. //tab1.style.marginTop = "";
  83. let height = window.innerHeight - tab.clientHeight;
  84. return height;
  85. }
  86. export function stopAnimate () {
  87. stop = true;
  88. container.style.display = "none";
  89. clearAnimations();
  90. getSize(false);
  91. }
  92. stopAnimate();
  93. }