private async Task ActionWalk() { float step = 0.2f; float theta = (float)(random.NextDouble() * 2 * Math.PI); float yaw = AttachedObject.GetValue(EntityLookComponent.YawProperty); float head; EntityWorldPos pos = AttachedObject.GetValue(EntityWorldPositionComponent.EntityWorldPositionProperty); if (random.Next(50) == 0) { head = theta; } else { head = (float)(yaw / 180.0f * Math.PI); } await AttachedObject.SetLocalValue(EntityLookComponent.YawProperty, (float)(head / Math.PI * 180.0f)); await AttachedObject.SetLocalValue(EntityLookComponent.HeadYawProperty, (float)(head / Math.PI * 180.0f)); // 新的位置 EntityWorldPos entityPos = new EntityWorldPos(pos.X - step * (float)Math.Sin(head), pos.Y, pos.Z + step * (float)Math.Cos(head)); BlockWorldPos blockPos = entityPos.ToBlockWorldPos(); // 检测行进方向的方块是否满足要求 Cuboid entityBoundbox = new Cuboid(new Point3d(entityPos.X, entityPos.Y, entityPos.Z), new Size(1, 1, 2)); // TODO data from Boundbox component var chunkAccessor = AttachedObject.GetComponent <ChunkAccessorComponent>(); bool isCollided = false; // 检测此位置会不会与方块碰撞 for (int i = 1; blockPos.Y + i < 256 && i <= 3; ++i) { BlockWorldPos upblock = BlockWorldPos.Add(blockPos, 0, i, 0); BlockState upstate = await chunkAccessor.GetBlockState(upblock); if (upstate.IsMobCollided()) { Cuboid blockBoundbox = new Cuboid(new Point3d(upblock.X, upblock.Y, upblock.Z), new Size(1, 1, 1)); if (Collision.IsCollided(entityBoundbox, blockBoundbox)) { isCollided = true; break; } } } // 获得高度变化 int yJumpHeight = 0; bool canWalk = false; for (int i = 0; blockPos.Y + i >= 0 && i >= -2; --i) { BlockState upstate = await chunkAccessor.GetBlockState(BlockWorldPos.Add(blockPos, 0, i + 1, 0)); BlockState state = await chunkAccessor.GetBlockState(BlockWorldPos.Add(blockPos, 0, i, 0)); if (!upstate.IsMobCollided() && state.IsMobCollided() && state.CanMobStand()) { yJumpHeight = i + 1; canWalk = true; break; } } if (!isCollided && canWalk) { await AttachedObject.SetLocalValue( EntityWorldPositionComponent.EntityWorldPositionProperty, EntityWorldPos.Add(entityPos, 0, yJumpHeight, 0)); } }