Client & network general improvement
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package gltronic.tronio.business;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
@@ -13,6 +15,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import gltronic.tronio.model.Game;
|
||||
import gltronic.tronio.model.GameUpdate;
|
||||
import gltronic.tronio.model.Player;
|
||||
import gltronic.tronio.model.Wall;
|
||||
|
||||
@@ -54,8 +57,12 @@ public class GameManager implements IGameManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Scheduled(fixedDelay = 1000 / 60)
|
||||
@Scheduled(fixedDelay = 1000 / 120)
|
||||
public void step() throws InterruptedException, IOException {
|
||||
Instant currentTime = Instant.now();
|
||||
Duration durationSinceLastUpdate = Duration.between(game.getLastUpdate(), currentTime);
|
||||
game.setLastUpdate(currentTime);
|
||||
|
||||
// CHECK OUT OF BORDERS & COLISIONS
|
||||
game.getPlayers().forEach((id, player) -> {
|
||||
// OUT OF BORDERS
|
||||
@@ -92,21 +99,29 @@ public class GameManager implements IGameManager {
|
||||
|
||||
// ADD WALLS & MOVE PLAYER
|
||||
game.getPlayers().forEach((id, player) -> {
|
||||
// WALL
|
||||
player.setLastWall(player.getLastWall() + 1);
|
||||
if (player.getLastWall() > game.getSettings().getWallUpdate()) {
|
||||
player.getWalls().add(new Wall(player.getX(), player.getY()));
|
||||
player.setLastWall(0);
|
||||
}
|
||||
// On cherche le nombre de pas pour atteindre les 60 up/s
|
||||
long tickToSimulate = ( durationSinceLastUpdate.toMillis() * 120 ) / 1000;
|
||||
|
||||
// MOVE
|
||||
player.setX(player.getX() + game.getSettings().getPlayerSpeed() * Math.cos(player.getAngle()));
|
||||
player.setY(player.getY() + game.getSettings().getPlayerSpeed() * Math.sin(player.getAngle()));
|
||||
for (long i = 0; i <= tickToSimulate; i++) {
|
||||
// WALL
|
||||
player.setLastWall(player.getLastWall() + 1);
|
||||
if (player.getLastWall() > game.getSettings().getWallUpdate()) {
|
||||
player.getWalls().add(new Wall(player.getX(), player.getY()));
|
||||
player.setLastWall(0);
|
||||
}
|
||||
|
||||
// MOVE
|
||||
player.setX(player.getX() + game.getSettings().getPlayerSpeed() * Math.cos(player.getAngle()));
|
||||
player.setY(player.getY() + game.getSettings().getPlayerSpeed() * Math.sin(player.getAngle()));
|
||||
}
|
||||
});
|
||||
|
||||
// Broadcast une fois sur deux
|
||||
if (game.isUpdateNeeded()) {
|
||||
SocketUtils.broadcast(game, "gameUpdate", new ObjectMapper().writeValueAsString(game.getPlayers().values()));
|
||||
GameUpdate update = new GameUpdate();
|
||||
update.setTime(game.getLastUpdate());
|
||||
update.setPlayers(game.getPlayers().values());
|
||||
SocketUtils.broadcast(game, "gameUpdate", new ObjectMapper().writeValueAsString(update));
|
||||
game.setUpdateNeeded(false);
|
||||
} else game.setUpdateNeeded(true);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package gltronic.tronio.model;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -17,18 +18,13 @@ public class Game {
|
||||
private Map<String, WebSocketSession> sessions;
|
||||
private GameSettings settings;
|
||||
private boolean updateNeeded;
|
||||
private Instant lastUpdate;
|
||||
|
||||
public Game() {
|
||||
this.updateNeeded = false;
|
||||
this.lastUpdate = Instant.now();
|
||||
this.players = new HashMap<>();
|
||||
this.sessions = new HashMap<>();
|
||||
this.settings = new GameSettings();
|
||||
|
||||
this.settings.setArenaSize(1000);
|
||||
this.settings.setPlayerSize(10);
|
||||
this.settings.setPlayerSpeed(2);
|
||||
this.settings.setPlayerTurnSpeed(10);
|
||||
this.settings.setWallSize(8);
|
||||
this.settings.setWallUpdate(5);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,10 @@
|
||||
package gltronic.tronio.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class GameSettings {
|
||||
private double playerSize;
|
||||
private double playerSpeed;
|
||||
@@ -14,4 +12,13 @@ public class GameSettings {
|
||||
private double wallSize;
|
||||
private double wallUpdate;
|
||||
private double arenaSize;
|
||||
|
||||
public GameSettings () {
|
||||
this.arenaSize = 1000;
|
||||
this.playerSize = 10;
|
||||
this.playerSpeed = 2;
|
||||
this.playerTurnSpeed = 10;
|
||||
this.wallSize = 8;
|
||||
this.wallUpdate = 5;
|
||||
}
|
||||
}
|
||||
17
server/src/main/java/gltronic/tronio/model/GameUpdate.java
Normal file
17
server/src/main/java/gltronic/tronio/model/GameUpdate.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package gltronic.tronio.model;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Component
|
||||
public class GameUpdate {
|
||||
private Collection<Player> players;
|
||||
private Instant time;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
server/target/classes/gltronic/tronio/model/GameUpdate.class
Normal file
BIN
server/target/classes/gltronic/tronio/model/GameUpdate.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user