Commit 4c0db6d9 authored by Anders H. Rebner's avatar Anders H. Rebner
Browse files

cPlayerPieceMove implemented

parent 1abc43b1
package com.mygdx.game.controllers;
import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.controllers.commands.cPlayerPieceMove;
import com.mygdx.game.model.Game;
import com.mygdx.game.model.Player;
import com.mygdx.game.views.PlayView;
......@@ -13,7 +14,7 @@ public class GameController {
private StarPiece activePiece; // Previously clicked piece
private Vector3 previousCoordinateClicked; // Previously clicked coordinate
private PlayerController playerController;
private final PlayerController playerController;
public GameController(Game model, PlayView view, PlayerController playerController) {
this.model = model;
......@@ -51,13 +52,23 @@ public class GameController {
this.view.movePiece(activePiece, cubeCoordinates);
activePiece.setRotateHead(false);
activePiece = null;
playerController.getNetWorkController().sendToServer(new cPlayerPieceMove(previousCoordinateClicked, cubeCoordinates, playerController.getLobby().getID(), playerController.getPlayer().getID()));
previousCoordinateClicked = null;
// TODO: Check if all pieces of the player are in goal? (model.isPlayerFinished)
} else {
System.out.println("Move was not allowed");
// TODO: Give feedback (move was not valid/not executed)
}
}
}
}
}
public void playerMovedPiece(Vector3 fromCoordinates, Vector3 toCoordinates, int playerId) {
if (this.model.movePiece(fromCoordinates, toCoordinates, playerId)) {
this.view.moveOtherPlayerPiece(fromCoordinates, toCoordinates);
} else {
System.out.println("ERROR: Could not execute moving of other players piece");
}
}
}
package com.mygdx.game.controllers;
import com.badlogic.gdx.math.Vector3;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.mygdx.game.controllers.commands.Command;
......@@ -9,6 +10,7 @@ import com.mygdx.game.model.Player;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class NetworkController {
......@@ -21,7 +23,7 @@ public class NetworkController {
this.client = new Client();
client.start();
try {
String IP4_LAN_ADDRESS = "192.168.0.122"; //136 //"192.168.87.20";
String IP4_LAN_ADDRESS = "192.168.0.136"; //122 //"192.168.87.20";
client.connect(10000, IP4_LAN_ADDRESS, 54555, 54777);
} catch (IOException e) {
e.printStackTrace();
......@@ -50,6 +52,7 @@ public class NetworkController {
kryo.register(cSetPlayerReady.class, 26);
kryo.register(cSetPlayerName.class, 27);
kryo.register(cStartGame.class, 28);
kryo.register(Vector3.class, 29);
}
public Client getClient() { return client; }
......
......@@ -20,6 +20,7 @@ public class PlayerController {
private Player player; //This is the current player
private Lobby lobby; //Current lobby
private ArrayList<Lobby> lobbies; //List of all lobbies
private GameController gameController;
public PlayerController(NetworkController networkController){
net = networkController;
......@@ -103,5 +104,11 @@ public class PlayerController {
public Command getLastCommand(){ return receivedCommands.getFirst(); }
public void setGameController(GameController gameController) {
this.gameController = gameController;
}
public GameController getGameController() {
return gameController;
}
}
package com.mygdx.game.controllers.commands;
import com.badlogic.gdx.math.Vector3;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controllers.NetworkController;
import com.mygdx.game.controllers.PlayerController;
import java.util.ArrayList;
import java.util.List;
public class cPlayerPieceMove extends Command {
Vector3 fromCoordinates;
Vector3 toCordinates;
int lobby_id;
int playerId;
public cPlayerPieceMove() {
super("cPlayerPieceMove");
}
public cPlayerPieceMove(Vector3 fromCoordinates, Vector3 toCoordinates, int lobbyId, int playerId) {
super("cPlayerPieceMove");
this.fromCoordinates = fromCoordinates;
this.toCordinates = toCoordinates;
this.lobby_id = lobbyId;
this.playerId = playerId;
ArrayList<Object> data = new ArrayList<Object>();
data.add(fromCoordinates);
data.add(toCoordinates);
data.add(playerId);
this.data = data;
}
@Override
public void execute(PlayerController playerController, Connection connection){
//SERVER LOGIC
System.out.println("Execute on client. Update model on both-, with views, animations and assets on client side.");
if (data instanceof ArrayList) {
List<Object> receivedData = (ArrayList<Object>) data;
Vector3 fromCoordinates = (Vector3) receivedData.get(0);
Vector3 toCoordinates = (Vector3) receivedData.get(1);
int playerId = (int) receivedData.get(2);
playerController.getGameController().playerMovedPiece(fromCoordinates, toCoordinates, playerId);
System.out.printf("Received move from other player. Player ID = %d \n", playerId);
}
}
}
......@@ -7,15 +7,4 @@ class Piece(ownerId: Int) {
fun getOwnerId(): Int {
return this.ownerId
}
/*private var owner: Player
init {
this.owner = owner
}
fun getOwner(): Player {
return this.owner
}*/
}
\ No newline at end of file
}
......@@ -46,6 +46,7 @@ class DefaultBoard: AbstractBoard() {
override fun movePiece(fromCoordinates: Vector3, toCoordinates: Vector3): Boolean {
val piece = fields[fromCoordinates]?.getPiece()
if (piece != null) {
fields[toCoordinates]?.setPiece(piece)
fields[fromCoordinates]?.removePiece()
......
......@@ -10,5 +10,5 @@ abstract class AbstractRules {
abstract fun getPlayerStartfields(playerIndex: Int) : List<Vector3> // Returns a list of startfields for a given player
abstract fun getPlayerTargetFields(playerIndex: Int) : List<Vector3> // Returns a list of targetfields for a given player
abstract fun generateStartFields(playerCount: Int): List<List<Vector3>> // Returns a list with lists of startfields
abstract fun generateStartFields(playerCount: Int = 6): List<List<Vector3>> // Returns a list with lists of startfields
}
\ No newline at end of file
......@@ -5,8 +5,7 @@ import com.mygdx.game.model.AbstractRules
import com.mygdx.game.model.Field
class DefaultRules: AbstractRules() {
// Sequence: top, upperright, lowerright, bottom, lowerleft, upperleft
override var startFields = generateStartFields(6) // TODO: is this used/necessary?
override var startFields = generateStartFields()
override var moveRange = 1
override var jumpRange = 2
......@@ -27,6 +26,7 @@ class DefaultRules: AbstractRules() {
return startFields[index]
}
// Sequence: top, upperright, lowerright, bottom, lowerleft, upperleft
override fun generateStartFields(playerCount: Int): List<List<Vector3>> {
val startFields: MutableList<List<Vector3>> = mutableListOf()
val allStartFields: List<List<Vector3>> = listOf(
......
......@@ -276,9 +276,11 @@ public class CinematicView extends View{
stage.clear();
startFadeout = false;
// Initialize model and set PlayView
Game model = new Game(new GameState(new GameMode(new DefaultRules(), new DefaultBoard())), playerController.getLobby().getPlayersID());
PlayView playView = new PlayView(gvm, playerController, assetManager, stage, skin, lobbyAvatars);
GameController gameController = new GameController(model, playView, playerController);
playerController.setGameController(gameController);
playView.setGameController(gameController);
gvm.set(playView);
......
......@@ -122,7 +122,6 @@ public class PlayView extends View{
public void render(float dt) {
stage.getBatch().begin();
//stage.getBatch().draw(background, 0, 0);
stage.getBatch().end();
stage.draw();
......@@ -217,6 +216,17 @@ public class PlayView extends View{
pieces.put(toCoordinates, piece);
}
public void moveOtherPlayerPiece(Vector3 fromCoordinates, Vector3 toCoordinates) {
Float[] pixelCoordinates = UtilsKt.cubeToPixel(toCoordinates, hex_side_length);
StarPiece piece = getPiece(fromCoordinates);
piece.setX((Gdx.graphics.getWidth() / 2F) + pixelCoordinates[0] - ((starPieceBase.getWidth() * scale_factor_piece) / 2F));
piece.setY(Gdx.graphics.getHeight() / 2F - pixelCoordinates[1] - ((starPieceBase.getHeight() * scale_factor_piece) / 2F));
// Replace key
pieces.put(toCoordinates, pieces.remove(fromCoordinates)); // TODO: May cause concurrency issues. If so, create new hashmap and assign it to this.pieces
}
private void debugDraw(SpriteBatch sb, String text, int xPos, int yPos){
}
......
package com.mygdx.game.controller;
import com.badlogic.gdx.math.Vector3;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
......@@ -10,6 +11,7 @@ import com.mygdx.game.model.Player;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
public class NetworkController {
......@@ -158,6 +160,7 @@ public class NetworkController {
kryo.register(cSetPlayerReady.class, 26);
kryo.register(cSetPlayerName.class, 27);
kryo.register(cStartGame.class, 28);
kryo.register(Vector3.class, 29);
}
public NetworkController getNetworkController() { return this; }
......
package com.mygdx.game.controller.commands;
import com.badlogic.gdx.math.Vector3;
import com.esotericsoftware.kryonet.Connection;
import com.mygdx.game.controller.NetworkController;
import java.util.ArrayList;
public class cPlayerPieceMove extends Command {
Vector3 fromCoordinates;
Vector3 toCordinates;
int lobby_id;
int playerId;
public cPlayerPieceMove() {
super("cPlayerPieceMove");
}
/*public cPlayerPieceMove(Vector3 fromCoordinates, Vector3 toCoordinates, int lobbyId, int playerId) {
super("cPlayerPieceMove");
this.fromCoordinates = fromCoordinates;
this.toCordinates = toCoordinates;
this.lobby_id = lobbyId;
this.playerId = playerId;
ArrayList<Object> data = new ArrayList<Object>();
data.add(fromCoordinates);
data.add(toCoordinates);
data.add(playerId);
this.data = data;
}*/
@Override
public void execute(NetworkController net, Connection connection){
System.out.println("Checking if valid move.. If valid, moving player piece from A to B... Updating model.. Sending move to clients....");
connection.sendTCP(this);
System.out.println(data);
if (data instanceof ArrayList) {
System.out.println("Checking if valid move.. If valid, moving player piece from A to B... Updating model.. Sending move to clients....");
}
for (Connection c : net.getConnections(net.getLobby(lobby_id))) {
if (c != connection) {
c.sendTCP(this);
}
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment