diff options
Diffstat (limited to 'src/main/java/fr/umlv')
25 files changed, 254 insertions, 435 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/block/Block.java b/src/main/java/fr/umlv/java/wallj/block/Block.java index ba299b4..7ae9c06 100644 --- a/src/main/java/fr/umlv/java/wallj/block/Block.java +++ b/src/main/java/fr/umlv/java/wallj/block/Block.java | |||
@@ -1,15 +1,11 @@ | |||
1 | package fr.umlv.java.wallj.block; | 1 | package fr.umlv.java.wallj.block; |
2 | 2 | ||
3 | import fr.umlv.java.wallj.board.TileVec2; | 3 | import fr.umlv.java.wallj.board.TileVec2; |
4 | import fr.umlv.java.wallj.context.Context; | ||
5 | import fr.umlv.java.wallj.context.Updateable; | 4 | import fr.umlv.java.wallj.context.Updateable; |
6 | import fr.umlv.java.wallj.controller.BlockController; | ||
7 | import fr.umlv.java.wallj.controller.Controller; | ||
8 | import fr.umlv.java.wallj.event.Event; | ||
9 | import org.jbox2d.common.Vec2; | 5 | import org.jbox2d.common.Vec2; |
6 | import org.jbox2d.dynamics.World; | ||
10 | 7 | ||
11 | import java.util.*; | 8 | import java.util.*; |
12 | import java.util.stream.Collectors; | ||
13 | 9 | ||
14 | /** | 10 | /** |
15 | * A block. | 11 | * A block. |
@@ -17,29 +13,13 @@ import java.util.stream.Collectors; | |||
17 | * @author Pacien TRAN-GIRARD | 13 | * @author Pacien TRAN-GIRARD |
18 | */ | 14 | */ |
19 | public abstract class Block implements Updateable { | 15 | public abstract class Block implements Updateable { |
20 | |||
21 | private final BlockType type; | 16 | private final BlockType type; |
22 | private List<Controller> controllers; | ||
23 | private Vec2 pos; | ||
24 | 17 | ||
25 | /** | 18 | /** |
26 | * @param type type of block | 19 | * @param type type of block |
27 | * @param pos initial position | ||
28 | */ | 20 | */ |
29 | Block(BlockType type, Vec2 pos) { | 21 | Block(BlockType type) { |
30 | this.type = Objects.requireNonNull(type); | 22 | this.type = Objects.requireNonNull(type); |
31 | this.pos = Objects.requireNonNull(pos).clone(); | ||
32 | } | ||
33 | |||
34 | void setControllers(List<BlockController> l) { | ||
35 | controllers = Collections.unmodifiableList(new LinkedList<>(Objects.requireNonNull(l))); | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * @param pos new position to set | ||
40 | */ | ||
41 | public void setPos(Vec2 pos) { | ||
42 | this.pos = Objects.requireNonNull(pos).clone(); | ||
43 | } | 23 | } |
44 | 24 | ||
45 | /** | 25 | /** |
@@ -50,28 +30,19 @@ public abstract class Block implements Updateable { | |||
50 | } | 30 | } |
51 | 31 | ||
52 | /** | 32 | /** |
53 | * @return the current position | ||
54 | */ | ||
55 | public Vec2 getPos() { | ||
56 | return pos.clone(); | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | * @return the current tile position | 33 | * @return the current tile position |
61 | */ | 34 | */ |
62 | public TileVec2 getTile() { | 35 | public TileVec2 getTile() { |
63 | return TileVec2.of(pos); | 36 | return TileVec2.of(getPos()); |
64 | } | 37 | } |
65 | 38 | ||
66 | /** | 39 | /** |
67 | * @param ctx execution context | 40 | * @return the current position |
68 | * @return list of generated events | ||
69 | */ | 41 | */ |
70 | @Override | 42 | public abstract Vec2 getPos(); |
71 | public List<Event> update(Context ctx) { | ||
72 | return controllers.stream() | ||
73 | .flatMap(controller -> controller.update(ctx).stream()) | ||
74 | .collect(Collectors.toList()); | ||
75 | } | ||
76 | 43 | ||
44 | /** | ||
45 | * @param world a JBox2D world | ||
46 | */ | ||
47 | public abstract void link(World world); | ||
77 | } | 48 | } |
diff --git a/src/main/java/fr/umlv/java/wallj/block/BlockFactory.java b/src/main/java/fr/umlv/java/wallj/block/BlockFactory.java index 951d182..acd280f 100644 --- a/src/main/java/fr/umlv/java/wallj/block/BlockFactory.java +++ b/src/main/java/fr/umlv/java/wallj/block/BlockFactory.java | |||
@@ -1,7 +1,6 @@ | |||
1 | package fr.umlv.java.wallj.block; | 1 | package fr.umlv.java.wallj.block; |
2 | 2 | ||
3 | import fr.umlv.java.wallj.board.TileVec2; | 3 | import fr.umlv.java.wallj.board.TileVec2; |
4 | import fr.umlv.java.wallj.controller.BlockControllerFactory; | ||
5 | import org.jbox2d.common.Vec2; | 4 | import org.jbox2d.common.Vec2; |
6 | 5 | ||
7 | /** | 6 | /** |
@@ -30,9 +29,7 @@ public final class BlockFactory { | |||
30 | } | 29 | } |
31 | 30 | ||
32 | public static Block build(BlockType type, TileVec2 pos) { | 31 | public static Block build(BlockType type, TileVec2 pos) { |
33 | Block block = forType(type, pos.toVec2()); | 32 | return forType(type, pos.toVec2()); |
34 | if (block != null) block.setControllers(BlockControllerFactory.build(block)); | ||
35 | return block; | ||
36 | } | 33 | } |
37 | 34 | ||
38 | private BlockFactory() { | 35 | private BlockFactory() { |
diff --git a/src/main/java/fr/umlv/java/wallj/block/BombBlock.java b/src/main/java/fr/umlv/java/wallj/block/BombBlock.java index de7a3b9..f58f1cc 100644 --- a/src/main/java/fr/umlv/java/wallj/block/BombBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/BombBlock.java | |||
@@ -1,28 +1,69 @@ | |||
1 | package fr.umlv.java.wallj.block; | 1 | package fr.umlv.java.wallj.block; |
2 | 2 | ||
3 | import fr.umlv.java.wallj.board.TileVec2; | ||
4 | import fr.umlv.java.wallj.context.Context; | ||
5 | import fr.umlv.java.wallj.context.GraphicsContext; | ||
6 | import fr.umlv.java.wallj.event.Event; | ||
3 | import org.jbox2d.common.Vec2; | 7 | import org.jbox2d.common.Vec2; |
8 | import org.jbox2d.dynamics.BodyType; | ||
9 | |||
10 | import java.awt.*; | ||
11 | import java.time.Duration; | ||
12 | import java.util.Arrays; | ||
13 | import java.util.Collections; | ||
14 | import java.util.List; | ||
4 | 15 | ||
5 | /** | 16 | /** |
6 | * A bomb block. | 17 | * A bomb block. |
7 | * | 18 | * |
8 | * @author Pacien TRAN-GIRARD | 19 | * @author Pacien TRAN-GIRARD |
20 | * @author Adam NAILI | ||
9 | */ | 21 | */ |
10 | public class BombBlock extends Block { | 22 | public class BombBlock extends JBoxBlock { |
11 | 23 | private static final Duration TIME_INCREMENT = Duration.ofSeconds(1); | |
12 | private static final int INITIAL_TIMER = 1; // sec | 24 | private static final Duration MIN_TIME = Duration.ofSeconds(1); |
25 | private static final Duration MAX_TIME = Duration.ofSeconds(9); | ||
13 | 26 | ||
14 | private int timer = INITIAL_TIMER; | 27 | private Duration timer = MIN_TIME; |
15 | 28 | ||
16 | BombBlock(Vec2 pos) { | 29 | BombBlock(Vec2 pos) { |
17 | super(BlockType.BOMB, pos); | 30 | super(BlockType.BOMB, BodyType.STATIC, SolidDef.squareShape(), pos); |
31 | } | ||
32 | |||
33 | private void incrementTimer() { | ||
34 | timer = timer.compareTo(MAX_TIME) <= 0 ? timer.plus(TIME_INCREMENT) : MIN_TIME; | ||
35 | } | ||
36 | |||
37 | private void decrementTimer(Duration d) { | ||
38 | if (timer.isNegative()) throw new IllegalStateException("This bomb has already exploded."); | ||
39 | timer = timer.minus(d); | ||
18 | } | 40 | } |
19 | 41 | ||
20 | public void setTimer(int sec) { | 42 | @Override |
21 | timer = sec; | 43 | public List<Event> update(Context context) { |
44 | if (containsUpdateEvent(context.getEvents())) incrementTimer(); | ||
45 | paint(context.getGraphicsContext()); | ||