added event Chunk load
This commit is contained in:
@@ -9,8 +9,11 @@ import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.events.CS_PlayerMoveEvent;
|
||||
import mc.core.events.EventBusGetter;
|
||||
import mc.core.events.SC_ChunkLoadEvent;
|
||||
import mc.core.player.PlayerManager;
|
||||
import mc.core.time.TimeProcessor;
|
||||
import mc.core.utils.CompactedCoords;
|
||||
import mc.core.world.chunk.ChunkSection;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@Slf4j
|
||||
@@ -46,6 +49,36 @@ public class GameLoop extends Thread {
|
||||
log.trace("(GameLoop) playerMoveEventHandler()");
|
||||
event.getPlayer().getLocation().setXYZ(event.getNewLocation());
|
||||
|
||||
ChunkSection chunkSection = event.getNewLocation().getChunkSection();
|
||||
int ncX = chunkSection.getX();
|
||||
int ncZ = chunkSection.getZ();
|
||||
chunkSection = event.getPlayer().getLocation().getChunkSection();
|
||||
int ccX = chunkSection.getX();
|
||||
int ccZ = chunkSection.getZ();
|
||||
|
||||
if (event.isRecalcChunk() || (ncX != ccX && ncZ != ccZ)) {
|
||||
/* FIXME заменить "8" на актуальный view distance */
|
||||
final int viewDistance = 8;
|
||||
int cMinX = chunkSection.getX() - viewDistance;
|
||||
int cMaxX = chunkSection.getX() + viewDistance;
|
||||
int cMinZ = chunkSection.getZ() - viewDistance;
|
||||
int cMaxZ = chunkSection.getZ() + viewDistance;
|
||||
|
||||
SC_ChunkLoadEvent eventChunkLoad = new SC_ChunkLoadEvent(event.getPlayer());
|
||||
for (int cZ = cMinZ; cZ <= cMaxZ; cZ++) {
|
||||
for (int cX = cMinX; cX <= cMaxX; cX++) {
|
||||
int compressXZ = CompactedCoords.compressXZ(cX, cZ);
|
||||
if (!event.getPlayer().getLoadedChunks().contains(compressXZ)) {
|
||||
eventChunkLoad.getNeedLoadChunks().add(compressXZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!eventChunkLoad.getNeedLoadChunks().isEmpty()) {
|
||||
EventBusGetter.INSTANCE.post(eventChunkLoad);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO отсылать клиенту только(!) для корректировки позиции
|
||||
// SC_PlayerMoveEvent nextEvent = new SC_PlayerMoveEvent(event.getPlayer());
|
||||
// nextEvent.setNewLocation(event.getNewLocation());
|
||||
|
||||
@@ -18,4 +18,6 @@ public class CS_PlayerMoveEvent extends EventBase {
|
||||
// вообще нужно будет создать реализацию "иммутабл локейшен" для подобных ситуаций
|
||||
@Setter
|
||||
private EntityLocation newLocation;
|
||||
@Setter
|
||||
private boolean recalcChunk = false;
|
||||
}
|
||||
|
||||
16
core/src/main/java/mc/core/events/SC_ChunkLoadEvent.java
Normal file
16
core/src/main/java/mc/core/events/SC_ChunkLoadEvent.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package mc.core.events;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.player.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class SC_ChunkLoadEvent extends EventBase {
|
||||
@Getter
|
||||
private final Player player;
|
||||
@Getter
|
||||
private List<Integer> needLoadChunks = new ArrayList<>();
|
||||
}
|
||||
@@ -7,6 +7,7 @@ package mc.core.player;
|
||||
import mc.core.EntityLocation;
|
||||
import mc.core.network.NetChannel;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface Player {
|
||||
@@ -15,10 +16,14 @@ public interface Player {
|
||||
String getName();
|
||||
boolean isOnline();
|
||||
|
||||
/** Compacted list of Chunk coords (x,z) */
|
||||
List<Integer> getLoadedChunks();
|
||||
|
||||
NetChannel getChannel();
|
||||
void setChannel(NetChannel channel);
|
||||
|
||||
EntityLocation getLocation();
|
||||
//TODO надо определиться - нужно ли здесь setLocation() или нет
|
||||
|
||||
boolean isFlying();
|
||||
void setFlying(boolean value);
|
||||
|
||||
@@ -8,6 +8,8 @@ import lombok.Data;
|
||||
import mc.core.EntityLocation;
|
||||
import mc.core.network.NetChannel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@@ -20,6 +22,7 @@ public class SimplePlayer implements Player {
|
||||
private EntityLocation location = new EntityLocation(0d, 0d, 0d, 0f, 0f, null);
|
||||
private boolean flying = false;
|
||||
private PlayerSettings settings;
|
||||
private List<Integer> loadedChunks = new ArrayList<>();
|
||||
|
||||
public void setLocation(EntityLocation location) {
|
||||
this.location.setXYZ(location);
|
||||
|
||||
Reference in New Issue
Block a user