-
Notifications
You must be signed in to change notification settings - Fork 36
/
MeditateSystem.java
118 lines (100 loc) · 4.37 KB
/
MeditateSystem.java
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
package server.systems.world.entity.user;
import com.artemis.Aspect;
import com.artemis.E;
import com.artemis.annotations.Wire;
import component.console.ConsoleMessage;
import component.entity.character.states.Meditating;
import component.entity.character.status.Mana;
import component.entity.world.CombatMessage;
import component.graphic.Effect;
import server.systems.network.EntityUpdateSystem;
import server.systems.network.MessageSystem;
import server.systems.world.IntervalFluidIteratingSystem;
import server.systems.world.WorldEntitiesSystem;
import server.systems.world.entity.factory.EffectEntitySystem;
import server.systems.world.entity.factory.SoundEntitySystem;
import server.utils.UpdateTo;
import shared.interfaces.Constants;
import shared.util.EntityUpdateBuilder;
import shared.util.Messages;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import static com.artemis.E.E;
@Wire
public class MeditateSystem extends IntervalFluidIteratingSystem {
private static final float MANA_RECOVERY_PERCENT = 0.1f;
private static Map<Integer, Integer> userMeditations = new HashMap<>();
private WorldEntitiesSystem worldEntitiesSystem;
private EntityUpdateSystem entityUpdateSystem;
private MessageSystem messageSystem;
private SoundEntitySystem soundEntitySystem;
private EffectEntitySystem effectEntitySystem;
public MeditateSystem(float timer) {
super(Aspect.all(Meditating.class, Mana.class), timer);
}
@Override
protected void process(E player) {
Mana mana = player.getMana();
EntityUpdateBuilder update = EntityUpdateBuilder.of(player.id());
EntityUpdateBuilder notify = EntityUpdateBuilder.of(player.id());
if (mana.min < mana.max) {
int manaMin = mana.min;
int prob = ThreadLocalRandom.current().nextInt(2);
if (prob == 1) {
// meditar
mana.min += mana.max * MANA_RECOVERY_PERCENT;
mana.min = Math.min(mana.min, mana.max);
int recoveredMana = mana.min - manaMin;
CombatMessage manaMessage = CombatMessage.magic("+" + recoveredMana);
notify.withComponents(manaMessage);
update.withComponents(mana);
// send console message
ConsoleMessage consoleMessage = ConsoleMessage.info(Messages.MANA_RECOVERED.name(), Integer.toString(recoveredMana));
messageSystem.add(player.id(), consoleMessage);
}
}
if (mana.min >= mana.max) {
notify.remove(Meditating.class);
ConsoleMessage consoleMessage = ConsoleMessage.info(Messages.MEDITATE_STOP.name());
messageSystem.add(player.id(), consoleMessage);
stopMeditationEffect(player.id());
}
if (!update.isEmpty()) {
entityUpdateSystem.add(update.build(), UpdateTo.ENTITY);
}
if (!notify.isEmpty()) {
entityUpdateSystem.add(notify.build(), UpdateTo.ALL);
}
}
public void toggle(int userId) {
E player = E(userId);
boolean meditating = player.isMeditating();
ConsoleMessage consoleMessage;
EntityUpdateBuilder update = EntityUpdateBuilder.of(userId);
if (meditating) {
stopMeditationEffect(userId);
consoleMessage = ConsoleMessage.info(Messages.MEDITATE_STOP.name());
update.remove(Meditating.class);
} else {
E entity = E(userId);
Mana mana = entity.getMana();
if (mana != null && mana.min == mana.max) {
consoleMessage = ConsoleMessage.info(Messages.MANA_FULL.name());
} else {
effectEntitySystem.addEffect(userId, Constants.MEDITATE_NW_FX, Effect.LOOP_INFINITE);
soundEntitySystem.add(player.id(), 18, true);
player.meditating();
consoleMessage = ConsoleMessage.info(Messages.MEDITATE_START.name());
update.withComponents(player.getMeditating());
}
}
messageSystem.add(userId, consoleMessage);
entityUpdateSystem.add(update.build(), UpdateTo.ALL);
}
private void stopMeditationEffect(int userId) {
effectEntitySystem.removeEffect(userId, Constants.MEDITATE_NW_FX);
soundEntitySystem.remove(userId, 18);
E(userId).removeMeditating();
}
}