Example #1
0
	//Successor
	public List<KeyValuePair<Vector2,State>> Successor(State state)
	{
		//Next states
		List<KeyValuePair<Vector2,State>> nextStates = new List<KeyValuePair<Vector2, State>> ();

        //Get all theoretically valid movements
        List<Vector2> totalMovs = new List<Vector2>() { Vector2.up, Vector2.right, Vector2.down, Vector2.left };
		//Create a list with valid movements
		List<Vector2> validMovs = new List<Vector2> ();

        //Get state position
        Vector2 actorPos = state.GetPosition();
		//Loop movements to validate using unity collisions
		foreach (Vector2 mov in totalMovs) {
            //If the movement is valid is moved to valid movements
            Vector2 newPos = state.GetPosition() + mov;
            int newX = Convert.ToInt32(newPos.x);
            int newY = Convert.ToInt32(newPos.y);
            if(newX >= 0 && newX < _matrix.GetLength(0) && newY >= 0 && newY < _matrix.GetLength(1))
            if (_matrix[newX, newY].overFloor != OverFloorType.Wall)
            {
                validMovs.Add(mov);
            }
		}

		//Loop valid movements to check result
		foreach (Vector2 validMov in validMovs) {

			//Set next character state
			Vector2 nextActorPos = actorPos + validMov;
			nextStates.Add(new KeyValuePair<Vector2,State>(validMov,new State(nextActorPos)));
		}

		return nextStates;
	}
Example #2
0
    internal void createReverseSnake(State state, int stateCount, int tails)
    {
        ReverseHead _reverseSnake = Instantiate(reverseSnake, state.GetPosition(), state.GetRotation());

        shadowSnakes++;
        _reverseSnake.Initialize(states, state, stateCount + 5 * snakehead.GetTails(), startTime, offset, tails, this);
    }
Example #3
0
    // Update is called once per frame
    void FixedUpdate()
    {
        currentState = states[stateCount];
        stateCount  += 2;

        lifeTime--;
        transform.position = currentState.GetPosition();
        transform.rotation = currentState.GetRotation();
        if (lifeTime == 0)
        {
            if (tail != null)
            {
                tail.FadeAll();
            }
            spriteHandler.FadeOut();
            if (notDestroyed)
            {
                notDestroyed = false;
                gh.DecrementShadows(true);
            }
            Destroy(gameObject, 2);
        }
        if (tailsToSpawn > 0)
        {
            Spawntail();
        }
        tailsToSpawn--;

        if (states.Count <= stateCount)
        {
            DemolishAll();
        }
    }
    bool Collision(List <Obstacle> obstacles, State s2, float radius, float length)      // disc collision
    {
        foreach (Obstacle obstacle in obstacles)
        {
            if (obstacle.Collision(position, s2.GetPosition(), radius, time, length, s2.GetJump()))
            {
                return(true);
            }
        }

        return(false);
    }
Example #5
0
    // Update is called once per frame
    void FixedUpdate()
    {
        currentState = states[offset];
        offset      += 2;

        /*
         * lifeTime--;
         * if (lifeTime == 0) {
         *  spriteHandler.FadeOut();
         *  Destroy(gameObject, 1);
         * }*/
        transform.position = currentState.GetPosition();
        transform.rotation = currentState.GetRotation();
    }
    public List <State> GetChildStates(State goal, List <AnimationClip> motions, List <float> mult, List <Obstacle> obstacles, float radius)
    {
        List <State> possible_states = new List <State>();
        Vector3      curr_pos        = new Vector3(position.x, 0, position.y);

        foreach (AnimationClip clip in motions)
        {
            float rotation = -180;
            while (rotation < 180)
            {
                float      new_euler   = theta + rotation;
                Quaternion newRotation = Quaternion.Euler(0, new_euler, 0);

                if (clip.name == "Walk_Fwd")
                {
                    foreach (float f in mult)
                    {
                        Vector3 child_pos = curr_pos + newRotation * (clip.length * clip.averageSpeed * f);
                        State   new_state = new State(new Vector2(child_pos.x, child_pos.z), new_euler, clip, clip.length + time)
                        {
                            p = f
                        };
                        new_state.SetParams(new Vector2(0, f));

                        if (!Collision(obstacles, new_state, radius, clip.length))
                        {
                            possible_states.Add(new_state);
                        }
                    }
                }

                else
                {
                    Vector3 child_pos = curr_pos + newRotation * (clip.length * clip.averageSpeed);
                    State   new_state = new State(new Vector2(child_pos.x, child_pos.z), new_euler, clip, clip.length + time);
                    if (clip.name == "Jog_Fwd")
                    {
                        new_state.SetParams(new Vector2(0, 2f));
                        new_state.weight = 1.5f;
                        if (Vector3.Magnitude(new_state.GetPosition() - goal.GetPosition()) <= 1.5f)
                        {
                            new_state.weight = 2f;
                        }
                    }
                    else if (motion_clip.name != "Idle_Ready")                        // jump
                    {
                        new_state.SetParams(new Vector2(0, 3f));
                        new_state.SetJump(true);
                        new_state.weight = 2.3f;
                        if (Vector3.Magnitude(new_state.GetPosition() - goal.GetPosition()) <= 1.5f)
                        {
                            new_state.weight = 3f;
                        }
                    }

                    if (!Collision(obstacles, new_state, radius, clip.length))
                    {
                        possible_states.Add(new_state);
                    }
                }

                rotation += 15;
            }
        }

        return(possible_states);
    }
 public bool Equal(State s)
 {
     return(position == s.GetPosition() && theta == s.GetOrientation());
 }
Example #8
0
 // Update is called once per frame
 void FixedUpdate()
 {
     currentState       = states[offset];
     transform.position = currentState.GetPosition();
     transform.rotation = currentState.GetRotation();
 }
Example #9
0
    List <State> FindPath()
    {
        State start = new State(new Vector2(startPos.position.x, startPos.position.z), startPos.rotation.eulerAngles.y, motions[1], 0);

        start.SetParams(new Vector2(0, 0));
        State goal = new State(new Vector2(goalPos.position.x, goalPos.position.z), 0, motions[1], 0);

        List <State> path = new List <State>();
        Dictionary <State, State>   came_from  = new Dictionary <State, State>();
        Dictionary <Vector3, State> closed_set = new Dictionary <Vector3, State>();

        PriorityQueue open_set = new PriorityQueue();

        open_set.Push(new Move(start, 0, Vector3.Magnitude(start.GetPosition() - goal.GetPosition())));

        bool reached_goal = false;
        int  iterations   = 0;

        while (!open_set.Empty() && iterations < 2000)
        {
            // get best state from queue
            Move m = open_set.Pop();
            closed_set[new Vector3(m.v.GetPosition().x, m.v.GetPosition().y, m.v.GetOrientation())] = m.v;              // add state to expanded state table

            if (debugMode)
            {
                debug.Add(new Vector3(m.v.GetPosition().x, m.v.GetOrientation(), m.v.GetPosition().y));
            }

            if (Vector3.Magnitude(m.v.GetPosition() - goal.GetPosition()) <= 0.5f)
            {
                Debug.Log("Goal reached");
                reached_goal = true;
                goal         = m.v;
                break;
            }

            // get possible states
            List <string>        allowable_names   = graph[m.v.GetParams().y];
            List <float>         mult              = multipliers[m.v.GetParams().y];
            List <AnimationClip> allowable_motions = new List <AnimationClip>();
            foreach (AnimationClip clip in motions)
            {
                if (allowable_names.Contains(clip.name))
                {
                    allowable_motions.Add(clip);
                }
            }

            List <State> child_states = m.v.GetChildStates(goal, allowable_motions, mult, obstacleObjects, radius);

            for (int i = 0; i < child_states.Count; ++i)
            {
                Vector2 new_pos   = child_states[i].GetPosition();
                float   new_theta = child_states[i].GetOrientation();
                //float clip_cost = Vector3.Magnitude(child_states[i].GetPosition() - m.v.GetPosition()); // does not let planning finish in time
                float curr_pathCost = m.pathCost + child_states[i].weight;

                int  v_id         = open_set.Get(child_states[i]);
                bool containsVert = v_id > -1 ? true : false;

                if (!closed_set.ContainsKey(new Vector3(new_pos.x, new_pos.y, new_theta)) && (!containsVert || curr_pathCost < open_set.GetMove(v_id).pathCost))
                {
                    float next_cost = curr_pathCost + Vector3.Magnitude(new_pos - goal.GetPosition());
                    Move  nextMove  = new Move(child_states[i], curr_pathCost, next_cost);

                    if (containsVert)
                    {
                        open_set.Remove(v_id);
                    }

                    open_set.Push(nextMove);
                    came_from[child_states[i]] = m.v;
                }
            }

            iterations++;
        }

        if (reached_goal)
        {
            while (goal != start)
            {
                // add state to path
                path.Add(goal);
                goal = came_from[goal];
            }

            path.Reverse();
            path.Add(new State(new Vector2(goalPos.position.x, goalPos.position.z), path[path.Count - 1].GetOrientation(), motions[1], 0));
            path[path.Count - 1].SetParams(Vector2.zero);

            // create cubes at each state
            for (int i = 0; i < path.Count; ++i)
            {
                GameObject test = GameObject.CreatePrimitive(PrimitiveType.Cube);
                test.transform.position   = new Vector3(path[i].GetPosition().x, 1, path[i].GetPosition().y);
                test.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
                test.transform.rotation   = Quaternion.Euler(0, path[i].GetOrientation(), 0);
                test.GetComponent <Renderer>().material.color = Color.red;
            }
        }

        Debug.Log("A* complete.");
        return(path);
    }
Example #10
0
	//Goal test
	public bool GoalTest(State state)
	{
        _guimanager.MarkThinkCell(state.GetPosition());
		return state.GetPosition() == _finalPosition;
	}