aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/client/XBlastComponent.java
blob: cab6fdebfe3d6f91fd8f0d84affbea604c51dca3 (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
package ch.epfl.xblast.client;

import ch.epfl.xblast.Cell;
import ch.epfl.xblast.PlayerID;

import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * The game graphical component.
 *
 * @author Pacien TRAN-GIRARD (261948)
 * @author Timothée FLOURE (257420)
 */
public final class XBlastComponent extends JComponent {

    private static final ImageObserver IMG_OBSERVER = null;

    private static final Dimension CELL_DIMENSION = new Dimension(64, 48);
    private static final Dimension SCORE_IMG_DIMENSION = new Dimension(48, 48);
    private static final Dimension TIME_LED_DIMENSION = new Dimension(16, 16);
    private static final Dimension PREFERRED_WINDOW_DIMENSION = frameDimension();

    private static final List<Point> CELL_POSITIONS = buildCellGrid(CELL_DIMENSION);
    private static final List<Point> SCORE_TXT_POSITIONS = buildScorePositionList(659);
    private static final List<Point> SCORE_BG_POSITIONS =
            buildLine(SCORE_IMG_DIMENSION, 20, new Point(0, CELL_DIMENSION.height * Cell.ROWS));
    private static final List<Point> TIME_LINE_POSITIONS =
            buildLine(TIME_LED_DIMENSION, 60, new Point(0, CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height));

    private static final Font TXT_FONT = new Font("Arial", Font.BOLD, 25);
    private static final Color TXT_COLOR = Color.WHITE;

    private static Dimension frameDimension() {
        return new Dimension(
                CELL_DIMENSION.width * Cell.COLUMNS,
                CELL_DIMENSION.height * Cell.ROWS + SCORE_IMG_DIMENSION.height + TIME_LED_DIMENSION.height);
    }

    private static List<Point> buildCellGrid(Dimension elementDim) {
        return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream()
                .map(c -> new Point(c.x() * elementDim.width, c.y() * elementDim.height))
                .collect(Collectors.toList()));
    }

    private static List<Point> buildLine(Dimension elementDim, int len, Point shift) {
        return Collections.unmodifiableList(IntStream.range(0, len)
                .mapToObj(x -> new Point(x * elementDim.width + shift.x, shift.y))
                .collect(Collectors.toList()));
    }

    private static List<Point> buildScorePositionList(int vShift) {
        return Arrays.asList(
                new Point(96, vShift),
                new Point(240, vShift),
                new Point(768, vShift),
                new Point(912, vShift));
    }

    private static void drawImage(Graphics2D g, Image img, Point pos) {
        g.drawImage(img, pos.x, pos.y, IMG_OBSERVER);
    }

    private static void drawGrid(Graphics2D g, List<Point> grid, List<Image> imgs) {
        for (int i = 0; i < grid.size() && i < imgs.size(); ++i)
            drawImage(g, imgs.get(i), grid.get(i));
    }

    private static Point playerPosition(GameState.Player p) {
        return new Point(
                4 * p.position().x() - 24,
                3 * p.position().y() - 52);
    }

    private GameState gameState;
    private PlayerID playerID;

    /**
     * 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) {
        // TODO: draw in preferred order

        for (GameState.Player p : players)
            drawImage(g, p.image(), playerPosition(p));
    }

    /**
     * Writes the remaining lives of each playerID 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(TXT_COLOR);
        g.setFont(TXT_FONT);

        players.stream().forEach(p -> writeScore(g, p));
    }

    private void writeScore(Graphics2D g, GameState.Player p) {
        String score = Integer.toString(p.lives());
        Point pos = SCORE_TXT_POSITIONS.get(p.id().ordinal());
        g.drawString(score, pos.x, pos.y);
    }

    /**
     * Display the given GameState from the point of view of the given playerID.
     *
     * @param gs  GameState to be displayed
     * @param pid playerID related to the view
     */
    public void setGameState(GameState gs, PlayerID pid) {
        this.gameState = gs;
        this.playerID = pid;

        repaint();
    }

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

    /**
     * Draw the component.
     *
     * @param g0 the Graphics context
     */
    @Override
    protected void paintComponent(Graphics g0) {
        Graphics2D g = (Graphics2D) g0;

        if (Objects.isNull(this.gameState))
            return;

        drawGrid(g, CELL_POSITIONS, this.gameState.board());
        drawGrid(g, CELL_POSITIONS, this.gameState.explosions());
        drawGrid(g, SCORE_BG_POSITIONS, this.gameState.scores());
        drawGrid(g, TIME_LINE_POSITIONS, this.gameState.ticks());

        drawPlayers(g, this.gameState.players());
        writeScores(g, this.gameState.players());
    }

}