Skip to content

Commit

Permalink
添加基础移动速度设置
Browse files Browse the repository at this point in the history
  • Loading branch information
lt-name committed Jul 18, 2021
1 parent 4be6c7b commit c2e952f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
12 changes: 9 additions & 3 deletions src/main/java/com/smallaswater/npc/data/RsNpcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public class RsNpcConfig {

private final ArrayList<String> cmds = new ArrayList<>();
private final ArrayList<String> messages = new ArrayList<>();


@Getter
private final int baseMoveSpeed;

@Getter
private final ArrayList<Vector3> route = new ArrayList<>();

Expand Down Expand Up @@ -89,9 +92,10 @@ public RsNpcConfig(@NonNull String name, @NonNull Config config) throws RsNpcLoa
this.canProjectilesTrigger = config.getBoolean("允许抛射物触发", true);

this.cmds.addAll(config.getStringList("点击执行指令"));

this.messages.addAll(config.getStringList("发送消息"));


this.baseMoveSpeed = config.getInt("基础移动速度", 1);

for (String string : config.getStringList("route")) {
String[] s = string.split(":");
this.route.add(new Vector3(Double.parseDouble(s[0]),
Expand Down Expand Up @@ -132,6 +136,8 @@ public void save() {

this.config.set("点击执行指令", this.cmds);
this.config.set("发送消息", this.messages);

this.config.set("基础移动速度", this.baseMoveSpeed);

ArrayList<String> list = new ArrayList<>();
for (Vector3 vector3 : this.route) {
Expand Down
53 changes: 27 additions & 26 deletions src/main/java/com/smallaswater/npc/entitys/EntityRsNpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockLiquid;
import cn.nukkit.entity.EntityHuman;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.math.Vector3;
Expand Down Expand Up @@ -57,9 +57,11 @@ public boolean onUpdate(int currentTick) {
this.close();
return false;
}
if (this.config.isLookAtThePlayer() &&
//看向玩家
if (currentTick%2 == 0 &&
this.config.isLookAtThePlayer() &&
this.config.getRoute().isEmpty() &&
!this.getLevel().getPlayers().isEmpty() && currentTick%2 == 0) {
!this.getLevel().getPlayers().isEmpty()) {
RsNpcX.THREAD_POOL_EXECUTOR.execute(() -> {
LinkedList<Player> npd = new LinkedList<>(this.getViewers().values());
npd.sort((mapping1, mapping2) ->
Expand All @@ -79,7 +81,8 @@ public boolean onUpdate(int currentTick) {
}
});
}


//表情
if (this.config.isEnableEmote() && !this.config.getEmoteIDs().isEmpty()) {
if (currentTick % 20 == 0) {
this.emoteSecond++;
Expand All @@ -93,7 +96,8 @@ public boolean onUpdate(int currentTick) {
Server.broadcastPacket(this.getViewers().values(), packet);
}
}


//寻路
if (!this.config.getRoute().isEmpty()) {
if (this.nodes.isEmpty()) {
if (!this.lockRoute) {
Expand All @@ -112,7 +116,7 @@ public boolean onUpdate(int currentTick) {
}

if (!this.nodes.isEmpty()) {
if (this.nowNode == null || this.distance(this.nowNode.getVector3()) <= ((this.getWidth()) / 2 + 0.05) /*this.distance(this.nowNode.getVector3()) < 0.3*/) {
if (this.nowNode == null || this.distance(this.nowNode.getVector3()) <= 0.35/*((this.getWidth()) / 2 + 0.05)*/) {
this.nowNode = this.nodes.poll();
this.lastUpdateNodeTick = currentTick;
}
Expand All @@ -128,34 +132,31 @@ public boolean onUpdate(int currentTick) {
double diff = Math.abs(x) + Math.abs(z);

this.motionY = vector3.y - this.y;
Block levelBlock = this.getLevelBlock();
if (levelBlock.getId() == 8) {
this.motionX = 0.05 * (x / diff);
this.motionZ = 0.05 * (z / diff);
this.motionY += 0.2;
} else if (levelBlock.getId() == 9) {
this.motionX = 0.05 * (x / diff);
this.motionZ = 0.05 * (z / diff);
if (this.getLevelBlock() instanceof BlockLiquid) {
this.motionX = this.config.getBaseMoveSpeed() * 0.05 * (x / diff);
this.motionZ = this.config.getBaseMoveSpeed() * 0.05 * (z / diff);
} else {
this.motionX = 0.15 * (x / diff);
this.motionZ = 0.15 * (z / diff);
this.motionX = this.config.getBaseMoveSpeed() * 0.15 * (x / diff);
this.motionZ = this.config.getBaseMoveSpeed() * 0.15 * (z / diff);
}

this.move(this.motionX, this.motionY, this.motionZ);
}

//视角计算
if (this.nodes.size() >= 2) {
vector3 = this.nodes.get(1).getVector3();
}
double npcx = this.x - vector3.x;
double npcz = this.z - vector3.z;
double yaw = Math.asin(npcx / Math.sqrt(npcx * npcx + npcz * npcz)) / 3.14D * 180.0D;
if (npcz > 0.0D) {
yaw = -yaw + 180.0D;
if (currentTick%4 == 0) {
if (this.nodes.size() >= 2) {
vector3 = this.nodes.get(1).getVector3();
}
double npcx = this.x - vector3.x;
double npcz = this.z - vector3.z;
double yaw = Math.asin(npcx / Math.sqrt(npcx * npcx + npcz * npcz)) / 3.14D * 180.0D;
if (npcz > 0.0D) {
yaw = -yaw + 180.0D;
}
this.yaw = yaw;
this.pitch = 0;
}
this.yaw = yaw;
this.pitch = 0;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/npc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ name: "NPC1"
- "give @p 260 1&con"
发送消息:
- "%npcName%: @p 您好"
- "送您一个苹果!"
- "送您一个苹果!"
基础移动速度: 1
route: []

0 comments on commit c2e952f

Please sign in to comment.