GameEntity.java 6.23 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
7
import emu.grasscutter.game.props.FightProperty;
import emu.grasscutter.game.props.LifeState;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.world.Scene;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.game.world.SpawnDataEntry;
Melledy's avatar
Melledy committed
10
import emu.grasscutter.game.world.World;
11
import emu.grasscutter.net.proto.FightPropPairOuterClass.FightPropPair;
Melledy's avatar
Melledy committed
12
13
14
15
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
16
import emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.utils.Position;
18
import it.unimi.dsi.fastutil.ints.Int2FloatMap;
Melledy's avatar
Melledy committed
19
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;
Melledy's avatar
Melledy committed
20
21
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
Melledy's avatar
Melledy committed
22

23
public abstract class GameEntity {
Melledy's avatar
Melledy committed
24
	protected int id;
25
	private final Scene scene;
Melledy's avatar
Melledy committed
26
	private SpawnDataEntry spawnEntry;
KingRainbow44's avatar
KingRainbow44 committed
27

28
29
30
	private int blockId;
	private int configId;
	private int groupId;
KingRainbow44's avatar
KingRainbow44 committed
31

Melledy's avatar
Melledy committed
32
33
34
	private MotionState moveState;
	private int lastMoveSceneTimeMs;
	private int lastMoveReliableSeq;
KingRainbow44's avatar
KingRainbow44 committed
35

Melledy's avatar
Melledy committed
36
37
38
	// Abilities
	private Map<String, Float> metaOverrideMap;
	private Int2ObjectMap<String> metaModifiers;
KingRainbow44's avatar
KingRainbow44 committed
39

40
	public GameEntity(Scene scene) {
41
		this.scene = scene;
42
		this.moveState = MotionState.MOTION_STATE_NONE;
Melledy's avatar
Melledy committed
43
	}
KingRainbow44's avatar
KingRainbow44 committed
44

Melledy's avatar
Melledy committed
45
46
47
	public int getId() {
		return this.id;
	}
KingRainbow44's avatar
KingRainbow44 committed
48

Melledy's avatar
Melledy committed
49
50
51
	public int getEntityType() {
		return getId() >> 24;
	}
KingRainbow44's avatar
KingRainbow44 committed
52

Melledy's avatar
Melledy committed
53
	public World getWorld() {
54
55
56
		return this.getScene().getWorld();
	}

57
	public Scene getScene() {
58
		return this.scene;
Melledy's avatar
Melledy committed
59
	}
KingRainbow44's avatar
KingRainbow44 committed
60

Melledy's avatar
Melledy committed
61
62
63
64
65
66
67
	public boolean isAlive() {
		return true;
	}

	public LifeState getLifeState() {
		return isAlive() ? LifeState.LIFE_ALIVE : LifeState.LIFE_DEAD;
	}
KingRainbow44's avatar
KingRainbow44 committed
68

Melledy's avatar
Melledy committed
69
70
71
72
73
74
	public Map<String, Float> getMetaOverrideMap() {
		if (this.metaOverrideMap == null) {
			this.metaOverrideMap = new HashMap<>();
		}
		return this.metaOverrideMap;
	}
KingRainbow44's avatar
KingRainbow44 committed
75

Melledy's avatar
Melledy committed
76
77
78
79
80
81
82
	public Int2ObjectMap<String> getMetaModifiers() {
		if (this.metaModifiers == null) {
			this.metaModifiers = new Int2ObjectOpenHashMap<>();
		}
		return this.metaModifiers;
	}

Melledy's avatar
Melledy committed
83
	public abstract Int2FloatOpenHashMap getFightProperties();
KingRainbow44's avatar
KingRainbow44 committed
84

Melledy's avatar
Melledy committed
85
	public abstract Position getPosition();
KingRainbow44's avatar
KingRainbow44 committed
86

Melledy's avatar
Melledy committed
87
	public abstract Position getRotation();
KingRainbow44's avatar
KingRainbow44 committed
88

Melledy's avatar
Melledy committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
	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;
	}
KingRainbow44's avatar
KingRainbow44 committed
112

Melledy's avatar
Melledy committed
113
114
115
	public void setFightProperty(FightProperty prop, float value) {
		this.getFightProperties().put(prop.getId(), value);
	}
KingRainbow44's avatar
KingRainbow44 committed
116

Melledy's avatar
Melledy committed
117
118
119
	private void setFightProperty(int id, float value) {
		this.getFightProperties().put(id, value);
	}
KingRainbow44's avatar
KingRainbow44 committed
120

Melledy's avatar
Melledy committed
121
122
123
	public void addFightProperty(FightProperty prop, float value) {
		this.getFightProperties().put(prop.getId(), getFightProperty(prop) + value);
	}
KingRainbow44's avatar
KingRainbow44 committed
124

Melledy's avatar
Melledy committed
125
126
127
	public float getFightProperty(FightProperty prop) {
		return getFightProperties().getOrDefault(prop.getId(), 0f);
	}
KingRainbow44's avatar
KingRainbow44 committed
128

129
130
131
132
133
134
135
136
137
	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);
		}
	}
KingRainbow44's avatar
KingRainbow44 committed
138

139
140
141
142
143
144
145
	public int getBlockId() {
		return blockId;
	}

	public void setBlockId(int blockId) {
		this.blockId = blockId;
	}
KingRainbow44's avatar
KingRainbow44 committed
146

147
148
149
150
151
152
153
	public int getConfigId() {
		return configId;
	}

	public void setConfigId(int configId) {
		this.configId = configId;
	}
KingRainbow44's avatar
KingRainbow44 committed
154

155
156
157
158
159
160
161
	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}
KingRainbow44's avatar
KingRainbow44 committed
162

Melledy's avatar
Melledy committed
163
164
165
166
167
168
169
	protected MotionInfo getMotionInfo() {
		MotionInfo proto = MotionInfo.newBuilder()
				.setPos(getPosition().toProto())
				.setRot(getRotation().toProto())
				.setSpeed(Vector.newBuilder())
				.setState(this.getMotionState())
				.build();
KingRainbow44's avatar
KingRainbow44 committed
170

Melledy's avatar
Melledy committed
171
172
		return proto;
	}
Melledy's avatar
Melledy committed
173
174
175
176
177
178
179
180

	public SpawnDataEntry getSpawnEntry() {
		return spawnEntry;
	}

	public void setSpawnEntry(SpawnDataEntry spawnEntry) {
		this.spawnEntry = spawnEntry;
	}
KingRainbow44's avatar
KingRainbow44 committed
181

Melledy's avatar
Melledy committed
182
183
184
185
186
187
188
	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);
KingRainbow44's avatar
KingRainbow44 committed
189

Melledy's avatar
Melledy committed
190
191
192
		if (curHp >= maxHp) {
			return 0f;
		}
KingRainbow44's avatar
KingRainbow44 committed
193

Melledy's avatar
Melledy committed
194
195
		float healed = Math.min(maxHp - curHp, amount);
		this.addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, healed);
KingRainbow44's avatar
KingRainbow44 committed
196

Melledy's avatar
Melledy committed
197
		getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, FightProperty.FIGHT_PROP_CUR_HP));
KingRainbow44's avatar
KingRainbow44 committed
198

Melledy's avatar
Melledy committed
199
200
		return healed;
	}
KingRainbow44's avatar
KingRainbow44 committed
201

Melledy's avatar
Melledy committed
202
203
204
	public void damage(float amount) {
		damage(amount, 0);
	}
KingRainbow44's avatar
KingRainbow44 committed
205

Melledy's avatar
Melledy committed
206
207
208
209
210
	public void damage(float amount, int killerId) {
		// Sanity check
		if (getFightProperties() == null) {
			return;
		}
KingRainbow44's avatar
KingRainbow44 committed
211

Melledy's avatar
Melledy committed
212
213
		// Lose hp
		addFightProperty(FightProperty.FIGHT_PROP_CUR_HP, -amount);
KingRainbow44's avatar
KingRainbow44 committed
214

Melledy's avatar
Melledy committed
215
216
217
218
219
220
		// Check if dead
		boolean isDead = false;
		if (getFightProperty(FightProperty.FIGHT_PROP_CUR_HP) <= 0f) {
			setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, 0f);
			isDead = true;
		}
KingRainbow44's avatar
KingRainbow44 committed
221

Melledy's avatar
Melledy committed
222
223
		// Packets
		this.getScene().broadcastPacket(new PacketEntityFightPropUpdateNotify(this, FightProperty.FIGHT_PROP_CUR_HP));
KingRainbow44's avatar
KingRainbow44 committed
224

Melledy's avatar
Melledy committed
225
226
		// Check if dead
		if (isDead) {
227
			getScene().killEntity(this, killerId);
Melledy's avatar
Melledy committed
228
229
		}
	}
KingRainbow44's avatar
KingRainbow44 committed
230
231
232
233
234
235
236
237
238
239
240
241

    /**
     * Move this entity to a new position.
     * @param position The new position.
     * @param rotation The new rotation.
     */
    public void move(Position position, Position rotation) {
        // Set the position and rotation.
        this.getPosition().set(position);
        this.getRotation().set(rotation);
    }

242
243
244
245
    /**
     * Called when this entity is added to the world
     */
	public void onCreate() {
KingRainbow44's avatar
KingRainbow44 committed
246

247
	}
KingRainbow44's avatar
KingRainbow44 committed
248

249
250
251
252
253
	/**
     * Called when this entity dies
     * @param killerId Entity id of the entity that killed this entity
     */
	public void onDeath(int killerId) {
KingRainbow44's avatar
KingRainbow44 committed
254

255
	}
KingRainbow44's avatar
KingRainbow44 committed
256

257
	public abstract SceneEntityInfo toProto();
Melledy's avatar
Melledy committed
258
}