Пример #1
0
 /// <summary>
 /// Initializes a resource using tile as position
 /// </summary>
 /// <param id="entry">Entry defining the type of this game object</param>
 /// <param id="game">Game this game object is in</param>
 /// <param id="player">Owner of this game object</param>
 /// <param id="mapTiles">Tile where this game object is placed</param>
 public override void Initialize(EntryDb entry, WildmenGame game, Player player, Tile mapTile)
 {
   AssignUID(game);
   this.entry = (ResourceDb)entry;
   this.Amount = this.entry.InitialAmount;
   this.game = game;
   this.Owner = player;
   player.Assign(this);
   this.NearestTile = mapTile;
   this.MapPosition = mapTile.MapPosition;
   this.RecalculatePosition = true;
 }
Пример #2
0
    /// <summary>
    /// Method called when the screen is switched to; creates MapUI and assigns controlling player
    /// </summary>
    /// <param id="screenParams">Parameters passed from the last screen, WildmenGame, NetworkServer, NetworkClient and Player in this order</param>
    public override void OnActivate(object[] screenParams)
    {
      game = screenParams[0] as WildmenGame;
      server = screenParams[1] as NetworkServer;
      client = screenParams[2] as NetworkClient;
      player = screenParams[3] as Player;

      serverView = player == null;

      currentClientGameState = GameState.Started;

      MakeMapUI(game);

      mapUI.AssignControllingPlayer(player);

      messageQueue.Clear();

      if (serverView)
      {
        sm.CallSubscreen("escapemenu", client, server, player);
      }
    }
Пример #3
0
 public Player(int id, WildmenGame game, string name)
 {
   State = PlayerState.Playing;
   this.game = game;
   this.GameObjectListModified = true;
   Name = name;
   this.Id = id;
   if (id < 0)
   {
     PlayerColor = Color.DarkGray;
   }
   else
   {
     PlayerColor = colors[id % 4];
   }
   Units = new List<Unit>();
   Buildings = new List<Building>();
   Resources = new List<Resource>();
   ResourceStockpiles = new Dictionary<ResourceDb, int>();
   foreach (var item in Db.Instance.Resources.Values)
   {
     ResourceStockpiles.Add(item, 0);
   }
 }
Пример #4
0
 /// <summary>
 /// Initializes a game object using tile as position
 /// </summary>
 /// <param id="entry">Entry defining the type of this game object</param>
 /// <param id="game">Game this game object is in</param>
 /// <param id="player">Owner of this game object</param>
 /// <param id="mapTiles">Tile where this game object is placed</param>
 public abstract void Initialize(EntryDb entry, WildmenGame game, Player player, Tile mapTiles);
Пример #5
0
    /// <summary>
    /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
    /// </summary>
    /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
    /// <param id="line">String array containing the serialized data</param>
    /// <param id="position">Current position in the array</param>
    /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
    public virtual void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
    {
      if ((GameEntityType)(int.Parse(line[position++])) != GetEntityType) throw new Exception();
      int tmp; int tmp2;
      RecalculatePosition = true;
      // ObjectUID
      long oid = long.Parse(line[position++]);
      if (mt == MessageType.GameTransfer || mt == MessageType.GameObjCreate)
      {
        ObjectUID = oid;
      }
      else
      {
        if (ObjectUID != oid)
        {
          throw new Exception();
        }
      }

      switch (mt)
      {
        case MessageType.GameTransfer:
        case MessageType.GameObjUpdate:
        case MessageType.GameObjCreate:
          // Only server can update these values
          if (!context.IsServer)
          {
            // Health
            Health = int.Parse(line[position++]);
            // Owner ID
            int pId = int.Parse(line[position++]);
            if (Owner != null && Owner.Id != pId && mt == MessageType.GameObjUpdate)
            {
              Owner.Unassign(this);
              if (pId == -1) Owner = context.NaturePlayer;
              else Owner = context.Players.First(p => p.Id == pId);
              Owner.Assign(this);
            }
            else if (mt == MessageType.GameTransfer || mt == MessageType.GameObjCreate)
            {
              if (pId == -1) Owner = context.NaturePlayer;
              else Owner = context.Players.First(p => p.Id == pId);
              Owner.Assign(this);
            }

            // MapPosition
            MapPosition = new Vector2(float.Parse(line[position++], CultureInfo.InvariantCulture), float.Parse(line[position++], CultureInfo.InvariantCulture));
            // NearestTile
            tmp = int.Parse(line[position++]); tmp2 = int.Parse(line[position++]);
            if (tmp != -1 && tmp2 != -1)
            {
              if (NearestTile != null) { NearestTile.Unassign(this); }
              NearestTile = context.Map.Tiles[tmp, tmp2];
              NearestTile.Assign(this);
            }
            else
            {
              if (NearestTile != null) { NearestTile.Unassign(this); }
              NearestTile = null;
            }
            // Is movable
            IsMovable = (int.Parse(line[position++]) == 1 ? true : false);
            // Action cooldown
            actionCooldown = int.Parse(line[position++]);
          }

          // State
          State = (GameObjectState)int.Parse(line[position++]);
          // Order
          Order = (GameObjectOrder)int.Parse(line[position++]);
          // Order Position
          OrderPosition = new Vector2(float.Parse(line[position++], CultureInfo.InvariantCulture), float.Parse(line[position++], CultureInfo.InvariantCulture));
          // Order Target
          OrderTargetUR = long.Parse(line[position++]);
          // Order Range
          OrderRange = float.Parse(line[position++], CultureInfo.InvariantCulture);
          // Order Timeout
          OrderTimeout = int.Parse(line[position++]);
          // Order Entry
          string orderEntryName = line[position++];
          if (orderEntryName != Db.NOENTRY)
          {
            EffectDb eDb;
            UnitDb uDb;
            BuildingDb bDb;

            if (Db.Instance.Effects.TryGetValue(orderEntryName, out eDb)) { OrderEntry = eDb; }
            else if (Db.Instance.Units.TryGetValue(orderEntryName, out uDb)) { OrderEntry = uDb; }
            else if (Db.Instance.Buildings.TryGetValue(orderEntryName, out bDb)) { OrderEntry = bDb; }
            else throw new Exception("Entry doesn't exist.");
          }
          else
          {
            OrderEntry = null;
          }

          // Resolve
          if (mt != MessageType.GameTransfer)
          {
            ResolveReferences(context.GetGameObjectById);
          }
          break;
        case MessageType.GameObjKill:
          Owner.Unassign(this);
          this.Dispose();
          break;
        case MessageType.GameObjEntryUpdate:
          break;
        default:
          throw new Exception();
      }
    }
Пример #6
0
 /// <summary>
 /// Creates a new instance of the Unit class using the serialized data
 /// </summary>
 /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 /// <returns>Unit created using the provided serialized data</returns>
 public static Unit Create(MessageType mt, string[] line, ref int position, WildmenGame context)
 {
   Unit u = new Unit();
   u.Deserialize(mt, line, ref position, context);
   u.game = context;
   u.InitializeGraphics();
   return u;
 }
Пример #7
0
 /// <summary>
 /// Initializes this unit
 /// </summary>
 /// <param id="entry">Entry defining the type of this game object</param>
 /// <param id="game">Game this game object is in</param>
 /// <param id="player">Owner of this game object</param>
 /// <param id="mapPosition">Vector where this game object is placed</param>
 public override void Initialize(EntryDb entry, WildmenGame game, Player player, Vector2 mapPosition)
 {
   AssignUID(game);
   Logger.Log(string.Format("Unit #{0} initialized", this.ObjectUID), "Server update");
   player.Assign(this);
   this.State = GameObjectState.Idle;
   this.Order = GameObjectOrder.Idle;
   this.game = game;
   this.Owner = player;
   this.entry = ((UnitDb)entry).Clone();
   this.MapPosition = mapPosition;
   this.RecalculatePosition = true;
   this.IsMovable = true;
   this.Health = this.entry.Health;
 }
Пример #8
0
    /// <summary>
    /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
    /// </summary>
    /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
    /// <param id="line">String array containing the serialized data</param>
    /// <param id="position">Current position in the array</param>
    /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
    public void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
    {
      Name = line[position++];

      Valid = true;
    }
Пример #9
0
 /// <summary>
 /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
 /// </summary>
 /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 public override void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
 {
   base.Deserialize(mt, line, ref position, context);
   switch (mt)
   {
     case MessageType.GameTransfer:
     case MessageType.GameEffectCreate:
       anim.Deserialize(mt, line, ref position, context);
       this.position = new Vector2(float.Parse(line[position++], CultureInfo.InvariantCulture), float.Parse(line[position++], CultureInfo.InvariantCulture));
       break;
     default:
       throw new Exception();
   }
 }
Пример #10
0
    /// <summary>
    /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
    /// </summary>
    /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
    /// <param id="line">String array containing the serialized data</param>
    /// <param id="position">Current position in the array</param>
    /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
    public override void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
    {
      if ((GameEntityType)(int.Parse(line[position++])) != GetEntityType) throw new Exception();

      switch (mt)
      {
        case MessageType.GameTransfer:
        case MessageType.GameObjCreate:
        case MessageType.GameObjUpdate:
          long oid = long.Parse(line[position++]);
          if (mt == MessageType.GameTransfer || mt == MessageType.GameObjCreate)
          {
            ObjectUID = oid;
          }
          else
          {
            if (ObjectUID != oid)
            {
              throw new Exception();
            }
          }
          if (mt == MessageType.GameTransfer || mt == MessageType.GameObjCreate)
          {
            int pId = int.Parse(line[position++]);
            if (pId == -1) Owner = context.NaturePlayer;
            else Owner = context.Players.First(p => p.Id == pId);
            Owner.Assign(this);
          }
          MapPosition = new Vector2(float.Parse(line[position++], CultureInfo.InvariantCulture), float.Parse(line[position++], CultureInfo.InvariantCulture));

          if (NearestTile != null) { NearestTile.Unassign(this); }
          NearestTile = context.Map.Tiles[int.Parse(line[position++]), int.Parse(line[position++])];
          NearestTile.Assign(this);
          entry = Db.Instance.Resources[line[position++]];
          Amount = int.Parse(line[position++]);
          Cooldown = int.Parse(line[position++]);
          break;
        case MessageType.GameObjKill:
          break;
        case MessageType.GameObjEntryUpdate:
          entry = Db.Instance.Resources[line[position++]];
          break;
        default:
          throw new Exception("Resource deserialization error");
      }
    }
Пример #11
0
    //

    /// <summary>
    /// Disposes resources used by this instance
    /// </summary>
    public virtual void Dispose()
    {
      Entry = null;
      game = null;
      LocalData = null;
      targetTile = null;
      targetEntity = null;
    }
Пример #12
0
    // GAME-RELATED METHODS

    /// <summary>
    /// Creates a new instance of a game, sends it to all clients and start the game
    /// </summary>
    public void StartGame()
    {
      Accepting = false;
      SendToAll(Network.MakeServerMessage(MessageType.GameState, GameState.Starting));
      CurrentGameState = GameState.Starting;

      clientMessages = new Queue<string[]>();

      lock (ClientConnections)
      {

        Game = new WildmenGame();
        Game.IsServer = true;
        Game.MakeMap(MapWidth, MapHeight, ClientConnections.Count(), UnitCap);

        if (!Game.Map.Valid)
        {
          SendMessage("Unable to create map with given parameters.");
          SendToAll(Network.MakeServerMessage(MessageType.GameState, GameState.Lobby));
          CurrentGameState = GameState.Lobby;
          Accepting = true;
          return;
        }

        Game.Map.LoadContent();
        Game.SendData = SendToAll;
        Game.ReceiveData = GetClientMessage;

        for (int i = 0; i < ClientConnections.Count(); i++)
        {
          ClientConnection c = ClientConnections[i];
          c.IngamePlayerId = i;
          Player p = new Player(i, Game, c.Name);
          p.Initialize();
          Game.PlacePlayer(i, p);
        }

        string savegame = Game.SaveGame().TrimStart(':');

        for (int i = 0; i < ClientConnections.Count(); i++)
        {
          ClientConnection c = ClientConnections[i];
          Player p = Game.Players[i];

          c.StartSend(Network.MakeServerMessage(MessageType.GameTransfer, savegame));
          c.StartSend(Network.MakeServerMessage(MessageType.PlayerAssign, i));
        }

        SendToAll(Network.MakeServerMessage(MessageType.GameState, GameState.Started));
        CurrentGameState = GameState.Started;
      }
      SendMessage("Game started.");
    }
Пример #13
0
 /// <summary>
 /// Creates a new instance of the Resource class using the serialized data
 /// </summary>
 /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 /// <returns>Resource created using the provided serialized data</returns>
 public static Resource Create(MessageType mt, string[] line, ref int position, WildmenGame context)
 {
   Resource r = new Resource();
   r.Deserialize(mt, line, ref position, context);
   r.NearestTile.Assign(r);
   r.game = context;
   r.InitializeGraphics();
   return r;
 }
Пример #14
0
 /// <summary>
 /// Handles messages received from the server
 /// </summary>
 private void HandleClientMessages()
 {
   string[] message;
   while ((message = client.GetServerMessage()) != null)
   {
     int position = 1;
     MessageType mt = Network.GetMessageType(message);
     switch (mt)
     {
       case MessageType.GameState:
         GameState oldState = currentClientGameState;
         GameState newState = currentClientGameState = (GameState)int.Parse(message[position++]);
         //Network.AddMessage("Changing game state from " + oldState + " to " + newState);
         switch (newState)
         {
           case GameState.INVALID:
             break;
           case GameState.NotConnected:
           case GameState.Lobby:
           case GameState.Starting:
             game = null;
             player = null;
             break;
           case GameState.Started:
             SwitchToGameScreen(game);
             return;
           default:
             break;
         }
         break;
       case MessageType.SettingsUpdate:
         width = int.Parse(message[position++]);
         height = int.Parse(message[position++]);
         unitCap = int.Parse(message[position++]);
         break;
       case MessageType.GameTransfer:
         game = new WildmenGame();
         game.Deserialize(mt, message, ref position, game);
         game.SendData = client.StartSend;
         game.ReceiveData = client.GetServerMessage;
         break;
       case MessageType.PlayerAssign:
         Debug.Assert(game != null);
         int playerId = int.Parse(message[position++]);
         player = game.Players.First(p => p.Id == playerId);
         break;
       default:
         throw new Exception("Invalid message type");
     }
   }
 }
Пример #15
0
    /// <summary>
    /// Switches to the game screen
    /// </summary>
    /// <param id="targetGame">Game to use</param>
    private void SwitchToGameScreen(WildmenGame targetGame)
    {
      // Copy values to local variables
      var game2 = targetGame;
      var server2 = server;
      var client2 = client;
      var player2 = player;

      // Detach all references from current screen
      //    (switching screen calls OnDeactivate that would dispose these variables)
      game = null;
      server = null;
      client = null;
      player = null;

      // Switch active screen
      sm.SwitchScreen("game", game2, server2, client2, player2);
    }
Пример #16
0
 /// <summary>
 /// Method called when the screen is switched from
 /// </summary>
 public override void OnDeactivate()
 {
   if (server != null)
   {
     StopServer(null);
     server.Dispose();
     server = null;
   }
   if (client != null)
   {
     Disconnect(null);
     client.Dispose();
     client = null;
   }
   if (game != null)
   {
     game.Dispose();
     game = null;
     player = null;
   }
 }
Пример #17
0
 /// <summary>
 /// Initializes a game object using vector as position
 /// </summary>
 /// <param id="entry">Entry defining the type of this game object</param>
 /// <param id="game">Game this game object is in</param>
 /// <param id="player">Owner of this game object</param>
 /// <param id="mapPosition">Vector where this game object is placed</param>
 public abstract void Initialize(EntryDb entry, WildmenGame game, Player player, Vector2 mapPosition);
Пример #18
0
    /// <summary>
    /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
    /// </summary>
    /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
    /// <param id="line">String array containing the serialized data</param>
    /// <param id="position">Current position in the array</param>
    /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
    public virtual void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
    {
      string entry;
      switch (mt)
      {
        case MessageType.GameTransfer:
        case MessageType.GameEffectCreate:
          entry = line[position++];
          if (entry != Db.NOENTRY) { Entry = Db.Instance.Effects[entry]; }
          else { Entry = null; }
          Speed = int.Parse(line[position++]);
          Duration = int.Parse(line[position++]);
          Time = int.Parse(line[position++]);
          UpdateOffset = ((long.Parse(line[position++]) - UI.Instance.UpdateNumber)) % Speed;
          int tx = int.Parse(line[position++]);
          int ty = int.Parse(line[position++]);
          if (tx == -1 && ty == -1)
            Tile = null;
          else
            Tile = context.Map.Tiles[tx, ty];

          Graphical = line[position++] == "1";
          GraphicalOverlay = line[position++] == "1";

          string target = line[position++];
          if (target != "null") { targetEntity = context.GetGameObjectById(int.Parse(line[position++])); }
          target = line[position++];
          if (target != "null") { targetTile = context.Map.Tiles[int.Parse(target), int.Parse(line[position++])]; }

          int eLocalCount = int.Parse(line[position++]);
          LocalData = new List<object>();
          for (int i = 0; i < eLocalCount; i++)
          {
            target = line[position++];
            if (target == "g") { LocalData.Add(context.GetGameObjectById(int.Parse(line[position++]))); }
            else if (target == "t") { LocalData.Add(context.Map.Tiles[int.Parse(line[position++]), int.Parse(line[position++])]); }
            else { LocalData.Add(line[position++]); }
          }

          break;
        default:
          throw new Exception("GameEffect deserialization error");
      }
    }
Пример #19
0
    /// <summary>
    /// Initialize the GameEffect
    /// </summary>
    /// <param id="game">game this effect belongs to</param>
    /// <param id="tile"></param>
    public virtual void Initialize(WildmenGame game, Tile tile)
    {
      this.game = game;
      this.Tile = tile;

      if (Entry != null)
      {
        if (Entry.Spell.OnSpellStartGameEntity != null && Entry.Spell.Target == SpellEntry.TargetType.GameEntity)
        {
          Entry.Spell.OnSpellStartGameEntity(game, this, caster, targetEntity);
        }
        else if (Entry.Spell.OnSpellStartTile != null && Entry.Spell.Target == SpellEntry.TargetType.Tile)
        {
          Entry.Spell.OnSpellStartTile(game, this, caster, targetTile);
        }
      }

      targetEntity = null;
      targetTile = null;

      UpdateOffset = -UI.Instance.UpdateNumber % Speed;
      UpdateOffset++;

    }
Пример #20
0
    // INITIALIZE

    /// <summary>
    /// Constructor for the MapUI class
    /// </summary>
    /// <param id="game">game, the MapUI will overlook</param>
    public MapUI(WildmenGame game)
    {
      this.game = game;
      this.map = this.game.Map;
      this.inGameState = InGameStateEnum.Default;
    }
Пример #21
0
 /// <summary>
 /// Creates a new instance of the GameEffect class using the serialized data
 /// </summary>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 /// <returns>GameEffect created using the provided serialized data</returns>
 public static GameEffect Create(string[] line, ref int position, WildmenGame context)
 {
   GameEffect e = null;
   EffectTypes et = (EffectTypes)int.Parse(line[position++]);
   switch (et)
   {
     case EffectTypes.Scripted:
       e = new GameEffect();
       break;
     case EffectTypes.UnitDeath:
       e = new UnitDeathEffect();
       break;
     case EffectTypes.ScrollText:
       e = new ScrollTextEffect();
       break;
     case EffectTypes.Animation:
       e = new AnimationEffect();
       break;
     case EffectTypes.Spawner:
       e = new SpawnerEffect();
       break;
     default:
       throw new Exception("Unknown effect");
   }
   e.game = context;
   e.Deserialize(MessageType.GameEffectCreate, line, ref position, context);
   return e;
 }
Пример #22
0
 /// <summary>
 /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
 /// </summary>
 /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 public void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
 {
   throw new InvalidOperationException("This method is not supposed to be used.");
 }
Пример #23
0
 /// <summary>
 /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
 /// </summary>
 /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 public override void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
 {
   int id;
   base.Deserialize(mt, line, ref position, context);
   switch (mt)
   {
     case MessageType.GameTransfer:
     case MessageType.GameEffectCreate:
       id = int.Parse(line[position++]);
       owner = context.Players.First(p => p.Id == id);
       unitEntry = Db.Instance.Units[line[position++]];
       color = new Color(byte.Parse(line[position++]), byte.Parse(line[position++]), byte.Parse(line[position++]));
       this.position = new Vector2(float.Parse(line[position++], CultureInfo.InvariantCulture), float.Parse(line[position++], CultureInfo.InvariantCulture));
       tile = context.Map.Tiles[int.Parse(line[position++]), int.Parse(line[position++])];
       break;
     default:
       throw new Exception();
   }
 }
Пример #24
0
    /// <summary>
    /// Initializes this instance and claims the tiles
    /// </summary>
    /// <param name="entry">Type of this building</param>
    /// <param name="game">The game this building is in</param>
    /// <param name="player">The owner of this building</param>
    /// <param name="baseTile">The base tile determining the position of this building</param>
    public override void Initialize(EntryDb entry, WildmenGame game, Player player, Tile baseTile)
    {
      AssignUID(game);
      Logger.Log(string.Format("Building #{0} initialized", this.ObjectUID), "Server update");
      player.Assign(this);
      this.game = game;
      this.Owner = player;
      this.entry = (BuildingDb)entry;
      this.NearestTile = baseTile;
      ClaimTiles(baseTile);
      this.MapPosition = baseTile.MapPosition;
      this.RecalculatePosition = true;

      this.Health = this.entry.Health;
      this.Construction = 0;

      if (Construction >= this.entry.ConstructionAmount)
      {
        Completed(false);
      }
    }
Пример #25
0
 /// <summary>
 /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
 /// </summary>
 /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 public override void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
 {
   base.Deserialize(mt, line, ref position, context);
   switch (mt)
   {
     case MessageType.GameTransfer:
     case MessageType.GameEffectCreate:
       spawner = (Building)context.GetGameObjectById(int.Parse(line[position++]));
       ((Building)spawner).ResetSpawner(this);
       Spawnee = Db.Instance.Units[line[position++]];
       int playerId = int.Parse(line[position++]);
       owner = context.Players.First(u => u.Id == playerId);
       spawnPoint = new Vector2(float.Parse(line[position++], CultureInfo.InvariantCulture), float.Parse(line[position++], CultureInfo.InvariantCulture));
       break;
     default:
       throw new Exception();
   }
 }
Пример #26
0
 /// <summary>
 /// Creates a new instance of the Building class using the serialized data
 /// </summary>
 /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 /// <returns>Building created using the provided serialized data</returns>
 public static Building Create(MessageType mt, string[] line, ref int position, WildmenGame context)
 {
   Building b = new Building();
   b.Deserialize(mt, line, ref position, context);
   b.game = context;
   b.ClaimTiles(b.NearestTile);
   b.InitializeGraphics();
   return b;
 }
Пример #27
0
 /// <summary>
 /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
 /// </summary>
 /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 public override void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
 {
   base.Deserialize(mt, line, ref position, context);
   long lastGatheredResId;
   string carryTypeId;
   switch (mt)
   {
     case MessageType.GameTransfer:
     case MessageType.GameObjCreate:
       // Entry
       entry = Db.Instance.Units[line[position++]].Clone();
       entry.Deserialize(mt, line, ref position, context);
       // Last gathered resource
       lastGatheredResId = long.Parse(line[position++]);
       if (lastGatheredResId == -1) { lastGatheredResource = null; }
       else { lastGatheredResource = (Resource)game.GetGameObjectById(lastGatheredResId); }
       // Carrying resource entry
       carryTypeId = line[position++];
       if (carryTypeId == Db.NOENTRY) { carryType = null; }
       else { carryType = Db.Instance.Resources[carryTypeId]; }
       // Carrying resource amount
       carryAmount = int.Parse(line[position++]);
       break;
     case MessageType.GameObjUpdate:
       // Entry
       lastGatheredResId = long.Parse(line[position++]);
       // Last gathered resource
       if (lastGatheredResId == -1) { lastGatheredResource = null; }
       else { lastGatheredResource = (Resource)game.GetGameObjectById(lastGatheredResId); }
       // Carrying resource entry
       carryTypeId = line[position++];
       if (carryTypeId == Db.NOENTRY) { carryType = null; }
       else { carryType = Db.Instance.Resources[carryTypeId]; }
       // Carrying resource amount
       carryAmount = int.Parse(line[position++]);
       break;
     case MessageType.GameObjEntryUpdate:
       entry = Db.Instance.Units[line[position++]].Clone();
       entry.Deserialize(mt, line, ref position, context);
       break;
     case MessageType.GameObjKill:
       break;
     default:
       throw new Exception("Unit deserialization error");
   }
 }
Пример #28
0
 /// <summary>
 /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
 /// </summary>
 /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
 /// <param id="line">String array containing the serialized data</param>
 /// <param id="position">Current position in the array</param>
 /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
 public override void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
 {
   base.Deserialize(mt, line, ref position, context);
   switch (mt)
   {
     case MessageType.GameTransfer:
     case MessageType.GameObjCreate:
       entry = Db.Instance.Buildings[line[position++]];
       Construction = int.Parse(line[position++]);
       break;
     case MessageType.GameObjUpdate:
       Construction = int.Parse(line[position++]);
       break;
     case MessageType.GameObjEntryUpdate:
       entry = Db.Instance.Buildings[line[position++]];
       break;
     case MessageType.GameObjKill:
       break;
     default:
       throw new Exception("Building deserialization error");
   }
 }
Пример #29
0
    /// <summary>
    /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
    /// </summary>
    /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
    /// <param id="line">String array containing the serialized data</param>
    /// <param id="position">Current position in the array</param>
    /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
    public void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
    {
      int n;
      switch (mt)
      {
        case MessageType.GameTransfer:
          Map = new MapBoard(context, int.Parse(line[position + 0]), int.Parse(line[position + 1]));
          Map.Initialize();
          Map.Deserialize(mt, line, ref position, context);
          Map.LoadContent();

          int evCount = int.Parse(line[position++]);
          for (int i = 0; i < evCount; i++)
          {
            Effects.Add(GameEffect.Create(line, ref position, context));
          }


          UnitCap = int.Parse(line[position++]);
          n = int.Parse(line[position++]);
          for (int i = 0; i < n; i++)
          {
            var p = new Player(i, context, null);
            Players.Add(p);
            p.Initialize();
          }
          for (int i = 0; i < n; i++)
          {
            Players[i].Deserialize(mt, line, ref position, context);
          }
          NaturePlayer = new Player(-1, context, null);
          NaturePlayer.Initialize();
          NaturePlayer.Deserialize(mt, line, ref position, context);
          List<Player> tempPlayers = new List<Player>(Players);
          tempPlayers.Add(NaturePlayer);
          foreach (var player in tempPlayers)
          {
            foreach (var unit in player.Units)
            {
              unit.ResolveReferences(GetGameObjectById);
            }
            foreach (var bldg in player.Buildings)
            {
              bldg.ResolveReferences(GetGameObjectById);
            }
          }
          NextUID = int.Parse(line[position++]);
          break;
        default:
          throw new Exception("WildmenGame deserialization error");
      }
    }
Пример #30
0
    // INITIALIZATION

    /// <summary>
    /// Assigns unique ID to this game object
    /// </summary>
    /// <param id="game">game that stores UID counter</param>
    protected void AssignUID(WildmenGame game)
    {
      this.ObjectUID = game.NextUID++;
    }