aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/server/Bomb.java
blob: 3f49f646bf6816c1ce4789c9d5c06d0c5eb4ac87 (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
package ch.epfl.xblast.server;

import ch.epfl.xblast.PlayerID;
import ch.epfl.xblast.Cell;
import ch.epfl.cs108.Sq;
import ch.epfl.xblast.ArgumentChecker;
import ch.epfl.xblast.Direction;

import java.util.Objects;
import java.util.List;
import java.util.ArrayList;

/**
 * @author Timothée FLOURE (257420)
 */
public final class Bomb {
    private PlayerID ownerId;
    private Cell position;
    private Sq<Integer> fuseLengths;
    private int range;

    /**
     * Generate one arm of explosion
     */
    private Sq<Sq<Cell>> explosionArmTowards(Direction dir) {
        return Sq.constant( Sq.iterate(position, position -> position.neighbor(dir)).limit(range)).limit(Ticks.EXPLOSION_TICKS);
    }

    /**
     * Instanciates a new Bomb.
     *
     * @param ownerId id of the owner of the bomb
     * @param position position of the bomb
     * @param fuseLengths length of the bomb's fuse
     * @param range range of the bomb
     * @throws IllegalArguementException if range is negative or fuseLenghts is empty
     * @throws NullPointerException if ownerId, position or fuseLengths is null
     */
    public Bomb(PlayerID ownerId, Cell position, Sq<Integer> fuseLengths, int range) {
        this.ownerId = Objects.requireNonNull(ownerId);
        this.position = Objects.requireNonNull(position);
        if (fuseLengths.isEmpty()) {
            throw new IllegalArgumentException();
        } else {
            this.fuseLengths = Objects.requireNonNull(fuseLengths); 
        }
        this.range = ArgumentChecker.requireNonNegative(range);
    }

    /**
     * Instanciates a new Bomb.
     *
     * @param ownerId id of the owner of the bomb
     * @param position position of the bomb
     * @param fuseLengths length of the bomb's fuse
     * @param range range of the bomb
     * @throws IllegalArguementException if range or fuseLengths is negative
     * @throws NullPointerException if ownerId, position or fuseLengths is null
     */
    public Bomb(PlayerID ownerId, Cell position, int fuseLength, int range) {
        this.ownerId = Objects.requireNonNull(ownerId);
        this.position = Objects.requireNonNull(position);
        if (fuseLength == 0) {
            throw new IllegalArgumentException();
        } else {
            fuseLengths = Sq.iterate(fuseLength, fuseLengths -> fuseLength - 1 );
        }
        this.range = ArgumentChecker.requireNonNegative(range);
    }

    /**
     * @return the ID of the owner of the bomb
     */
    public PlayerID ownerId() {
        return ownerId;
    }

    /**
     * @return the position of the bomb
     */
    public Cell position() {
        return position;
    }

    /**
     * @return the length of the fuse
     */
    public Sq<Integer> fuseLengths() {
        return fuseLengths;
    }

    /**
     * @return the remaining time before the explosion
     */
    public int fuseLength() {
        return fuseLengths.head();
    }

    /**
     * @return the range of the Bomb
     */
    public int range() {
        return range;
    }

    /**
     * @return the explosion
     */
    public List<Sq<Sq<Cell>>> explosion() {
        List<Sq<Sq<Cell>>> explosion = new ArrayList<>();
        for (Direction dir : Direction.values()) {
            explosion.add(explosionArmTowards(dir));
        }
        return explosion;
    }
}