EaseVectorEffect.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2009-2026 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.vectoreffect;
  33. import com.jme3.math.EaseFunction;
  34. import com.jme3.math.Easing;
  35. import com.jme3.math.Vector4f;
  36. /**
  37. *
  38. * @author yaRnMcDonuts
  39. */
  40. public final class EaseVectorEffect extends AbstractVectorEffect implements VectorEffect {
  41. private VectorGroup targetVectors;
  42. private VectorGroup startVectors;
  43. private float duration = 0f;
  44. private float easeTimer = 0f;
  45. private float delay = 0f;
  46. private float delayTimer = 0f;
  47. private EaseFunction easeFunction = Easing.linear;
  48. private final Vector4f tempTargetVec = new Vector4f();
  49. private final Vector4f tempStartVec = new Vector4f();
  50. public EaseVectorEffect(VectorGroup vectorToModify) {
  51. super(vectorToModify);
  52. }
  53. public EaseVectorEffect(VectorGroup vectorToModify, VectorGroup targetVector, float duration) {
  54. this(vectorToModify);
  55. setEaseToValueOverDuration(duration, targetVector);
  56. }
  57. public EaseVectorEffect(VectorGroup vectorToModify, VectorGroup targetVector, float duration,
  58. float delay) {
  59. this(vectorToModify);
  60. setEaseToValueOverDuration(duration, targetVector);
  61. setDelayTime(delay);
  62. }
  63. public EaseVectorEffect(VectorGroup vectorToModify, VectorGroup targetVector, float duration,
  64. EaseFunction easeFunction) {
  65. this(vectorToModify, targetVector, duration);
  66. setEaseFunction(easeFunction);
  67. }
  68. public EaseVectorEffect(VectorGroup vectorToModify, VectorGroup targetVector, float duration,
  69. EaseFunction easeFunction, float delay) {
  70. this(vectorToModify, targetVector, duration);
  71. setEaseFunction(easeFunction);
  72. setDelayTime(delay);
  73. }
  74. @Override
  75. public void update(float tpf) {
  76. super.update(tpf);
  77. if (delayTimer <= delay) {
  78. delayTimer += tpf;
  79. return;
  80. }
  81. if (startVectors == null) {
  82. startVectors = vectorsToModify.clone();
  83. }
  84. easeTimer += tpf;
  85. float t = Math.min(easeTimer / duration, 1f);
  86. float easedT = easeFunction.apply(t);
  87. for(int v = 0; v < vectorsToModify.getSize(); v++){
  88. int targetIndex = Math.min(v, targetVectors.getSize() - 1); //allows multiple vectors to share a lesser number of targets if desired
  89. targetVectors.getAsVector4(targetIndex, tempTargetVec);
  90. startVectors.getAsVector4(v, tempStartVec);
  91. tempTargetVec.subtractLocal(tempStartVec);
  92. tempTargetVec.multLocal(easedT);
  93. vectorsToModify.updateVectorObject(tempStartVec.addLocal(tempTargetVec), v);
  94. }
  95. if (t >= 1f) {
  96. super.setIsFinished(true);
  97. }
  98. }
  99. public EaseVectorEffect setEaseToValueOverDuration(float dur, VectorGroup targetVector) {
  100. this.targetVectors = targetVector;
  101. duration = dur;
  102. startVectors = null;
  103. return this;
  104. }
  105. public void setDelayTime(float delay) {
  106. this.delay = delay;
  107. }
  108. public void setEaseFunction(EaseFunction func) {
  109. this.easeFunction = func;
  110. }
  111. @Override
  112. public void reset() {
  113. delayTimer = 0;
  114. easeTimer = 0;
  115. startVectors = null;
  116. super.reset();
  117. }
  118. }