1
|
|
|
/* |
2
|
|
|
* This file is part of Araknemu. |
3
|
|
|
* |
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2020 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.game.player.experience; |
21
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.core.event.Dispatcher; |
23
|
|
|
import fr.quatrevieux.araknemu.core.event.EventsSubscriber; |
24
|
|
|
import fr.quatrevieux.araknemu.core.event.Listener; |
25
|
|
|
import fr.quatrevieux.araknemu.data.living.entity.player.Player; |
26
|
|
|
import fr.quatrevieux.araknemu.data.world.entity.character.PlayerExperience; |
27
|
|
|
import fr.quatrevieux.araknemu.data.world.repository.character.PlayerExperienceRepository; |
28
|
|
|
import fr.quatrevieux.araknemu.game.GameConfiguration; |
29
|
|
|
import fr.quatrevieux.araknemu.game.PreloadableService; |
30
|
|
|
import fr.quatrevieux.araknemu.game.listener.player.RebuildLifePointsOnLevelUp; |
31
|
|
|
import fr.quatrevieux.araknemu.game.listener.player.SendLevelUp; |
32
|
|
|
import fr.quatrevieux.araknemu.game.listener.player.SendPlayerXp; |
33
|
|
|
import fr.quatrevieux.araknemu.game.player.event.PlayerLoaded; |
34
|
|
|
import org.apache.logging.log4j.Logger; |
35
|
|
|
import org.checkerframework.checker.index.qual.Positive; |
36
|
|
|
|
37
|
|
|
import java.util.ArrayList; |
38
|
|
|
import java.util.List; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Handle player experience and levels |
42
|
|
|
*/ |
43
|
|
|
public final class PlayerExperienceService implements PreloadableService, EventsSubscriber { |
44
|
|
|
private final PlayerExperienceRepository repository; |
45
|
|
|
private final GameConfiguration.PlayerConfiguration configuration; |
46
|
|
|
|
47
|
1 |
|
private final List<PlayerExperience> levels = new ArrayList<>(); |
48
|
|
|
|
49
|
1 |
|
public PlayerExperienceService(PlayerExperienceRepository repository, GameConfiguration.PlayerConfiguration configuration) { |
50
|
1 |
|
this.repository = repository; |
51
|
1 |
|
this.configuration = configuration; |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
@Override |
55
|
|
|
public void init(Logger logger) { |
56
|
1 |
|
logger.info("Loading player experience..."); |
57
|
|
|
|
58
|
1 |
|
levels.clear(); |
59
|
1 |
|
levels.addAll(repository.all()); |
60
|
|
|
|
61
|
1 |
|
logger.info("{} player levels loaded", levels.size()); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
@Override |
65
|
|
|
public String name() { |
66
|
1 |
|
return "player.experience"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
@Override |
70
|
|
|
public Listener[] listeners() { |
71
|
1 |
|
return new Listener[] { |
72
|
1 |
|
new Listener<PlayerLoaded>() { |
73
|
|
|
@Override |
74
|
|
|
public void on(PlayerLoaded event) { |
75
|
1 |
|
event.player().dispatcher().add(new RebuildLifePointsOnLevelUp(event.player())); // Issue #55 : Before send stats |
76
|
1 |
|
event.player().dispatcher().add(new SendLevelUp(event.player())); |
77
|
1 |
|
event.player().dispatcher().add(new SendPlayerXp(event.player())); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
@Override |
81
|
|
|
public Class<PlayerLoaded> event() { |
82
|
1 |
|
return PlayerLoaded.class; |
83
|
|
|
} |
84
|
|
|
}, |
85
|
|
|
}; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Load player level |
90
|
|
|
* |
91
|
|
|
* @param dispatcher The event dispatcher |
92
|
|
|
* @param player Player to load |
93
|
|
|
*/ |
94
|
|
|
public GamePlayerExperience load(Dispatcher dispatcher, Player player) { |
95
|
1 |
|
return new GamePlayerExperience(player, this, dispatcher); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the experience related to the level |
100
|
|
|
* If the level is higher than maximum level, the maximum level is returned |
101
|
|
|
* |
102
|
|
|
* @param level Level to get |
103
|
|
|
*/ |
104
|
|
|
PlayerExperience byLevel(int level) { |
105
|
1 |
|
return level <= levels.size() |
106
|
1 |
|
? levels.get(level - 1) |
107
|
1 |
|
: levels.get(levels.size() - 1) |
108
|
|
|
; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the maximum player level |
113
|
|
|
*/ |
114
|
|
|
int maxLevel() { |
115
|
1 |
|
return levels.size(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Apply the level up bonus |
120
|
|
|
* |
121
|
|
|
* @param entity The player entity |
122
|
|
|
* @param newLevel The reached level |
123
|
|
|
*/ |
124
|
|
|
void applyLevelUpBonus(Player entity, @Positive int newLevel) { |
125
|
1 |
|
final int diff = newLevel - entity.level(); |
126
|
|
|
|
127
|
1 |
|
if (diff > 0) { |
128
|
1 |
|
entity.setSpellPoints(entity.spellPoints() + configuration.spellBoostPointsOnLevelUp() * diff); |
129
|
1 |
|
entity.setBoostPoints(entity.boostPoints() + configuration.characteristicPointsOnLevelUp() * diff); |
130
|
|
|
} |
131
|
1 |
|
} |
132
|
|
|
} |
133
|
|
|
|