/// <summary> /// <c>Player</c> attack towards a direction. /// </summary> /// <param name="playerId">Id of attacking <c>Player</c>.</param> /// <param name="weaponId">weaponId of attacking <c>Player</c>.</param> /// <param name="clickPositionX">X position of the attack <c>Player</c>.</param> /// <param name="clickPositionY">Y position of the attack<c>Player</c>.</param> public void PlayerAttack(UInt32 playerId, short weaponId, float clickPositionX, float clickPositionY) { //Debug.Log("Start of PlayerAttack"); Vector2 clickPosition; clickPosition.x = clickPositionX; clickPosition.y = clickPositionY; Player player = (Player)GetEntity(playerId); AttackMessage AttackInit = new AttackMessage(playerId, 0, 0, 0, weaponId, 0, 0, clickPositionX, clickPositionY, 1, 0); Debug.Log("Broadcasting in PlayerATtack"); UDPServer.getInstance().BroadcastMessage(AttackInit); player.TryToAttack(clickPosition, weaponId); }
protected virtual void Dispose(bool disposing) { if (!this.disposed) { // If disposing equals true, dispose all managed and unmanaged resources. if (disposing) { GameState.instance.DestroyEntityID(this.PlayerID); EntityUpdateMessage message = new EntityUpdateMessage( EntityUpdateMessage.Type.PLAYER, EntityUpdateMessage.Action.DELETE, this.PlayerID, 0 ); UDPServer.getInstance().BroadcastMessage(message); } disposed = true; } }
// Start is called before the first frame update void Start() { Debug.Log("Starting server on port " + port + "..."); Debug.Log("Registering with lobbymanager... (" + lobbyManager + ")"); UnityWebRequest wr = UnityWebRequest.Post(lobbyManager + "/api/lobby/new/" + port, ""); wr.SendWebRequest(); while (!wr.isDone) { ; } try { this.lobby = JsonUtility.FromJson <LobbyResponse>(wr.downloadHandler.text); } catch (ArgumentException e) { Debug.LogError(wr.downloadHandler.text); Debug.LogError(e); } Debug.Log("Sever has ID " + this.lobby.id); // Start lobbymanager heartbeat InvokeRepeating("Heartbeat", 1f, 10f); Debug.Log("Loading game..."); //Runs the GameLoader script (Resources.Load("GameLoader") as GameObject).GetComponent <GameLoader>().Init(); this.TaskQueue = new ConcurrentQueue <Action>(); Server.instance = this; Debug.Log("Listening for new connections..."); UDPServer.getInstance().Init(this, port); Debug.Log("Loading complete"); }
/// <summary> /// Initializes Player, must be run on the main thread /// </summary> public void Init() { try { // Update gamestate of current client foreach (KeyValuePair <UInt32, Entity> kv in GameState.instance.Entities) { if (kv.Value.GetType() == typeof(Player)) // TODO: Create all entities, not just player { Player e = (Player)kv.Value; EntityUpdateMessage entity = new EntityUpdateMessage( EntityUpdateMessage.Type.PLAYER, EntityUpdateMessage.Action.CREATE, kv.Key, 0 ); MovementMessage move = new MovementMessage(kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY); this.SendMessage(entity); this.SendMessage(move); this.UpdateHP(kv); } if (kv.Value.GetType() == typeof(Enemy)) { Enemy e = (Enemy)kv.Value; EntityUpdateMessage entity = new EntityUpdateMessage( EntityUpdateMessage.Type.ENEMY, EntityUpdateMessage.Action.CREATE, kv.Key, 0 ); MovementMessage move = new MovementMessage(kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY); this.SendMessage(entity); this.SendMessage(move); this.UpdateHP(kv); } if (kv.Value.GetType() == typeof(Bow) || kv.Value.GetType() == typeof(Crossbow)) { EntityUpdateMessage entity = null; Weapon e = (Weapon)kv.Value; if (e.GetType() == typeof(Bow)) { entity = new EntityUpdateMessage( EntityUpdateMessage.Type.WEAPON_BOW, EntityUpdateMessage.Action.CREATE, kv.Key, 0 ); } else if (e.GetType() == typeof(Crossbow)) { entity = new EntityUpdateMessage( EntityUpdateMessage.Type.WEAPON_CROSSBOW, EntityUpdateMessage.Action.CREATE, kv.Key, 0 ); } else { throw new Exception("Weapon type not found"); } Debug.Log("Creating a weapon move message with x: " + e.X + " y: " + e.Y); MovementMessage move = new MovementMessage(kv.Key, 0, 0, e.X, e.Y, 0, e.DX, e.DY); this.SendMessage(entity); this.SendMessage(move); } } //this.Player = GameObject.Instantiate(Resources.Load("Player") as GameObject); this.PlayerID = GameState.instance.CreatePlayer(this, 50, 50); //Server.instance.Entities.TryAdd(this.Player.GetComponent<Player>().ID, this.Player.GetComponent<Player>()); Debug.Log("Entity id is " + this.PlayerID); EntityUpdateMessage NewClient = new EntityUpdateMessage( EntityUpdateMessage.Type.PLAYER, EntityUpdateMessage.Action.CREATE, this.PlayerID, 0 ); UDPServer.getInstance().BroadcastMessage(NewClient); MovementMessage ClientMovement = new MovementMessage(this.PlayerID, 0, 0, 0, 0, 0, 0, 0); UDPServer.getInstance().BroadcastMessage(ClientMovement); EntityUpdateMessage control = new EntityUpdateMessage( EntityUpdateMessage.Type.PLAYER, EntityUpdateMessage.Action.CONTROL, this.PlayerID, 0 ); this.SendMessage(control); } catch (Exception e) { Debug.Log(e); } }
private void OnDestroy() { UDPServer.getInstance().Stop(); }