Esempio n. 1
0
        public static AddUnitAction GetAddUnitAction(IUnitAction state)
        {
            AddUnitAction aus = new AddUnitAction();

            aus.TargetUnitTag = -1;
            if (state.GetType() == typeof(RotateAction))
            {
                aus.Rotate = (RotateAction)state;
            }
            else
            {
                aus.Rotate = null;
            }
            if (state.GetType() == typeof(MoveAction))
            {
                aus.Move = (MoveAction)state;
            }
            else
            {
                aus.Move = null;
            }
            if (state.GetType() == typeof(AttackAction))
            {
                aus.Attack = (AttackAction)state;
                if (aus.Attack.TargetUnit != null)
                {
                    aus.TargetUnitTag = aus.Attack.TargetUnit.Tag;
                }
                else
                {
                    aus.TargetUnitTag = -1;
                }
            }
            else
            {
                aus.Attack = null;
            }
            if (state.GetType() == typeof(DeathAction))
            {
                aus.Death = (DeathAction)state;
            }
            else
            {
                aus.Death = null;
            }
            return(aus);
        }
Esempio n. 2
0
        public bool Process()
        {
            if (!MapLogic.Instance.IsLoaded)
            {
                return(false);
            }

            MapUnit unit = MapLogic.Instance.GetUnitByTag(Tag);

            if (unit == null)
            {
                Debug.LogFormat("Attempted to add state for nonexistent unit {0}", Tag);
            }
            else
            {
                // we can't expect ideal sync here.
                // for this reason we don't just "add" state.
                // we put it exactly where it was on server's side at the moment.
                // ..except death which ends everything
                int pPos = Math.Min(Position, unit.Actions.Count);
                // reverse iteration
                AddUnitAction[] States = new AddUnitAction[2] {
                    State1, State2
                };
                for (int i = States.Length - 1; i >= 0; i--)
                {
                    if (States[i] == null)
                    {
                        continue;
                    }

                    if (States[i].Rotate != null)
                    {
                        States[i].Rotate.Unit = unit;
                        unit.Actions.Insert(pPos, States[i].Rotate);
                    }

                    if (States[i].Move != null)
                    {
                        States[i].Move.Unit = unit;
                        unit.Actions.Insert(pPos, States[i].Move);
                    }

                    if (States[i].Attack != null)
                    {
                        States[i].Attack.Unit = unit;
                        MapUnit targetUnit = MapLogic.Instance.GetUnitByTag(States[i].TargetUnitTag);
                        States[i].Attack.TargetUnit = targetUnit;
                        States[i].Attack.Spell      = unit.GetSpell((Spell.Spells)States[i].Attack.SpellID);
                        if (States[i].Attack.Spell == null && States[i].Attack.TargetUnit == null)
                        {
                            return(false); // bad packet
                        }
                        unit.Actions.Insert(pPos, States[i].Attack);
                    }

                    if (States[i].Death != null)
                    {
                        States[i].Death.Unit = unit;
                        unit.Actions.RemoveRange(1, unit.Actions.Count - 1);
                        unit.Actions.Add(States[i].Death);
                    }
                }
            }

            return(true);
        }