void BrushArea(int midX, int midY, float brushStrength, BrushTemplate brush) { // Safety Checks int startX = midX - brush.m_halfWidth; int startY = midY - brush.m_halfHeight; int pixWidth = brush.m_halfWidth * 2; int startOffset = startX + startY * m_TexRes; for (int i = 0; i < brush.m_strength.Length; i++) { // Get and Update Buffer int offsetIndex = startOffset + (i % pixWidth) + m_TexRes * (i / pixWidth); int x = startX + (i % pixWidth); int y = startY + (i / pixWidth); if ((x < 0) || (y < 0) || (x >= m_TexRes) || (y >= m_TexRes)) { // Do Nothing } else { if (m_damageMap[offsetIndex] < 255) { int digDataIndex = offsetIndex + m_dugDepth[offsetIndex] * m_TexRes * m_TexRes; int dmg = Mathf.FloorToInt(brush.m_strength[i] * brushStrength * 254) + (brush.m_strength[i] > 0 ? 1 : 0); if (m_damageMap[offsetIndex] <= dmg) { bool isDino = false; // Stats switch (m_digData[digDataIndex]) { case 1: m_stats.m_dirtDug += 1; break; case 2: m_stats.m_stoneDug += 1; break; case 10: isDino = true; m_dinoDestroyed += 1; m_stats.m_dinoDestroyed += 1; break; } // Destroy and Update m_digData[digDataIndex] = 0; while ((m_dugDepth[offsetIndex] > 0) && (m_digData[digDataIndex] == 0)) { m_dugDepth[offsetIndex] -= 1; digDataIndex = offsetIndex + m_dugDepth[offsetIndex] * m_TexRes * m_TexRes; } switch (m_digData[offsetIndex]) { case 1: m_damageMap[i] = 40; break; case 2: m_damageMap[i] = 150; break; case 10: m_damageMap[i] = 200; break; case 255: m_damageMap[i] = 255; break; } // Expose Dino if (!isDino && (m_digData[digDataIndex] == 10)) { m_dinoExposed += 1; m_stats.m_dinoExposed += 1; } } else { m_damageMap[offsetIndex] -= (byte)dmg; } } } } }
// Use this for initialization void Start() { m_IsSetup = false; /////////////////////////////////////////////////////////////// // Game Stats m_stats = new GameStats(); m_stats.m_gameMode = PlayerPrefs.GetInt("GameState gameMode"); m_stats.m_levelRes = m_TexRes; m_stats.m_levelDepth = m_MaxDepth; /////////////////////////////////////////////////////////////// m_timSinceBrush = 0.0f; // Get Component m_boxCollider = GetComponent <BoxCollider>(); // Brush m_brushID = 0; m_brushStrength = 0; m_brush = new BrushTemplate[m_brushTemplate.Length]; for (int i = 0; i < m_brushTemplate.Length; i++) { m_brush[i] = new BrushTemplate(); m_brush[i].SetBrush(m_brushTemplate[i]); } UpdateBrush(); // Setup Dig Texture m_dugDepth = new byte[m_TexRes * m_TexRes]; for (int i = 0; i < m_dugDepth.Length; i++) { m_dugDepth[i] = (byte)Mathf.Max(0, m_MaxDepth - Random.Range(1, 3)); } // Create Dig Data and fill with Dirt m_digData = new byte[m_TexRes * m_TexRes * m_MaxDepth]; for (int i = 0; i < m_digData.Length; i++) { m_digData[i] = 1; } /////////////////////////////////////////////////////////////// // Bedrock for (int x = 0; x < m_TexRes; x++) { for (int y = 0; y < m_TexRes; y++) { m_digData[x + y * m_TexRes + 0] = 255; } } for (int x = 0; x < m_TexRes; x++) { for (int z = m_dugDepth[x + 0 * m_TexRes]; z >= 0; --z) { m_digData[x + 0 * m_TexRes + z * m_TexRes * m_TexRes] = 255; } for (int z = m_dugDepth[x + (m_TexRes - 1) * m_TexRes]; z >= 0; --z) { m_digData[x + (m_TexRes - 1) * m_TexRes + z * m_TexRes * m_TexRes] = 255; } } for (int y = 0; y < m_TexRes; y++) { for (int z = m_dugDepth[0 + y * m_TexRes]; z >= 0; --z) { m_digData[0 + y * m_TexRes + z * m_TexRes * m_TexRes] = 255; } for (int z = m_dugDepth[(m_TexRes - 1) + y * m_TexRes]; z >= 0; --z) { m_digData[(m_TexRes - 1) + y * m_TexRes + z * m_TexRes * m_TexRes] = 255; } } //////////////////////////////////////////////////////////////////////// // Air for (int x = 0; x < m_TexRes; x++) { for (int y = 0; y < m_TexRes; y++) { for (int z = m_dugDepth[x + 0 * m_TexRes] + 1; z < m_MaxDepth; z++) { m_digData[x + y * m_TexRes + z * m_TexRes * m_TexRes] = 0; } } } /* * byte[] digDataCopy = new byte[m_digData.Length]; * for (int i = 0; i < m_digData.Length; i++) * digDataCopy [i] = m_digData [i]; */ //////////////////////////////////////////////////////////////////////// /// Placing Stones float stonePercentage = 0.05f; int stoneCount = Mathf.FloorToInt((float)(m_TexRes * m_TexRes * m_MaxDepth) * stonePercentage); while (stoneCount > 0) { Vector3 stoneMid = new Vector3( Random.value * (m_TexRes - 2) + 1, Random.value * (m_TexRes - 2) + 1, Random.value * (m_MaxDepth * 0.75f - 2) + 1); for (float fx = -3.5f; fx < +3.6f; fx += 1.0f) { for (float fy = -3.5f; fy < +3.6f; fy += 1.0f) { for (float fz = -2.5f; fz < +2.6f; fz += 1.0f) { if ((Mathf.Abs(fx) + Mathf.Abs(fy) + Mathf.Abs(fz)) > 4.5f) { continue; } Vector3 offset = new Vector3(fx, fy, fz); Vector3 fPt = stoneMid + offset; int x = Mathf.FloorToInt(fPt.x); int y = Mathf.FloorToInt(fPt.y); int z = Mathf.FloorToInt(fPt.z); if ((x < 0) || (y < 0) || (z < 0) || (x >= m_TexRes) || (y >= m_TexRes) || (z >= m_MaxDepth)) { continue; } if (m_digData[x + y * m_TexRes + z * m_TexRes * m_TexRes] == 1) { m_digData[x + y * m_TexRes + z * m_TexRes * m_TexRes] = 2; stoneCount -= 1; } stoneCount -= 1; // Safety Net } } } } //////////////////////////////////////////////////////////////////////// /// Placing Dino /// { // Dino Stats m_dinoSurf = 0; m_dinoBlock = 0; m_dinoDestroyed = 0; m_dinoExposed = 0; // Dino Params int randDino = Random.Range(0, m_dinoList.Length - 1); Texture2D dinoTex = m_dinoList[randDino]; bool flipX = (Random.value > 0.5); bool flipY = (Random.value > 0.5); int offX = Random.Range(0, m_TexRes - dinoTex.width - 1); int offY = Random.Range(0, m_TexRes - dinoTex.height - 1); int dinoDepth = Random.Range(5, Mathf.FloorToInt(m_MaxDepth * 0.7f)); // Place Dino in Dirt Color32[] colBuf = dinoTex.GetPixels32(); for (int x = 0; x < dinoTex.width; x++) { for (int y = 0; y < dinoTex.height; y++) { Color32 c = colBuf[x + y * dinoTex.width]; int dH = ((c.r + c.g + c.b) * 6) / (255 * 3); bool isSurf = false; for (int z = Mathf.Max(1, dinoDepth - dH); z < (dinoDepth + dH); z++) { int ax = flipX ? (offX + dinoTex.width - x - 1) : (offX + x); int ay = flipY ? (offY + dinoTex.height - y - 1) : (offY + y); isSurf = true; m_digData[ax + ay * m_TexRes + dinoDepth * m_TexRes * m_TexRes] = 10; m_dinoBlock += 1; } if (isSurf) { m_dinoSurf += 1; } } } // Stats m_stats.m_dinoNumber = randDino; m_stats.m_dinoFlippedX = flipX; m_stats.m_dinoFlippedY = flipY; m_stats.m_dinoX = offX; m_stats.m_dinoY = offY; m_stats.m_dinoZ = dinoDepth; m_stats.m_dinoSurfArea = m_dinoSurf; m_stats.m_dinoBlocks = m_dinoBlock; } ////////////////////////////////////////////////////// // Setup Damage Tex m_damageMap = new byte[m_TexRes * m_TexRes]; for (int i = 0; i < m_damageMap.Length; i++) { m_damageMap[i] = 50; } ////////////////////////////////////////////////////// /// Gui Setup GUIStyle gs = new GUIStyle(); gs.border = new RectOffset(2, 2, 2, 2); gs.font = m_guiFont; gs.fontSize = 20; gs.alignment = TextAnchor.MiddleCenter; gs.normal.background = m_buttonBackTex; gs.normal.textColor = Color.grey; gs.active.background = m_buttonBackTexActive; gs.active.textColor = Color.black; m_buttonStyle = gs; ////////////////////////////////////////////////////// /// Startup m_xRayOn = false; m_gTrack = GameObject.FindGameObjectWithTag("GALog").GetComponent <GoogleTracker>(); m_gTrack.PostEvent("Level", "Start", "Dino", m_stats.m_dinoNumber); m_IsSetup = true; }