Beispiel #1
0
        private void Start()
        {
            _minimapSize         = gameObject.GetComponent <RectTransform>().rect.width;
            _offsetFromRightSide =
                (-1) * (transform.parent.GetComponent <RectTransform>().rect.width / 2
                        + transform.parent.GetComponent <RectTransform>().anchoredPosition.x);
            _targetedScreenSize =
                transform.parent.parent.GetComponent <RectTransform>().rect.width;

            _matchSession = MatchSession.Current;
            TerrainMap map = _matchSession.TerrainMap;

            if (map == null)
            {
                // Hack: When loading a map, the camera is moved to the loading scene,
                // where the terrain map is not yet loaded. Don't throw errors in that case.
                return;
            }

            //_terrainSize = _terrain.terrainData.bounds.size;
            _terrainSize = map.MapMax - map.MapMin;
            _terrainPos  = map.MapMin;
            _miniMapCamera.orthographicSize = _terrainSize.x / 2f;

            //convert camera to a texture
            RenderTexture.active = _miniMapCamera.targetTexture;
            _miniMapCamera.Render();
            Texture2D image = new Texture2D(_miniMapCamera.targetTexture.width, _miniMapCamera.targetTexture.height);

            image.ReadPixels(new Rect(0, 0, _miniMapCamera.targetTexture.width, _miniMapCamera.targetTexture.height), 0, 0);
            image.Apply();
            _miniMapImage.texture  = image;
            _miniMapCamera.enabled = false;
        }
Beispiel #2
0
        private void ClampCameraXZPosition()
        {
            TerrainMap map = _session.TerrainMap;

            if (map == null)
            {
                // Hack: When loading a map, the camera is moved to the loading scene,
                // where the terrain map is not yet loaded. Don't throw errors in that case.
                return;
            }

            _targetPosition.x = Mathf.Clamp(
                _targetPosition.x,
                map.MapMin.x - _maxCameraHorizontalDistanceFromTerrain,
                map.MapMax.x + _maxCameraHorizontalDistanceFromTerrain);
            _targetPosition.z = Mathf.Clamp(
                _targetPosition.z,
                map.MapMin.z - _maxCameraHorizontalDistanceFromTerrain,
                map.MapMax.z + _maxCameraHorizontalDistanceFromTerrain);
        }
Beispiel #3
0
        private float GetSlopeFactor(
            TerrainMap terrain, Vector3 location, Vector3 direction)
        {
            direction.y = 0f;
            direction.Normalize();
            direction *= 10f * Constants.MAP_SCALE;
            Vector3 perpendicular = new Vector3(-direction.z, 0f, direction.x);

            float height        = terrain.GetTerrainCachedHeight(location);                 //SampleHeight(location);
            float forwardHeight = terrain.GetTerrainCachedHeight(location - direction);     //SampleHeight(location - direction);
            float sideHeight    = terrain.GetTerrainCachedHeight(location + perpendicular); //SampleHeight(location + perpendicular);

            float forwardSlope = forwardHeight - height;
            float sideSlope    = sideHeight - height;
            float slopeSquared = forwardSlope * forwardSlope + sideSlope * sideSlope;

            float overallSlopeFactor     = SlopeSensitivity * slopeSquared;
            float directionalSlopeFactor = SlopeSensitivity * DirectionalSlopeSensitivity * forwardSlope;
            float speed = 1.0f / (1.0f + overallSlopeFactor + directionalSlopeFactor);

            return(Mathf.Max(speed - 0.1f, 0f));
        }
Beispiel #4
0
        /// <summary>
        /// Gives the relative speed of a unit with the given MobilityType
        /// at the given location. Relative speed is 0 if the terrain is
        /// impassible and 1 for road, otherwise between 0 and 1.
        /// If radius > 0, check for units in the way, otherwise just look at terrain.
        /// </summary>
        public float GetUnitSpeedMultiplier(
            TerrainMap map,
            Vector3 location,
            float unitRadius,
            Vector3 direction)
        {
            //Terrain terrain = map.GetTerrainAtPos(location);
            //if (terrain == null)
            //    return 0f;


            // This is a slow way to do it, and we will probably need a fast, generic method to find units within a given distance of a location
            if (unitRadius > 0f)
            {
                // TODO maybe move this logic into its own method?

                foreach (UnitDispatcher unit in MatchSession.Current.Units)
                {
                    float dist = Vector3.Distance(location, unit.Transform.position);
                    if (dist < unitRadius + unit.GetComponent <MovementComponent>().Data.Radius)
                    {
                        return(0f);
                    }
                }
            }

            // Find unit speed on terrain
            int terrainType = map.GetTerrainType(location);

            float speed = 0f;

            if (terrainType == TerrainMap.BRIDGE)
            {
                speed = 1.0f;
            }
            else if (terrainType == TerrainMap.BUILDING)
            {
                speed = 0.0f;
            }
            else if (terrainType == TerrainMap.ROAD)
            {
                speed = 1.0f;
            }
            else if (terrainType == TerrainMap.FOREST)
            {
                speed = ForestSpeed;
            }
            else if (terrainType == TerrainMap.PLAIN)
            {
                speed = PlainSpeed;
            }
            else if (terrainType == TerrainMap.WATER)
            {
                speed = WaterSpeed;
            }

            if (speed <= 0)
            {
                return(0f);
            }

            if (terrainType == TerrainMap.BRIDGE || terrainType == TerrainMap.WATER)
            {
                return(speed);
            }

            return(speed * GetSlopeFactor(map, location, direction));
        }