Exemple #1
0
    public void CreatePathDebug(TerrainManager TerrainManager_, ja2.TerrainTile From, ja2.TerrainTile To)
    {
        if (actualTask != null)
        {
            actualTask.Stop();
        }

        actualTask = new utils.Task(CreatePathDebug_CoRo(TerrainManager_, From, To));
    }
Exemple #2
0
    //! Create A* path.
    public ja2.TerrainTile[] CreatePath(TerrainManager TerrainManager_, ja2.TerrainTile From, ja2.TerrainTile To)
    {
        lastPath = new ja2.AStarPathMap(TerrainManager_, From, To);
        while (lastPath.RunOnce() == ja2.AStarPathMap.State.WAIT)
        {
            ;
        }

        return(lastPath.Path());
    }
Exemple #3
0
    //! Get tile position for given tile.
    public Vector3 GetPosition(ja2.TerrainTile Tile, short Vertex)
    {
        // Get the partition used
        int partition_x = Tile.x / ja2.TerrainPartition.PARTITION_WIDTH;
        int partition_y = Tile.y / ja2.TerrainPartition.PARTITION_HEIGHT;
        // Compute normalized terrain tile position
        int normalized_x = Tile.x - ja2.TerrainPartition.PARTITION_WIDTH * partition_x;
        int normalized_y = Tile.y - ja2.TerrainPartition.PARTITION_HEIGHT * partition_y;

        GameObject terrain_go = GameObject.Find(PARTITION_NAME + partition_x + "_" + partition_y);

        return(terrain_go.transform.TransformPoint(terrain_go.GetComponent <Terrain>().GetTilePosition(normalized_x, normalized_y, Vertex)));
    }
Exemple #4
0
    public bool CheckDirection(Direction Dir, float Ratio)
    {
        bool    ret = false;
        Vector3 point_to_check;

        if (Dir == Direction.LEFT || Dir == Direction.BOTTOM)
        {
            point_to_check = new Vector3(0, 0, 0);
        }
        else
        {
            point_to_check = new Vector3(1, 1, 0);
        }
        // Find if we are beyond the terrain
        Ray   ray = Camera.main.ViewportPointToRay(point_to_check);
        float point;

        new Plane(Vector3.up, 0).Raycast(ray, out point);
        Vector3 point_on_plane  = ray.GetPoint(point);
        var     terrain_manager = GameObject.Find("Map").GetComponent <TerrainManager>();

        ja2.Map         map             = terrain_manager.map;
        ja2.TerrainTile last_tile       = map.GetTile(map.width - 1, map.height - 1);
        Vector3         last_tile_pos_1 = terrain_manager.GetPosition(last_tile, 1);
        Vector3         last_tile_pos_2 = terrain_manager.GetPosition(last_tile, 2);

        float amount_ratio = amount * Ratio;

        // Check position
        switch (Dir)
        {
        case Direction.LEFT:
            ret = point_on_plane.z - amount_ratio > ja2.TerrainPartition.TILE_WIDTH;
            break;

        case Direction.TOP:
            ret = point_on_plane.x - amount_ratio > ja2.TerrainPartition.TILE_HEIGHT;
            break;

        case Direction.BOTTOM:
            ret = point_on_plane.x + amount_ratio < last_tile_pos_1.x;
            break;

        case Direction.RIGHT:
            ret = point_on_plane.z + amount_ratio < last_tile_pos_2.z;
            break;
        }

        return(ret);
    }
Exemple #5
0
    // Update is called once per frame
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out m_Hit, Mathf.Infinity, Terrain.LAYER_MASK))
        {
            // Find the tile based on triangles
            Terrain terrain = m_Hit.transform.gameObject.GetComponent <Terrain>();
            ja2.TerrainPartition.TriangleMap tile_x_y = terrain.GetTile(m_Hit.triangleIndex);
            tile = map.GetTile(tile_x_y.x, tile_x_y.y);

            Vector3 v0 = ja2.TerrainPartition.TileVertex(tile.x, tile.y, 0);
            Vector3 v1 = ja2.TerrainPartition.TileVertex(tile.x, tile.y, 1);
            transform.position = new Vector3(v1.x, 0, v0.z);
        }
    }
    public SoldierPathManager(SoldierController Soldier, ja2.TerrainTile[] Tiles)
    {
        ja2.TerrainTile previous = null;
        // Move distance
        ushort move_distance = 0;

        // Last direction
        ja2.Map.Direction last_direction = ja2.Map.Direction.NONE;
        // Create action controller
        soldierActions = new ja2.SoldierActionController(Soldier);
        // Actual vertex
        foreach (var tile in Tiles)
        {
            // Not first tile
            if (previous != null)
            {
                // Get actual direction
                ja2.Map.Direction dir = ja2.Map.GetDirection(previous, tile);
                // Direction change
                if (last_direction != dir)
                {
                    // Queue move first if needed
                    if (move_distance > 0)
                    {
                        var action = new ja2.SoldierActionMove(move_distance);
                        soldierActions.Add(action);
                        move_distance = 0;
                    }
                    // Set rotation
                    soldierActions.Add(new ja2.SoldierActionRotate(ja2.LookDirectionConverter.Convert(dir)));
                }
//				byte rot = (byte)ja2.LookDirectionConverter.Convert(dir);
                // Must be move
                ++move_distance;
                // Update last direction
                last_direction = dir;
            }
            // Update previous tile
            previous = tile;
        }
        // Queue move if was any
        if (move_distance != 0)
        {
            var action = new ja2.SoldierActionMove(move_distance);
            soldierActions.Add(action);
        }
    }
    //! Move as coroutine.
    public IEnumerator Move_Coro(ushort NumberOfTiles)
    {
        // Does the position of GO need to be clamped to center of tile
        bool clamp_position = false;
        // Save constraints
        RigidbodyConstraints rigid_body_constrains = rigidBody.constraints;

        // Compute target position
        ja2.TerrainTile target_tile = terrainManager.map.GetTile(mercenary.tile, LookDirToMoveDir(mercenary.lookDirection), NumberOfTiles);
        Vector3         target_pos  = terrainManager.GetPosition(target_tile);
        // Beginning position must actual transform position because we will
        // never be in the center of tile
        Vector3 beg_pos      = transform.position;
        Vector3 tile_beg_pos = terrainManager.GetPosition(mercenary.tile);

        // Set walk state
        animator.SetBool(walkParam, true);

        // Normal vector of target plane
        Vector3 target_normal_plane = (beg_pos - target_pos).normalized;

        while (true)
        {
            // Actual distance to targetm if < 0 we're beyond the target
            float distance_to_go = utils.Vector3Helper.DistanceSigned(transform.position, target_pos, target_normal_plane);
            // We're in the proximity of error
            if (distance_to_go <= MOVE_DIFF)
            {
#if JA_MERCENARY_CONTROLLER_PRINT_MOVE
                print("Stopping walk: " + distance_to_go);
#endif
                // No transition comes to play
                animator.SetBool(walkParam, false);
                yield return(new WaitForFixedUpdate());

                // Store actual distance to go
                float distance_to_go_pre = distance_to_go;
                while (true)
                {
                    distance_to_go = utils.Vector3Helper.DistanceSigned(transform.position, target_pos, target_normal_plane);
                    // We're in proximity of diff transition error or we're beyond the center of tile
                    if (distance_to_go < MOVE_DIFF_TRANSITION)
                    {
#if JA_MERCENARY_CONTROLLER_PRINT_MOVE
                        print("To go in transition: " + distance_to_go);
#endif
                        break;
                    }
                    // We're stalling in one place because transition isn't
                    // long enough, move a bit closer
                    if (animator.GetCurrentAnimatorStateInfo(0).nameHash == idleState)
                    {
                        Debug.LogWarning("Stall: " + distance_to_go_pre + ", " + distance_to_go);
                        rigidBody.MovePosition(target_pos);
                        break;
                    }
                    distance_to_go_pre = distance_to_go;
                    // Update tile position
                    UpdateTilePosition();
                    yield return(new WaitForFixedUpdate());
                }
                // Stop updating pos
                rigidBody.constraints |= RigidbodyConstraints.FreezePosition;
#if JA_MERCENARY_CONTROLLER_PRINT_MOVE
                print("Distance: " + utils.Vector3Helper.DistanceSigned(transform.position, target_pos, target_normal_plane));
#endif
                // If we're beyond and off at least MOVE_DIFF_TRANSITION, clamp
                // position to center
                clamp_position = (distance_to_go < 0);

                break;
            }
            // Compute angle change from offset; There will be always some
            // offset from center of tile, and therefor we need to always
            // direct the object toward the center because otherwise
            // the accumulated offset could cause that we will end up in between
            // tiles or other weird position
            transform.LookAt(new Vector3(target_pos.x, transform.position.y, target_pos.z));
            // Debug drawing of distance
            if (debugPathDraw)
            {
                Debug.DrawLine(tile_beg_pos, target_pos);
                Debug.DrawLine(transform.position, target_pos, Color.red);
            }
            // Update actual tile
            UpdateTilePosition();
            // Wait till next update
            yield return(new WaitForFixedUpdate());
        }
        // Must be in idle and no transition
        while (!(animator.GetCurrentAnimatorStateInfo(0).nameHash == idleState && !animator.IsInTransition(0)))
        {
#if JA_MERCENARY_CONTROLLER_PRINT_MOVE
            print("Waiting...");
#endif
            yield return(new WaitForFixedUpdate());
        }
#if JA_MERCENARY_CONTROLLER_PRINT_MOVE
        print("Off Distance: " + utils.Vector3Helper.DistanceSigned(transform.position, target_pos, target_normal_plane));
#endif
        // Need to adjust position of mercenary
        mercenary.tile = target_tile;
        // Need to update position
        if (clamp_position)
        {
#if JA_MERCENARY_CONTROLLER_PRINT_MOVE
            print("Clamping position.");
#endif
            UpdatePosition();
        }
        // Reset constraints
        rigidBody.constraints = rigid_body_constrains;
    }
Exemple #8
0
 public Soldier(TerrainTile Tile, CharacterGroup Group)
     : this()
 {
     tile  = Tile;
     group = Group;
 }
Exemple #9
0
    // Update is called once per frame
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out m_Hit, Mathf.Infinity, Terrain.LAYER_MASK))
        {
            // Find the tile based on triangles
            Terrain terrain = m_Hit.transform.gameObject.GetComponent<Terrain>();
            ja2.TerrainPartition.TriangleMap tile_x_y = terrain.GetTile(m_Hit.triangleIndex);
            tile = map.GetTile(tile_x_y.x, tile_x_y.y);

            Vector3 v0 = ja2.TerrainPartition.TileVertex(tile.x, tile.y, 0);
            Vector3 v1 = ja2.TerrainPartition.TileVertex(tile.x, tile.y, 1);
            transform.position = new Vector3(v1.x, 0, v0.z);
        }
    }
Exemple #10
0
        public static Direction GetDirection(TerrainTile From, TerrainTile To)
        {
            Direction dir = Direction.NONE;

            // Same y
            if (From.y == To.y)
            {
                if (From.x < To.x)
                {
                    dir = Direction.EAST;
                }
                else
                {
                    dir = Direction.WEST;
                }
            }
            else
            {
                if (From.y % 2 == 1)
                {
                    switch (From.y - To.y)
                    {
                    // Row up
                    case 1:
                        if (From.x == To.x)
                        {
                            dir = Direction.NORTH_WEST;
                        }
                        else if (From.x < To.x)
                        {
                            dir = Direction.NORTH_EAST;
                        }
                        break;

                    // Row down
                    case -1:
                        if (From.x == To.x)
                        {
                            dir = Direction.SOUTH_WEST;
                        }
                        else if (From.x < To.x)
                        {
                            dir = Direction.SOUTH_EAST;
                        }
                        break;

                    // 2 Row up
                    case 2:
                        if (From.x == To.x)
                        {
                            dir = Direction.NORTH;
                        }
                        break;

                    // 2 Row down
                    case -2:
                        if (From.x == To.x)
                        {
                            dir = Direction.SOUTH;
                        }
                        break;
                    }
                }
                else
                {
                    switch (From.y - To.y)
                    {
                    // Row up
                    case 1:
                        if (From.x == To.x)
                        {
                            dir = Direction.NORTH_EAST;
                        }
                        else if (From.x > To.x)
                        {
                            dir = Direction.NORTH_WEST;
                        }
                        break;

                    // Row down
                    case -1:
                        if (From.x == To.x)
                        {
                            dir = Direction.SOUTH_EAST;
                        }
                        else if (From.x > To.x)
                        {
                            dir = Direction.SOUTH_WEST;
                        }
                        break;

                    // 2 Row up
                    case 2:
                        if (From.x == To.x)
                        {
                            dir = Direction.NORTH;
                        }
                        break;

                    // 2 Row down
                    case -2:
                        if (From.x == To.x)
                        {
                            dir = Direction.SOUTH;
                        }
                        break;
                    }
                }
            }

            return(dir);
        }
Exemple #11
0
 //! Get position of center of tile.
 public Vector3 GetPosition(ja2.TerrainTile Tile)
 {
     return(new Vector3(GetPosition(Tile, 1).x, 0, GetPosition(Tile, 0).z));
 }
Exemple #12
0
    public System.Collections.IEnumerator CreatePathDebug_CoRo(TerrainManager TerrainManager_, ja2.TerrainTile From, ja2.TerrainTile To)
    {
        lastPath = new ja2.AStarPathMap(TerrainManager_, From, To);
        ja2.AStarPathMap.State state;
        do
        {
            yield return(new WaitForSeconds(time));

            state = lastPath.RunOnce();
        } while (state == ja2.AStarPathMap.State.WAIT);
    }
Exemple #13
0
 public TileKey(ja2.TerrainTile Tile)
 {
     tile = Tile;
 }
Exemple #14
0
    //! Create path visualization.
    public void CreatePath(ja2.TerrainTile[] Tiles)
    {
        // Mesh for path visualizer
        Mesh mesh = new Mesh();
        // Create arrays needed
        var vertices  = new Vector3[(Tiles.Length - 1) * 4];
        var uv1       = new Vector2[vertices.Length];
        var triangles = new int[(vertices.Length / 4) * 2 * 3];
        // Vertex iterator
        int vertex_iterator = vertices.Length - 4;

        ja2.TerrainTile previous = null;
        // Actual vertex
        foreach (var tile in Tiles)
        {
            // Actual vertex
            int actual_vertex = vertex_iterator;
            // Not first tile
            if (previous != null)
            {
                // Get actual direction
                ja2.Map.Direction dir = ja2.Map.GetDirection(previous, tile);
                // Set vertices
                vertices[actual_vertex++] = new Vector3(terrainManager.GetPosition(tile, 0).x, 0, terrainManager.GetPosition(tile, 0).z);
                vertices[actual_vertex++] = new Vector3(terrainManager.GetPosition(tile, 1).x, 0, terrainManager.GetPosition(tile, 1).z);
                vertices[actual_vertex++] = new Vector3(terrainManager.GetPosition(tile, 2).x, 0, terrainManager.GetPosition(tile, 2).z);
                vertices[actual_vertex++] = new Vector3(terrainManager.GetPosition(tile, 3).x, 0, terrainManager.GetPosition(tile, 3).z);

                // Hardcoded for now, need to change
                float texel_step = 0.125f;
                byte  rot        = (byte)ja2.LookDirectionConverter.Convert(dir);

                uv1[actual_vertex - 4] = new Vector2(texel_step * rot + texel_step / 2, 1);
                uv1[actual_vertex - 3] = new Vector2(texel_step * rot, 0.5f);
                uv1[actual_vertex - 2] = new Vector2(texel_step * rot + texel_step / 2, 0);
                uv1[actual_vertex - 1] = new Vector2(texel_step * rot + texel_step, 0.5f);

                int triangle_index = (((actual_vertex / 4) - 1) * 3 * 2);

                triangles[triangle_index++] = actual_vertex - 4;
                triangles[triangle_index++] = actual_vertex - 1;
                triangles[triangle_index++] = actual_vertex - 3;

                triangles[triangle_index++] = actual_vertex - 3;
                triangles[triangle_index++] = actual_vertex - 1;
                triangles[triangle_index++] = actual_vertex - 2;
                // Go backwards
                vertex_iterator -= 4;
            }
            // Update previous tile
            previous = tile;
        }
        // Visualize path
        mesh.vertices  = vertices;
        mesh.uv        = uv1;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();

        meshFilter.sharedMesh   = mesh;
        renderer.sharedMaterial = (Material)Resources.Load("Materials/Steps", typeof(Material));
    }