Beispiel #1
0
        void Update()
        {
            if (potentialSys == null)
            {
                return;
            }

            currentNode = potentialSys.GetClosestNode(transform.position, currentNode);

            for (int i = 0; i < entries.Length; i++)
            {
                var req = new SampleRequest
                {
                    nodes = new List <IFieldNodeRef> {
                        currentNode
                    },
                    potentialLayerMask = entries[i].layers
                };

                var potentials = potentialSys.SamplePotential(req);

                if (Evaluate(potentials[0], entries[i].comparison, entries[i].value))
                {
                    signal.DispatchFlag(gameObject, entries[i].flag);
                }
            }
        }
Beispiel #2
0
        void Update()
        {
            if (this.potentialField.IsReady)
            {
                if (this.debugEnabled)
                {
                    if (this.debugParticleSystem == null)
                    {
                        this.BuildDebugParticleSystem();
                    }

                    var sampleRequest = new SampleRequest
                    {
                        nodes = this.nodes,
                        potentialLayerMask = this.debugLayerMask.value
                    };

                    var potentials = this.potentialField.SamplePotential(sampleRequest);

                    if (this.debugPotentialAutoScale)
                    {
                        this.highestPotential = 0f;

                        for (int i = 0; i < this.nodes.Count; i++)
                        {
                            var potential = potentials[i];

                            this.highestPotential = Mathf.Max(this.highestPotential, Mathf.Abs(potential));
                        }

                        this.debugPotentialScale = this.highestPotential > 0f ? 1f / this.highestPotential : 1;
                    }

                    for (int i = 0; i < this.nodes.Count; i++)
                    {
                        var potential     = potentials[i];
                        var normPotential = Mathf.Clamp(this.debugPotentialScale * potential, -1f, 1f);

                        debugParticles[i].startColor = ColorHelper.HeatMapColor(normPotential, this.neutral, this.positive1, this.positive2, this.negative1, this.negative2);
                    }

                    potentials.ReturnToPool();

                    debugParticleSystem.SetParticles(debugParticles, debugParticles.Length);
                }
                else
                {
                    if (this.debugParticleSystem != null)
                    {
                        Destroy(this.debugParticleSystem);
                        this.debugParticleSystem = null;
                        this.debugParticles      = null;
                    }
                }
            }
        }
Beispiel #3
0
        public IList <float> SamplePotential(SampleRequest request)
        {
            var potentials = ListPool <float> .Take();

            for (int i = 0; i < request.nodes.Count; i++)
            {
                var node      = (SimpleGraphFieldNodeRef)request.nodes[i];
                var potential = 0f;

                var cacheHit = this.cacheConfig.enabled &&
                               this.potentialCache.TryGet(node.Node, request.potentialLayerMask, request.filter, this.time, out potential);

                if (!cacheHit)
                {
                    for (int j = 0; j < this.layers.Count; j++)
                    {
                        var layer = this.layers[j];

                        if (layer.IsOneOfLayers(request.potentialLayerMask))
                        {
                            var layerPotential = 0f;

                            switch (layer.Kind)
                            {
                            case PotentialLayerKind.Field:
                                layerPotential = this.calculationModule.SamplePotential(node, layer.Sources, request.filter, this.CombinePotential);
                                break;

                            case PotentialLayerKind.Flow:
                                layerPotential = this.flowModule.Sample(layer.LayerNum, node.Node);
                                break;

                            default:
                                throw new System.InvalidOperationException(string.Format("Unknown potential layer kind {0}", layer.Kind));
                            }

                            potential = this.CombinePotential(potential, layerPotential);
                        }
                    }

                    if (this.cacheConfig.enabled)
                    {
                        this.potentialCache.Set(node.Node, request.potentialLayerMask, request.filter, this.time, potential);
                    }
                }

                potentials.Add(potential);
            }

            return(potentials);
        }
Beispiel #4
0
        public IList <NodeSample> SampleNodes(IPotentialFieldSystem potentialField, IFieldNodeRef startNode, int layerMask, PotentialFilter filter)
        {
            var neighbours = potentialField.GetNeighbours(startNode);

            neighbours.Insert(0, startNode);
            var sampleRequest = new SampleRequest();

            sampleRequest.potentialLayerMask = layerMask;
            sampleRequest.filter             = filter;
            sampleRequest.nodes = neighbours;

            var potentials = potentialField.SamplePotential(sampleRequest);

            var maxP = potentials[0];
            var maxN = 0;

            for (int i = 1; i < potentials.Count; i++)
            {
                var p2 = potentials[i];

                if (p2 > maxP)
                {
                    maxN = i;
                    maxP = p2;
                }
            }

            potentials.ReturnToPool();

            var results = ListPool <NodeSample> .Take();

            results.Add(new NodeSample(neighbours[maxN], maxP));

            neighbours.ReturnToPool();

            return(results);
        }