コード例 #1
0
ファイル: Rulebook.cs プロジェクト: MoyTW/SpaceDodgeRL
        private static bool ResolveDestroy(DestroyAction action, EncounterState state)
        {
            Entity entity = state.GetEntityById(action.ActorId);

            var onDeathComponent = entity.GetComponent <OnDeathComponent>();
            // this 'shouldRemoveEntity' code is slightly confusing, simplify it if you come back to it
            bool shouldRemoveEntity = true;

            if (onDeathComponent != null)
            {
                foreach (var effectType in onDeathComponent.ActiveEffectTypes)
                {
                    var effectStopsRemoval = !ResolveOnDeathEffect(effectType, state);
                    if (effectStopsRemoval)
                    {
                        shouldRemoveEntity = false;
                    }
                }
            }

            if (shouldRemoveEntity)
            {
                state.RemoveEntity(entity);
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #2
0
ファイル: Rulebook.cs プロジェクト: MoyTW/SpaceDodgeRL
        // Currently, each use effect is its own component. If we run into a case where we have too many effects, we can push the
        // effects into the usable component itself, similarly to status effects (though status effects are their own mess right now)
        // which would probably be better for building on.
        private static bool ResolveUse(UseAction action, EncounterState state)
        {
            var user = state.GetEntityById(action.ActorId);

            // This is another issue that'd be solved with a global Entity lookup - though not the removal part.
            Entity usable = null;

            if (action.FromInventory)
            {
                var userInventory = user.GetComponent <InventoryComponent>();
                usable = userInventory.StoredEntityById(action.UsableId);
                if (usable.GetComponent <UsableComponent>() == null)
                {
                    state.LogMessage(string.Format("{0} is not usable!", usable.EntityName), failed: true);
                    return(false);
                }
                else
                {
                    userInventory.RemoveEntity(usable);
                }
            }
            else
            {
                usable = state.GetEntityById(action.UsableId);
                if (usable.GetComponent <UsableComponent>() == null)
                {
                    state.LogMessage(string.Format("{0} is not usable!", usable.EntityName), failed: true);
                    return(false);
                }
                else
                {
                    state.RemoveEntity(usable);
                }
            }

            state.LogMessage(string.Format("{0} used {1}!", user.EntityName, usable.EntityName));

            ResolveUseEffects(user, usable, state);

            // We assume all items are single-use; this will change if I deviate from the reference implementation!
            usable.QueueFree();
            return(true);
        }
コード例 #3
0
ファイル: Rulebook.cs プロジェクト: MoyTW/SpaceDodgeRL
        private static bool ResolveGetItem(GetItemAction action, EncounterState state)
        {
            var actor = state.GetEntityById(action.ActorId);
            var inventoryComponent = actor.GetComponent <InventoryComponent>();
            var actorPosition      = actor.GetComponent <PositionComponent>().EncounterPosition;
            var item = state.EntitiesAtPosition(actorPosition.X, actorPosition.Y)
                       .FirstOrDefault(e => e.GetComponent <StorableComponent>() != null);

            if (item == null)
            {
                state.LogMessage("No item found!", failed: true);
                return(false);
            }
            else if (item.GetComponent <UsableComponent>() != null && item.GetComponent <UsableComponent>().UseOnGet)
            {
                // The responsibility for removing/not removing the usable from the EncounterState is in the usage code.
                bool successfulUsage = ResolveUse(new UseAction(actor.EntityId, item.EntityId, false), state);
                if (!successfulUsage)
                {
                    GD.PrintErr(string.Format("Item {0} was not successfully used after being picked up!", item.EntityName));
                }
                return(true);
            }
            else if (!inventoryComponent.CanFit(item))
            {
                state.LogMessage(string.Format("[b]{0}[/b] can't fit the [b]{1}[/b] in its inventory!",
                                               actor.EntityName, item.EntityName), failed: true);
                return(false);
            }
            else
            {
                state.RemoveEntity(item);
                actor.GetComponent <InventoryComponent>().AddEntity(item);

                var logMessage = string.Format("[b]{0}[/b] has taken the [b]{1}[/b]", actor.EntityName, item.EntityName);
                state.LogMessage(logMessage);
                return(true);
            }
        }