Example #1
0
        public int FromBytes(byte[] bytes, int startIndex)
        {
            int count = 0;

            try
            {
                Grid      = new HexGrid <Hex>();
                count    += Grid.FromBytes(bytes, startIndex + count);
                StartPos  = new HexPoint();
                count    += StartPos.FromBytes(bytes, startIndex + count);
                MinMoves  = BitConverter.ToInt32(bytes, startIndex + count);
                count    += 4;
                Completed = (bytes[startIndex + count] & (1 << 0)) > 0;
                Perfect   = (bytes[startIndex + count] & (1 << 1)) > 0;
                count    += 1;

                int hintLength = 0;
                count += hintLength.FromBytes(bytes, startIndex + count);
                Hint   = Encoding.UTF8.GetString(bytes, startIndex + count, hintLength);
                count += hintLength;

                // Read hex data
                int dataCount = BitConverter.ToInt32(bytes, startIndex + count);
                count += 4;

                Data = new HexData[dataCount];

                for (int i = 0; i < dataCount; i++)
                {
                    HexData hexData = new HexData();
                    count  += hexData.FromBytes(bytes, startIndex + count);
                    Data[i] = hexData;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"[Level]: Error while loading: {e.Message}");
            }

            return(count);
        }
Example #2
0
 /// <summary>
 /// Creates a new hex with the specified position.
 /// </summary>
 /// <param name="pos">The position of the hex.</param>
 public Hex(HexPoint pos)
 {
     Position = pos;
     IDs      = new List <byte>();
 }
Example #3
0
        private void UpdateHex(Hex hex, int player, ref HexGrid <Hex> nextGrid)
        {
            foreach (HexData type in hex.GetTypes(Level.Data))
            {
                HexPoint position = hex.Position;

                foreach (HexAction action in type.Changes)
                {
                    bool     conditionMet = false;
                    bool     move         = true;
                    HexPoint nextPos      = position + new HexPoint(action.MoveX, action.MoveY);

                    switch (action.Condition)
                    {
                    case HexCondition.Move:
                        conditionMet = true;
                        break;

                    case HexCondition.PlayerEnter:
                        conditionMet = position == Players[player].Position;
                        break;

                    case HexCondition.PlayerLeave:
                        conditionMet = position == Players[player].LastPosition;
                        break;

                    case HexCondition.NextFlag:
                        conditionMet = Level.Grid[nextPos] == null || (Level.Grid[nextPos]?.GetFlags(Level.Data) & (HexFlags)action.Data) != 0;
                        move         = false;
                        break;

                    case HexCondition.NextNotFlag:
                        conditionMet = Level.Grid[nextPos] != null && (Level.Grid[nextPos]?.GetFlags(Level.Data) & (HexFlags)action.Data) == 0;
                        break;

                    case HexCondition.NextID:
                        conditionMet = Level.Grid[nextPos] == null || Level.Grid[nextPos]?.IDs.Contains(action.Data) == true;
                        move         = false;
                        break;

                    case HexCondition.NextNotID:
                        conditionMet = Level.Grid[nextPos] != null && Level.Grid[nextPos]?.IDs.Contains(action.Data) != true;
                        break;
                    }

                    if (conditionMet && nextGrid[position].Value.IDs.Contains(type.ID))
                    {
                        if (nextPos == position || !move)
                        {
                            Hex temp = nextGrid[position].Value;
                            temp.IDs.Remove(type.ID);
                            temp.IDs.Add(action.ChangeTo);
                            nextGrid[position] = temp;
                        }
                        else if (nextGrid[nextPos].HasValue && move)
                        {
                            Hex temp = nextGrid[position].Value;
                            temp.IDs.Remove(type.ID);

                            Hex next = nextGrid[nextPos].Value;
                            next.IDs.Add(action.ChangeTo);

                            nextGrid[position] = temp;
                            nextGrid[nextPos]  = next;

                            position = nextPos;
                        }
                    }
                }
            }
        }
Example #4
0
 /// <summary>
 /// Creates a new hex with the specified position.
 /// </summary>
 /// <param name="x">The x-coordinate of the position.</param>
 /// <param name="y">The y-coordinate of the position.</param>
 public Hex(int x, int y)
 {
     Position = new HexPoint(x, y);
     IDs      = new List <byte>();
 }