ScriptMonsterSpawnService.java 2.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
package emu.grasscutter.scripts.service;

import emu.grasscutter.data.GameData;
import emu.grasscutter.data.def.MonsterData;
import emu.grasscutter.data.def.WorldLevelData;
import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.scripts.SceneScriptManager;
import emu.grasscutter.scripts.constants.EventType;
import emu.grasscutter.scripts.data.SceneMonster;
import emu.grasscutter.scripts.data.ScriptArgs;
11
import emu.grasscutter.scripts.listener.ScriptMonsterListener;
12
13
14
15
16
17
18

import java.util.ArrayList;
import java.util.List;

public class ScriptMonsterSpawnService {

    private final SceneScriptManager sceneScriptManager;
19
    private final List<ScriptMonsterListener> onMonsterCreatedListener = new ArrayList<>();
20

21
    private final List<ScriptMonsterListener> onMonsterDeadListener = new ArrayList<>();
22
23
24
25
26

    public ScriptMonsterSpawnService(SceneScriptManager sceneScriptManager){
        this.sceneScriptManager = sceneScriptManager;
    }

27
28
    public void addMonsterCreatedListener(ScriptMonsterListener scriptMonsterListener){
        onMonsterCreatedListener.add(scriptMonsterListener);
29
    }
30
31
32
33
34
35
36
37
    public void addMonsterDeadListener(ScriptMonsterListener scriptMonsterListener){
        onMonsterDeadListener.add(scriptMonsterListener);
    }
    public void removeMonsterCreatedListener(ScriptMonsterListener scriptMonsterListener){
        onMonsterCreatedListener.remove(scriptMonsterListener);
    }
    public void removeMonsterDeadListener(ScriptMonsterListener scriptMonsterListener){
        onMonsterDeadListener.remove(scriptMonsterListener);
38
39
    }
    public void onMonsterDead(EntityMonster entityMonster){
40
        onMonsterDeadListener.forEach(l -> l.onNotify(entityMonster));
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
    }
    public void spawnMonster(int groupId, SceneMonster monster) {
        if(monster == null){
            return;
        }

        MonsterData data = GameData.getMonsterDataMap().get(monster.monster_id);

        if (data == null) {
            return;
        }

        // Calculate level
        int level = monster.level;

        if (sceneScriptManager.getScene().getDungeonData() != null) {
            level = sceneScriptManager.getScene().getDungeonData().getShowLevel();
        } else if (sceneScriptManager.getScene().getWorld().getWorldLevel() > 0) {
            WorldLevelData worldLevelData = GameData.getWorldLevelDataMap().get(sceneScriptManager.getScene().getWorld().getWorldLevel());

            if (worldLevelData != null) {
                level = worldLevelData.getMonsterLevel();
            }
        }

        // Spawn mob
        EntityMonster entity = new EntityMonster(sceneScriptManager.getScene(), data, monster.pos, level);
        entity.getRotation().set(monster.rot);
        entity.setGroupId(groupId);
        entity.setConfigId(monster.config_id);

72
        onMonsterCreatedListener.forEach(action -> action.onNotify(entity));
73
74
75
76
77
78

        sceneScriptManager.getScene().addEntity(entity);

        sceneScriptManager.callEvent(EventType.EVENT_ANY_MONSTER_LIVE, new ScriptArgs(entity.getConfigId()));
    }
}