001package com.fs.starfarer.api.impl.campaign.eventide;
002
003import java.awt.Color;
004import java.util.LinkedHashSet;
005import java.util.Set;
006
007import org.lwjgl.opengl.GL11;
008import org.lwjgl.util.vector.Vector2f;
009
010import com.fs.starfarer.api.Global;
011import com.fs.starfarer.api.graphics.SpriteAPI;
012import com.fs.starfarer.api.util.Misc;
013
014public class AnimAction {
015        public CharAnim anim;
016        public CharAnimFrame curr;
017        public float progress;
018        public Actor actor;
019        public boolean done = false;
020        
021        public boolean performedBlock = false;
022        public boolean wasBlocked = false;
023        public boolean scoredHit = false;
024        
025        public boolean makeCurrentFrameLast = false;
026        
027        public SpriteAPI sprite;
028        public CharAnimFrame preFrame;
029        public float returnToIdleXCorrection = 0f;
030        
031        public Set<CharAnimFrame> playedSoundsFor = new LinkedHashSet<CharAnimFrame>();
032        
033        public AnimAction(Actor actor, Object animKey, CharAnimFrame preFrame) {
034                this.actor = actor;
035                this.preFrame = preFrame;
036                this.anim = Actions.ANIMATIONS.get(animKey);
037                this.sprite = Global.getSettings().getSprite(anim.textureId);
038                if (actor.texId != null) {
039                        this.sprite = Global.getSettings().getSprite(actor.texId);
040                }
041        }
042        
043//      public AnimAction(Actor actor, CharAnim anim) {
044//              this.actor = actor;
045//              this.anim = anim;
046//      }
047        public int framesUntilAttackFrame() {
048                int index = 0;
049                for (CharAnimFrame frame : anim.frames) {
050                        if (!frame.attackArea.isEmpty()) {
051                                index = anim.frames.indexOf(frame);
052                        }
053                }
054                int currIndex = anim.frames.indexOf(curr);
055                
056                return index - currIndex;
057        }
058        
059        public void undoLastMove() {
060                if (curr != null) {
061                        actor.loc.x -= curr.move.x * anim.scale * actor.facing;
062                        actor.loc.y -= curr.move.y * anim.scale * actor.facing;
063                }
064        }
065        
066        public void advance(float amount) {
067                progress += amount;
068                float total = 0f;
069                for (CharAnimFrame f : anim.frames) {
070                        total += f.dur;
071                        if (total > progress) {
072                                if (curr != f) {
073                                        if (makeCurrentFrameLast) {
074                                                progress = anim.getTotalTime();
075                                        } else {
076                                                curr = f;
077                                                actor.loc.x += curr.move.x * anim.scale * actor.facing;
078                                                actor.loc.y += curr.move.y * anim.scale * actor.facing;
079                                                
080                                                if (!playedSoundsFor.contains(curr)) {
081                                                        playedSoundsFor.add(curr);
082                                                        for (String soundId : curr.soundIds) {
083                                                                if (soundId == null || soundId.isEmpty()) continue;
084                                                                //Global.getSoundPlayer().playUISound(soundId, 1f, 1f);
085                                                                Vector2f soundLoc = new Vector2f(actor.loc);
086                                                                soundLoc.x *= DuelPanel.SOUND_LOC_MULT;
087                                                                Global.getSoundPlayer().playSound(soundId, 1f, 1f, soundLoc, new Vector2f());
088                                                        }
089                                                }
090                                                
091                                                if (curr == anim.frames.get(0) && preFrame != null && anim.initialRelativeOffset != null) {
092                                                        float preW = preFrame.width * anim.scale;
093                                                        float w = curr.width * anim.scale;
094                                                        returnToIdleXCorrection = (preW - w) / 2f * actor.facing + anim.initialRelativeOffset * anim.scale;
095                                                        actor.loc.x -= returnToIdleXCorrection;
096                                                        returnToIdleXCorrection = 0f;
097                                                }
098                                        }
099                                }
100                                break;
101                        }
102                }
103                if (progress >= anim.getTotalTime()) {
104                        actor.loc.x += anim.moveToIdle.x * anim.scale * actor.facing + returnToIdleXCorrection;
105                        actor.loc.y += anim.moveToIdle.y * anim.scale * actor.facing;
106                        done = true;
107                }
108        }
109        
110        public void render(float alphaMult) {
111                if (curr == null) return;
112                
113//              if (actor.facing > 0 && curr != null && curr == anim.frames.get(0)) {
114//                      System.out.println("Progress: " + progress);
115//              }
116                
117                float x = actor.loc.x;
118                float y = actor.loc.y;
119                sprite.setAlphaMult(alphaMult);
120                sprite.setSize(curr.width * anim.scale, curr.height * anim.scale);
121                
122                //sprite.setBlendFunc(GL11.GL_SRC_COLOR, GL11.GL_ONE_MINUS_SRC_COLOR);
123                
124                if (actor.facing > 0) {
125                        sprite.setTexX(curr.tx);
126                        sprite.setTexY(curr.ty);
127                        sprite.setTexWidth(curr.tw);
128                        sprite.setTexHeight(curr.th);
129                } else {
130                        sprite.setTexX(curr.tx + curr.tw);
131                        sprite.setTexY(curr.ty);
132                        sprite.setTexWidth(-curr.tw);
133                        sprite.setTexHeight(curr.th);
134                }
135                sprite.renderAtCenter(x, y);
136                
137//              GL11.glDisable(GL11.GL_TEXTURE_2D);
138//              GL11.glEnable(GL11.GL_BLEND);
139//              GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
140//              Misc.renderQuad(x - curr.width / 2f, y - curr.height / 2f, curr.width, curr.height, Color.red, 0.5f);
141                
142                if (DuelPanel.DEBUG) {
143                        GL11.glDisable(GL11.GL_TEXTURE_2D);
144                        GL11.glEnable(GL11.GL_BLEND);
145                        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
146                        for (HitArea area : curr.hittableArea) {
147                                area = area.getAdjustedForAction(this);
148                                Misc.renderQuad(area.x, area.y, area.w, area.h, new Color(120,120,120,127), alphaMult);
149                        }
150                        for (HitArea area : curr.attackArea) {
151                                area = area.getAdjustedForAction(this);
152                                Misc.renderQuad(area.x, area.y, area.w, area.h, new Color(200,60,60,127), alphaMult);
153                        }
154                        for (HitArea area : curr.blockArea) {
155                                area = area.getAdjustedForAction(this);
156                                Misc.renderQuad(area.x, area.y, area.w, area.h, new Color(60,200,60,127), alphaMult);
157                        }
158                }
159        }
160
161        public boolean isDone() {
162                return done;
163        }
164        
165}
166
167
168
169
170