Package game.view

Source Code of game.view.PlayScreen

package game.view;

import beans.serializable.EventBloc;
import beans.core.GeneralConstant;
import game.entity.Player;
import game.model.GUIModel;
import game.model.GameModel;
import game.view.dialog.DialogBox;
import game.view.inventory.InventoryScreen;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Observable;
import java.util.Observer;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
*
* @author mastersnes
*/
public class PlayScreen extends JPanel implements Observer {

    private InventoryScreen inventoryScreen;
    /**
     *
     */
    protected GameModel gameModel;
    /**
     *
     */
    protected GUIModel guiModel;
    private final DialogBox dialogBox = new DialogBox();

    /**
     *
     * @param guiModel
     * @param gameModel
     */
    public PlayScreen(final GUIModel guiModel, final GameModel gameModel) {
        super();
        this.guiModel = guiModel;
        setBackground(Color.BLACK);
        this.gameModel = gameModel;
        gameModel.addObserver(this);
        this.setOpaque(false);

        inventoryScreen = new InventoryScreen(gameModel.getPlayer().getInventory());

        requestFocus();
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        g = (Graphics2D) g;

        final Map<Integer, BufferedImage> couches = gameModel.getCouches();

        final Image firstImage = couches.get(1).getSubimage(gameModel.getCamx(), gameModel.getCamy(), couches.get(1).getWidth() - gameModel.getCamx(), couches.get(1).getHeight() - gameModel.getCamy());
        final Image secondImage = couches.get(2).getSubimage(gameModel.getCamx(), gameModel.getCamy(), couches.get(2).getWidth() - gameModel.getCamx(), couches.get(2).getHeight() - gameModel.getCamy());
        final Image thirdImage = couches.get(3).getSubimage(gameModel.getCamx(), gameModel.getCamy(), couches.get(3).getWidth() - gameModel.getCamx(), couches.get(3).getHeight() - gameModel.getCamy());
        g.drawImage(firstImage, 0, 0, this);
        g.drawImage(secondImage, 0, 0, this);
        for (final Entry<Point, EventBloc> entry : gameModel.getConfig().getEvent().entrySet()) {
            final Point eventPoint = gameModel.getRelatif(entry.getKey().x * GeneralConstant.BLOC_WIDTH, entry.getKey().y * GeneralConstant.BLOC_HEIGHT);
            final File eventPath = new File(entry.getValue().getImagePath());
            if (eventPath != null && !eventPath.isDirectory()) {
                final BufferedImage eventImage;
                try {
                    eventImage = ImageIO.read(eventPath);
                    switch (entry.getValue().getCurrentMode()) {
                        case ON:
                            g.drawImage(eventImage.getSubimage(0, 0, GeneralConstant.BLOC_WIDTH, GeneralConstant.BLOC_HEIGHT), eventPoint.x, eventPoint.y, this);
                            break;
                        case OFF:
                            g.drawImage(eventImage.getSubimage(GeneralConstant.BLOC_WIDTH, 0, GeneralConstant.BLOC_WIDTH, GeneralConstant.BLOC_HEIGHT), eventPoint.x, eventPoint.y, this);
                            break;
                        default:
                            break;
                    }

                } catch (final IOException IOexception) {
                }

            }
        }

        final Player player = gameModel.getPlayer();
        final Point positionPlayerRelative = gameModel.getRelatif(player.getX(), player.getY());

        g.drawImage(gameModel.getPlayer().getImage(), positionPlayerRelative.x, positionPlayerRelative.y, this);

        g.drawImage(thirdImage, 0, 0, this);

        dialogBox.reset(gameModel.getLastMessage());
        if (dialogBox.isAffichable()) {
            g.drawImage(dialogBox.getImage(), 0, GeneralConstant.TAILLE_ECRAN_Y - GeneralConstant.TAILLE_DIALOG, this);
        }
       
        if (gameModel.isInventory()) {
            g.setColor(new Color(100, 100, 100, 100));
            g.fillRect(0, 0, GeneralConstant.TAILLE_ECRAN_X, GeneralConstant.TAILLE_ECRAN_Y);
            final Image inventoryImage = inventoryScreen.getImage();
            final Point inventoryPosition = new Point();
            inventoryPosition.x = ((GeneralConstant.TAILLE_ECRAN_X * 17) / 32) - (inventoryImage.getWidth(this) / 2);
            inventoryPosition.y = (GeneralConstant.TAILLE_ECRAN_Y / 2) - (inventoryImage.getHeight(this) / 2);
            g.drawImage(inventoryImage, inventoryPosition.x, inventoryPosition.y, this);
        };
    }

    @Override
    public void update(final Observable o, final Object arg) {
        gameModel = (GameModel) o;
        repaint();
    }
}
TOP

Related Classes of game.view.PlayScreen

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.