public GameObject SpawnHerbivore(Vector3 pos, Quaternion rot, HerbivoreType type)
    {
        var herb = HerbivoreFactory.InstantiateHerbivore(pos, rot, type);

        herb.transform.SetParent(transform);
        NetworkServer.Spawn(herb.gameObject);

        if (!m_Herbivores.ContainsKey(type))
        {
            m_Herbivores.Add(type, new HashSet <HerbivoreBase>());
        }

        m_Herbivores[type].Add(herb);
        OctreeManager.Get(OctreeType.Herbivore).Add(herb.transform);
        return(herb.gameObject);
    }
    public GameObject SpawnCarnivore(Vector3 pos, Quaternion rot, CarnivoreType type)
    {
        var carn = CarnivoreFactory.InstantiateCarnivore(pos, rot, type);

        carn.transform.SetParent(transform);
        NetworkServer.Spawn(carn.gameObject);

        if (!m_Carnivores.ContainsKey(type))
        {
            m_Carnivores.Add(type, new HashSet <CarnivoreBase>());
        }

        m_Carnivores[type].Add(carn);
        OctreeManager.Get(OctreeType.Carnivore).Add(carn.transform);
        return(carn.gameObject);
    }
Esempio n. 3
0
                    public static OctreeManager FromBaseObject(BaseObject baseObj)
                    {
                        if (baseObj == null || baseObj.NativeObject == IntPtr.Zero)
                        {
                            return(null);
                        }
                        OctreeManager obj = baseObj as  OctreeManager;

                        if (object.Equals(obj, null))
                        {
                            obj = new OctreeManager(CreatedWhenConstruct.CWC_NotToCreate);
                            obj.BindNativeObject(baseObj.NativeObject, "COctreeManager");
                            obj.IncreaseCast();
                        }

                        return(obj);
                    }
Esempio n. 4
0
    private void CalculateColliderAcitvationRange()
    {
        var colliderActivationRange = GetBoundingSphereRadius();

        var sc = GetComponent <SphereCollider>();

        sc.radius = colliderActivationRange + CubeInteractionRadius;
        InteractableCollider.Radius = colliderActivationRange;

        if (OctreeManager.Get(OctreeType.Player) != null)
        {
            var colliders = OctreeManager.Get(OctreeType.Player).GetObjectsInRange(transform.position, colliderActivationRange + CubeInteractionRadius);

            TriggeredCount = colliders.Length;
            EvaluateTriggeredCount();
        }
    }
    public void KillCarnivore(CarnivoreBase carn, bool predation = false)
    {
        if (carn == null || carn.gameObject == null)
        {
            return;
        }

        if (m_Carnivores.ContainsKey(carn.Type))
        {
            m_Carnivores[carn.Type].Remove(carn);
        }

        OctreeManager.Get(OctreeType.Carnivore).Remove(carn.transform);

        if (predation && OnPredationEvent != null)
        {
            OnPredationEvent(carn);
        }

        NetworkServer.Destroy(carn.gameObject);

        ValidateCarnivoreCount();
    }
    public void KillHerbivore(HerbivoreBase herb, bool predation = false)
    {
        if (herb == null || herb.gameObject == null)
        {
            return;
        }

        if (m_Herbivores.ContainsKey(herb.Type))
        {
            m_Herbivores[herb.Type].Remove(herb);
        }

        OctreeManager.Get(OctreeType.Herbivore).Remove(herb.transform);

        if (predation && OnPredationEvent != null)
        {
            OnPredationEvent(herb);
        }

        NetworkServer.Destroy(herb.gameObject);

        ValidateHerbivoreCount();
    }
Esempio n. 7
0
    private void AvoidPlayerUpdate()
    {
        m_CheckForPlayerCounter += Time.deltaTime;
        if (m_CheckForPlayerCounter > CheckForPlayerRate)
        {
            m_CheckForPlayerCounter = 0f;
            var closestPlayer = OctreeManager.Get(OctreeType.Player).GetClosestObject(transform.position);

            var vectorToStart = (new Vector3(m_StartPos.x, transform.position.y, m_StartPos.z) - transform.position).normalized * FloatSpeed;

            //Make sure there is a player in range
            if (closestPlayer != null &&
                (closestPlayer.position - transform.position).sqrMagnitude < CheckForPlayerRange * CheckForPlayerRange)
            {
                var evadeVector = Steering.Evade(gameObject, closestPlayer.gameObject, FloatSpeed);

                //Go to start XZ pos if that is close to the evade vector
                m_FloatVel = Vector3.Angle(evadeVector, vectorToStart) < 90f
                            ? new Vector3(vectorToStart.x, m_FloatVel.y, vectorToStart.z)
                            : new Vector3(evadeVector.x, m_FloatVel.y, evadeVector.z);

                return;
            }

            //Are we not close to our starting position on the horizontal plane
            if ((transform.position - new Vector3(m_StartPos.x, transform.position.y, m_StartPos.z)).sqrMagnitude > 2f)
            {
                //Float towards start on the horizontal
                m_FloatVel = new Vector3(vectorToStart.x, m_FloatVel.y, vectorToStart.z);
            }
            else
            {
                m_FloatVel = new Vector3(0, m_FloatVel.y, 0);
            }
        }
    }
Esempio n. 8
0
    private void CheckForPlayer()
    {
        var player = OctreeManager.Get(OctreeType.Player).GetClosestObject(transform.position, m_InteractionRadius);

        ProximityShrink(player);
    }
Esempio n. 9
0
    public override void OnStartServer()
    {
        m_AnimAudioController = GetComponent <TriHorseAnimAudioController>();
        m_AnimAudioController.OnEatingFinished += OnEatingFinished;
        m_AnimAudioController.OnDyingFinished  += Die;

        Grower = new ScaledGrowth(transform,
                                  DataStore.GetFloat(Data.TriHorseInitialScale),
                                  DataStore.GetFloat(Data.TriHorseFinalScaleMin),
                                  DataStore.GetFloat(Data.TriHorseFinalScaleMax));

        Grower.StartGrowing();
        Scale = Grower.Scale;

        AgeData.DaysToGrown = DataStore.GetFloat(Data.TriHorseDaysToGrown);
        AgeData.LifeSpan    = DataStore.GetFloat(Data.TriHorseLifeSpan);
        BaseSpeed           = DataStore.GetFloat(Data.TriHorseBaseSpeed);

        DaysBeforeReproducing          = DataStore.GetFloat(Data.TriHorseDaysBeforeReproducing);
        DaysBetweenReproductions       = DataStore.GetFloat(Data.TriHorseDaysBetweenReproductions);
        StarvingDamageAmount           = DataStore.GetFloat(Data.TriHorseStarvingDamageAmount);
        StructureCollisionDamageAmount = DataStore.GetFloat(Data.TriHorseStructureCollisionDamageAmount);
        SensingRadius = DataStore.GetFloat(Data.TriHorseSensingRadius);

        WanderParameters.Radius              = DataStore.GetFloat(Data.TriHorseWanderRadius);
        WanderParameters.Distance            = DataStore.GetFloat(Data.TriHorseWanderDistance);
        WanderParameters.Jitter              = DataStore.GetFloat(Data.TriHorseWanderJitter);
        FlockingOptions.WanderWeight         = DataStore.GetFloat(Data.TriHorseWanderWeight);
        FlockingOptions.AlignmentWeight      = DataStore.GetFloat(Data.TriHorseAlignWeight);
        FlockingOptions.MinDispersion        = DataStore.GetFloat(Data.TriHorseMinFlockDispersion);
        FlockingOptions.MaxDispersion        = DataStore.GetFloat(Data.TriHorseMaxFlockDispersion);
        FlockingOptions.MinDispersionSquared = FlockingOptions.MinDispersion * FlockingOptions.MinDispersion;
        FlockingOptions.MaxDispersionSquared = FlockingOptions.MaxDispersion * FlockingOptions.MaxDispersion;

        m_FlockmateSensor             = new OctreeSensor <TriHorse>(transform, SensingRadius, 20, OctreeManager.Get(OctreeType.Herbivore));
        m_FlockmateSensor.RefreshRate = m_FlockmateSensor.RefreshRate.Randomize(.05f);

        base.OnStartServer();
        Scale = Grower.Scale;

        BehaviourBrain.In(BehaviourState.SeekingFood)
        .If(() => Health <= 0)
        .GoTo(BehaviourState.Death)
        .If(() => m_InDeathThrows)
        .GoTo(BehaviourState.Dying)
        .If(() => m_TimeSinceEating < DayClock.Singleton.DaysToSeconds(.4f))
        .GoTo(BehaviourState.Flocking)
        .If(CanReproduce)
        .GoTo(BehaviourState.Reproducing)
        .If(() => m_EatingNodule)
        .GoTo(BehaviourState.Eating)
        .ExecuteWhileIn(SeekFood, StarvationCheck);

        BehaviourBrain.In(BehaviourState.Flocking)
        .If(() => Health <= 0)
        .GoTo(BehaviourState.Death)
        .If(() => m_InDeathThrows)
        .GoTo(BehaviourState.Dying)
        .If(() => m_TimeSinceEating > DayClock.Singleton.DaysToSeconds(.4f))
        .GoTo(BehaviourState.SeekingFood)
        .If(CanReproduce)
        .GoTo(BehaviourState.Reproducing)
        .ExecuteWhileIn(Flocking);

        BehaviourBrain.In(BehaviourState.Eating)
        .If(() => m_InDeathThrows)
        .GoTo(BehaviourState.Dying)
        .If(() => !m_EatingNodule)
        .GoTo(BehaviourState.Flocking)
        .ExecuteOnEntry(StartEating)
        .ExecuteWhileIn(EatNodule)
        .ExecuteOnExit(EndEating);

        BehaviourBrain.In(BehaviourState.Reproducing)
        .DoOnce(Reproduce)
        .If(() => true)
        .GoTo(BehaviourState.SeekingFood);

        //Empty state. All action is handled through anim callbacks or outside function calls
        //Once we enter this state, we do not leave it.
        BehaviourBrain.In(BehaviourState.Dying);

        BehaviourBrain.In(BehaviourState.Death)
        .DoOnce(Die);

        BehaviourBrain.Initialize(BehaviourState.SeekingFood);

        //This forces the herbivore to start in the seeking food state
        m_TimeSinceEating = DayClock.Singleton.DaysToSeconds(.4f);
    }
Esempio n. 10
0
    private IEnumerator InitializePlayerSensor()
    {
        while (!OctreeManager.Contains(OctreeType.Player))
        {
            yield return(new WaitForSeconds(.5f));
        }

        m_PlayerSensor = new OctreeSensor <Transform>(transform, CalculateEnclosingRadius(), OctreeManager.Get(OctreeType.Player))
        {
            RefreshRate = .1f.Randomize(.05f)
        };
    }
 private void Start()
 {
     if (isServer)
         m_PlayerSensor = new OctreeSensor<NetworkIdentity>(OriginTransform, m_InteractRange, OctreeManager.Get(OctreeType.Player))
         {
             RefreshRate = .1f.Randomize(.03f)
         };
 }
Esempio n. 12
0
 private void NodeGroup_OnRemoveChildNode(object sender, OnChildNodeChangedArgs e)
 {
     OctreeManager?.RemoveItem(e);
 }
Esempio n. 13
0
                    public BaseObject Create()
                    {
                        OctreeManager emptyInstance = new OctreeManager(CreatedWhenConstruct.CWC_NotToCreate);

                        return(emptyInstance);
                    }
Esempio n. 14
0
 protected override void UpdateInstancesBounds()
 {
     OctreeManager?.Clear();
     base.UpdateInstancesBounds();
 }
Esempio n. 15
0
        protected void ItemsModel3D_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Reset:
                OctreeManager?.Clear();
                OctreeManager?.RequestRebuild();
                break;
            }
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Replace:
            case NotifyCollectionChangedAction.Remove:
                if (e.OldItems != null)
                {
                    foreach (var item in e.OldItems)
                    {
                        if (mDictionary.ContainsKey(item))
                        {
                            var model = mDictionary[item];
                            if (model is GeometryModel3D)
                            {
                                OctreeManager?.RemoveItem(model as GeometryModel3D);
                            }
                            model.DataContext = null;
                            this.Children.Remove(model);
                            mDictionary.Remove(item);
                        }
                    }
                    InvalidateRender();
                }
                break;

            case NotifyCollectionChangedAction.Reset:
                var array = this.Children.ToArray();
                foreach (var item in array)
                {
                    item.DataContext = null;
                    this.Children.Remove(item);
                }
                mDictionary.Clear();
                break;
            }

            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Reset:
                if (this.ItemsSource != null)
                {
                    if (this.ItemTemplate == null)
                    {
                        foreach (var item in this.ItemsSource)
                        {
                            var model = item as Model3D;
                            if (model != null)
                            {
                                this.Children.Add(model);
                                mDictionary.Add(item, model);
                            }
                            else
                            {
                                throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate.");
                            }
                        }
                    }
                    else
                    {
                        foreach (var item in this.ItemsSource)
                        {
                            var model = this.ItemTemplate.LoadContent() as Model3D;
                            if (model != null)
                            {
                                model.DataContext = item;
                                this.Children.Add(model);
                                mDictionary.Add(item, model);
                            }
                            else
                            {
                                throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate.");
                            }
                        }
                    }
                }
                InvalidateRender();
                break;

            case NotifyCollectionChangedAction.Add:
            case NotifyCollectionChangedAction.Replace:
                if (e.NewItems != null)
                {
                    if (this.ItemTemplate != null)
                    {
                        foreach (var item in e.NewItems)
                        {
                            if (mDictionary.ContainsKey(item))
                            {
                                continue;
                            }
                            var model = this.ItemTemplate.LoadContent() as Model3D;
                            if (model != null)
                            {
                                OctreeManager?.AddPendingItem(model);
                                model.DataContext = item;
                                this.Children.Add(model);
                                mDictionary.Add(item, model);
                            }
                            else
                            {
                                throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate.");
                            }
                        }
                    }
                    else
                    {
                        foreach (var item in e.NewItems)
                        {
                            if (mDictionary.ContainsKey(item))
                            {
                                continue;
                            }
                            var model = item as Model3D;
                            if (model != null)
                            {
                                OctreeManager?.AddPendingItem(model);
                                this.Children.Add(model);
                                mDictionary.Add(item, model);
                            }
                            else
                            {
                                throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate.");
                            }
                        }
                    }
                }
                break;
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Handles changes in the ItemsSource property.
        /// </summary>
        /// <param name="e">
        /// The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.
        /// </param>
        /// <exception cref="System.InvalidOperationException">
        /// Cannot create a Model3D from ItemTemplate.
        /// </exception>
        private void ItemsSourceChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue is INotifyCollectionChanged)
            {
                (e.OldValue as INotifyCollectionChanged).CollectionChanged -= ItemsModel3D_CollectionChanged;
            }

            foreach (Model3D item in Children)
            {
                item.DataContext = null;
            }

            OctreeManager?.Clear();
            mDictionary.Clear();
            Children.Clear();

            if (e.NewValue is INotifyCollectionChanged)
            {
                (e.NewValue as INotifyCollectionChanged).CollectionChanged -= ItemsModel3D_CollectionChanged;
                (e.NewValue as INotifyCollectionChanged).CollectionChanged += ItemsModel3D_CollectionChanged;
            }

            if (ItemsSource == null)
            {
                return;
            }
            if (this.ItemTemplate == null)
            {
                foreach (var item in this.ItemsSource)
                {
                    if (mDictionary.ContainsKey(item))
                    {
                        continue;
                    }
                    var model = item as Model3D;
                    if (model != null)
                    {
                        this.Children.Add(model);
                        mDictionary.Add(item, model);
                    }
                    else
                    {
                        throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate.");
                    }
                }
            }
            else
            {
                foreach (var item in this.ItemsSource)
                {
                    if (mDictionary.ContainsKey(item))
                    {
                        continue;
                    }
                    var model = this.ItemTemplate.LoadContent() as Model3D;
                    if (model != null)
                    {
                        model.DataContext = item;
                        this.Children.Add(model);
                        mDictionary.Add(item, model);
                    }
                    else
                    {
                        throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate.");
                    }
                }
            }
            if (Children.Count > 0)
            {
                OctreeManager?.RequestRebuild();
            }
        }
Esempio n. 17
0
        //private void ItemsModel3D_Unloaded(object sender, RoutedEventArgs e)
        //{
        //    loaded = false;
        //    OctreeManager?.Clear();
        //}

        //private void ItemsModel3D_Loaded(object sender, RoutedEventArgs e)
        //{
        //    loaded = true;
        //    UpdateBounds();
        //    //if (Children.Count > 0)
        //    //{
        //    //    OctreeManager?.RequestRebuild();
        //    //}
        //}

        private void UpdateOctree()
        {
            OctreeManager?.RebuildTree(this.Children);
        }
    private void CheckForPlayer()
    {
        var player = OctreeManager.Get(OctreeType.Player).GetClosestObject(transform.position, m_InteractionRadius);

        SetState(player == null ? State.Inactive : State.Active);
    }
Esempio n. 19
0
 private void NodeGroup_OnClear(object sender, OnChildNodeChangedArgs e)
 {
     OctreeManager?.Clear();
     OctreeManager?.RequestRebuild();
 }
    protected override void Start()
    {
        Rigidbody = GetComponent <Rigidbody>();

        base.Start();

        if (!isServer && isClient)
        {
            var anim = GetComponent <Animator>();
            if (anim != null)
            {
                anim.applyRootMotion = false;
            }
            return;
        }

        if (!isServer)
        {
            return;
        }

        HerbivoreSensor = new OctreeSensor <HerbivoreBase>(transform, SensingRadius, MaximumHerdSizeToAttack + 1, OctreeManager.Get(OctreeType.Herbivore));

        //Starts the carnivore slightly hungry
        TimeSinceEating = DayClock.Singleton.DaysToSeconds(1.01f);
    }
Esempio n. 21
0
 private void NodeGroup_OnAddChildNode(object sender, OnChildNodeChangedArgs e)
 {
     OctreeManager?.AddPendingItem(e);
 }
Esempio n. 22
0
 private void Start()
 {
     m_Sensor = new OctreeSensor <Transform>(transform, Range, 1, OctreeManager.Get(OctreeType.Player));
 }