Ejemplo n.º 1
0
    protected void Act()
    {
        switch (currentAction)
        {
        case CreatureAction.Exploring:
            StartMoveToCoord(Environment.GetNextTileWeighted(coord, moveFromCoord));
            break;

        case CreatureAction.GoingToFood:
            if (Coord.AreNeighbours(coord, foodTarget.coord))
            {
                LookAt(foodTarget.coord);
                currentAction = CreatureAction.Eating;
            }
            else
            {
                StartMoveToCoord(path[pathIndex]);
                pathIndex++;
            }
            break;

        case CreatureAction.GoingToWater:
            if (Coord.AreNeighbours(coord, waterTarget))
            {
                LookAt(waterTarget);
                currentAction = CreatureAction.Drinking;
            }
            else
            {
                StartMoveToCoord(path[pathIndex]);
                pathIndex++;
            }
            break;
        }
    }
Ejemplo n.º 2
0
    protected virtual void ChooseNextAction()
    {
        Surroundings surroundings = Environment.Sense(coord);

        if (surroundings.nearestFoodSource != null)
        {
            currentAction = CreatureAction.GoingToFood;
            foodTarget    = surroundings.nearestFoodSource;
        }

        // If exploring, move to random tile
        if (currentAction == CreatureAction.Exploring)
        {
            StartMoveToCoord(Environment.GetNextTileWeighted(coord, moveFromCoord));
        }
        else if (currentAction == CreatureAction.GoingToFood)
        {
            if (Coord.AreNeighbours(coord, foodTarget.coord))
            {
                currentAction = CreatureAction.Eating;
            }
            else
            {
                StartMoveToCoord(EnvironmentUtility.GetNextInPath(coord.x, coord.y, foodTarget.coord.x, foodTarget.coord.y));
            }
        }
    }
Ejemplo n.º 3
0
    protected void giveBirth()
    {
        mate.currentAction = CreatureAction.Resting;
        currentAction      = CreatureAction.Resting;
        int fSeconds = Environment.getInt(1, 4);

        mate.Wait(fSeconds);
        Wait(fSeconds);
        for (int i = 0; i < numOffsprings; ++i)
        {
            Animal entity = Environment.objectPools[species].Get() as Animal;
            //var entity = Instantiate(EnvironmentUtility.prefabBySpecies[species]);
            entity.Init(coord);
            entity.onEnable();
            Environment.RegisterBirth(entity, coord);
        }


        mate.reproductionWill = 0;
        mate.path             = null;
        reproductionWill      = 0;
        if (!(this is Human))
        {
            mate.mate = null;
            mate      = null;
        }
    }
Ejemplo n.º 4
0
    // Animals choose their next action after each movement step (1 tile),
    // or, when not moving (e.g interacting with food etc), at a fixed time interval.
    protected virtual void ChooseNextAction()
    {
        lastActionChooseTime = Time.time;

        float[] states = { hunger, thirst, stamina, desire };
        float   max    = states.Max();

        if (max == desire && canReproduce && lifespan > 0.5)
        {
            FindPotentialMates();
        }
        else if (max == stamina)
        {
            currentAction = CreatureAction.Resting;
        }
        else if (max == hunger)
        {
            FindFood();
        }
        else if (max == thirst)
        {
            FindWater();
        }

        Act();
    }
Ejemplo n.º 5
0
    protected virtual void FindMate()
    {
        if (mate != null)
        {
            Mate(mate);
            return;
        }
        List <Animal> mates = Environment.SensePotentialMates(coord, this);

        if (mates.Count != 0)
        {
            foreach (Animal possibleMate in mates)
            {
                if (possibleMate.reproductionWill > 0.65)
                {
                    if (genes.isMale)
                    {
                        Mate(possibleMate);
                        return;
                    }
                    else
                    {
                        currentAction = CreatureAction.SearchingForMate;
                    }
                }
            }
        }
    }
Ejemplo n.º 6
0
    private void DoAction(GameObject goalObject)
    {
        CreatureAction action = null;
        Item           item   = goalObject.GetComponent <Item>();

        if (item == null)
        {
            return;
        }
        switch (item.itemType)
        {
        case ItemType.CardboardBox:
            break;

        case ItemType.Furniture:
            action = new FurnitureCatAction().AsCreatureAction();
            break;

        case ItemType.Foodbowl:
            var bowl = item.GetComponent <BowlScript>();
            action = bowl.CatAction;
            break;
        }
        if (action != null)
        {
            action(this, item);
        }
    }
Ejemplo n.º 7
0
 // This function handles collisions with other objects
 void OnCollisionEnter(Collision other)
 {
     if (other.gameObject.CompareTag("wall"))
     {
         currentAction = CreatureAction.HitWall;
     }
 }
Ejemplo n.º 8
0
    // This function controls the movement of the Blob
    void Explore()
    {
        // Sets kinematic to false
        if (rb.isKinematic)
        {
            rb.isKinematic = false;
        }
        // The blob moves foward (relative to its own rotation) at a constant speed
        rb.velocity = transform.forward * speed;

        // There is a 10% chance that the blob will change direction
        if (Random.value < .05f)
        {
            rb.transform.Rotate(new Vector3(0, Random.Range(-100, 100), 0));
        }
        // If there is food within senseRadius, change creature action
        if (nearestFood != null)
        {
            currentAction = CreatureAction.GoingToFood;
        }

        if (isHome)
        {
            currentAction = CreatureAction.IsHome;
        }

        // This if statement makes the blob go home if it already and just barely has enough energy to get back
        if (!goingHome && eatenFood > 0 && energy <= energyToGetBack)
        {
            goingHome     = true;
            currentAction = CreatureAction.GoingHome;
        }
    }
Ejemplo n.º 9
0
    protected void Act()
    {
        switch (currentAction)
        {
        case CreatureAction.Exploring:
            StartMoveToCoord(Environment.GetNextTileWeighted(coord, moveFromCoord));
            break;

        case CreatureAction.GoingToFood:
            if (Coord.AreNeighbours(coord, foodTarget.coord))
            {
                LookAt(foodTarget.coord);
                currentAction = CreatureAction.Eating;
            }
            else
            {
                StartMoveToCoord(path[pathIndex]);
                pathIndex++;
            }
            break;

        case CreatureAction.GoingToWater:
            if (Coord.AreNeighbours(coord, waterTarget))
            {
                LookAt(waterTarget);
                currentAction = CreatureAction.Drinking;
            }
            else
            {
                StartMoveToCoord(path[pathIndex]);
                pathIndex++;
            }
            break;

        case CreatureAction.SearchingForMate:
            if (Coord.AreNeighbours(coord, mateTarget.coord))
            {
                LookAt(mateTarget.coord);
                if (mateTarget && Math.Abs(mateTarget.lifespan - lifespan) < 0.1 && desire > 0 && mateTarget.desire > 0)
                {
                    desire            = 0;
                    mateTarget.desire = 0;
                    //mateTarget.canReproduce = false;
                    //canReproduce = false;

                    Animal entity = (Animal)Instantiate(prefab);
                    entity.Init(coord);
                    entity.genes = Genes.InheritedGenes(genes, mateTarget.genes);
                    Environment.speciesMaps[entity.species].Add(entity, coord);
                }
            }
            else
            {
                StartMoveToCoord(path[pathIndex]);
                pathIndex++;
            }
            break;
        }
    }
Ejemplo n.º 10
0
 public void addAction(CreatureAction ca)
 {
     while (actionQueue[actionQueue.Count - 1].lowPrio)
     {
         actionQueue.RemoveAt(actionQueue.Count - 1);
     }
     actionQueue.Add(ca);
 }
Ejemplo n.º 11
0
 // Start is called before the first frame update
 void Start()
 {
     rb = GetComponent <Rigidbody>();
     // Starts the creature action on None
     currentAction = CreatureAction.None;
     // Decreases energy with 1 every 10th of a second
     InvokeRepeating("Energy", 0f, 0.1f);
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Функция инициализации, вызывается до чего-либо другого.
 /// </summary>
 private void Awake()
 {
     _groundCheckRayLenght = _collider.size.y / 2 * transform.localScale.y + _groundCheckRayExtraLenght;
     _movementDirection    = Vector2.zero;
     _creatrureAction      = CreatureAction.idle;
     _isGrounded           = false;
     _rigidbody            = gameObject.GetComponent <Rigidbody2D>();
 }
Ejemplo n.º 13
0
    // Animals choose their next action after each movement step (1 tile),
    // or, when not moving (e.g interacting with food etc), at a fixed time interval
    protected virtual void ChooseNextAction()
    {
        lastActionChooseTime = Time.time;
        // Get info about surroundings

        hunter = Environment.SensePredators(this);
        if (hunter != null)
        {
            currentAction = CreatureAction.RunningAway;
            Act();
            return;
        }

        if (currentAction == CreatureAction.GoingToMate && mate != null)
        {
            Act();
            return;
        }

        //if ((currentAction == CreatureAction.Drinking && thirst > 0.01) || (currentAction == CreatureAction.Eating && hunger > 0.01))
        //  return;
        // Decide next action:
        // Eat if (more hungry than thirsty) or (currently eating and not critically thirsty)
        bool currentlyEating   = currentAction == CreatureAction.Eating && foodTarget != null && !foodTarget.Equals(null) && hunger > 0;
        bool currentlyDrinking = currentAction == CreatureAction.Drinking && thirst > 0;

        if ((hunger >= thirst && hunger > 0.55) || currentlyEating && thirst < criticalPercent)
        {
            FindFood();
        }
        // More thirsty than hungry
        else
        {
            if (thirst > 0.55 || currentlyDrinking)
            {
                FindWater();
            }
            else
            if (reproductionWill > 0.55)
            {
                currentAction = CreatureAction.SearchingForMate;
                FindMate();
            }
            else
            if (Environment.getRandomDouble() > 0.9)
            {
                currentAction = CreatureAction.Resting;
            }
            else
            {
                currentAction = CreatureAction.Exploring;
            }
        }


        Act();
    }
Ejemplo n.º 14
0
 public static bool CreatureActionKillCreature(Creature __instance, ref CreatureAction __result)
 {
     if (TryKillCreature(__instance))
     {
         __result = null;
         return(false);
     }
     return(true);
 }
Ejemplo n.º 15
0
        public async Task <ActionResult <CreatureCardDto> > Edit(CreatureCardDto creatureCard)
        {
            var idToReturn       = 0;
            var creatureCardInDb = _db.CreatureCards.FirstOrDefault(cc => cc.CreatureCardId == creatureCard.CreatureCardId);

            if (creatureCardInDb != null)
            {
                // edit this creature card
                _mapper.Map(creatureCard, creatureCardInDb);
                creatureCard.Actions.ForEach(a =>
                {
                    // remove all existing creatureCard Entries
                    var action         = new CreatureAction();
                    action.Name        = a.Name;
                    action.Description = a.Description;
                    _db.CreatureAction.Add(action);
                    _db.SaveChanges();
                    var actionsToRemove = _db.CreatureCardActions.Where(cca => cca.CreatureCardId == creatureCard.CreatureCardId).ToList();
                    _db.CreatureCardActions.RemoveRange(actionsToRemove);
                    //add entry to linking table
                    var creatureCardAction = new CreatureCardAction();
                    creatureCardAction.CreatureActionId = action.CreatureActionId;
                    creatureCardAction.CreatureCardId   = creatureCard.CreatureCardId;
                    _db.CreatureCardActions.Add(creatureCardAction);
                });
                idToReturn = creatureCardInDb.CreatureCardId;
                creatureCard.CreatureCardId = creatureCardInDb.CreatureCardId;
            }
            else
            {
                var creature = _mapper.Map <CreatureCard>(creatureCard);

                _db.CreatureCards.Add(creature);
                _db.CreatureAction.AddRange(creature.Actions);
                _db.SaveChanges();
                // add the actions to the creature card
                creature.Actions.ForEach(a =>
                {
                    //var action = new CreatureAction();
                    //action.Name = a.Name;
                    //action.Description = a.Description;
                    //_db.CreatureAction.Add(action);
                    _db.SaveChanges();
                    //add entry to linking table
                    var creatureCardAction = new CreatureCardAction();
                    creatureCardAction.CreatureActionId = a.CreatureActionId;
                    creatureCardAction.CreatureCardId   = creature.CreatureCardId;
                    _db.CreatureCardActions.Add(creatureCardAction);
                });
                idToReturn = creature.CreatureCardId;
                creatureCard.CreatureCardId = creature.CreatureCardId;
            }
            _db.SaveChanges();

            return(CreatedAtAction("Edit", new { id = idToReturn }, creatureCard));
        }
Ejemplo n.º 16
0
 public static CreatureAction create(ActionType actionType, int sequenceID, Vector3 location, int targetID, int what, bool lowPrio = false)
 {
     CreatureAction ca = new CreatureAction();
     ca.rawActiontype = (int)actionType;
     ca.location = location;
     ca.targetID = targetID;
     ca.lowPrio = lowPrio;
     ca.what = what;
     return ca;
 }
        public override void Process(CreatureActionChanged packet)
        {
            Optional <GameObject> opGameObject = GuidHelper.GetObjectFrom(packet.Guid);

            if (opGameObject.IsPresent())
            {
                CreatureAction action = packet.NewAction.GetCreatureAction(opGameObject.Get());
                ActionByGuid[packet.Guid] = action;
            }
        }
Ejemplo n.º 18
0
        public override void Process(CreatureActionChanged packet)
        {
            Optional <GameObject> opGameObject = NitroxIdentifier.GetObjectFrom(packet.Id);

            if (opGameObject.IsPresent())
            {
                CreatureAction action = packet.NewAction.GetCreatureAction(opGameObject.Get());
                ActionByCreatureId[packet.Id] = action;
            }
        }
        public override void Process(CreatureActionChanged packet)
        {
            Optional <GameObject> opGameObject = NitroxEntity.GetObjectFrom(packet.Id);

            if (opGameObject.HasValue)
            {
                CreatureAction action = packet.NewAction.GetCreatureAction(opGameObject.Value);
                ActionByCreatureId[packet.Id] = action;
            }
        }
Ejemplo n.º 20
0
    public QueueActionManager()
    {
        isServer = Network.isServer;
        actionQueue = new List<CreatureAction>();

        currentQueueActionSequenceID = 0;
        queueActionIndex = 0;
        idleAction = CreatureAction.create(CreatureAction.ActionType.NoAction, 0, Vector3.zero, 0, 0);
        //TODO register this instance to be watched by the networkview!
    }
Ejemplo n.º 21
0
 // This function subtracts one from energy and sets the blob's state to "Out of Energy" if it doesn't make it home
 void Energy()
 {
     if (energy > 0)
     {
         energy -= energyLoss;
     }
     else if (!isHome)
     {
         currentAction = CreatureAction.OutOfEnergy;
     }
 }
Ejemplo n.º 22
0
    protected virtual void Mate(Animal mate)
    {
        this.mate = mate;
        mate.mate = this;
        Coord meetingPoint = getMatingPoint(mate);

        CreatePath(meetingPoint);
        currentAction = CreatureAction.GoingToMate;
        mate.CreatePath(meetingPoint);
        mate.currentAction = CreatureAction.GoingToMate;
    }
Ejemplo n.º 23
0
 // This function moves the blob to the position of the object stored in nearestFood
 void MoveToFood()
 {
     if (nearestFood != null)
     {
         // Sets the velocity equal to a direction vector pointing to nearestFood, multiplied by speed
         rb.velocity = (nearestFood.transform.position - transform.position).normalized * speed;
     }
     else
     {
         currentAction = CreatureAction.Exploring;
     }
 }
        public static void Postfix(Creature __instance, ref CreatureAction __result)
        {
            string guid = GuidHelper.GetGuid(__instance.gameObject);

            if (Multiplayer.Logic.SimulationOwnership.HasOwnership(guid))
            {
                if (previousAction != __result)
                {
                    // Multiplayer.Logic.AI.CreatureActionChanged(guid, __result);
                }
            }
        }
        public static void Postfix(Creature __instance, ref CreatureAction __result)
        {
            NitroxId id = NitroxIdentifier.GetId(__instance.gameObject);

            if (NitroxServiceLocator.LocateService <SimulationOwnership>().HasAnyLockType(id))
            {
                if (previousAction != __result)
                {
                    // Multiplayer.Logic.AI.CreatureActionChanged(id, __result);
                }
            }
        }
Ejemplo n.º 26
0
        protected void Act()
        {
            switch (currentAction)
            {
            case CreatureAction.None:
                break;

            case CreatureAction.Exploring:
                StartMoveToCoord(Environments.Environment.GetNextTileWeighted(coord, moveFromCoord));
                break;

            case CreatureAction.GoingToFood:
                if (Coord.AreNeighbours(coord, foodTarget.coord))
                {
                    LookAt(foodTarget.coord);
                    currentAction = CreatureAction.Eating;
                }
                else
                {
                    //StartMoveToCoord (EnvironmentUtility.GetNextInPath (coord.x, coord.y, foodTarget.coord.x, foodTarget.coord.y), true);
                    StartMoveToCoord(path[pathIndex], true);
                    pathIndex++;
                }

                break;

            case CreatureAction.GoingToWater:
                if (Coord.AreNeighbours(coord, waterTarget))
                {
                    LookAt(waterTarget);
                    currentAction = CreatureAction.Drinking;
                }
                else
                {
                    StartMoveToCoord(path[pathIndex], true);
                    pathIndex++;
                }
                break;

            case CreatureAction.Resting:
                break;

            case CreatureAction.Eating:
                break;

            case CreatureAction.Drinking:
                break;

            default:
                Debug.LogException(new ArgumentOutOfRangeException());
                break;
            }
        }
Ejemplo n.º 27
0
 // This function makes the blob take the shortest route home
 void GoHome()
 {
     if (energy > 0)
     {
         rb.velocity = (closestPoint - transform.position).normalized * speed;
         isHome      = true;
         Debug.DrawLine(closestPoint, transform.position, Color.red);
     }
     else
     {
         currentAction = CreatureAction.OutOfEnergy;
     }
 }
        public static bool Prefix(Creature __instance, ref CreatureAction __result)
        {
            string guid = GuidHelper.GetGuid(__instance.gameObject);

            if (Multiplayer.Logic.SimulationOwnership.HasOwnership(guid))
            {
                previousAction = (CreatureAction)typeof(Creature).GetField("prevBestAction", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance);
                return(true);
            }

            // CreatureActionChangedProcessor.ActionByGuid.TryGetValue(guid, out __result);

            return(false);
        }
        public static bool Prefix(Creature __instance, ref CreatureAction __result)
        {
            NitroxId id = NitroxIdentifier.GetId(__instance.gameObject);

            if (NitroxServiceLocator.LocateService <SimulationOwnership>().HasAnyLockType(id))
            {
                previousAction = (CreatureAction)typeof(Creature).GetField("prevBestAction", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance);
                return(true);
            }

            // CreatureActionChangedProcessor.ActionById.TryGetValue(id, out __result);

            return(false);
        }
        public static bool Prefix(Creature __instance, ref CreatureAction __result)
        {
            NitroxId id = NitroxEntity.GetId(__instance.gameObject);

            if (NitroxServiceLocator.LocateService <SimulationOwnership>().HasAnyLockType(id))
            {
                previousAction = __instance.prevBestAction;
                return(true);
            }

            // CreatureActionChangedProcessor.ActionById.TryGetValue(id, out __result);

            return(false);
        }
Ejemplo n.º 31
0
    protected virtual void FindPotentialMates()
    {
        List <Animal> potentialMates = Environment.SensePotentialMates(coord, this);

        if (potentialMates.Count > 0)
        {
            currentAction = CreatureAction.SearchingForMate;
            mateTarget    = potentialMates[UnityEngine.Random.Range(0, potentialMates.Count)];
            CreatePath(mateTarget.coord);
        }
        else
        {
            currentAction = CreatureAction.Exploring;
        }
    }
Ejemplo n.º 32
0
    protected virtual void FindWater()
    {
        Coord waterTile = Environment.SenseWater(coord);

        if (waterTile != Coord.invalid)
        {
            currentAction = CreatureAction.GoingToWater;
            waterTarget   = waterTile;
            CreatePath(waterTarget);
        }
        else
        {
            currentAction = CreatureAction.Exploring;
        }
    }
Ejemplo n.º 33
0
    protected virtual void FindFood()
    {
        LivingEntity foodSource = Environment.SenseFood(coord, this, FoodPreferencePenalty);

        if (foodSource)
        {
            currentAction = CreatureAction.GoingToFood;
            foodTarget    = foodSource;
            CreatePath(foodTarget.coord);
        }
        else
        {
            currentAction = CreatureAction.Exploring;
        }
    }