Пример #1
0
        public void execute()
        {
            if (!valid || body == null)
            {
                return;
            }
            Unit info = body.GetComponent <Unit>();

            if (info == null)
            {
                return;
            }
            else if (!info.canaction)
            {
                return;
            }

            bool complete = false;

            switch (type)
            {
            case "move4direc":
            {
                if (!started)
                {
                    if (time <= 0)
                    {
                        complete = true;
                        break;
                    }

                    if (slist.Count < 1)
                    {
                        if (i == null)
                        {
                            complete = true;
                            break;
                        }
                        else if (i.Length < 1)
                        {
                            complete = true;
                            break;
                        }

                        slist.Add(body.inttodirec(i[0]));
                    }
                }


                if (info.movereq(slist[0], time))
                {
                    Debug.Log("move4direc action completed(Unit.movereq is called and returned true)");
                    complete = true;
                }
                else
                {
                }
            }
            break;



            case "movedest":
            {
                if (!started)
                {
                    if (f.Length < 3)        // x,y,이동할 거리(실제 목적지까지의 거리를 초과할 수 없음)
                    {
                        complete = true;
                        break;
                    }
                    if (f[2] < 0)
                    {
                        f[0] = -f[0];
                    }

                    f    = new float[] { f[0], f[1], f[2], f[0] - body.gameObject.transform.position.x, f[1] - body.gameObject.transform.position.y, info.speed / 50 };      //x,y,dx-x,dy-y,speed, 50번 이동해야 speed*1만큼 이동하는듯
                    f[2] = Mathf.Min(f[2], Mathf.Abs(f[3] + f[4]));
                }

                if (f[2] <= f[5])
                {
                    complete = true;
                    Debug.Log("movedest end(distance completed)");
                    break;
                }

                if (info.moving)
                {
                    complete = false;
                    break;
                }


                if (f[5] <= 0)
                {
                    complete = true;
                    break;
                }
                else if (Mathf.Abs(f[3]) < f[5] && Mathf.Abs(f[4]) < f[5])
                {
                    complete = true;
                    break;
                }


                bool ishor = UnityEngine.Random.Range(0, 2) == 0;
                if (ishor)
                {
                    ishor = Mathf.Abs(f[3]) >= f[5];
                }
                else
                {
                    ishor = Mathf.Abs(f[4]) < f[5];
                }


                int dbuf, tbuf;
                if (ishor)
                {
                    dbuf = f[3] > 0 ? body.stringtointdirec("right") : body.stringtointdirec("left");
                    tbuf = Mathf.Min(Mathf.Abs((int)(f[3] / f[5])), (int)(f[2] / f[5]));
                }
                else
                {
                    dbuf = f[4] > 0 ? body.stringtointdirec("up") : body.stringtointdirec("down");
                    tbuf = Mathf.Min(Mathf.Abs((int)(f[4] / f[5])), (int)(f[2] / f[5]));
                }

                int[] ibuf = new int[] { dbuf };
                body.add("move4direc", tbuf, ibuf, null);
                f[2]       -= tbuf * f[5];   //f[5] is deltaspeed, distance must be decreased by moving distance from f[2]( planned distance).
                body.pushed = true;
            }
            break;



            case "moverandom":     // movedest를 사용, float로 거리를받아서 그만큼 랜덤한 곳에 떨어진 곳으로 movedest push 후  컴플리트
            {
                if (f.Length < 1)
                {
                    complete = true;
                    break;
                }

                float x = body.transform.position.x, y = body.transform.position.y, distance = f[0], speed = info.speed;
                float mposx, mposy;

                if (speed <= 0)
                {
                    complete = true;
                    break;
                }


                float angle = UnityEngine.Random.Range(0, Mathf.PI * 2);
                mposx = x + Mathf.Cos(angle) * distance;
                mposy = y + Mathf.Sin(angle) * distance;

                body.add("movedest", 1, null, new float[] { mposx, mposy, distance });
                body.pushed = true;
                complete    = true;
            }
            break;

            case "useweapon":
            {
                if (i == null || f == null)
                {
                    complete = true;
                    break;
                }
                else if (i.Length < 1 || f.Length < 2)
                {
                    complete = true;
                    break;
                }

                weapon[] wps   = body.GetComponents <weapon>();
                int      index = i[0];
                if (index < 0 || index >= wps.Length)
                {
                    complete = true;
                    break;
                }

                weapon wp = wps[index];
                if (wp.able())
                {
                    wp.start(f[0], f[1]);
                    complete = true;
                }
                else
                {
                    complete = false;
                }
            }
            break;


            case "wait":
            {
                if (i == null)
                {
                    complete = true;
                    break;
                }
                if (i[0]-- <= 0)
                {
                    complete = true;
                }
            }
            break;

            case "stop":
            {
                body.reset = true;
                complete   = true;
            }
            break;

            case "startunitbuild":
            {
                if (i == null)
                {
                    complete = true;
                    break;
                }
                else if (i.Count() < 1)
                {
                    complete = true;
                    break;
                }

                if (!info.canaction)
                {
                    complete = false;
                    break;
                }

                Unitbuilder builder = body.GetComponent <Unitbuilder>();
                if (builder == null)
                {
                    complete = true;
                    break;
                }

                if (builder.getstarted(i[0]))
                {
                    complete = true;
                    break;
                }
            }
            break;

            default:
                complete = true;
                break;
            }

            if (!started)
            {
                started = true;
            }



            if (complete)
            {
                reset();
                return;
            }
        }