Example #1
0
    // -----------------------------------------------------------------------------------------------------------------
    private void Update()
    {
        // Remove TILE tag from level
        m_level_tags.ClearTag((int)CTile.ETag.T_TAIL);

        // Reset path search
        m_path.ResetOffsetSearch();

        float   offset      = m_segment_len + m_config.segment_pad;
        Vector3 offset_pos  = new Vector3();
        bool    eol_reached = false;

        for (LinkedListNode <GameObject> it = m_segments.First; it != null; it = it.Next)
        {
            // Search for next segment offset position
            if (!eol_reached)
            {
                eol_reached = !m_path.SearchNextOffset(offset, ref offset_pos);
            }

            // Update segment according to offset
            GameObject segment = it.Value;
            if (!eol_reached)
            {
                offset += (m_segment_len + m_config.segment_pad);
                segment.transform.position = offset_pos;

                CTile t = m_level.GetTileByCoord(offset_pos.x, offset_pos.z);
                m_level_tags.SetTag(t, (int)CTile.ETag.T_TAIL);


                // Ignore segment if EOL is reached
            }
            else
            {
                segment.transform.position = new Vector3();
            }
        }

        // Increase path if EOL was reached
        if (eol_reached)
        {
            m_path.IncreasePath();
        }
    }
Example #2
0
    // -----------------------------------------------------------------------------------------------------------------
    private void Update()
    {
        // Turn right
        if (Input.GetKeyDown("right"))
        {
            m_direction = CTile.DIR_EX[(int)m_direction].m_next;

            // Turn left
        }
        else if (Input.GetKeyDown("left"))
        {
            m_direction = CTile.DIR_EX[(int)m_direction].m_prev;
        }

        // Current player position & underlying tile
        Vector3 cur_pos  = m_player.transform.position;
        CTile   cur_tile = m_level.GetTileByCoord(cur_pos.x, cur_pos.z);

        // Update compas
        m_compas.SetPosition(cur_pos);

        // Active tile is changed
        if (cur_tile != m_prev_frame_tile && m_prev_frame_tile != null)
        {
            // Update current and previously visited tiles
            m_prev_tile = m_prev_frame_tile;
            m_level_tags.SetTag(m_prev_tile, (int)CTile.ETag.T_VISITED);
            m_level_tags.SetTag(cur_tile, (int)CTile.ETag.T_CURRENT);

            // Update destination path
            m_level_tags.ClearTag((int)CTile.ETag.T_DEST_PATH);
            CTile t_glow = cur_tile;
            while (true)
            {
                t_glow = t_glow.GetNeighbor(m_direction);
                if (t_glow == null ||
                    !m_level_tags.SetTag(t_glow, (int)CTile.ETag.T_DEST_PATH))
                {
                    break;
                }
            }

            // Update trail - finalize border vertex between tiles
            Vector3 tile_border = m_prev_frame_tile.GetPosition() + (cur_tile.GetPosition() - m_prev_frame_tile.GetPosition()) / 2;
            tile_border.y += 0.5f;
            m_path.AddVertex(tile_border, true);

            // Active tile was not changed
        }
        else
        {
            // Update trail - update head vertex
            m_path.AddVertex(cur_pos, false);
        }

        // Destination tile is reached
        CTile.ERelPosition rel_pos = cur_tile.GetRelPosition(m_dest_tile, m_direction);
        if (rel_pos == CTile.ERelPosition.RP_CURRENT ||
            rel_pos == CTile.ERelPosition.RP_BEHIND ||
            rel_pos == CTile.ERelPosition.RP_NONE)
        {
            m_direction       = SetPlayerDestination(cur_tile, m_direction);
            m_prev_frame_tile = cur_tile;
        }

        // Save tile of previous frame
        m_prev_frame_tile = cur_tile;

        // Eat apple
        if (m_level_tags.IsTagSet(cur_tile, (int)CTile.ETag.T_APPLE))
        {
            m_level_tags.UnsetTag(cur_tile, (int)CTile.ETag.T_APPLE);
            m_tail.AddSegment();
            m_config.score++;
        }

        // Create new apple
        if (m_level_tags.GetTagCnt((int)CTile.ETag.T_APPLE) == 0)
        {
            CTile t = CreateApple();
            m_compas.SetDestination(t.GetPosition());
        }

        // Apply level changeset
        Hashtable changeset = m_level_tags.GetChangeset();

        if (changeset.Count > 0)
        {
            // Update all tiles in changeset
            foreach (DictionaryEntry entry in changeset)
            {
                CTile tile = (CTile)entry.Key;
                UpdateLevelMaterial(tile);
                UpdateLevelGameplay(tile);
            }

            // Clear changeset
            changeset.Clear();
        }
    }