001package com.fs.starfarer.api.impl.campaign.eventide;
002
003import java.util.ArrayList;
004import java.util.LinkedHashSet;
005import java.util.List;
006import java.util.Set;
007
008import org.lwjgl.util.vector.Vector2f;
009
010import com.fs.starfarer.api.Global;
011import com.fs.starfarer.api.graphics.SpriteAPI;
012
013public class CharAnim implements Cloneable {
014        
015        public String textureId;
016        public SpriteAPI sprite;
017        public float scale = 1f;
018        public float frameHeight;
019        public float widthSoFar = 0f;
020        public float totalTime = 0f;
021        public List<CharAnimFrame> frames = new ArrayList<CharAnimFrame>();
022        public CharAnimFrame last;
023        public Float initialRelativeOffset = null;
024        public Vector2f moveToIdle = new Vector2f();
025        public String action;
026        
027        public Set<String> interruptableBy = new LinkedHashSet<String>();
028        
029        @Override
030        public CharAnim clone() {
031                try {
032                        CharAnim copy = (CharAnim) super.clone();
033                        copy.frames = new ArrayList<CharAnimFrame>();
034                        for (CharAnimFrame frame : frames) {
035                                CharAnimFrame cf = frame.clone();
036                                copy.frames.add(cf);
037                        }
038                        copy.sprite = Global.getSettings().getSprite(textureId);
039                        copy.totalTime = totalTime;
040                        copy.interruptableBy = new LinkedHashSet<String>(interruptableBy);
041                        copy.moveToIdle = new Vector2f(moveToIdle);
042                        copy.last = last.clone();
043                        return copy;
044                } catch (CloneNotSupportedException e) {
045                        throw new RuntimeException(e);
046                }
047        }
048        
049        public void removeFirstFrame() {
050                CharAnimFrame frame = frames.remove(0);
051                totalTime -= frame.dur;
052        }
053
054        public CharAnim(String textureId, String action, float frameHeight) {
055                this.textureId = textureId;
056                this.action = action;
057                sprite = Global.getSettings().getSprite(textureId);
058                this.frameHeight = frameHeight;
059        }
060        
061        public boolean hasBlockFrames() {
062                for (CharAnimFrame frame : frames) {
063                        if (!frame.blockArea.isEmpty()) return true;
064                }
065                return false;
066        }
067        public boolean hasAttackFrames() {
068                for (CharAnimFrame frame : frames) {
069                        if (!frame.attackArea.isEmpty()) return true;
070                }
071                return false;
072        }
073        
074        public void interruptableBy(String ...actions) {
075                for (String action : actions) {
076                        interruptableBy.add(action);
077                }
078        }
079        
080//      public CharAnim createReverse(String action) {
081//              CharAnim rev = new CharAnim(textureId, action, frameHeight);
082//              for (CharAnimFrame frame : frames) {
083//                      CharAnimFrame copy = frame.clone();
084//                      copy.move.negate();
085//                      rev.frames.add(0, copy);
086//              }
087//              rev.last = rev.frames.get(rev.frames.size() - 1);
088//              rev.scale = scale;
089//              rev.totalTime = totalTime;
090//              rev.widthSoFar = widthSoFar;
091//              //rev.moveToIdle = // depends on the new last frame; can't auto-figure-this-out
092//              return rev;
093//      }
094
095        public void skip(CharAnim anim) {
096                widthSoFar += anim.widthSoFar;
097        }
098        public void skip(float frameWidth) {
099                widthSoFar += frameWidth;
100        }
101        public void addFrame(float y, float frameWidth, float dur) {
102                float txPerPixel = sprite.getTextureWidth() / sprite.getWidth();
103                float tyPerPixel = sprite.getTextureHeight() / sprite.getHeight();
104                
105                CharAnimFrame frame = new CharAnimFrame();
106                frame.dur = dur;
107                frame.tx = widthSoFar * txPerPixel; 
108                frame.ty = y * tyPerPixel;
109                frame.tw = frameWidth * txPerPixel;
110                frame.th = frameHeight * tyPerPixel;
111                frame.width = frameWidth;
112                frame.height = frameHeight;
113                widthSoFar += frameWidth;
114                
115                totalTime += dur;
116                
117                frames.add(frame);
118                last = frame;
119        }
120        
121        
122        public float getTotalTime() {
123                return totalTime;
124        }
125        
126        public void updateTextureScale(float scale) {
127                for (CharAnimFrame frame : frames) { 
128                        frame.tx *= scale;
129                        frame.ty *= scale;
130                        frame.tw *= scale;
131                        frame.th *= scale;
132                }
133        }
134}
135
136
137
138
139
140