public override void Update(long gameTime, NamelessGame namelessGame)
        {
            while (namelessGame.Commander.DequeueCommand(out AttackCommand ac))
            {
                var random = new InternalRandom();

                var source = ac.getSource();
                var stats  = source.GetComponentOfType <Stats>();

                //TODO: attack damage based on stats, equipment etc.
                int damage = stats.Attack.Value;
                DamageHelper.ApplyDamage(ac.getTarget(), ac.getSource(), damage);

                Description targetDescription = ac.getTarget().GetComponentOfType <Description>();
                Description sourceDescription = ac.getSource().GetComponentOfType <Description>();
                if (targetDescription != null && sourceDescription != null)
                {
                    var logCommand = new HudLogMessageCommand();
                    namelessGame.Commander.EnqueueCommand(logCommand);

                    logCommand.LogMessage += (sourceDescription.Name + " deals " + (damage) +
                                              " damage to " + targetDescription.Name);
                    //namelessGame.WriteLineToConsole;
                }
            }
        }
Exemple #2
0
        public override void Update(long gameTime, NamelessGame namelessGame)
        {
            foreach (IEntity entity in RegisteredEntities)
            {
                InputComponent inputComponent = entity.GetComponentOfType <InputComponent>();
                if (inputComponent != null)
                {
                    var playerEntity = namelessGame.PlayerEntity;
                    foreach (Intent intent in inputComponent.Intents)
                    {
                        switch (intent.Intention)
                        {
                        case IntentEnum.MoveUp:
                        case IntentEnum.MoveDown:
                        case IntentEnum.MoveLeft:
                        case IntentEnum.MoveRight:
                        case IntentEnum.MoveTopLeft:
                        case IntentEnum.MoveTopRight:
                        case IntentEnum.MoveBottomLeft:
                        case IntentEnum.MoveBottomRight:
                        {
                            Position position     = playerEntity.GetComponentOfType <Position>();
                            var      actionPoints = playerEntity.GetComponentOfType <ActionPoints>();
                            if (position != null && actionPoints.Points >= 100)
                            {
                                int newX =
                                    intent.Intention == IntentEnum.MoveLeft || intent.Intention == IntentEnum.MoveBottomLeft ||
                                    intent.Intention == IntentEnum.MoveTopLeft ? position.Point.X - 1 :
                                    intent.Intention == IntentEnum.MoveRight || intent.Intention == IntentEnum.MoveBottomRight ||
                                    intent.Intention == IntentEnum.MoveTopRight ? position.Point.X + 1 :
                                    position.Point.X;
                                int newY =
                                    intent.Intention == IntentEnum.MoveDown || intent.Intention == IntentEnum.MoveBottomLeft ||
                                    intent.Intention == IntentEnum.MoveBottomRight ? position.Point.Y - 1 :
                                    intent.Intention == IntentEnum.MoveUp || intent.Intention == IntentEnum.MoveTopLeft ||
                                    intent.Intention == IntentEnum.MoveTopRight ? position.Point.Y + 1 :
                                    position.Point.Y;

                                IEntity        worldEntity   = namelessGame.TimelineEntity;
                                IWorldProvider worldProvider = null;
                                if (worldEntity != null)
                                {
                                    worldProvider = worldEntity.GetComponentOfType <TimeLine>().CurrentTimelineLayer.Chunks;
                                }

                                Tile tileToMoveTo = worldProvider.GetTile(newX, newY);


                                IEntity entityThatOccupiedTile = null;
                                foreach (IEntity tileEntity in tileToMoveTo.GetEntities())
                                {
                                    OccupiesTile occupiesTile =
                                        tileEntity.GetComponentOfType <OccupiesTile>();
                                    if (occupiesTile != null)
                                    {
                                        entityThatOccupiedTile = tileEntity;
                                        break;
                                    }
                                }


                                if (entityThatOccupiedTile != null)
                                {
                                    Door      door = entityThatOccupiedTile.GetComponentOfType <Door>();
                                    Character characterComponent =
                                        entityThatOccupiedTile.GetComponentOfType <Character>();
                                    if (door != null)
                                    {
                                        SimpleSwitch simpleSwitch =
                                            entityThatOccupiedTile.GetComponentOfType <SimpleSwitch>();
                                        if (simpleSwitch != null == simpleSwitch.isSwitchActive())
                                        {
                                            entityThatOccupiedTile.GetComponentOfType <Drawable>()
                                            .Representation = 'o';
                                            entityThatOccupiedTile.RemoveComponentOfType <BlocksVision>();
                                            entityThatOccupiedTile.RemoveComponentOfType <OccupiesTile>();

                                            namelessGame.Commander.EnqueueCommand(
                                                new ChangeSwitchStateCommand(simpleSwitch, false));
                                            var ap = playerEntity.GetComponentOfType <ActionPoints>();
                                            ap.Points -= Constants.ActionsMovementCost;
                                            //   playerEntity.RemoveComponentOfType<HasTurn>();
                                        }
                                        else
                                        {
                                            worldProvider.MoveEntity(playerEntity,
                                                                     new Point(newX, newY));
                                            var ap = playerEntity.GetComponentOfType <ActionPoints>();
                                            ap.Points -= Constants.ActionsMovementCost;
                                        }
                                    }

                                    if (characterComponent != null)
                                    {
                                        //TODO: if hostile
                                        namelessGame.Commander.EnqueueCommand(new AttackCommand(playerEntity,
                                                                                                entityThatOccupiedTile));

                                        var ap = playerEntity.GetComponentOfType <ActionPoints>();
                                        ap.Points -= Constants.ActionsAttackCost;
                                        // playerEntity.RemoveComponentOfType<HasTurn>();

                                        //TODO: do something else if friendly: chat, trade, etc
                                    }
                                }
                                else
                                {
                                    worldProvider.MoveEntity(playerEntity,
                                                             new Point(newX, newY));
                                    var ap = playerEntity.GetComponentOfType <ActionPoints>();
                                    ap.Points -= Constants.ActionsMovementCost;
                                }
                            }
                        }

                        break;

                        case IntentEnum.LookAtMode:
                            //InputReceiver receiver = new InputReceiver();
                            //Player player = entity.GetComponentOfType<Player>();
                            //cursor = entity.GetComponentOfType<Cursor>();
                            //entity.RemoveComponentOfType<InputReceiver>();
                            //if (player != null)
                            //{
                            //    IEntity cursorEntity = namelessGame.CursorEntity;
                            //    cursorEntity.AddComponent(receiver);
                            //    Drawable cursorDrawable = cursorEntity.GetComponentOfType<Drawable>();
                            //    cursorDrawable.setVisible(true);
                            //    Position cursorPosition = cursorEntity.GetComponentOfType<Position>();
                            //    Position playerPosition = entity.GetComponentOfType<Position>();
                            //    cursorPosition.p.X = (playerPosition.p.X);
                            //    cursorPosition.p.Y = (playerPosition.p.Y);

                            //}
                            //else if (cursor != null)
                            //{
                            //    IEntity playerEntity = namelessGame.PlayerEntity;
                            //    playerEntity.AddComponent(receiver);
                            //    Drawable cursorDrawable = entity.GetComponentOfType<Drawable>();
                            //    cursorDrawable.setVisible(false);

                            //}

                            break;

                        case IntentEnum.PickUpItem:
                        {
                            var actionPoints = playerEntity.GetComponentOfType <ActionPoints>();

                            if (actionPoints.Points >= 100)
                            {
                                IEntity        worldEntity   = namelessGame.TimelineEntity;
                                IWorldProvider worldProvider = null;
                                if (worldEntity != null)
                                {
                                    worldProvider = worldEntity.GetComponentOfType <TimeLine>().CurrentTimelineLayer
                                                    .Chunks;
                                }


                                var position   = playerEntity.GetComponentOfType <Position>();
                                var itemHolder = playerEntity.GetComponentOfType <ItemsHolder>();
                                var tile       = worldProvider.GetTile(position.Point.X, position.Point.Y);

                                List <IEntity> itemsToPickUp = new List <IEntity>();
                                foreach (var entityOnTIle in tile.GetEntities())
                                {
                                    var itemComponent = entityOnTIle.GetComponentOfType <Item>();
                                    if (itemComponent != null)
                                    {
                                        itemsToPickUp.Add(entityOnTIle);
                                    }
                                }

                                if (itemsToPickUp.Any())
                                {
                                    if (itemsToPickUp.Count > 1)
                                    {
                                        namelessGame.ContextToSwitch =
                                            ContextFactory.GetPickUpItemContext(namelessGame);
                                        UiFactory.PickUpItemsScreen.FillItems(namelessGame);
                                        if (UiFactory.PickUpItemsScreen.ItemsTable.Items.Any())
                                        {
                                            UiFactory.PickUpItemsScreen.ItemsTable.SelectedIndex = 0;
                                        }
                                    }
                                    else
                                    {
                                        StringBuilder builder      = new StringBuilder();
                                        var           itemsCommand = new PickUpItemCommand(itemsToPickUp, itemHolder,
                                                                                           position.Point);
                                        namelessGame.Commander.EnqueueCommand(itemsCommand);

                                        foreach (var entity1 in itemsToPickUp)
                                        {
                                            var desc = entity1.GetComponentOfType <Description>();
                                            if (desc != null)
                                            {
                                                builder.Append($"Picked up: {desc.Name} \n");
                                            }
                                        }

                                        var logCommand = new HudLogMessageCommand();
                                        logCommand.LogMessage += builder.ToString();
                                        namelessGame.Commander.EnqueueCommand(logCommand);
                                    }

                                    var ap = playerEntity.GetComponentOfType <ActionPoints>();
                                    ap.Points -= Constants.ActionsPickUpCost;
                                    //playerEntity.RemoveComponentOfType<HasTurn>();
                                }
                            }

                            break;
                        }

                        case IntentEnum.SkipTurn:
                        {
                            var actionPoints = playerEntity.GetComponentOfType <ActionPoints>();

                            if (actionPoints.Points >= 100)
                            {
                                var ap = entity.GetComponentOfType <ActionPoints>();
                                ap.Points -= Constants.ActionsMovementCost;
                                var logCommand = new HudLogMessageCommand();
                                logCommand.LogMessage += "Waiting";
                                namelessGame.Commander.EnqueueCommand(logCommand);

                                //   playerEntity.RemoveComponentOfType<HasTurn>();
                            }
                        }
                        break;

                        case IntentEnum.Quicksave:
                            namelessGame.ScheduleSave();
                            break;

                        case IntentEnum.Quickload:
                            namelessGame.ScheduleLoad();
                            break;

                        case IntentEnum.ZoomIn:
                            var zoomCommand = new ZoomCommand(false);
                            namelessGame.Commander.EnqueueCommand(zoomCommand);
                            break;

                        case IntentEnum.ZoomOut:
                            var zoomOutCommand = new ZoomCommand();
                            namelessGame.Commander.EnqueueCommand(zoomOutCommand);
                            break;

                        default:
                            break;
                        }
                    }

                    inputComponent.Intents.Clear();
                }
            }
        }