Game loop
This commit is contained in:
65
src/main/java/mc/core/GameLoop.java
Normal file
65
src/main/java/mc/core/GameLoop.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2018-04-21
|
||||||
|
*/
|
||||||
|
package mc.core;
|
||||||
|
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.slf4j.helpers.MessageFormatter;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class GameLoop extends Thread {
|
||||||
|
@Autowired
|
||||||
|
PlayerManager playerManager;
|
||||||
|
private int tps;
|
||||||
|
private long pause;
|
||||||
|
@Setter
|
||||||
|
private boolean traceTPS = false;
|
||||||
|
|
||||||
|
public GameLoop() {
|
||||||
|
super();
|
||||||
|
setTps(20);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTps(int tps) {
|
||||||
|
if (tps > 1000) {
|
||||||
|
log.warn("TPS can't be '{}'. Set 1000", tps);
|
||||||
|
tps = 1000;
|
||||||
|
}
|
||||||
|
this.tps = tps;
|
||||||
|
this.pause = (1000 / tps);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
int tpsFact = 0;
|
||||||
|
long lastTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
while (!isInterrupted()) {
|
||||||
|
if ((System.currentTimeMillis() - lastTime) > 1000) {
|
||||||
|
lastTime = System.currentTimeMillis();
|
||||||
|
if (traceTPS) {
|
||||||
|
String msg = MessageFormatter.format("TPS: {}/{}", tpsFact, tps).getMessage();
|
||||||
|
if (tpsFact < tps) {
|
||||||
|
log.warn(msg);
|
||||||
|
} else {
|
||||||
|
log.info(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tpsFact = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
long futureTime = System.currentTimeMillis() + pause;
|
||||||
|
|
||||||
|
// code there //
|
||||||
|
|
||||||
|
tpsFact++;
|
||||||
|
try {
|
||||||
|
Thread.sleep(futureTime - System.currentTimeMillis());
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,11 +14,17 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
|
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
|
||||||
|
|
||||||
|
GameLoop gameLoop = appContext.getBean(GameLoop.class);
|
||||||
|
gameLoop.start();
|
||||||
|
|
||||||
Server server = appContext.getBean("server", Server.class);
|
Server server = appContext.getBean("server", Server.class);
|
||||||
try {
|
try {
|
||||||
server.start();
|
server.start();
|
||||||
} catch (StartServerException e) {
|
} catch (StartServerException e) {
|
||||||
log.error("Can't start server", e);
|
log.error("Can't start server", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gameLoop.interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,11 @@
|
|||||||
<property name="faviconBase64" value="icon.png"/>
|
<property name="faviconBase64" value="icon.png"/>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
|
<bean id="gameLoop" class="mc.core.GameLoop">
|
||||||
|
<property name="tps" value="20"/>
|
||||||
|
<property name="traceTPS" value="true"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
<bean id="playerManager" class="mc.core.embedded.InMemoryPlayerManager">
|
<bean id="playerManager" class="mc.core.embedded.InMemoryPlayerManager">
|
||||||
<property name="keepAliveInterval" value="10"/>
|
<property name="keepAliveInterval" value="10"/>
|
||||||
</bean>
|
</bean>
|
||||||
|
|||||||
Reference in New Issue
Block a user