コード例 #1
0
    public void onControllerEvent(IsoUnity.ControllerEventArgs args)
    {
        // # Avoid responding controller event when inactive
        if (!active)
        {
            return;
        }

        // # Normal threatment
        // Multiple controller events only give one launch result per tick
        if (toLaunch == null)
        {
            if (args.cell != null)
            {
                var cell = args.cell;
                if (args.cell.VirtualNeighbors.Count > 0)
                {
                    cell = args.cell.VirtualNeighbors[0].Destination;
                }



                GameEvent ge = new GameEvent();
                ge.setParameter("mover", this.Entity.mover);
                ge.setParameter("cell", cell);
                ge.Name = "teleport";
                Game.main.enqueueEvent(ge);
            }
            // Otherwise, the controller event should contain keys pressed
            else
            {
                int to = -1;
                if (args.LEFT)
                {
                    to = 0;
                }
                else if (args.UP)
                {
                    to = 1;
                }
                else if (args.RIGHT)
                {
                    to = 2;
                }
                else if (args.DOWN)
                {
                    to = 3;
                }

                if (to > -1)
                {
                    if (movement == null || !movingArrow)
                    {
                        if (Entity == null)
                        {
                            Debug.Log("Null!");
                        }
                        IsoUnity.Cell destination = Entity.Position.Map.getNeightbours(Entity.Position)[to];
                        // Can move to checks if the entity can DIRECT move to this cells.
                        // This should solve bug #29
                        IsoUnity.Entities.Mover em = this.Entity.GetComponent <IsoUnity.Entities.Mover>();
                        if (em != null && em.CanMoveTo(destination))
                        {
                            IsoUnity.GameEvent ge = new IsoUnity.GameEvent();
                            ge.setParameter("mover", this.Entity.mover);
                            ge.setParameter("cell", destination);
                            ge.setParameter("synchronous", true);
                            ge.Name = "move";
                            IsoUnity.Game.main.enqueueEvent(ge);
                            movement = ge;
                        }
                        else
                        {
                            IsoUnity.GameEvent ge = new IsoUnity.GameEvent();
                            ge.setParameter("mover", this.Entity.mover);
                            ge.setParameter("direction", fromIndex(to));
                            ge.setParameter("synchronous", true);
                            ge.Name = "turn";
                            IsoUnity.Game.main.enqueueEvent(ge);
                            movement = ge;
                        }
                        movingArrow = true;
                    }
                }
            }
        }
    }
コード例 #2
0
 public static bool hasRoute(Mover mover)
 {
     return(routes.ContainsKey(mover));
 }
コード例 #3
0
 public static bool planifyRoute(Mover mover, Cell destination)
 {
     return(planifyRoute(mover, destination, 0));
 }
コード例 #4
0
        private static Stack <Cell> calculateRoute(Cell from, Cell to, Mover mover, int distance)
        {
            Dictionary <Cell, int> cellToPos = new Dictionary <Cell, int>();

            IPriorityQueueHandle <CellDistance> handle  = null;
            IPriorityQueue <CellDistance>       abierta = new IntervalHeap <CellDistance>();

            Dictionary <Cell, bool>  cerrada  = new Dictionary <Cell, bool>();
            Dictionary <Cell, float> f        = new Dictionary <Cell, float>();
            Dictionary <Cell, float> g        = new Dictionary <Cell, float>();
            Dictionary <Cell, Cell>  anterior = new Dictionary <Cell, Cell>();
            Dictionary <Cell, IPriorityQueueHandle <CellDistance> > handles = new Dictionary <Cell, IPriorityQueueHandle <CellDistance> >();

            Cell posInicial = from;

            g[posInicial]        = 0;
            f[posInicial]        = estimaMeta(from, to);
            anterior[posInicial] = null;
            abierta.Add(ref handle, new CellDistance(posInicial, f[posInicial]));
            handles.Add(posInicial, handle);

            List <Cell> ends = GetSurroundCellsAtRadius(to, distance);

            while (!abierta.IsEmpty)
            {
                CellDistance candidata      = abierta.DeleteMin();
                Cell         celdaCandidata = candidata.cell;
                handles.Remove(celdaCandidata);

                if (ends.Contains(celdaCandidata))
                {
                    Stack <Cell> ruta = new Stack <Cell>();
                    reconstruyeCamino(ruta, celdaCandidata, anterior);
                    return(ruta);
                }

                Cell[] accesibles = getCeldasAccesibles(celdaCandidata, mover);
                cerrada[celdaCandidata] = true;
                foreach (Cell accesible in accesibles)
                {
                    float posibleG = g[celdaCandidata] + estimaAvance(celdaCandidata, accesible);
                    float posibleF = posibleG + estimaMeta(accesible, to);

                    if (cerrada.ContainsKey(accesible) && cerrada[accesible] && posibleF >= f[accesible])
                    {
                        continue;
                    }

                    if (!handles.ContainsKey(accesible) || posibleF < f[accesible])
                    {
                        anterior[accesible] = celdaCandidata;
                        g[accesible]        = posibleG;
                        f[accesible]        = posibleF;

                        // Modifica inserta si no esta dentro
                        var cd = new CellDistance(accesible, f[accesible]);
                        if (handles.ContainsKey(accesible))
                        {
                            abierta[handles[accesible]] = cd;
                        }
                        else
                        {
                            handle = null;
                            abierta.Add(ref handle, cd);
                            handles.Add(accesible, handle);
                        }
                    }
                }
            }

            return(null);
        }
コード例 #5
0
        public void onControllerEvent(ControllerEventArgs args)
        {
            // # Avoid responding controller event when inactive
            if (!active)
            {
                return;
            }

            // # Normal threatment
            // Multiple controller events only give one launch result per tick
            if (toLaunch == null)
            {
                // If options received (from entities)
                if (args.options != null)
                {
                    // If there's only one, proceed to launch
                    if (args.options.Length == 1)
                    {
                        // The action is the event we have to launch,
                        // so in order to know who's launching, we put a mark
                        if (args.options[0].Action != null)
                        {
                            args.options[0].Action.setParameter("Executer", this.Entity);
                        }

                        actionCell   = args.cell;
                        actionEntity = args.entity;

                        // If we've to move to perform the action
                        if (args.options[0].HasToMove)
                        {
                            GameEvent ge = new GameEvent();
                            ge.setParameter("mover", this.Entity.mover);
                            ge.setParameter("follow", args.entity);
                            ge.setParameter("distance", args.options[0].Distance);
                            ge.setParameter("synchronous", true);
                            ge.Name     = "follow";
                            movement    = ge;
                            movingArrow = false;
                            Game.main.enqueueEvent(ge);

                            // Here we've launched the movement. As it's synchronous, we'll receive
                            // the movement finished when the Mover considers it's done.
                        }

                        toLaunch = args.options[0].Action;
                    }
                    // If there're multiple actions we have to display a menu so player can choose
                    else if (args.options.Length > 1)
                    {
                        OptionsGUI gui = ScriptableObject.CreateInstance <OptionsGUI>();
                        gui.init(args, Camera.main.WorldToScreenPoint(args.entity.transform.position), args.options);
                        GUIManager.addGUI(gui, 100);
                    }
                }
                // If the argument doesn't contain options but it has a cell, we'll try to move over there
                else if (args.cell != null)
                {
                    var cell = args.cell;
                    if (args.cell.VirtualNeighbors.Count > 0)
                    {
                        cell = args.cell.VirtualNeighbors[0].Destination;
                    }

                    GameEvent ge = new GameEvent();
                    ge.setParameter("mover", this.Entity.mover);
                    ge.setParameter("cell", cell);
                    ge.Name = "move";
                    Game.main.enqueueEvent(ge);
                }
                // Otherwise, the controller event should contain keys pressed
                else
                {
                    int to = -1;
                    if (args.LEFT)
                    {
                        to = 0;
                    }
                    else if (args.UP)
                    {
                        to = 1;
                    }
                    else if (args.RIGHT)
                    {
                        to = 2;
                    }
                    else if (args.DOWN)
                    {
                        to = 3;
                    }

                    if (to > -1)
                    {
                        if (movement == null || !movingArrow)
                        {
                            if (Entity == null)
                            {
                                Debug.Log("Null!");
                            }
                            Cell destination = Entity.Position.Map.getNeightbours(Entity.Position)[to];
                            // Can move to checks if the entity can DIRECT move to this cells.
                            // This should solve bug #29
                            Mover em = this.Entity.GetComponent <Mover>();
                            if (em != null && em.CanMoveTo(destination))
                            {
                                GameEvent ge = new GameEvent();
                                ge.setParameter("mover", this.Entity.mover);
                                ge.setParameter("cell", destination);
                                ge.setParameter("synchronous", true);
                                ge.Name = "move";
                                Game.main.enqueueEvent(ge);
                                movement = ge;
                            }
                            else
                            {
                                GameEvent ge = new GameEvent();
                                ge.setParameter("mover", this.Entity.mover);
                                ge.setParameter("direction", fromIndex(to));
                                ge.setParameter("synchronous", true);
                                ge.Name = "turn";
                                Game.main.enqueueEvent(ge);
                                movement = ge;
                            }
                            movingArrow = true;
                        }
                    }
                }
            }
        }