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.exploration.map.cell.trigger; |
21
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.core.event.EventsSubscriber; |
23
|
|
|
import fr.quatrevieux.araknemu.core.event.Listener; |
24
|
|
|
import fr.quatrevieux.araknemu.data.world.entity.environment.MapTrigger; |
25
|
|
|
import fr.quatrevieux.araknemu.data.world.repository.environment.MapTriggerRepository; |
26
|
|
|
import fr.quatrevieux.araknemu.game.PreloadableService; |
27
|
|
|
import fr.quatrevieux.araknemu.game.event.GameStarted; |
28
|
|
|
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap; |
29
|
|
|
import fr.quatrevieux.araknemu.game.exploration.map.cell.trigger.action.CellAction; |
30
|
|
|
import fr.quatrevieux.araknemu.game.exploration.map.cell.trigger.action.CellActionFactory; |
31
|
|
|
import fr.quatrevieux.araknemu.game.exploration.map.event.MapLoaded; |
32
|
|
|
import fr.quatrevieux.araknemu.game.listener.map.PerformCellActions; |
33
|
|
|
import org.apache.logging.log4j.Logger; |
34
|
|
|
|
35
|
|
|
import java.util.ArrayList; |
36
|
|
|
import java.util.Collection; |
37
|
|
|
import java.util.Collections; |
38
|
|
|
import java.util.HashMap; |
39
|
|
|
import java.util.Map; |
40
|
|
|
import java.util.stream.Collectors; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Handle exploration map triggers |
44
|
|
|
*/ |
45
|
|
|
public final class MapTriggerService implements PreloadableService, EventsSubscriber { |
46
|
|
|
private final MapTriggerRepository repository; |
47
|
|
|
private final CellActionFactory actionFactory; |
48
|
|
|
|
49
|
1 |
|
private final Map<Integer, Collection<MapTrigger>> triggers = new HashMap<>(); |
50
|
1 |
|
private boolean preloading = false; |
51
|
|
|
|
52
|
1 |
|
public MapTriggerService(MapTriggerRepository repository, CellActionFactory actionFactory) { |
53
|
1 |
|
this.repository = repository; |
54
|
1 |
|
this.actionFactory = actionFactory; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
@Override |
58
|
|
|
public void preload(Logger logger) { |
59
|
1 |
|
logger.info("Loading map cells triggers..."); |
60
|
|
|
|
61
|
1 |
|
final Collection<MapTrigger> mapTriggers = repository.all(); |
62
|
|
|
|
63
|
1 |
|
for (MapTrigger trigger : mapTriggers) { |
64
|
1 |
|
triggers |
65
|
1 |
|
.computeIfAbsent(trigger.map(), mapId -> new ArrayList<>()) |
66
|
1 |
|
.add(trigger) |
67
|
|
|
; |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
preloading = true; |
71
|
1 |
|
logger.info("{} triggers loaded", mapTriggers.size()); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
@Override |
75
|
|
|
public String name() { |
76
|
|
|
return "map.trigger"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
@Override |
80
|
|
|
public Listener[] listeners() { |
81
|
1 |
|
return new Listener[] { |
82
|
1 |
|
new Listener<MapLoaded>() { |
83
|
|
|
@Override |
84
|
|
|
public void on(MapLoaded event) { |
85
|
1 |
|
event.map().dispatcher().add(new PerformCellActions()); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
@Override |
89
|
|
|
public Class<MapLoaded> event() { |
90
|
1 |
|
return MapLoaded.class; |
91
|
|
|
} |
92
|
|
|
}, |
93
|
1 |
|
new Listener<GameStarted>() { |
94
|
|
|
@Override |
95
|
|
|
public void on(GameStarted event) { |
96
|
|
|
preloading = false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
@Override |
100
|
|
|
public Class<GameStarted> event() { |
101
|
1 |
|
return GameStarted.class; |
102
|
|
|
} |
103
|
|
|
}, |
104
|
|
|
}; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get all triggers for a map |
109
|
|
|
*/ |
110
|
|
|
public Collection<CellAction> forMap(ExplorationMap map) { |
111
|
1 |
|
Collection<MapTrigger> triggersOnMap = triggers.get(map.id()); |
112
|
|
|
|
113
|
1 |
|
if (triggersOnMap == null) { |
114
|
|
|
// Triggers are preloaded but not found on this map |
115
|
1 |
|
if (preloading) { |
116
|
|
|
return Collections.emptyList(); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
triggers.put(map.id(), triggersOnMap = repository.findByMap(map.id())); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
return triggersOnMap.stream() |
123
|
1 |
|
.map(actionFactory::create) |
124
|
1 |
|
.collect(Collectors.toList()) |
125
|
|
|
; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|