Example #1
0
        public static void Main(string[] args)
        {
            var hero       = new Hero();
            var littleHeal = new HealCommand(10);
            var bigHeal    = new HealCommand(50);
            var dmg        = new DamageCommand(20);

            var queue = new Queue <ICommand>();

            queue.Enqueue(dmg);
            queue.Enqueue(littleHeal);
            queue.Enqueue(dmg);
            queue.Enqueue(bigHeal);

            foreach (var cmd in queue)
            {
                hero.ConsumeCommand(cmd);
                Console.WriteLine("Current health: {0}", hero.CurrentHealth());
            }

            /*
             * Taking 20 damage
             * Current health: 80
             * Healing by 10 hp
             * Current health: 90
             * Taking 20 damage
             * Current health: 70
             * Healing by 50 hp
             * Current health: 100
             */
        }
Example #2
0
    /// <summary>
    /// 데미지 커맨드 생성
    /// </summary>
    public static DamageCommand Create(DamageInfo info)
    {
        var cmd = new DamageCommand();

        cmd.damageInfo = info;

        return(cmd);
    }
    /// <summary>
    /// 충돌 및 데미지 이벤트 전달을 처리합니다.
    /// </summary>
    public void ProcessCollision()
    {
        Vector2 point   = owner.transform.position;
        Vector2 forward = owner.transform.right;

        var   hitInfos  = Physics2D.CircleCastAll(point, data.Radius, forward, data.CircleCastDistance, data.CollideLayerMask.value);
        float halfAngle = data.Angle * 0.5f;

        foreach (var hitInfo in hitInfos)
        {
            //범위(Angle) 내에서 충돌이 발생한 Collider만 충돌로 판정해야 한다.
            Vector2 dir   = ((Vector2)hitInfo.transform.position - point).normalized;
            float   angle = Vector2.Angle(forward, dir);

            bool withinRange = angle < halfAngle;


            if (withinRange)
            {
                //범위 내 Collider인 경우, 충돌 이벤트 처리.
                FieldObject target = hitInfo.collider.GetComponent <FieldObject>();
                if (target == null)
                {
                    target = hitInfo.collider.GetComponentInParent <FieldObject>();
                }

                if (target != null && !attackedEntityList.Contains(target))
                {
                    // 적이면 데미지 가함
                    if (owner.EntityGroup.IsHostileTo(target.EntityGroup))
                    {
                        var info = new DamageInfo
                        {
                            Sender = owner,
                            Target = target,
                            amount = data.Damage
                        };
                        var cmd = DamageCommand.Create(info);
                        cmd.Execute();

                        attackedEntityList.Add(target);
                    }
                }
            }
        }

        //GLDebug.DrawSector(new Circle(point, Radius, forward), Angle, DebugColor, 1f);
    }
Example #4
0
        private static void MakeDamage(Army sourceArmy, int?sourceUnitIndex,
                                       Army targetArmy, int targetUnitIndex, int damage)
        {
            if (damage <= 0 ||
                (sourceUnitIndex.HasValue && sourceArmy[sourceUnitIndex.Value].CurrentHealth <= 0))
            {
                return;
            }

            var target = targetArmy[targetUnitIndex];

            if (target.CurrentHealth <= 0)
            {
                return;
            }

            double CountDamage(int defense)
            {
                return(1.0 * damage * (100 - defense) / 100);
            }

            var resultDamage = (int)CountDamage(target.Defense);
            var dmgCommand   = new DamageCommand(sourceArmy, sourceUnitIndex, targetArmy, targetUnitIndex, resultDamage);

            CommandsInvoker.Execute(dmgCommand);

            if (target.CurrentHealth <= 0 || !(target is BuffedUnit buffUnit))
            {
                return;
            }

            var removeBuff = new RemoveBuffCommand(sourceArmy, sourceUnitIndex,
                                                   targetArmy, targetUnitIndex, Random.Next(buffUnit.BuffCount));

            CommandsInvoker.Execute(removeBuff);
        }
        public static void InitializeControllersForGameplay(Game1 game, KeyboardController keyboard, GamepadController gamepad)
        {
            keyboard.ClearDictionary();
            gamepad.ClearDictionary();
            //Background sound
            ICommand mutecommand = new MuteCommand(Stage.sound);

            keyboard.Add((int)Keys.M, mutecommand);
            //Add the Reset command to the r
            ICommand resetcommand = new ResetCommand();

            keyboard.Add((int)Keys.R, resetcommand);
            gamepad.Add((int)Buttons.Back, resetcommand);
            //Add the Exit Command to the controllers
            ICommand exitcommand = new ExitCommand(game);

            keyboard.Add((int)Keys.Q, exitcommand);
            gamepad.Add((int)Buttons.Start, exitcommand);
            //add the left command
            ICommand leftcommand = new LeftCommand(Stage.mario);

            keyboard.Add((int)Keys.A, leftcommand);
            keyboard.Add((int)Keys.Left, leftcommand);
            gamepad.Add((int)Buttons.DPadLeft, leftcommand);
            //add the right commmand
            ICommand rightcommand = new RightCommand(Stage.mario);

            keyboard.Add((int)Keys.D, rightcommand);
            keyboard.Add((int)Keys.Right, rightcommand);
            gamepad.Add((int)Buttons.DPadRight, rightcommand);
            //Add the srpint command
            ICommand sprintcommand = new SprintCommand(Stage.mario);

            keyboard.Add((int)Keys.LeftShift, sprintcommand);
            keyboard.Add((int)Keys.RightShift, sprintcommand);
            //add the down command
            ICommand downcommand = new DownCommand(Stage.mario);

            keyboard.Add((int)Keys.S, downcommand);
            keyboard.Add((int)Keys.Down, downcommand);
            gamepad.Add((int)Buttons.DPadDown, downcommand);
            //add the up command
            ICommand upcommand = new UpCommand(Stage.mario);

            keyboard.Add((int)Keys.W, upcommand);
            keyboard.Add((int)Keys.Up, upcommand);
            gamepad.Add((int)Buttons.DPadUp, upcommand);
            //add fireflower command
            ICommand firecommand = new FireMarioCommand(Stage.mario);

            keyboard.Add((int)Keys.I, firecommand);
            //add super command
            ICommand supercommand = new SuperCommand(Stage.mario);

            keyboard.Add((int)Keys.U, supercommand);
            //add normal mario command
            ICommand normalcommand = new NormalCommand(Stage.mario);

            keyboard.Add((int)Keys.Y, normalcommand);
            //add take damage command
            ICommand damagecommand = new DamageCommand(Stage.mario);

            keyboard.Add((int)Keys.O, damagecommand);
            //add star power command
            ICommand starcommand = new StarCommand(Stage.mario);

            keyboard.Add((int)Keys.L, starcommand);
            //add the fireball command with space
            ICommand fireballcommand = new MakeFireBall(Stage.mario);

            keyboard.Add((int)Keys.Space, fireballcommand);
            //add the Pause hud command with N
            ICommand pausehudcommand = new PauseCommand();

            keyboard.Add((int)Keys.P, pausehudcommand);
            ICommand interact = new InteractCommand();

            keyboard.Add((int)Keys.V, interact);
            gamepad.Add((int)Buttons.RightTrigger, interact);
            //add hold command E, no current mapping on gamepad
            ICommand holdcommand = new HoldingCommand(Stage.mario);

            keyboard.Add((int)Keys.E, holdcommand);


            /*
             * Add release commands. These are mainly for mario movement.
             */
            ICommand rightR = new ReleaseRightCommand(Stage.mario);

            keyboard.AddRelease((int)Keys.D, rightR);
            keyboard.AddRelease((int)Keys.Right, rightR);


            ICommand leftR = new ReleaseLeftCommand(Stage.mario);

            keyboard.AddRelease((int)Keys.A, leftR);
            keyboard.AddRelease((int)Keys.Left, leftR);

            ICommand downR = new ReleaseDownCommand(Stage.mario);

            keyboard.AddRelease((int)Keys.S, downR);
            keyboard.AddRelease((int)Keys.Down, downR);


            ICommand upR = new ReleaseUpCommand(Stage.mario);

            keyboard.AddRelease((int)Keys.W, upR);
            keyboard.AddRelease((int)Keys.Up, upR);

            ICommand sprintR = new ReleaseSprintCommand(Stage.mario);

            keyboard.AddRelease((int)Keys.LeftShift, sprintR);
            keyboard.AddRelease((int)Keys.RightShift, sprintR);

            ICommand holdR = new ReleaseHoldingCommand(Stage.mario);

            keyboard.AddRelease((int)Keys.E, holdR);
        }