Example #1
0
 public ViewSettings(Vector2i viewDistance)
 {
     ViewDistance = new Vector3i(viewDistance.X, viewDistance.Y, viewDistance.X);
     ViewDistanceCache = new Vector3i(viewDistance.X + 1, viewDistance.Y + 1, viewDistance.X + 1);
     Location = new Vector3i();
     PreviousLocation = new Vector3i(int.MinValue, int.MinValue, int.MinValue);
 }
Example #2
0
        private void HandleArea(Area area, Area areaMask, byte[] mask)
        {
            var location2i = new Vector2i(area.Info.Location.X, area.Info.Location.Z);

            random = new Random(location2i.GetHashCode());
            NoiseAreaType noiseAreaType;

            // Loop through all columns of the area
            for (int z = 0; z < Area.Size.Z; z++)
            {
                int currentZ = z * Area.Size.X;

                for (int x = 0; x < Area.Size.X; x++)
                {
                    // Skip unaffected columns
                    if (mask[currentZ + x] == 0)
                    {
                        continue;
                    }

                    // Get the heights of the two areas at the current column location
                    int areaHeight = areaTilesGenerators[area.Info.Type].GetHeight(location2i, x, z);
                    int maskAreaHeight = areaTilesGenerators[areaMask.Info.Type].GetHeight(location2i, x, z);
                    int newAreaHeight;

                    // For the outer edges of the mask affected by this threshold, clamp the area height to the areaMask height for a rounded/smoother visual effect
                    if (mask[currentZ + x] > AreaEdgeThreshold)
                    {
                        newAreaHeight = areaTilesGenerators[areaMask.Info.Type].GetHeight(location2i, x, z);
                        noiseAreaType = areaMask.Info.Type;
                    }
                    else
                    {
                        float maskPercentage = mask[currentZ + x] / (float)AreaEdgeThreshold;

                        newAreaHeight = Math.Max(0, areaHeight + (int)((maskAreaHeight - areaHeight) * maskPercentage));

                        // Find out if we should use the theme from the area or the areaMask for this tile column (weighted for a gradient effect by using the mask[])
                        noiseAreaType = random.Next(256) < mask[currentZ + x] ? areaMask.Info.Type : area.Info.Type;
                    }

                    // Lower the column
                    if (maskAreaHeight < areaHeight)
                    {
                        newAreaHeight = Math.Max(0, newAreaHeight - area.Info.TileHeight.Start);
                        areaTilesGenerators[noiseAreaType].TrimHeight(ref area, x, z, newAreaHeight);
                    }
                    // Raise the column
                    else if (maskAreaHeight > areaHeight)
                    {
                        newAreaHeight = Math.Min(Area.Size.Y - 1, newAreaHeight - areaMask.Info.TileHeight.Start);
                        areaTilesGenerators[noiseAreaType].ExpandHeight(ref area, x, z, newAreaHeight);
                    }
                }
            }
        }
Example #3
0
        public MouseHandler(GameContext context)
        {
            this.context = context;

            IsEnabled = Environment.OSVersion.Platform != PlatformID.Xbox;
            MouseState = new Dictionary<MouseInputType, InputState>();
            previousState = Mouse.GetState();
            Position = new Vector2i(previousState.X, previousState.Y);
            Sensitivity = 0.03f;

            // Map all button types of the gamepad per default
            foreach (int i in Enum.GetValues(typeof(MouseInputType)))
            {
                MouseState.Add((MouseInputType)i, new InputState());
            }
        }
        private void GenerateNoiseMountains(int x, int z)
        {
            var location = new Vector2i(x, z);

            if (heightMapCache.HeightMaps.ContainsKey(location))
            {
                surface = heightMapCache.HeightMaps[location];
            }
            else
            {
                // Mountains are based on 2 noise patterns
                noiseManager.Areas.Generators[NoiseAreaType.Mountains].Generate(x, z);
                noiseManager.Areas.Generators[NoiseAreaType.Mountains_Ground].Generate(x, z);

                // Extract the noise
                surface = noiseManager.Areas.Generators[NoiseAreaType.Mountains].Output;
                var surfaceGround = noiseManager.Areas.Generators[NoiseAreaType.Mountains_Ground].Output;

                // Combine the noise patterns
                noiseManager.Areas.Modifiers.Combine(ref surface, surfaceGround, CombineMode.Add);

                heightMapCache.HeightMaps.Add(location, surface);
            }
        }
        public int GetHeight(Vector2i location, int x, int z)
        {
            if (!heightMapCache.HeightMaps.ContainsKey(location))
            {
                var area = new Area();
                area.Info.Location = new Vector3i(location.X, 0, location.Y);
                GenerateSurface(ref area);
            }

            return heightMapCache.HeightMaps[location][(z * Area.Size.X) + x];
        }
Example #6
0
 public int GetHeight(Vector2i location, int x, int z)
 {
     return 0;
 }
        public void Initialize(Vector2i viewDistance, TerrainContext terrainContext)
        {
            this.terrainContext = terrainContext;

            areasAlwaysVisibleWithinDistance = Area.Size.X * 2;

            view = new ViewSettings(viewDistance);

            AreaCollection = new AreaCollection();
            AreaCache = new AreaCacheCollection();
            areaRange = new AreaRange();
            viewDistanceAreaRange = new AreaRange();

            visibilityQueue = new VisibilityQueue(terrainContext);
            Statistics = new TerrainVisibilityStatistics(this, AreaCache);
            SpawnPointHelper = new AreaSpawnPointHelper(terrainContext);

            Logger.RegisterLogLevelsFor<TerrainVisibility>(Logger.LogLevels.Adaptive);
        }
Example #8
0
        private void HandleEmptyArea(Area area, Area areaMask, byte[] mask)
        {
            var location2i = new Vector2i(area.Info.Location.X, area.Info.Location.Z);

            bool isEmpty = true;

            // Loop through all columns of the area
            for (int z = 0; z < Area.Size.Z; z++)
            {
                int currentZ = z * Area.Size.X;

                for (int x = 0; x < Area.Size.X; x++)
                {
                    // Skip unaffected columns
                    if (mask[currentZ + x] == 0)
                    {
                        continue;
                    }

                    // Get the heights of the two areas at the current column location
                    int areaHeight = areaTilesGenerators[area.Info.Type].GetHeight(location2i, x, z);
                    int maskAreaHeight = areaTilesGenerators[areaMask.Info.Type].GetHeight(location2i, x, z);

                    // Calculate the new height based on the weight between the two areas
                    float maskPercentage = mask[currentZ + x] / 255f;
                    int delta = Math.Max(0, (int)((maskAreaHeight - areaHeight) * maskPercentage));
                    int newHeight = areaHeight + delta;

                    // Completely filled column
                    if (newHeight > area.Info.TileHeight.End)
                    {
                        isEmpty = false;
                        areaTilesGenerators[area.Info.Type].ExpandHeight(ref area, x, z, newHeight);
                    }
                    // The column height is within the bounds of the area
                    else if (delta > 0)
                    {
                        newHeight -= area.Info.TileHeight.Start;
                        areaTilesGenerators[area.Info.Type].ExpandHeight(ref area, x, z, newHeight);

                        if (isEmpty)
                        {
                            isEmpty = newHeight > 0;
                        }
                    }
                }
            }

            area.Info.IsEmpty = isEmpty;
        }
Example #9
0
        public void Update()
        {
            if (!IsEnabled)
            {
                return;
            }

            var currentState = Mouse.GetState();

            Position = new Vector2i(currentState.X, currentState.Y);

            // Loop through all mapped keys
            foreach (var pair in MouseState)
            {
                pair.Value.Clear();
                MouseInputType type = pair.Key;

                switch (type)
                {
                    case MouseInputType.LeftButton:
                        SetMouseButtonState(type, currentState.LeftButton, previousState.LeftButton);
                        break;

                    case MouseInputType.MiddleButton:
                        SetMouseButtonState(type, currentState.MiddleButton, previousState.MiddleButton);
                        break;

                    case MouseInputType.RightButton:
                        SetMouseButtonState(type, currentState.RightButton, previousState.RightButton);
                        break;

                    case MouseInputType.ScrollWheelUp:
                        SetMouseAnalogState(type, currentState.ScrollWheelValue, previousState.ScrollWheelValue);
                        break;

                    case MouseInputType.ScrollWheelDown:
                        SetMouseAnalogState(type, currentState.ScrollWheelValue, previousState.ScrollWheelValue);
                        break;

                    case MouseInputType.MoveUp:
                        SetMouseAnalogState(type, currentState.Y, previousState.Y);
                        break;

                    case MouseInputType.MoveDown:
                        SetMouseAnalogState(type, currentState.Y, previousState.Y);
                        break;

                    case MouseInputType.MoveLeft:
                        SetMouseAnalogState(type, currentState.X, previousState.X);
                        break;

                    case MouseInputType.MoveRight:
                        SetMouseAnalogState(type, currentState.X, previousState.X);
                        break;
                }
            }

            // Auto center the mouse position?
            if (AutoCenter && context.Game.IsActive)
            {
                // Lock the mouse at the center of the game window (we need to call Mouse.GetState() again in order for this to work as expected)
                Mouse.SetPosition(context.View.Area.Center.X, context.View.Area.Center.Y);
                currentState = Mouse.GetState();
            }

            // Store the keyboard state
            previousState = currentState;
        }
Example #10
0
 public void Initialize(GameContext context, Vector2i viewDistance, Vector3 gravity, int seed)
 {
     TerrainContext = new TerrainContext(context, viewDistance, seed);
     PhysicsHandler = new JitterPhysicsHandler(gravity);
     EntityHandler = new EntityHandler();
 }