Beispiel #1
0
    protected virtual void Start()
    {
        GameManager _gameManager = GameObject.Find("GameManager").GetComponent <GameManager>();

        _potentialField = PotentialField.createPotentialField(_gameManager._tileMap, _tile);
        PotentialField.Print2DArray <int>(_potentialField);
    }
Beispiel #2
0
    public void Populate()
    {
        LastUpdated = Time.time;
        Flows.Clear();
        SetPotentialsFromNavigationField();
        Target.SeedPotentials(AddTraversal);
        DebugInstance = this;
        while (_heap.Any())
        {
            CellPotentialHeapEntry cellPotentialHeapEntry = _heap.ExtractDominating();
            Vector2i position = cellPotentialHeapEntry.Position;

            float currentPotential = Potentials[position.x, position.y];

            if (currentPotential < cellPotentialHeapEntry.Potential)
            {
                continue;
            }

            Potentials[position.x, position.y] = cellPotentialHeapEntry.Potential;

            TryAddTraversal(position, DownLeft, currentPotential);
            TryAddTraversal(position, DownRight, currentPotential);
            TryAddTraversal(position, UpLeft, currentPotential);
            TryAddTraversal(position, UpRight, currentPotential);

            TryAddTraversal(position, Left, currentPotential);
            TryAddTraversal(position, Right, currentPotential);
            TryAddTraversal(position, Down, currentPotential);
            TryAddTraversal(position, Up, currentPotential);

            CellPotentialHeapEntry.ReturnCellCostHeapEntry(cellPotentialHeapEntry);
        }
        Target.Finished(this);
    }
        private void CheckHeading(PotentialField potentialField, Vector2 worldPosition, Vector2 expectedHeading)
        {
            var heading = potentialField.GetHeading(worldPosition);

            Assert.Equal(expectedHeading.X, heading.X, 3);
            Assert.Equal(expectedHeading.Y, heading.Y, 3);
        }
Beispiel #4
0
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            PotentialField field = new PotentialField();

            field.AddPotentialPoint(100, 100, 1);
            DataContext = vf.VectorField2D.CreateTangentPotentialField(field, 200, 200);
        }
        public void GetHeading_ZeroPotentials_ZeroVector(Vector2 worldPosition)
        {
            const int width          = 3;
            const int height         = 3;
            var       transformer    = new GridTransformer(new Point2(width, height), new Vector2(1, 1));
            var       potentials     = new Array2D <float>(width, height);
            var       potentialField = new PotentialField(transformer, new Point2(), potentials);

            CheckHeading(potentialField, worldPosition, new Vector2(0, 0));
        }
Beispiel #6
0
    public Vector2 GetFlowToTarget(Vector2 position)
    {
        Vector2 actualTarget;

        if (IsAtTarget(position, out actualTarget))
        {
            Vector2 delta = actualTarget - position;
            return(delta * 0.1f);
        }
        PotentialField potentialField = NavigationHandler.Instance.GetPotentialField(this);

        return(potentialField.GetSmoothFlow(position));
    }
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            const double   xDelta = width / xCount;
            const double   yDelta = height / yCount;
            PotentialField field  = new PotentialField();

            for (int ix = 0; ix < xCount; ix++)
            {
                for (int iy = 0; iy < yCount; iy++)
                {
                    field.AddPotentialPoint(new Point(ix * xDelta, iy * yDelta), (ix + iy) % 2 == 0 ? 1 : -1);
                }
            }

            DataContext = VectorField2D.CreateTangentPotentialField(field, (int)width, (int)height);
        }
Beispiel #8
0
    public PotentialField GetPotentialField(Target target)
    {
        PotentialField potentialField = _potentialFields.SingleOrDefault(p => p.Target == target);

        if (potentialField == null)
        {
            potentialField = new PotentialField(NavigationField.Instance, target);
            _potentialFields.Add(potentialField);
        }

        potentialField.LastRequested = Time.time;

        if (Time.time - potentialField.LastUpdated >= updateInterval)
        {
            //Debug.LogFormat("potentialField.Populate for {0}", potentialField.Target);
            potentialField.Populate();
        }

        return potentialField;
    }
Beispiel #9
0
    public PotentialField GetPotentialField(Target target)
    {
        PotentialField potentialField = _potentialFields.SingleOrDefault(p => p.Target == target);

        if (potentialField == null)
        {
            potentialField = new PotentialField(NavigationField.Instance, target);
            _potentialFields.Add(potentialField);
        }

        potentialField.LastRequested = Time.time;

        if (Time.time - potentialField.LastUpdated >= updateInterval)
        {
            //Debug.LogFormat("potentialField.Populate for {0}", potentialField.Target);
            potentialField.Populate();
        }

        return(potentialField);
    }
    /// <summary>
    /// Generate and return the potential field for the static layer (terrain) of the game.
    /// If the static layer has already been generated, re-generate or simply return.
    /// </summary>
    /// <param name="forceGeneration">force regeneration of static layer if it was already generated.</param>
    /// <returns>the potential field for the static layer of the map</returns>
    public static PotentialField GenerateStaticLayer(bool forceGeneration = false)
    {
        // return static layer if it already exists and we don't need to regenerated it
        if (staticLayer != null && !forceGeneration) return staticLayer;

        if (map == null) map = (GameObject.FindObjectOfType(typeof(Map)) as GameObject).GetComponent<Map>();
        if (map == null) throw new UnityException("No map object found. Cannot generate the StaticLayer potential field");

        staticLayer = new PotentialField() { Width = map.Width, Height = map.Height };

        for (int r = 0; r < map.Height; r++)
        {
            for (int c = 0; c < map.Width; c++)
            {
                staticLayer.Values[r, c] = Tile.PotentialFieldWeight(map.GetTile(r,c).GetComponent<Tile>().pathingType);
            }
        }

        return staticLayer;
    }
 public override void Finished(PotentialField potentialField)
 {
     base.Finished(potentialField);
     // Reverse it all!
     potentialField.ReverseFlows();
 }
Beispiel #12
0
 public virtual void Finished(PotentialField potentialField)
 {
 }
Beispiel #13
0
    public float GetWalkingDistanceFrom(Vector2 position)
    {
        PotentialField potentialField = NavigationHandler.Instance.GetPotentialField(this);

        return(potentialField.GetDistanceFrom(position));
    }
		public static Vector[,] CreatePotentialField()
		{
			PotentialField field = new PotentialField();
			field.AddPotentialPoint(0, 0, 1);
			field.AddPotentialPoint(20, 30, -1);
			field.AddPotentialPoint(50, 100, 1);
			field.AddPotentialPoint(100, 200, 3);

			return DataSource2DHelper.CreateVectorData(width, height, (x, y) =>
			{
				return field.GetPotential(new Point(x, y));
			});
		}
Beispiel #15
0
 public override void Finished(PotentialField potentialField)
 {
     base.Finished(potentialField);
     // Reverse it all!
     potentialField.ReverseFlows();
 }
 // Use this for initialization
 void Awake()
 {
     _potentialField = FindObjectOfType(typeof(PotentialField)) as PotentialField;
 }
Beispiel #17
0
 public virtual void Finished(PotentialField potentialField)
 {
 }