GameEntity.java 6.34 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
package emu.grasscutter.game.entity;

Melledy's avatar
Melledy committed
3
4
5
import java.util.HashMap;
import java.util.Map;

Melledy's avatar
Melledy committed
6
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
7
8
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.game.props.LifeState;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.game.world.Scene;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.game.world.SpawnDataEntry;
Melledy's avatar
Melledy committed
11
import emu.grasscutter.game.world.World;
12
import emu.grasscutter.net.proto.FightPropPairOuterClass.FightPropPair;
Melledy's avatar
Melledy committed
13
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
Melledy's avatar
Melledy committed
14
15
16
17
import emu.grasscutter.net.proto.MotionInfoOuterClass.MotionInfo;
import emu.grasscutter.net.proto.MotionStateOuterClass.MotionState;
import emu.grasscutter.net.proto.SceneEntityInfoOuterClass.SceneEntityInfo;
import emu.grasscutter.net.proto.VectorOuterClass.Vector;
Melledy's avatar
Melledy committed
18
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
Melledy's avatar
Melledy committed
19
import emu.grasscutter.utils.Position;
20
import it.unimi.dsi.fastutil.ints.Int2FloatMap;
Melledy's avatar
Melledy committed
21
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;
Melledy's avatar
Melledy committed
22
23
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
Melledy's avatar
Melledy committed
24

25
public abstract class GameEntity {
Melledy's avatar
Melledy committed
26
	protected int id;
27
	private final Scene scene;
Melledy's avatar
Melledy committed
28
	private SpawnDataEntry spawnEntry;
Melledy's avatar
Melledy committed
29
	
30
31
32
33
	private int blockId;
	private int configId;
	private int groupId;
	
Melledy's avatar
Melledy committed
34
35
36
37
	private MotionState moveState;
	private int lastMoveSceneTimeMs;
	private int lastMoveReliableSeq;
	
Melledy's avatar
Melledy committed
38
39
40
41
	// Abilities
	private Map<String, Float> metaOverrideMap;
	private Int2ObjectMap<String> metaModifiers;
	
42
	public GameEntity(Scene scene) {
43
		this.scene = scene;
44
		this.moveState = MotionState.MOTION_STATE_NONE;
Melledy's avatar
Melledy committed
45
46
47
48
49
	}
	
	public int getId() {
		return this.id;
	}
50
	
Melledy's avatar
Melledy committed
51
52
53
54
	public int getEntityType() {
		return getId() >> 24;
	}
	
Melledy's avatar
Melledy committed
55
	public World getWorld() {
56
57
58
		return this.getScene().getWorld();
	}

59
	public Scene getScene() {
60
		return this.scene;
Melledy's avatar
Melledy committed
61
62
63
64
65
66
67
68
69
70
	}
	
	public boolean isAlive() {
		return true;
	}

	public LifeState getLifeState() {
		return isAlive() ? LifeState.LIFE_ALIVE : LifeState.LIFE_DEAD;
	}
	
Melledy's avatar
Melledy committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	public Map<String, Float> getMetaOverrideMap() {
		if (this.metaOverrideMap == null) {
			this.metaOverrideMap = new HashMap<>();
		}
		return this.metaOverrideMap;
	}
	
	public Int2ObjectMap<String> getMetaModifiers() {
		if (this.metaModifiers == null) {
			this.metaModifiers = new Int2ObjectOpenHashMap<>();
		}
		return this.metaModifiers;
	}

Melledy's avatar
Melledy committed
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
119
120
121
122
123
124
125
126
127
128
129
130
	public abstract Int2FloatOpenHashMap getFightProperties();
	
	public abstract Position getPosition();
	
	public abstract Position getRotation();
	
	public MotionState getMotionState() {
		return moveState;
	}

	public void setMotionState(MotionState moveState) {
		this.moveState = moveState;
	}

	public int getLastMoveSceneTimeMs() {
		return lastMoveSceneTimeMs;
	}

	public void setLastMoveSceneTimeMs(int lastMoveSceneTimeMs) {
		this.lastMoveSceneTimeMs = lastMoveSceneTimeMs;
	}

	public int getLastMoveReliableSeq() {
		return lastMoveReliableSeq;
	}

	public void setLastMoveReliableSeq(int lastMoveReliableSeq) {
		this.lastMoveReliableSeq = lastMoveReliableSeq;
	}
	
	public void setFightProperty(FightProperty prop, float value) {
		this.getFightProperties().put(prop.getId(), value);
	}
	
	private void setFightProperty(int id, float value) {
		this.getFightProperties().put(id, value);
	}
	
	public void addFightProperty(FightProperty prop, float value) {
		this.getFightProperties().put(prop.getId(), getFightProperty(prop) + value);
	}
	
	public float getFightProperty(FightProperty prop) {
		return getFightProperties().getOrDefault(prop.getId(), 0f);
	}
	
131
132
133
134
135
136
137
138
139
140
	public void addAllFightPropsToEntityInfo(SceneEntityInfo.Builder entityInfo) {
		for (Int2FloatMap.Entry entry : getFightProperties().int2FloatEntrySet()) {
			if (entry.getIntKey() == 0) {
				continue;
			}
			FightPropPair fightProp = FightPropPair.newBuilder().setPropType(entry.getIntKey()).setPropValue(entry.getFloatValue()).build();
			entityInfo.addFightPropList(fightProp);
		}
	}
	
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	public int getBlockId() {
		return blockId;
	}

	public void setBlockId(int blockId) {
		this.blockId = blockId;
	}
	
	public int getConfigId() {
		return configId;
	}

	public void setConfigId(int configId) {
		this.configId = configId;
	}
	
	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}
	
Melledy's avatar
Melledy committed
165
166
167
168
169
170
171
172
173
174
	protected MotionInfo getMotionInfo() {
		MotionInfo proto = MotionInfo.newBuilder()
				.setPos(getPosition().toProto())
				.setRot(getRotation().toProto())
				.setSpeed(Vector.newBuilder())
				.setState(this.getMotionState())
				.build();
		
		return proto;
	}
Melledy's avatar
Melledy committed
175
176
177
178
179
180
181
182

	public SpawnDataEntry getSpawnEntry() {
		return spawnEntry;
	}

	public void setSpawnEntry(SpawnDataEntry spawnEntry) {
		this.spawnEntry = spawnEntry;
	}
Melledy's avatar
Melledy committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
	
	public float heal(float amount) {
		if (this.getFightProperties() == null) {
			return 0f;
		}

		float curHp = getFightProperty(FightProperty.FIGHT_PROP_CUR_HP);
		float maxHp = getFightProperty(FightProperty.FIGHT_PROP_MAX_HP);
		
		if (curHp >= maxHp) {
			return 0f;
		}
		
		float healed = Math.min(maxHp - curHp, amount);
		this.addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, healed);
		
		getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, FightProperty.FIGHT_PROP_CUR_HP));
		
		return healed;
	}
	
	public void damage(float amount) {
		damage(amount, 0);
	}
	
	public void damage(float amount, int killerId) {
		// Sanity check
		if (getFightProperties() == null) {
			return;
		}
		
		// Lose hp
		addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, -amount);
		
		// Check if dead
		boolean isDead = false;
		if (getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) <= 0f) {
			setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, 0f);
			isDead = true;
		}
		
		// Packets
		this.getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, FightProperty.FIGHT_PROP_CUR_HP));
		
		// Check if dead
		if (isDead) {
229
			getScene().killEntity(this, killerId);
Melledy's avatar
Melledy committed
230
231
		}
	}
232
	
Melledy's avatar
Melledy committed
233
234
235
236
237
238
239
240
241
	/**
     * Called when a player interacts with this entity
     * @param player Player that is interacting with this entity
	 * @param interactReq Interact request protobuf data
     */
    public void onInteract(Player player, GadgetInteractReq interactReq) {
        
    }
	
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    /**
     * Called when this entity is added to the world
     */
	public void onCreate() {
		
	}
	
	/**
     * Called when this entity dies
     * @param killerId Entity id of the entity that killed this entity
     */
	public void onDeath(int killerId) {
		
	}
	
	public abstract SceneEntityInfo toProto();
Melledy's avatar
Melledy committed
258
}