diff options
Diffstat (limited to 'src/esieequest/game/characters/MovingCharacter.java')
-rw-r--r-- | src/esieequest/game/characters/MovingCharacter.java | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/esieequest/game/characters/MovingCharacter.java b/src/esieequest/game/characters/MovingCharacter.java new file mode 100644 index 0000000..1e536fa --- /dev/null +++ b/src/esieequest/game/characters/MovingCharacter.java | |||
@@ -0,0 +1,137 @@ | |||
1 | package esieequest.game.characters; | ||
2 | |||
3 | import java.util.ArrayList; | ||
4 | import java.util.List; | ||
5 | |||
6 | import lombok.Getter; | ||
7 | import lombok.Setter; | ||
8 | import net.pacien.util.CleanJSONObject; | ||
9 | |||
10 | import org.json.simple.JSONObject; | ||
11 | |||
12 | import esieequest.game.Game; | ||
13 | import esieequest.game.map.Direction; | ||
14 | import esieequest.game.map.Room; | ||
15 | |||
16 | /** | ||
17 | * A character that can move from rooms to rooms. | ||
18 | * | ||
19 | * @author Pacien TRAN-GIRARD | ||
20 | */ | ||
21 | public abstract class MovingCharacter extends SimpleCharacter { | ||
22 | |||
23 | private static List<MovingCharacter> instances; | ||
24 | |||
25 | static { | ||
26 | MovingCharacter.instances = new ArrayList<MovingCharacter>(); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Moves all the MovingCharacter in the map. | ||
31 | */ | ||
32 | public static void moveAll(final Game game) { | ||
33 | for (final MovingCharacter movingCharacter : MovingCharacter.instances) { | ||
34 | movingCharacter.move(game); | ||
35 | } | ||
36 | } | ||
37 | |||
38 | private static final String CURRENT_ROOM_LABEL = "C"; | ||
39 | @Getter | ||
40 | private Room currentRoom; | ||
41 | |||
42 | private static final String CURRENT_DIRECTION_LABEL = "D"; | ||
43 | @Getter | ||
44 | private Direction currentDirection; | ||
45 | |||
46 | private static final String MOBILE_LABEL = "M"; | ||
47 | @Getter | ||
48 | @Setter | ||
49 | private boolean mobile; | ||
50 | |||
51 | /** | ||
52 | * Creates a MovingCharacter with a given name and location. | ||
53 | * | ||
54 | * @param name | ||
55 | * the name of the Character | ||
56 | * @param room | ||
57 | * the Room where the Character is located | ||
58 | * @param direction | ||
59 | * the Direction at which the Character is located | ||
60 | */ | ||
61 | public MovingCharacter(final String name, final Room room, final Direction direction) { | ||
62 | super(name); | ||
63 | MovingCharacter.instances.add(this); | ||
64 | |||
65 | this.currentRoom = room; | ||
66 | this.currentDirection = direction; | ||
67 | this.mobile = false; | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * Changes the position of the MovingCharacter. | ||
72 | * | ||
73 | * @param room | ||
74 | * the new Room | ||
75 | * @param direction | ||
76 | * the new Direction | ||
77 | */ | ||
78 | public void changePosition(final Room room, final Direction direction) { | ||
79 | if (room.getSide(direction).hasCharacter()) { | ||
80 | return; | ||
81 | } | ||
82 | |||
83 | this.currentRoom.getSide(this.currentDirection).setCharacter(null); | ||
84 | this.currentRoom = room; | ||
85 | this.currentDirection = direction; | ||
86 | this.currentRoom.getSide(this.currentDirection).setCharacter(Character.getCharacter(this)); | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Changes the Room of the MovingCharacter. | ||
91 | * | ||
92 | * @param room | ||
93 | * the Room | ||
94 | */ | ||
95 | public void changeRoom(final Room room) { | ||
96 | this.changePosition(room, this.currentDirection); | ||
97 | } | ||
98 | |||
99 | /** | ||
100 | * Changes the Direction of the MovingCharacter. | ||
101 | * | ||
102 | * @param direction | ||
103 | * the Direction | ||
104 | */ | ||
105 | public void changeDirection(final Direction direction) { | ||
106 | this.changePosition(this.currentRoom, direction); | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * Makes the character move. | ||
111 | */ | ||
112 | public abstract void move(final Game game); | ||
113 | |||
114 | @Override | ||
115 | public JSONObject serialise() { | ||
116 | final CleanJSONObject o = new CleanJSONObject(); | ||
117 | |||
118 | if (this.currentRoom != null) { | ||
119 | o.put(MovingCharacter.CURRENT_ROOM_LABEL, this.currentRoom.name()); | ||
120 | } | ||
121 | if (this.currentDirection != null) { | ||
122 | o.put(MovingCharacter.CURRENT_DIRECTION_LABEL, this.currentDirection.name()); | ||
123 | } | ||
124 | o.put(MovingCharacter.MOBILE_LABEL, this.mobile); | ||
125 | |||
126 | return o; | ||
127 | } | ||
128 | |||
129 | @Override | ||
130 | public void deserialise(final JSONObject o) { | ||
131 | this.currentRoom = Room.valueOf((String) o.get(MovingCharacter.CURRENT_ROOM_LABEL)); | ||
132 | this.currentDirection = Direction.valueOf((String) o | ||
133 | .get(MovingCharacter.CURRENT_DIRECTION_LABEL)); | ||
134 | this.mobile = (Boolean) o.get(MovingCharacter.MOBILE_LABEL); | ||
135 | } | ||
136 | |||
137 | } | ||