|
@@ -1,66 +0,0 @@
|
|
|
-import com.artemis.Entity;
|
|
|
-import com.artemis.World;
|
|
|
-import com.artemis.WorldConfiguration;
|
|
|
-import com.artemis.WorldConfigurationBuilder;
|
|
|
-import message.MessageHandlerSystem;
|
|
|
-import message.WebSocketEventDispatcherSystem;
|
|
|
-import network.NettySystem;
|
|
|
-
|
|
|
-import java.time.Duration;
|
|
|
-import java.util.concurrent.CountDownLatch;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
-import java.util.concurrent.ScheduledExecutorService;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-/**
|
|
|
- * Main 入口
|
|
|
- */
|
|
|
-public class App {
|
|
|
-
|
|
|
- private static World world;
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
-
|
|
|
- try {
|
|
|
- // 构建 WorldConfiguration,注册 RealmSystem 和 GameSystem
|
|
|
- WorldConfiguration config = new WorldConfigurationBuilder()
|
|
|
- .with(new WebSocketEventDispatcherSystem())
|
|
|
- .with(new MessageHandlerSystem())
|
|
|
- .with(new NettySystem())
|
|
|
- .build();
|
|
|
-
|
|
|
- // 创建 World 对象(服务端主实体)
|
|
|
- world = new World(config);
|
|
|
-
|
|
|
- // 根据类型创建 Game 实体
|
|
|
- Entity gameEntity = world.createEntity();
|
|
|
- // 可以在 gameEntity 上挂载其它组件,示例略
|
|
|
-
|
|
|
- // 使用虚拟线程创建 ScheduledExecutorService(JDK23 新特性)
|
|
|
- ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(Thread.ofVirtual().factory());
|
|
|
- // 每 16 毫秒更新一次,大约 60 次/秒,使用 Duration 表达时间间隔
|
|
|
- executor.scheduleAtFixedRate(() -> {
|
|
|
- world.setDelta(0.016f); // 单位为秒
|
|
|
- world.process(); // 更新所有系统
|
|
|
- }, 0, Duration.ofMillis(16).toMillis(), TimeUnit.MILLISECONDS);
|
|
|
-
|
|
|
- // 添加 JVM 关闭钩子,确保退出时释放资源
|
|
|
- Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
- System.out.println("Shutdown hook triggered, releasing resources...");
|
|
|
- executor.shutdown();
|
|
|
- world.dispose();
|
|
|
- }));
|
|
|
-
|
|
|
- // 阻塞 main 线程,保持服务运行(使用 CountDownLatch 更语义化)
|
|
|
- try {
|
|
|
- new CountDownLatch(1).await();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- System.err.println("Main thread interrupted: " + e.getMessage());
|
|
|
- Thread.currentThread().interrupt();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|