コード例 #1
0
ファイル: Organism.cs プロジェクト: morelli690/Terrarium
        /// <summary>
        ///  <para>
        ///   Use this function to command your creature to reproduce.  There are many
        ///   conditions on whether your creature can reproduce.  If these conditions
        ///   are not met, an exception will be thrown.  The easiest way to make
        ///   sure all pre-existing conditions have been met is to check the CanReproduce
        ///   property.
        ///  </para>
        ///  <para>
        ///   If you call this method multiple times in the same turn, then the last call
        ///   will be used, and all previous calls will be ignored.  This method is also
        ///   asynchronous, and a ReproduceCompletedEvent will be fired when your creature
        ///   has actually given birth.  The time between start and completion is 10 ticks.
        ///  </para>
        /// </summary>
        /// <param name="dna">
        ///  A byte array that gets passed to the child.  This can be any information you
        ///  want to pass to the child creature.  The byte array is truncated at 8000 bytes.
        /// </param>
        /// <exception cref="AlreadyReproducingException">
        ///  Thrown when your creature is already in the process of reproduction.
        /// </exception>
        /// <exception cref="NotMatureException">
        ///  Thrown when your creature is not yet mature and you try to reproduce.
        /// </exception>
        /// <exception cref="NotEnoughEnergyException">
        ///  Thrown when your creature does not have enough energy to start reproduction.
        /// </exception>
        /// <exception cref="NotReadyToReproduceException">
        ///  Thrown when your creature is not yet ready to reproduce because the appropriate number of ticks has not elapsed.
        /// </exception>
        public void BeginReproduction(byte[] dna)
        {
            if (IsReproducing)
            {
                throw new AlreadyReproducingException();
            }

            if (!State.IsMature)
            {
                throw new NotMatureException();
            }

            if (State.EnergyState < EnergyState.Normal)
            {
                throw new NotEnoughEnergyException();
            }

            if (!State.ReadyToReproduce)
            {
                throw new NotReadyToReproduceException();
            }

            var actionID = GetNextActionID();
            var action   = new ReproduceAction(ID, actionID, dna);

            lock (PendingActions)
            {
                PendingActions.SetReproduceAction(action);
                InProgressActions.SetReproduceAction(action);
            }
        }
コード例 #2
0
        /// <summary>
        ///  Helper function used to fire reproduction events.
        /// </summary>
        /// <param name="e">The reproduction event arguments.</param>
        /// <param name="clearOnly">Only clear the action, don't fire the event.</param>
        private void OnReproduceCompleted(ReproduceCompletedEventArgs e, bool clearOnly)
        {
            if (InProgressActions.ReproduceAction != null &&
                e.ActionID == InProgressActions.ReproduceAction.ActionID)
            {
                InProgressActions.SetReproduceAction(null);
            }

            if (!clearOnly && ReproduceCompleted != null)
            {
                ReproduceCompleted(this, e);
            }
        }
コード例 #3
0
ファイル: Plant.cs プロジェクト: IDisposable/terrarium-sdk
        /// <internal/>
        public override sealed void InternalMain(bool clearOnly)
        {
            OrganismEventResults events = State.OrganismEvents;

            // Call Initialize once
            if (!IsInitialized)
            {
                Initialize();
                IsInitialized = true;
            }

            if (events != null)
            {
                if (events.ReproduceCompleted != null)
                {
                    if (InProgressActions.ReproduceAction != null &&
                        events.ReproduceCompleted.ActionID == InProgressActions.ReproduceAction.ActionID)
                    {
                        InProgressActions.SetReproduceAction(null);
                    }
                }
            }

            if (CanReproduce)
            {
                BeginReproduction(null);
            }

            if (clearOnly)
            {
                InternalTurnsSkipped++;
            }
            else
            {
                InternalTurnsSkipped = 0;
            }
        }