mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-13 22:04:36 +01:00
Add basic game loop handling to the game server
This commit is contained in:
@@ -530,6 +530,12 @@ public class Player {
|
|||||||
this.getSession().send(packet);
|
this.getSession().send(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onTick() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Database
|
||||||
|
|
||||||
public void save() {
|
public void save() {
|
||||||
if (this.uid <= 0) {
|
if (this.uid <= 0) {
|
||||||
@@ -570,7 +576,7 @@ public class Player {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proto
|
// Protobuf serialization
|
||||||
|
|
||||||
public PlayerBasicInfo toProto() {
|
public PlayerBasicInfo toProto() {
|
||||||
var proto = PlayerBasicInfo.newInstance()
|
var proto = PlayerBasicInfo.newInstance()
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package emu.lunarcore.server.game;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
import emu.lunarcore.Config.GameServerConfig;
|
import emu.lunarcore.Config.GameServerConfig;
|
||||||
import emu.lunarcore.LunarCore;
|
import emu.lunarcore.LunarCore;
|
||||||
@@ -18,20 +20,20 @@ import kcp.highway.KcpServer;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
public class GameServer extends KcpServer {
|
public class GameServer extends KcpServer {
|
||||||
|
private final InetSocketAddress address;
|
||||||
private final GameServerConfig serverConfig;
|
private final GameServerConfig serverConfig;
|
||||||
private final RegionInfo info;
|
private final RegionInfo info;
|
||||||
private final InetSocketAddress address;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
private final GameServerPacketHandler packetHandler;
|
|
||||||
private final Int2ObjectMap<Player> players;
|
private final Int2ObjectMap<Player> players;
|
||||||
|
private final Timer gameLoopTimer;
|
||||||
|
|
||||||
// Managers
|
// Managers
|
||||||
|
@Getter private final GameServerPacketHandler packetHandler;
|
||||||
@Getter private final BattleService battleService;
|
@Getter private final BattleService battleService;
|
||||||
@Getter private final DropService dropService;
|
@Getter private final DropService dropService;
|
||||||
@Getter private final InventoryService inventoryService;
|
@Getter private final InventoryService inventoryService;
|
||||||
@Getter private final GachaService gachaService;
|
@Getter private final GachaService gachaService;
|
||||||
|
|
||||||
public GameServer(GameServerConfig serverConfig) {
|
public GameServer(GameServerConfig serverConfig) {
|
||||||
// Game Server base
|
// Game Server base
|
||||||
this.serverConfig = serverConfig;
|
this.serverConfig = serverConfig;
|
||||||
@@ -46,6 +48,15 @@ public class GameServer extends KcpServer {
|
|||||||
this.dropService = new DropService(this);
|
this.dropService = new DropService(this);
|
||||||
this.inventoryService = new InventoryService(this);
|
this.inventoryService = new InventoryService(this);
|
||||||
this.gachaService = new GachaService(this);
|
this.gachaService = new GachaService(this);
|
||||||
|
|
||||||
|
// Game loop
|
||||||
|
this.gameLoopTimer = new Timer();
|
||||||
|
this.gameLoopTimer.scheduleAtFixedRate(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
onTick();
|
||||||
|
}
|
||||||
|
}, 0, 1000);
|
||||||
|
|
||||||
// Hook into shutdown event.
|
// Hook into shutdown event.
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
|
Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
|
||||||
@@ -97,6 +108,18 @@ public class GameServer extends KcpServer {
|
|||||||
// Done
|
// Done
|
||||||
LunarCore.getLogger().info("Game Server started on " + address.getPort());
|
LunarCore.getLogger().info("Game Server started on " + address.getPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void onTick() {
|
||||||
|
synchronized (this.players) {
|
||||||
|
for (Player player : this.players.values()) {
|
||||||
|
try {
|
||||||
|
player.onTick();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LunarCore.getLogger().error("[UID: " + player.getUid() + "] Player tick error: ", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void onShutdown() {
|
private void onShutdown() {
|
||||||
// Close server socket
|
// Close server socket
|
||||||
|
|||||||
Reference in New Issue
Block a user