示例#1
0
                /// <summary>
                /// This constructor is to create a new value and give it's type and value
                /// </summary>
                /// <param name="type">The type of the value</param>
                /// <param name="value">The value of the value</param>
                public Value(Type type, object value)
                {
                    Object    = value;
                    this.type = type;
                    switch (type)
                    {
                    case Type.Boolean: {
                        if (value is bool b)
                        {
                            BooleanValue = b;
                        }
                        else
                        {
                            throw new ArgumentException("Entered non-boolean value for a boolean type value", nameof(value));
                        }
                        break;
                    }

                    case Type.Integer: {
                        if (int.TryParse(value.ToString(), out int ignoredInt))
                        {
                            IntegerValue = value.ToString();
                        }
                        else
                        {
                            throw new ArgumentException("Entered non-integer value for a integer type value", nameof(value));
                        }
                        break;
                    }

                    case Type.Double: {
                        if (double.TryParse(value.ToString(), out double d))
                        {
                            DoubleValue = d;
                        }
                        else
                        {
                            throw new ArgumentException("Entered non-double value for a double type value", nameof(value));
                        }
                        break;
                    }

                    case Type.Timestamp: {
                        if (value is DateTime date)
                        {
                            TimestampValue = new Timestamp(date).ToString();
                        }
                        else
                        {
                            throw new ArgumentException("Entered non-timestamp value for a timestamp type value", nameof(value));
                        }
                        break;
                    }

                    case Type.String: {
                        StringValue = value.ToString();
                        break;
                    }

                    case Type.Bytes: {
                        try {
                            BytesValue = Convert.ToBase64String(Encoding.Unicode.GetBytes(value.ToString()));
                        } catch {
                            throw new ArgumentException("Entered non-bytes value for a bytes type value", nameof(value));
                        }
                        break;
                    }

                    case Type.Reference: {
                        ReferenceValue = value.ToString();
                        break;
                    }

                    case Type.GeoPoint: {
                        if (value is LatLon latlon)
                        {
                            GeoPointValue = latlon;
                        }
                        else
                        {
                            throw new ArgumentException("Entered non-geo-point value for a geo-point type value", nameof(value));
                        }
                        break;
                    }

                    case Type.Array: {
                        if (value is List <Value> array)
                        {
                            ArrayValue = new ArrayValueType(array);
                        }
                        else
                        {
                            throw new ArgumentException("Entered non-array value for an array type value", nameof(value));
                        }
                        break;
                    }

                    case Type.Map: {
                        if (value is Dictionary <string, Value> map)
                        {
                            MapValue = new MapValueType(map);
                        }
                        else
                        {
                            throw new ArgumentException("Entered non-map value for a map type value", nameof(value));
                        }
                        break;
                    }
                    }
                }
        public float GetValue(int x, int y, MapValueType mapValueType)
        {
            if( !IsValid(x,y))
                return 0f;

            switch (mapValueType)
            {
                case MapValueType.CityFoundValue:
                    float sum = 0f;

                    HexPoint center = new HexPoint(x,y);
                    foreach( HexPoint pt in center.Neighbors )
                        if( IsValid(pt) )
                            sum += this[pt].Food;

                    return this[x, y].Food + sum / 2;
            }

            return 0f;
        }