aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/client/XBlastComponent.java
blob: ac9f223e670889a40821395986a99f993a92846a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package ch.epfl.xblast.client;

import ch.epfl.xblast.PlayerID;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * @author Timothée FLOURE (257420)
 */
public final class XBlastComponent extends JComponent {
    private static final int PREFERRED_WIDTH = 960;
    private static final int PREFERRED_HEIGHT = 688;
    private static final int BLOCK_WIDTH = 64;
    private static final int BLOCK_HEIGHT = 48;
    private static final int SCORE_IMAGE_WIDTH = 48;
    private static final int SCORE_IMAGE_HEIGHT = 48;
    private static final int TIME_LED_WIDTH = 16;
    private static final int TIME_LED_HEIGHT = 16;

    private static final Font font = new Font("Arial", Font.BOLD, 25);
    private static final int SCORES_TEXT_VERTICAL_POSITION = 659;
    private static final int SCORES_P1_TEXT_HORIZONTAL_POSITION = 96;
    private static final int SCORES_P2_TEXT_HORIZONTAL_POSITION = 240;
    private static final int SCORES_P3_TEXT_HORIZONTAL_POSITION = 768;
    private static final int SCORES_P4_TEXT_HORIZONTAL_POSITION = 912;

    /**
     * Displayed gameState.
     */
    private GameState gamestate;

    /**
     * Display a list of images on the given graphic context.
     *
     * @param g the graphic context
     * @param images the given list of images, ordered in row-major order
     */
    private void drawMap(Graphics2D g, List<Image> images) {
        int x = 0;
        int y = 0;

        for (Image img : images) {
            if (x + BLOCK_WIDTH > PREFERRED_WIDTH) {
                y += BLOCK_HEIGHT; // Or img.getHeight(null) ?
                x = 0;
            }

            if (img != null)
                g.drawImage(img, x, y, null);

            x += BLOCK_WIDTH; // Or img.getWidth(null) ?
        }
    }

    /**
     * Compute the horizontal position of the player in px.
     *
     * @param player the given player
     * @return the horizontal position in pixel
     */
    private int playerXPosition(GameState.Player player) { return 4 * player.position().x() - 24; }

    /**
     * Compute the vertical position of the player in px.
     *
     * @param player the given player
     * @return the vertical position in pixel
     */
    private int playerYPosition(GameState.Player player) { return 3 * player.position().y() - 52; }

    /**
     * Draw the players on the graphic context.
     *
     * @param g the graphic context
     * @param players the list of players to be displayed
     */
    private void drawPlayers(Graphics2D g, List<GameState.Player> players) {
        for (GameState.Player player : players) {
            g.drawImage(player.image(), playerXPosition(player), playerYPosition(player), null);
        }
    }

    /**
     * Draw the scores on the graphic context.
     *
     * @param g the graphic context
     * @param scores the list of images composing the scores to be displayed
     */
    private void drawScores(Graphics2D g, List<Image> scores) {
        int x = 0;
        int y = PREFERRED_HEIGHT - SCORE_IMAGE_HEIGHT - TIME_LED_HEIGHT;
        for (Image img : scores) {
            g.drawImage(img, x, y, null);
            x += SCORE_IMAGE_WIDTH;
        }
    }

    /**
     * Write the remaining lives of each player on the graphic context.
     *
     * @param g the graphic context
     * @param players list of players
     */
    private void writeScores(Graphics2D g,List<GameState.Player> players) {
        g.setColor(Color.WHITE);
        g.setFont(font);

        // Ugly !!!!!
        g.drawString(Integer.toString(players.get(0).lives()), SCORES_P1_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION);
        g.drawString(Integer.toString(players.get(1).lives()), SCORES_P2_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION);
        g.drawString(Integer.toString(players.get(2).lives()), SCORES_P3_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION);
        g.drawString(Integer.toString(players.get(3).lives()), SCORES_P4_TEXT_HORIZONTAL_POSITION,SCORES_TEXT_VERTICAL_POSITION);

    }

    /**
     * Draw the time "line" on the graphic context
     *
     * @param g the graphic context
     * @param time the list of images composing the line
     */
    private void drawTime(Graphics2D g, List<Image> time) {
        int x = 0;
        int y = PREFERRED_HEIGHT - TIME_LED_HEIGHT;
        for (Image img : time) {
            g.drawImage(img, x, y, null);
            x += TIME_LED_WIDTH;
        }
    }

    /**
     * Display the given GameState from the point of view of the given player.
     *
     * @param gameState GameState to be displayer
     * @param player player related to the view
     */
    public void setGameState(GameState gameState, PlayerID player) {
        this.gamestate = gameState;
        repaint();
    }

    /**
     * Returns the value of the preferredSize property.
     *
     * @return the value of the preferredSize property
     */
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT);
    }

    /**
     * Draw the component.
     *
     * @param g0 the Graphics context
     */
    @Override
    protected void paintComponent(Graphics g0) {
        if (this.gamestate != null) {
            Graphics2D g = (Graphics2D) g0;
            drawMap(g, this.gamestate.board());
            drawMap(g, this.gamestate.explosions());
            drawPlayers(g, this.gamestate.players());
            drawScores(g, this.gamestate.scores());
            writeScores(g, this.gamestate.players());
            drawTime(g, this.gamestate.ticks());
        };
    }
}