public void eatPellet(int id, Player p) { for(int i = 0; i < pelletList.Length; ++i) { if(id == pelletList[i].id) { p.increaseSize(pelletList[i].size); p.addScore(1); Console.WriteLine("Size: " + p.size); int oldPelletID = pelletList[i].id; spawnPellet(i); Player[] lockedPlayerList; lock (playerList) { lockedPlayerList = new Player[playerList.Count]; playerList.CopyTo(lockedPlayerList); } foreach (Player player in lockedPlayerList) { player.sendCommand(Command.eatPelletCommand(0, p, oldPelletID, pelletList[i])); } break; } } }
public void addPlayer(Player p) { lock (playerList) { playerList.Add(p); } sendPlayerInfo(p); }
public static Command eatPlayerCommand(long ts, Player p1, Player p2) { Command newCommand = new Command(); newCommand.timeStamp = ts; newCommand.cType = CType.EatPlayer; newCommand.message = newCommand.cType + ":" + ts + ":" + p1.playerNumber + ":" + p1.size + ":" + p2.playerNumber + ":" + p2.size + ":" + p2.x + ":" + p2.y + ":" + p2.dir + ";"; return newCommand; }
public static Command eatPelletCommand(long ts, Player p1, int oldPelletID, Pellet newPellet) { Command newCommand = new Command(); newCommand.timeStamp = ts; newCommand.cType = CType.EatPellet; newCommand.message = newCommand.cType + ":" + ts + ":" + p1.playerNumber + ":" + p1.size + ":" + oldPelletID + ":" + newPellet.id + ":" + newPellet.x + ":" + newPellet.y + ":" + newPellet.size + ";"; return newCommand; }
public static Command deathCommand(long ts, Player p1) { Command newCommand = new Command(); newCommand.timeStamp = ts; newCommand.cType = CType.Death; newCommand.message = newCommand.cType + ":" + ts + ":" + p1.playerNumber + ":" + p1.size + ":" + p1.x + ":" + p1.y + ":" + p1.dir + ";"; return newCommand; }
public void death(Player p) { p.isDead = true; p.resetPosition(); Player[] lockedPlayerList; lock (playerList) { lockedPlayerList = new Player[playerList.Count]; playerList.CopyTo(lockedPlayerList); } foreach (Player player in lockedPlayerList) { player.sendCommand(Command.deathCommand(0, p)); } //p.isDead = false; }
public static Command startGameInProgressCommand(long ts, Player[] playerList, Pellet[] pelletList) { Command newCommand = new Command(); newCommand.timeStamp = ts; newCommand.cType = CType.StartGame; newCommand.message = newCommand.cType + ":" + 1 + ":" + playerList.Length; for (int i = 0; i < playerList.Length; ++i ) { newCommand.message += ":" + playerList[i].playerNumber + ":" + playerList[i].x + ":" + playerList[i].y + ":" + playerList[i].dir + ":" + playerList[i].size; } newCommand.message += ":" + pelletList.Length; for (int i = 0; i < pelletList.Length; ++i) { newCommand.message += ":" + pelletList[i].id + ":" + pelletList[i].x + ":" + pelletList[i].y + ":" + pelletList[i].size; } newCommand.message += ";"; return newCommand; }
public static Command startGameCommand(long ts, Player p1, Player p2, float x1, float y1, float x2, float y2, int d1, int d2, Pellet[] pelletList) { Command newCommand = new Command(); newCommand.timeStamp = ts; newCommand.cType = CType.StartGame; newCommand.message = newCommand.cType + ":" + 0 + ":" + p1.playerNumber + ":" + x1 + ":" + y1 + ":" + d1 + ":" + p2.playerNumber + ":" + x2 + ":" + y2 + ":" + d2; for (int i = 0; i < pelletList.Length; ++i) { newCommand.message += ":" + pelletList[i].id + ":" + pelletList[i].x + ":" + pelletList[i].y + ":" + pelletList[i].size; } newCommand.message += ";"; return newCommand; }
public static Command playerPositionCommand(long ts, Player p1, float x1, float y1, int d1) { Command newCommand = new Command(); newCommand.timeStamp = ts; newCommand.cType = CType.PlayerPosition; newCommand.message = newCommand.cType + ":" + ts + ":" + p1.playerNumber + ":" + x1 + ":" + y1 + ":" + d1 + ";"; return newCommand; }
public void startGame() { for (int i = 0; i < pelletList.Length; ++i) { spawnPellet(i); } Player[] lockedPlayerList; lock (playerList) { lockedPlayerList = new Player[playerList.Count]; playerList.CopyTo(lockedPlayerList); } int dir = 1; foreach (Player player in lockedPlayerList) { if (dir > 3) break; player.setPosition(dir, (dir==1?PLAYER_ONE_START_POSITION_X:PLAYER_TWO_START_POSITION_X),(dir==1?PLAYER_ONE_START_POSITION_Y:PLAYER_TWO_START_POSITION_Y)); player.sendCommand(Command.startGameCommand(0, lockedPlayerList[0], lockedPlayerList[1], PLAYER_ONE_START_POSITION_X, PLAYER_ONE_START_POSITION_Y, PLAYER_TWO_START_POSITION_X, PLAYER_TWO_START_POSITION_Y, 3, 1, pelletList)); dir += 2; } gameStarted = true; }
void Update(object source, ElapsedEventArgs e) { Player[] lockedPlayerList; lock (playerList) { lockedPlayerList = new Player[playerList.Count]; playerList.CopyTo(lockedPlayerList); } foreach (Player player in lockedPlayerList) { //grab the readqueue then unlock the readqueue. lock (player.readQueueGameState) { Queue<Command> prq = player.readQueueGameState; while (prq.Count != 0) { Command cmd = prq.Dequeue(); switch (cmd.cType) { case CType.PlayerPosition: playerPositionResponse(player, cmd.x, cmd.y, cmd.dir); break; case CType.SizeUpdate: break; case CType.EatPellet: break; case CType.EatPlayer: break; case CType.Death: break; default: break; } } } } for (int i = 0; i < lockedPlayerList.Length; ++i) { for (int n = i + 1; n < lockedPlayerList.Length; ++n) { Player p1 = lockedPlayerList[i]; Player p2 = lockedPlayerList[n]; if (!p1.isDead && !p2.isDead) { Rectangle r = Rectangle.Intersect(p1.playerRect, p2.playerRect); if (!r.IsEmpty) { if (p1.size > p2.size) { eatPlayer(p1, p2); } else if (p1.size < p2.size) { eatPlayer(p2, p1); } else { death(p1); death(p2); } } } } } }
public void sendPlayerInfoOnLeave(Player player) { Player[] lockedPlayerList; lock (playerList) { lockedPlayerList = new Player[playerList.Count]; playerList.CopyTo(lockedPlayerList); } for (int i = 0; i < lockedPlayerList.Length; i++) { lockedPlayerList[i].sendCommand(Command.leftPlayerCommand(0, player.playerName, player.playerNumber)); } }
public void sendPlayerInfo(Player player) { Player[] lockedPlayerList; lock (playerList) { lockedPlayerList = new Player[playerList.Count]; playerList.CopyTo(lockedPlayerList); } for (int i = 0; i < lockedPlayerList.Length; i++) { lockedPlayerList[i].sendCommand(Command.newPlayerCommand(0, player.playerName, player.playerNumber, player.score)); if (player != lockedPlayerList[i]) { player.sendCommand(Command.newPlayerCommand(0, ((Player)lockedPlayerList[i]).playerName, ((Player)lockedPlayerList[i]).playerNumber, ((Player)lockedPlayerList[i]).score)); } } }
public void removePlayer(Player p) { lock (playerList) { playerList.Remove(p); } sendPlayerInfoOnLeave(p); }
public void playerPositionResponse(Player p, float x, float y, int dir) { p.setPosition(dir, x, y); Player[] lockedPlayerList; lock (playerList) { lockedPlayerList = new Player[playerList.Count]; playerList.CopyTo(lockedPlayerList); } foreach(Player player in lockedPlayerList) { if(player.playerNumber != p.playerNumber) { player.sendCommand(Command.playerPositionCommand(0, p, x, y, dir)); } } }
public void eatPlayer(Player p1, Player p2) { p2.isDead = true; p1.increaseSize(p2.size); p1.addScore(10); p2.resetPosition(); Player[] lockedPlayerList; lock (playerList) { lockedPlayerList = new Player[playerList.Count]; playerList.CopyTo(lockedPlayerList); } foreach (Player player in lockedPlayerList) { player.sendCommand(Command.eatPlayerCommand(0, p1, p2)); } //p2.isDead = false; }