/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pesegato.MonkeySheet;

import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.control.AbstractControl;
import com.pesegato.MonkeySheet.actions.MSAction;
import com.pesegato.timing.Timeable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Pesegato
 */
public class MSControl extends AbstractControl {

    static Logger log = LoggerFactory.getLogger(MSControl.class);

    public MTween anim;
    public String animation;
    public int position;
    boolean runOnce = false;
    MSAnimationManager animManager;
    public MSAction msAction;

    public MSControl(String anim, Timeable timeable) {
        playForever(anim);
        MonkeySheetAppState.timeable = timeable;
    }

    public MSControl(MSAnimationManager animManager) {
        this.animManager = animManager;
    }

    final public void playForever(String ani) {
        log.debug("now playing FOREVER animation {}", ani);
        anim = MonkeySheetAppState.getAnim(ani);
        position = 0;
        runOnce = false;
        if (anim == null) {
            log.warn("Running UNINITIALIZED animation {}, GOING TO CRASH VERY SOON!!!", ani);
        }
    }

    public void playOnce(String ani) {
        log.debug("now playing ONCE animation {}", ani);
        anim = MonkeySheetAppState.getAnim(ani);
        position = 0;
        runOnce = true;
        if (anim == null) {
            log.warn("Running UNINITIALIZED animation {}, GOING TO CRASH VERY SOON!!!" + ani);
        }
    }

    @Override
    protected void controlUpdate(float tpf) {
        if (MonkeySheetAppState.tTPF == 0) {
            log.trace("position: {} - {}", position, anim.anim[position].position);
            position++;
            if (position >= anim.anim.length - 1) {
                if (runOnce) {
                    if (animManager != null) {
                        log.trace("loading animManager {}", animManager);
                        animManager.whatPlay(this);
                    }
                    if (msAction != null) {
                        log.trace("end of MSAction {}", msAction);
                        msAction.terminatedAnim();
                        msAction.whatPlay(this);
                    }
                } else {
                    position = 0;
                }
            }
        }

    }

    public int getCurrentHitbox() {
        return anim.hitbox[position];
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

}

@@ -1906,32 +1906,8 @@
 n);%0A
-            position++;%0A
     
@@ -1931,10 +1931,9 @@
 ion 
-%3E=
+%3C
  ani
@@ -1965,24 +1965,55 @@
             
+position++;%0A            %7D else 
 if (runOnce)
@@ -2027,28 +2027,24 @@
             
-  
-  
 if (animMana
@@ -2050,36 +2050,32 @@
 ager != null) %7B%0A
-    
                 
@@ -2144,28 +2144,24 @@
             
-    
 animManager.
@@ -2184,36 +2184,32 @@
                 
-    
 %7D%0A              
@@ -2206,28 +2206,24 @@
             
- 
-   
 if (msAction
@@ -2226,36 +2226,32 @@
 tion != null) %7B%0A
-    
                 
@@ -2309,36 +2309,32 @@
                 
-    
 msAction.termina
@@ -2344,20 +2344,16 @@
 Anim();%0A
-    
         
@@ -2401,34 +2401,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -2436,36 +2436,32 @@
                 
-    
 position = 0;%0A  
@@ -2458,34 +2458,16 @@
 on = 0;%0A
-                %7D%0A
         

Fix ArrayIndexOutOfBounds