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

import ch.epfl.xblast.PlayerID;
import ch.epfl.xblast.client.GameState;
import ch.epfl.xblast.client.ImageCollection;

import java.awt.*;
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.Stream;

/**
 * The score painter.
 *
 * @author Pacien TRAN-GIRARD (261948)
 * @author Timothée FLOURE (257420)
 */
public class ScorePainter {

    private static final int SCORE_SEPARATOR_SIZE = 8;
    private static final int PLAYER_IMG_MULTIPLIER_OFFSET = 2;
    private static final int PLAYER_IMG_DEAD_OFFSET = 1;

    private static final Image TEXT_MIDDLE_IMG = ImageCollection.SCORES_IMAGES.imageOrNull(10);
    private static final Image TEXT_RIGHT_IMG = ImageCollection.SCORES_IMAGES.imageOrNull(11);
    private static final Image VOID_IMG = ImageCollection.SCORES_IMAGES.imageOrNull(12);

    private ScorePainter() {
        // Static class
    }

    /**
     * Builds the score panel for the given players.
     *
     * @param players the players
     * @return the score panel
     */
    public static List<Image> buildScorePanel(List<GameState.Player> players) {
        if (Objects.isNull(players))
            throw new IllegalArgumentException();

        return players.stream()
                .flatMap(p -> Stream.concat(
                        buildPlayerScorePanel(p).stream(),
                        p.id().ordinal() % 2 == 0 ? buildSeparator().stream() : Stream.empty()))
                .collect(Collectors.toList());
    }

    /**
     * Builds the score panel for the given player.
     *
     * @param p the player
     * @return the score panel
     */
    private static List<Image> buildPlayerScorePanel(GameState.Player p) {
        if (Objects.isNull(p))
            throw new IllegalArgumentException();

        return Arrays.asList(
                ImageCollection.PLAYERS_IMAGES.imageOrNull(imageIDForPlayer(p)),
                TEXT_MIDDLE_IMG,
                TEXT_RIGHT_IMG);
    }

    /**
     * Returns the image ID for the given player.
     *
     * @param p the player
     * @return the image ID
     */
    private static int imageIDForPlayer(GameState.Player p) {
        return imageIDOffsetForPlayerID(p.id()) + imageIDOffsetForLifeState(p.lives());
    }

    /**
     * Returns the image ID offset corresponding to a player ID.
     *
     * @param pid the player ID
     * @return the image ID offset
     */
    private static int imageIDOffsetForPlayerID(PlayerID pid) {
        return pid.ordinal() * PLAYER_IMG_MULTIPLIER_OFFSET;
    }

    /**
     * Returns the image ID offset corresponding to a life state.
     *
     * @param lives the number of remaining lives
     * @return the image ID offset
     */
    private static int imageIDOffsetForLifeState(int lives) {
        return lives == 0 ? PLAYER_IMG_DEAD_OFFSET : 0;
    }

    /**
     * Builds and returns a separator image sequence.
     *
     * @return the separator
     */
    private static List<Image> buildSeparator() {
        return Collections.nCopies(SCORE_SEPARATOR_SIZE, VOID_IMG);
    }

}