selamat malam kakak-kakak senior . saya yang masih newbie ini. mau mencoba game online multiplayer dari buku yang ane baca judulnya " jago bikin game online ". ane mau bikin modifikasi game asteroid duel.
ane coba utak atik codingnya tapi ada kegagalan yang ane dapatkan...
ane mau bikin karakter tiap pemain itu berbeda yakni dalam hal ini yaitu asteroidnya.
karena game online jadi ada client side sama server side...
client side mengunakan action script 3 dan server side mengunakan bhs program C#
ini kode Server sidenya mengunakan C#.
ini screenshotnyaCode:using System; using System.Collections.Generic; using System.Text; using System.Collections; using PlayerIO.GameLibrary; using System.Drawing; using System.Timers; namespace MyGame { public class Player : BasePlayer { public string Name; public int posX; public int posY; public int rotation; public int hit; } [RoomType("asteroidduel")] public class GameCode : Game<Player> { private Player[] playerArr; public override void GameStarted() { Console.WriteLine("Game is started: " + RoomId); playerArr = new Player[5]; } public override void GameClosed() { Console.WriteLine("Game closed: " + RoomId); } public override void UserJoined(Player player) { Console.WriteLine("UserJoined: " + player.ConnectUserId); JoinGame(player); Broadcast("ChatJoin", player.ConnectUserId); } public override void UserLeft(Player player) { Broadcast("ChatLeft", player.ConnectUserId); for (int i = 0; i < playerArr.Length; i++) { if (playerArr[i] != null) { if (playerArr[i].ConnectUserId == player.ConnectUserId) { playerArr[i] = null; break; } } } Broadcast("left", player.ConnectUserId); } public override void GotMessage(Player player, Message message) { for (int i = 0; i < playerArr.Length; i++) { if (playerArr[i] != null) { if (playerArr[i].ConnectUserId == player.ConnectUserId) { player = playerArr[i]; } } } Console.WriteLine("GotMessage: " + message.Type); switch (message.Type) { case "Chat": { String messageText = message.GetString(0); Broadcast("Chat", player.ConnectUserId, messageText); break; } case "RotateLeft": { player.rotation -= 5; if (player.rotation <= 0) player.rotation = 360; Broadcast("UpdateRocket", player.ConnectUserId, player.posX, player.posY, player.rotation); break; } case "MoveUp": { player.posY += (int)(Math.Sin((player.rotation - 90) * (Math.PI / 180)) * 10); player.posX += (int)(Math.Cos((player.rotation - 90) * (Math.PI / 180)) * 10); if (player.posX < 32 || player.posX > 365 || player.posY < 42 || player.posY > 290) { player.posY -= (int)(Math.Sin((player.rotation - 90) * (Math.PI / 180)) * 10); player.posX -= (int)(Math.Cos((player.rotation - 90) * (Math.PI / 180)) * 10); } else { Broadcast("UpdateRocket", player.ConnectUserId, player.posX, player.posY, player.rotation); } break; } case "RotateRight": { player.rotation += 5; if (player.rotation >= 360) player.rotation = 0; Broadcast("UpdateRocket", player.ConnectUserId, player.posX, player.posY, player.rotation); break; } case "MoveDown": { player.posY -= (int)(Math.Sin((player.rotation - 90) * (Math.PI / 180)) * 10); player.posX -= (int)(Math.Cos((player.rotation - 90) * (Math.PI / 180)) * 10); if (player.posX < 32 || player.posX > 365 || player.posY < 42 || player.posY > 290) { player.posY += (int)(Math.Sin((player.rotation - 90) * (Math.PI / 180)) * 10); player.posX += (int)(Math.Cos((player.rotation - 90) * (Math.PI / 180)) * 10); } else { Broadcast("UpdateRocket", player.ConnectUserId, player.posX, player.posY, player.rotation); } break; } case "Shoot": { Broadcast("Shoot", player.ConnectUserId); break; } case "HitRocket": { Console.WriteLine("HitRocket: " + player.ConnectUserId); player.hit++; Broadcast("HitRocket", message.GetString(0), message.GetInt(1)); if (player.hit >= 5) { EndGame(); } break; } } } private void EndGame() { String result = ""; for (int i = 0; i < playerArr.Length; i++) { if (playerArr[i] != null) { for (int j = i; j < playerArr.Length; j++) { if (playerArr[j] != null) { if (playerArr[j].hit < playerArr[i].hit) { Player tempPlayer = playerArr[i]; playerArr[i] = playerArr[j]; playerArr[j] = tempPlayer; } } } } } for (int i = 0; i < playerArr.Length; i++) { if (playerArr[i] != null) { result += (i + 1) + ". " + playerArr[i].ConnectUserId + ", " + playerArr[i].hit + "x hit\n"; } } Console.WriteLine("EndGame: " + result); Broadcast("EndGame", result); } private void JoinGame(Player user) { Random rand = new Random(); int posX = rand.Next(350) + 30; int posY = rand.Next(250) + 40; user.posX = posX; user.posY = posY; user.rotation = 0; user.hit = 0; for (int i = 0; i < playerArr.Length; i++) { if (playerArr[i] == null) { playerArr.SetValue(user, i); Console.WriteLine("user " + user.Id); Broadcast("join", user.ConnectUserId, posX, posY); break; } } for (int i = 0; i < playerArr.Length; i++) { if (playerArr[i] != null && playerArr[i] != user) { Console.WriteLine("user " + user.Id); Broadcast("addRocket", playerArr[i].ConnectUserId, playerArr[i].posX, playerArr[i].posY, playerArr[i].rotation); break; } } } } }
yang ane tanyakan di dalam server side ini bagaimana supaya karakternya bisa berbeda . pada tampilan screenshot diatas. pemain 1 dan pemain 2 tetap sama karakternya. sedangkan di pemain kedua berbeda.. padahal dalam 1 permainan
mohon bantuannya para kakak-kakak master semua. ane masih newbie....



LinkBack URL
About LinkBacks


Reply With Quote


Bookmarks