Beispiel #1
0
        public override void Update(GameManager game, float delta)
        {
            foreach (int entity in _entityList)
            {
                GAstarComponent astarComponent = game.Entities.GetComponentOf <GAstarComponent>(entity);
                GTransform      transform      = game.Entities.GetComponentOf <GTransform>(entity);
                ActionQueue     _actionQueue   = game.Entities.GetComponentOf <ActionQueue>(entity);
                Vector3         current        = astarComponent._current;
                bool            wait           = false;
                //Debug.Log("Astar value "  + astarComponent._path.ElementAt(astarComponent._index).Value);
                if (checkCollision(transform, current))
                {
                    if (!astarComponent._path.ElementAt(astarComponent._index).Value)
                    {
                        astarComponent._index++;
                        astarComponent._current = astarComponent._path.ElementAt(astarComponent._index).Key;
                    }
                }
                if (checkFullCollision(transform, current) && astarComponent._path.ElementAt(astarComponent._index).Value&& game.Entities.GetComponentOf <GMovement>(entity)._grounded)
                {
                    JumpAction a = JumpAction.Make();
                    a.Apply(game, transform.EntityID);
                    a.Recycle();
                    astarComponent._index++;
                    astarComponent._current = astarComponent._path.ElementAt(astarComponent._index).Key;
                }

                if (Mathf.Abs(transform._position.x - current.x) > 0.1f)
                {
                    MoveAction a = MoveAction.Make(current.x - transform._position.x);
                    a.Apply(game, transform.EntityID);
                    a.Recycle();
                }
                if (Mathf.Abs(transform._position.z - current.z) > 0.1f)
                {
                    //Debug.Log("Move Z " + Mathf.Sign(current.x - transform._position.x));
                    MoveZAction a = MoveZAction.Make(current.z - transform._position.z);
                    a.Apply(game, transform.EntityID);
                    a.Recycle();
                }
            }
        }
Beispiel #2
0
    private void createAI(GameManager game)
    {
        for (int i = 0; i < 1; i++)
        {
            Entity ent = new Entity();
            game.Entities.addEntity(ent);
            ent.AddComponent(GTransform.Make(ent.ID, Vector3.zero, new Vector2(0.4f, 1f)));
            ent.AddComponent(GRawInput.Make(ent.ID));
            ent.AddComponent(ActionQueue.Make(ent.ID));
            ent.AddComponent(GCollisionMask.Make(ent.ID));
            ent.AddComponent(GMovement.Make(ent.ID));
            ent.AddComponent(GAstarComponent.Make(ent.ID));
            ent.AddComponent(GBoxCollider2D.Make(ent.ID, new Vector2(0.2f, 0.5f),
                                                 new Vector2(0, -0.1f), new Vector2(0.2f, 0.5f)));

            GameObject clone = Instantiate(trans, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
            entityDic.Add(ent.ID, clone.transform);
            game.Systems.AddEntity(1, ent.ID);
            game.Systems.AddEntity(2, ent.ID);
            game.Systems.AddEntity(3, ent.ID);
        }
    }
Beispiel #3
0
 public override void Update(GameManager game, float delta)
 {
     foreach (int entity in _entityList)
     {
         GAstarComponent astarComponent = game.Entities.GetComponentOf <GAstarComponent>(entity);
         GTransform      transform      = game.Entities.GetComponentOf <GTransform>(entity);
         Node            endNode        = NodeDic[astarComponent._goal];
         if (astarComponent._active)
         {
             openList.Enqueue(NodeDic[new Vector2((int)transform._position.x, (int)-transform._position.z)], 0);
         }
         while (astarComponent._active)
         {
             Node current = openList.Dequeue();
             for (int i = 0; i < 8; i++)
             {
                 if (current.neighbours[i].value > 0)
                 {
                     if (NodeDic.ContainsKey(new Vector2(current.neighbours[i].position.x, current.neighbours[i].position.z)))
                     {
                         Node child = NodeDic[new Vector2(current.neighbours[i].position.x, current.neighbours[i].position.z)];
                         if (child == endNode)
                         {
                             astarComponent._active = false;
                             child.jump             = current.neighbours[i].jump;
                             child.parent           = current;
                             current = endNode;
                             break;
                         }
                         if (!openList.Contains(child) && !closedList.Contains(child))
                         {
                             gameObjs[new Vector2(current.position.x, current.position.z)].renderer.material.color = Color.red;
                             float dst = Vector3.Distance(child.position, endNode.position);
                             child.jump   = current.neighbours[i].jump;
                             child.parent = current;
                             child.value  = child.parent.value + current.neighbours[i].value;
                             openList.Enqueue(child, dst + child.value);
                         }
                     }
                     else
                     {
                         Debug.Log("Did not exist " + new Vector2(current.neighbours[i].position.x, current.neighbours[i].position.z));
                     }
                 }
             }
             closedList.Add(current);
             if (!astarComponent._active)
             {
                 while (current.parent != null)
                 {
                     finalList.Add(current.position, current.jump);
                     gameObjs[new Vector2(current.position.x, current.position.z)].transform.localScale   *= 4;
                     gameObjs[new Vector2(current.position.x, current.position.z)].renderer.material.color = Color.yellow;
                     current = current.parent;
                     //astarComponent._path.Add(current.position, current.jump);
                 }
                 //Sätt ihop dessa loopar
                 astarComponent._current = finalList.Last().Key;
                 for (int i = finalList.Count - 1; i >= 0; i--)
                 {
                     //finalList[finalList.ElementAt(i - 1).Key] = finalList.ElementAt(i).Value;
                     if (finalList.ElementAt(i).Value)
                     {
                         astarComponent._path.Add(finalList.ElementAt(i).Key, false);
                         astarComponent._path[finalList.ElementAt(i + 1).Key] = true;
                         Debug.Log("Fix Jump " + finalList.ElementAt(i + 1).Key);
                     }
                     else
                     {
                         astarComponent._path.Add(finalList.ElementAt(i).Key, finalList.ElementAt(i).Value);
                     }
                 }
                 astarComponent._index = 0;
                 finalList.Clear();
                 closedList.Clear();
                 openList.Clear();
             }
         }
     }
 }