Skip to content

Commit

Permalink
feat: 自由落体运动测试通过!
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Aug 8, 2023
1 parent a715c70 commit 89832e2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public static BlockAttributes fromNBT(NbtMap nbt) {
return BlockAttributes
.builder()
.aabbCollision(parseAABBStr(nbt.getString("aabbCollision")))
.hasCollision(nbt.getBoolean("hasCollision"))
.blocksPrecipitation(nbt.getBoolean("blocksPrecipitation"))
.canBeMovingBlock(nbt.getBoolean("canBeMovingBlock"))
.breaksFallingBlocks(nbt.getBoolean("breaksFallingBlocks"))
Expand Down
20 changes: 20 additions & 0 deletions Allay-API/src/main/java/cn/allay/api/data/VanillaEntityTypes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cn.allay.api.data;

import cn.allay.api.component.annotation.AutoRegister;
import cn.allay.api.component.interfaces.ComponentProvider;
import cn.allay.api.entity.component.impl.base.EntityBaseComponentImpl;
import cn.allay.api.entity.impl.*;
import cn.allay.api.entity.type.EntityInitInfo;
import cn.allay.api.entity.type.EntityType;
import cn.allay.api.entity.type.EntityTypeBuilder;
import org.joml.primitives.AABBd;
Expand Down Expand Up @@ -631,12 +635,28 @@ public interface VanillaEntityTypes {
EntityType<EntityVillager> VILLAGER_TYPE = EntityTypeBuilder
.builder(EntityVillager.class)
.vanillaEntity(VanillaEntityId.VILLAGER)
.addComponent(
ComponentProvider.of(
info -> new EntityBaseComponentImpl<>(
(EntityInitInfo<EntityVillager>) info,
//TODO: 小村民
e -> new AABBd(-0.3, 0, -0.3, 0.3, 1.8, 0.3)
), EntityBaseComponentImpl.class
))
.addBasicComponents()
.build();

EntityType<EntityVillagerV2> VILLAGER_V2_TYPE = EntityTypeBuilder
.builder(EntityVillagerV2.class)
.vanillaEntity(VanillaEntityId.VILLAGER_V2)
.addComponent(
ComponentProvider.of(
info -> new EntityBaseComponentImpl<>(
(EntityInitInfo<EntityVillagerV2>) info,
//TODO: 小村民
e -> new AABBd(-0.3, 0, -0.3, 0.3, 1.8, 0.3)
), EntityBaseComponentImpl.class
))
.addBasicComponents()
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,17 @@ default double getStepHeight() {
default double getGravity() {
return 0.08;
}

double SPRINTING_MOVEMENT_FACTOR = 1.3;
double WALKING_MOVEMENT_FACTOR = 1;
double SNEAKING_MOVEMENT_FACTOR = 0.3;
double STOP_MOVEMENT_FACTOR = 0;

/**
* 给定yaw,若移动乘数不为0实体将往yaw指定的方向前进 <p>
* 参见:<a href="https://www.mcpk.wiki/wiki/Horizontal_Movement_Formulas/zh">...</a>
*/
default double getMovementFactor() {
return STOP_MOVEMENT_FACTOR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void tick() {
protected void updateMotion(Entity entity) {
//TODO: 效果乘数
var effectFactor = 1;
double movementFactor = entity.getMovementFactor();
var blockUnder = world.getBlockState((int) entity.getLocation().x(), (int) (entity.getLocation().y() - 0.5), (int) entity.getLocation().z());
double slipperyFactor = blockUnder != null ?
blockUnder.blockType().getBlockBehavior().getBlockAttributes(blockUnder).friction() :
Expand All @@ -79,11 +80,11 @@ protected void updateMotion(Entity entity) {
var approachMz = mz * slipperyFactor * 0.91;
double yaw = entity.getLocation().yaw();
if (entity.isOnGround()) {
newMx = approachMx + 0.1 * effectFactor * Math.pow(0.6 / slipperyFactor, 3) * Math.sin(yaw);
newMz = approachMz + 0.1 * effectFactor * Math.pow(0.6 / slipperyFactor, 3) * Math.cos(yaw);
newMx = approachMx + 0.1 * movementFactor * effectFactor * Math.pow(0.6 / slipperyFactor, 3) * Math.sin(yaw);
newMz = approachMz + 0.1 * movementFactor * effectFactor * Math.pow(0.6 / slipperyFactor, 3) * Math.cos(yaw);
} else {
newMx = approachMx + 0.02 * Math.sin(yaw);
newMz = approachMz + 0.02 * Math.cos(yaw);
newMx = approachMx + 0.02 * movementFactor * Math.sin(yaw);
newMz = approachMz + 0.02 * movementFactor * Math.cos(yaw);
}
double newMy = (my - entity.getGravity()) * 0.98;
if (Math.abs(newMx) < MOTION_THRESHOLD) newMx = 0;
Expand Down
Binary file modified Data/block_attributes.nbt
Binary file not shown.

0 comments on commit 89832e2

Please sign in to comment.