Server game logic
This commit is contained in:
@@ -1,21 +0,0 @@
|
|||||||
# Getting Started
|
|
||||||
|
|
||||||
### Reference Documentation
|
|
||||||
For further reference, please consider the following sections:
|
|
||||||
|
|
||||||
* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
|
|
||||||
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/maven-plugin/reference/html/)
|
|
||||||
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/maven-plugin/reference/html/#build-image)
|
|
||||||
* [Spring Web](https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications)
|
|
||||||
* [Spring Data JDBC](https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/)
|
|
||||||
* [Spring Data JPA](https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/htmlsingle/#boot-features-jpa-and-spring-data)
|
|
||||||
|
|
||||||
### Guides
|
|
||||||
The following guides illustrate how to use some features concretely:
|
|
||||||
|
|
||||||
* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
|
|
||||||
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
|
|
||||||
* [Building REST services with Spring](https://spring.io/guides/tutorials/bookmarks/)
|
|
||||||
* [Using Spring Data JDBC](https://github.com/spring-projects/spring-data-examples/tree/master/jdbc/basics)
|
|
||||||
* [Accessing Data with JPA](https://spring.io/guides/gs/accessing-data-jpa/)
|
|
||||||
|
|
||||||
@@ -1,29 +1,130 @@
|
|||||||
package gltronic.tronio.business;
|
package gltronic.tronio.business;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.socket.WebSocketSession;
|
import org.springframework.web.socket.WebSocketSession;
|
||||||
|
|
||||||
|
import gltronic.tronio.model.Game;
|
||||||
import gltronic.tronio.model.Player;
|
import gltronic.tronio.model.Player;
|
||||||
|
import gltronic.tronio.model.Wall;
|
||||||
|
|
||||||
public class GameManager implements IGameManager {
|
public class GameManager implements IGameManager {
|
||||||
|
@Autowired
|
||||||
|
Game game;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void login(WebSocketSession session) throws InterruptedException, IOException {
|
public void login(WebSocketSession session) throws InterruptedException, IOException {
|
||||||
// TODO Auto-generated method stub
|
if (game.getSessions().containsKey(session.getId())) {
|
||||||
|
SocketUtils.sendMessage(session, "error", "cant login twice");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Player player = initPlayer(new Player());
|
||||||
|
|
||||||
|
game.getSessions().put(session.getId(), session);
|
||||||
|
game.getPlayers().put(session.getId(), player);
|
||||||
|
|
||||||
|
SocketUtils.forwardMessage(session, "login", new JSONObject(player));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void leave(WebSocketSession session) throws InterruptedException, IOException {
|
public void leave(WebSocketSession session) throws InterruptedException, IOException {
|
||||||
// TODO Auto-generated method stub
|
game.getSessions().remove(session.getId());
|
||||||
|
game.getPlayers().remove(session.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(WebSocketSession session, Player player) throws InterruptedException, IOException {
|
public void updatePlayer(WebSocketSession session, Player player) throws InterruptedException, IOException {
|
||||||
// TODO Auto-generated method stub
|
Player playerToUpdate = game.getPlayers().get(session.getId());
|
||||||
|
playerToUpdate.setAngle(player.getAngle());
|
||||||
|
playerToUpdate.setTargetAngle(player.getTargetAngle());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void step() throws InterruptedException, IOException {
|
||||||
|
// CHECK OUT OF BORDERS & COLISIONS
|
||||||
|
game.getPlayers().forEach((id, player) -> {
|
||||||
|
// OUT OF BORDERS
|
||||||
|
if (player.getX() - game.getSettings().getPlayerSize() < 0 ||
|
||||||
|
player.getX() + game.getSettings().getPlayerSize() > game.getSettings().getArenaSize() ||
|
||||||
|
player.getY() - game.getSettings().getPlayerSize() < 0 ||
|
||||||
|
player.getY() + game.getSettings().getPlayerSize() > game.getSettings().getArenaSize()) {
|
||||||
|
killPlayer(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// COLLISIONS
|
||||||
|
game.getPlayers().forEach((id2, player2) -> {
|
||||||
|
for (var i = 0; i < player2.getWalls().size() - 2; i++) {
|
||||||
|
Wall wallA = player2.getWalls().get(i);
|
||||||
|
Wall wallB = player2.getWalls().get(i + 1);
|
||||||
|
if (isCloseToWall(wallA.getX(), wallA.getY(), wallB.getX(), wallB.getY(), player.getX(), player.getY(), game.getSettings().getPlayerSize())) {
|
||||||
|
if (this.isCrossingLine(wallA.getX(), wallA.getY(), wallB.getX(), wallB.getY(), player.getX(), player.getY(), game.getSettings().getPlayerSize())) {
|
||||||
|
killPlayer(id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// MOVE
|
||||||
|
player.setX(player.getX() + game.getSettings().getPlayerSpeed() * Math.cos(player.getAngle()));
|
||||||
|
player.setY(player.getY() + game.getSettings().getPlayerSpeed() * Math.sin(player.getAngle()));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void killPlayer (String id) {
|
||||||
|
Player player = game.getPlayers().get(id);
|
||||||
|
initPlayer(player);
|
||||||
|
SocketUtils.sendMessage(game.getSessions().get(id), "dead", "yo dead");
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCloseToWall (double xa, double ya, double xb, double yb, double xc, double yc, double radius) {
|
||||||
|
var xar = Math.min(xa, xb) - radius;
|
||||||
|
var yar = Math.min(ya, yb) - radius;
|
||||||
|
var xbr = Math.max(xa, xb) + radius;
|
||||||
|
var ybr = Math.max(ya, yb) + radius;
|
||||||
|
|
||||||
|
return ((xc >= xar && xc <= xbr) && (yc >= yar && yc <= ybr));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCrossingLine (double xa, double ya, double xb, double yb, double xc, double yc, double radius) {
|
||||||
|
var a = xc - xa;
|
||||||
|
var b = xb - xa;
|
||||||
|
var c = yc - ya;
|
||||||
|
var d = yb - ya;
|
||||||
|
var result = (a * d - b * c) / Math.sqrt(Math.pow(xa - xb, 2) + Math.pow(ya - yb, 2));
|
||||||
|
|
||||||
|
return result < radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Player initPlayer(Player player) {
|
||||||
|
Random rand = new Random();
|
||||||
|
double x = game.getSettings().getArenaSize() * (0.25 + Math.random() * 0.5);
|
||||||
|
double y = game.getSettings().getArenaSize() * (0.25 + Math.random() * 0.5);
|
||||||
|
|
||||||
|
player.setAngle(0);
|
||||||
|
player.setTargetAngle(0);
|
||||||
|
player.setLastWall(0);
|
||||||
|
player.setWalls(new ArrayList<Wall>());
|
||||||
|
player.setColor(String.format("#%06x", rand.nextInt(0xffffff + 1)));
|
||||||
|
player.setX(x);
|
||||||
|
player.setY(y);
|
||||||
|
|
||||||
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -9,5 +9,7 @@ import gltronic.tronio.model.Player;
|
|||||||
public interface IGameManager {
|
public interface IGameManager {
|
||||||
void login(WebSocketSession session) throws InterruptedException, IOException;
|
void login(WebSocketSession session) throws InterruptedException, IOException;
|
||||||
void leave(WebSocketSession session) throws InterruptedException, IOException;
|
void leave(WebSocketSession session) throws InterruptedException, IOException;
|
||||||
void update(WebSocketSession session, Player player) throws InterruptedException, IOException;
|
void updatePlayer(WebSocketSession session, Player player) throws InterruptedException, IOException;
|
||||||
|
|
||||||
|
void step() throws InterruptedException, IOException;
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,6 @@ import lombok.Setter;
|
|||||||
@Setter
|
@Setter
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class Player {
|
public class Player {
|
||||||
private String id;
|
|
||||||
private double x;
|
private double x;
|
||||||
private double y;
|
private double y;
|
||||||
private double angle;
|
private double angle;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package gltronic.tronio.model;
|
package gltronic.tronio.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@@ -7,6 +8,7 @@ import lombok.Setter;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class Wall {
|
public class Wall {
|
||||||
private double x;
|
private double x;
|
||||||
private double y;
|
private double y;
|
||||||
|
|||||||
@@ -30,10 +30,8 @@ public class SocketHandler extends TextWebSocketHandler {
|
|||||||
|
|
||||||
String type = (String) jsonObject.get("type");
|
String type = (String) jsonObject.get("type");
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "alive":
|
|
||||||
break;
|
|
||||||
case "update":
|
case "update":
|
||||||
gameManager.update(session, (Player) jsonObject.get("update"));
|
gameManager.updatePlayer(session, (Player) jsonObject.get("update"));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
SocketUtils.sendMessage(session, "error", "unknow command");
|
SocketUtils.sendMessage(session, "error", "unknow command");
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user