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

import ch.epfl.xblast.ArgumentChecker;
import ch.epfl.xblast.Lists;
import ch.epfl.xblast.PlayerID;
import ch.epfl.xblast.SubCell;
import ch.epfl.xblast.client.painter.ScorePainter;
import ch.epfl.xblast.client.painter.TimeLinePainter;

import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * The client representation of a game state.
 *
 * @author Timothée FLOURE (257420)
 * @author Pacien TRAN-GIRARD (261948)
 */
public final class GameState {

    /**
     * A Player.
     */
    public static final class Player {

        static final Comparator<Player> POSITION_COMPARATOR = (p1, p2) -> p1.position().compareTo(p2.position());

        static Comparator<Player> idPushingComparator(PlayerID firstClassID) {
            return (p1, p2) -> p1.id() == firstClassID ? +1 : 0;
        }

        private final PlayerID id;
        private final int lives;
        private final SubCell position;
        private final Image image;

        /**
         * Instantiates a new Player.
         *
         * @param id       id of the player
         * @param lives    number of lives of the player
         * @param position position of the player
         * @param image    image related to the actual state of the player
         */
        public Player(PlayerID id, int lives, SubCell position, Image image) {
            this.id = id;
            this.lives = lives;
            this.position = Objects.requireNonNull(position);
            this.image = image;
        }

        /**
         * @return the player ID
         */
        public PlayerID id() {
            return this.id;
        }

        /**
         * @return the number of lives of the player
         */
        public int lives() {
            return this.lives;
        }

        /**
         * @return the position of the player
         */
        public SubCell position() {
            return this.position;
        }

        /**
         * @return the image related to the actual state of the player
         */
        public Image image() {
            return this.image;
        }

    }

    private final List<Player> players;
    private final List<Image> board;
    private final List<Image> explosions;
    private final List<Image> scores;
    private final List<Image> ticks;

    /**
     * Instantiates a new GameState.
     *
     * @param players    list containing the players
     * @param board      list containing all the images composing the board
     * @param explosions list containing all the images composing the blasts and the bombs
     * @param scores     list containing all the images composing the actual score
     * @param ticks      list containing all the images composing the time "line"
     */
    public GameState(List<Player> players, List<Image> board, List<Image> explosions, List<Image> scores, List<Image> ticks) {
        this.players = Lists.immutableList(ArgumentChecker.requireNonEmpty(players));
        this.board = Lists.immutableList(ArgumentChecker.requireNonEmpty(board));
        this.explosions = Lists.immutableList(Objects.requireNonNull(explosions));
        this.scores = Lists.immutableList(ArgumentChecker.requireNonEmpty(scores));
        this.ticks = Lists.immutableList(ArgumentChecker.requireNonEmpty(ticks));
    }

    /**
     * Instantiates a new GameState.
     *
     * @param players    list containing the players
     * @param board      list containing all the images composing the board
     * @param explosions list containing all the images composing the blasts and the bombs
     * @param ticks      the ticks
     */
    public GameState(List<Player> players, List<Image> board, List<Image> explosions, int ticks) {
        this(players, board, explosions, ScorePainter.buildScorePanel(players), TimeLinePainter.buildTimeLine(ticks));
    }

    /**
     * @return list containing the players
     */
    public List<Player> players() {
        return this.players;
    }

    /**
     * @return list containing all the images composing the board
     */
    public List<Image> board() {
        return this.board;
    }

    /**
     * @return ist containing all the images composing the blasts and the bombs
     */
    public List<Image> explosions() {
        return this.explosions;
    }

    /**
     * @return list containing all the images composing the score
     */
    public List<Image> scores() {
        return this.scores;
    }

    /**
     * @return list containing all the images composing the time "line"
     */
    public List<Image> ticks() {
        return this.ticks;
    }

    /**
     * @return map of players' scores
     */
    Map<PlayerID, Integer> playerScores() {
        return Collections.unmodifiableMap(this.players.stream()
                .collect(Collectors.toMap(Player::id, Player::lives)));
    }

}