SequencedVectorEffect.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 java.util.ArrayList;
  34. import java.util.Collections;
  35. /**
  36. *
  37. * @author yaRnMcDonuts
  38. */
  39. public class SequencedVectorEffect extends AbstractVectorEffect implements VectorEffect {
  40. private final ArrayList<VectorEffect> effects = new ArrayList<>();
  41. private int currentIndex = 0;
  42. private boolean isRepeatingInfinitely = false;
  43. private float numTimesToRepeat = -1;
  44. private float currentCycle = 0;
  45. public void setLooping(boolean repeat) { this.isRepeatingInfinitely = repeat; }
  46. public void setRepeatNumberOfTimes(float repititionCount){ this.numTimesToRepeat = repititionCount; }
  47. public void addEffect(VectorEffect effect) { effects.add(effect); }
  48. public SequencedVectorEffect(VectorEffect... effects) {
  49. super();
  50. Collections.addAll(this.effects, effects);
  51. }
  52. @Override
  53. public void update(float tpf) {
  54. super.update(tpf);
  55. if (effects.isEmpty()) {
  56. setIsFinished(true);
  57. return;
  58. }
  59. VectorEffect current = effects.get(currentIndex);
  60. current.update(tpf);
  61. if (current.isFinished()) {
  62. currentIndex++;
  63. if (currentIndex >= effects.size()) {
  64. currentCycle++;
  65. reset();
  66. if (!isRepeatingInfinitely && currentCycle >= numTimesToRepeat) {
  67. setIsFinished(true);
  68. currentCycle = 0;
  69. }
  70. }
  71. }
  72. }
  73. @Override
  74. public void reset() {
  75. super.reset();
  76. isFinished = false;
  77. currentIndex = 0;
  78. for (VectorEffect e : effects) {
  79. e.reset();
  80. }
  81. }
  82. }