MWPhases.java :  » Game » magicwars » mw » client » gui » Java Open Source

Java Open Source » Game » magicwars 
magicwars » mw » client » gui » MWPhases.java
package mw.client.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.EnumSet;

import javax.swing.ImageIcon;
import javax.swing.JComponent;

import mw.client.managers.ConnectionManager;
import mw.client.managers.ImageManager;
import mw.client.managers.SettingsManager;
import mw.client.managers.WindowManager;
import mw.client.utils.gui.Transparency;
import mw.server.model.MagicWarsModel.PhaseName;

@SuppressWarnings("serial")
public class MWPhases extends JComponent implements MouseListener {

    int table_height;

    // ////////////////////////////////////////////////////////////////////
    // phases
    MediaTracker media = new MediaTracker(this);
    Image imageSteps;
    Image imageStepsTransparent;
    Image imagePhase;
    Image imagePhaseTransparent;

    // mark
    Image imageRedMark;
    Image imageRedMarkTransparent;
    int mwidth = 0;
    int mheight = 0;

    private int phaseImageIndex = 0;
    private Image currentPhaseImage;
    
    int iwidth, iheight;
    ArrayList<Image> phaseImages = new ArrayList<Image>();
    // //////////////////////////////////////////////////////////////////

    // //////////////////////////////////////////////////////////////////
    static final PhaseName intToMTGPhase[] = { PhaseName.blank,
            PhaseName.untap, PhaseName.upkeep, PhaseName.draw, PhaseName.main1,
            PhaseName.combat_declare_attackers,
            // PhaseName.combat_declare_blockers,
            // PhaseName.combat_predamage_player,
            // PhaseName.combat_predamage_opponent,
            // PhaseName.combat_stack_damage_player,
            // PhaseName.combat_stack_damage_opponent,
            // PhaseName.combat_damage,
            PhaseName.main2, PhaseName.endofturn, PhaseName.cleanup, PhaseName.blank};
    private EnumSet<PhaseName> phaseMarks = EnumSet.noneOf(PhaseName.class);
    // //////////////////////////////////////////////////////////////////

    // //////////////////////////////////////////////////////////////////
    int owner_id;

    // //////////////////////////////////////////////////////////////////

    public MWPhases(int owner_id) {
        this.owner_id = owner_id;

        // ////////////////////////////////////////////////////////////////
        // load images and create them transparent
        {
            imageStepsTransparent = ImageManager.getImageSteps();

            // ////////////////////////////////////////////////////////////
            // phases
            phaseImages.add(ImageManager.getUntapPhaseImage());
            phaseImages.add(ImageManager.getUpkeepPhaseImage());
            phaseImages.add(ImageManager.getDrawPhaseImage());
            phaseImages.add(ImageManager.getMain1PhaseImage());
            phaseImages.add(ImageManager.getAttackPhaseImage());
            phaseImages.add(ImageManager.getMain2PhaseImage());
            phaseImages.add(ImageManager.getDiscardPhaseImage());
            phaseImages.add(ImageManager.getCleanPhaseImage());
            phaseImages.add(ImageManager.getEndOfTurnPhaseImage());
            iwidth = phaseImages.get(0).getWidth(null);
            iheight = phaseImages.get(0).getHeight(null);
            // ////////////////////////////////////////////////////////////

            // ////////////////////////////////////////////////////////////
            // mark
            imageRedMarkTransparent = ImageManager.getRedMark();
            mwidth = imageRedMarkTransparent.getWidth(this);
            mheight = imageRedMarkTransparent.getHeight(this);
            // ////////////////////////////////////////////////////////////
        }
        // ////////////////////////////////////////////////////////////////

        // ////////////////////////////////////////////////////////////
        phaseImageIndex = 0;
        // ////////////////////////////////////////////////////////////

        // ////////////////////////////////////////////////////////////
        // set default marks
        if (owner_id != 0) {
            phaseMarks.add(PhaseName.main1); // main1 phase
            phaseMarks.add(PhaseName.combat_declare_attackers);
            phaseMarks.add(PhaseName.main2); // main2 phase
        }
        // ////////////////////////////////////////////////////////////
    }

    public void loadImage(String path) throws InterruptedException {
        imagePhase = new ImageIcon(path).getImage();
        media.addImage(imagePhase, 0);
        media.waitForID(0);
        imagePhaseTransparent = Transparency.makeColorTransparent(imagePhase,
                Color.YELLOW);
        media.addImage(imagePhaseTransparent, 1);
        media.waitForID(1);
        phaseImages.add(imagePhaseTransparent);
        iwidth = imagePhase.getWidth(this);
        iheight = imagePhase.getHeight(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // ////////////////////////////////////////////////////////////////
        // Draw steps
        int width = imageStepsTransparent.getWidth(this);
        int height = imageStepsTransparent.getHeight(this);
        if (table_height == 0)
            table_height = getHeight();
        int y = ((table_height - height) / 2);
        int x = getWidth() - width;
        g.drawImage(imageStepsTransparent, x, y, (1 * width), (1 * height),
                this);
        // ////////////////////////////////////////////////////////////////

        // ////////////////////////////////////////////////////////////////
        // HighBean phase icon
        if (phaseImageIndex != 0) {
            g.drawImage(currentPhaseImage, x, y + (phaseImageIndex - 1) * 30, iwidth,
                    iheight, this);
        }
        // ////////////////////////////////////////////////////////////////

        /**
         * Draw rectangle around the "end of turn" image.
         */
        if (owner_id == 0) {
            g.setColor(Color.white);
            g.drawRect(x, y + 6 * 30, iwidth, iheight);
        }
        
        /**
         * Draw rectangle around opponent's "main1" image.
         */
        if (owner_id == 0) {
            g.setColor(Color.white);
            g.drawRect(x, y + 3 * 30, iwidth, iheight);
        }
        
        /**
         * Put marks.
         */
        for (int i = 1; i < 9; i++) {
            if (phaseMarks.contains(intToMTGPhase[i]) == true) {
                g.drawImage(imageRedMarkTransparent, 2 + x, (19) + y + (i - 1)
                        * 30, 10, 10, this);
            }
        }
    }

    public void setCurrentPhase(PhaseName phase) {
      
      if (phase.equals(PhaseName.blank)) {
        this.phaseImageIndex = 0;
      } else if (phase.equals(PhaseName.untap)) {
        this.phaseImageIndex = 1;  
      } else if (phase.equals(PhaseName.upkeep)) {
        this.phaseImageIndex = 2;
      } else if (phase.equals(PhaseName.draw)) {
        this.phaseImageIndex = 3;
      } else if (phase.equals(PhaseName.main1)) {
        this.phaseImageIndex = 4;
      } else if (phase.equals(PhaseName.before_combat)) {
        this.phaseImageIndex = 4;
      } else if (isCombatPhase(phase)) {
        this.phaseImageIndex = 5;
      } else if (phase.equals(PhaseName.main2)) {
        this.phaseImageIndex = 6;
      } else if (phase.equals(PhaseName.at_endofturn)) {
        this.phaseImageIndex = 7;
      } else if (phase.equals(PhaseName.cleanup)) {
        this.phaseImageIndex = 8;
      }
      
        if (phaseImageIndex != 0) {
            currentPhaseImage = phaseImages.get(phaseImageIndex - 1); // zero-based
        }
        repaint();
    }
    
    private boolean isCombatPhase(PhaseName phase) {
      return phase.equals(PhaseName.combat_declare_attackers) || phase.equals(PhaseName.combat_declare_blockers) ||
        phase.equals(PhaseName.combat_predamage_player) || phase.equals(PhaseName.combat_predamage_opponent) ||
        phase.equals(PhaseName.combat_stack_damage_player) || phase.equals(PhaseName.combat_stack_damage_opponent) ||
        phase.equals(PhaseName.combat_damage);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // nothing
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // nothing
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // nothing
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // nothing
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        if (e.getButton() == MouseEvent.BUTTON1) {
            Point p = e.getPoint();

            int width = imageStepsTransparent.getWidth(this);

            if (owner_id != 0) {

                int x = WindowManager.getManager().getMyTable().getWidth()
                        - width;
                if (p.x < x) {
                    return;
                }

                int height = imageStepsTransparent.getHeight(this);
                if (table_height == 0) {
                    table_height = getHeight();
                }
                int y = ((table_height - height) / 2);

                int nPhase = (p.y - y) / 30 + 1;

                //if (nPhase > 0 && nPhase < 10) {
                if (nPhase > 3 && nPhase < 7) { // main1, combat, main2 only
                    if (phaseMarks.contains(intToMTGPhase[nPhase]) == true) {
                        phaseMarks.remove(intToMTGPhase[nPhase]);
                    } else {
                        phaseMarks.add(intToMTGPhase[nPhase]);
                    }
                    repaint();

                    try {
                        ConnectionManager.getRMIConnection().setPhaseMarks(owner_id, phaseMarks);
                    } catch (RemoteException e0) {
                        e0.printStackTrace();
                    }
                }
            } else {
                /**
                 * It's opponent's phases so we can switch "before combat" && "end of turn"
                 */
                int x = WindowManager.getManager().getOpponentTable().getWidth() - width;
                if (p.x < x) {
                    return;
                }

                int height = imageStepsTransparent.getHeight(this);
                if (table_height == 0) {
                    table_height = getHeight();
                }
                int y = ((table_height - height) / 2);

                int nPhase = (p.y - y) / 30 + 1;

                // end of turn
                if (nPhase == 7) {
                  /*
                    if (phaseMarks.contains(intToMTGPhase[nPhase])) {
                        phaseMarks.remove(intToMTGPhase[nPhase]);
                    } else {
                        phaseMarks.add(intToMTGPhase[nPhase]);
                    }
                    repaint();
                  */
                  
                    SettingsManager.getManager().invertPlayAtEOT();
                }
                
                // before combat
                if (nPhase == 4) {
                     SettingsManager.getManager().invertPlayBeforeCombat();
                }
            }
        }
    }
    
    public void setEndOfTurn(boolean bPlay) {
        if (bPlay) {
            phaseMarks.add(PhaseName.endofturn);
        } else {
            phaseMarks.remove(PhaseName.endofturn);
        }
        repaint();
    }
    
    public void setPlayBeforeCombatMark(boolean bPlay) {
        if (bPlay) {
            phaseMarks.add(PhaseName.main1);
        } else {
            phaseMarks.remove(PhaseName.main1);
        }
        repaint();
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.