// mendapatkan index tempat ground yang sedang aktif, diaktifkan paling awal, dan memiliki prefab id prefabId, pada pool yang sesuai public int GetIndexLastActive(GroundModel.ID prefabId) { int prefabIndex = GetPrefabIndex(prefabId); if (app.model.groundModel.indexFirstActive [prefabIndex] == GroundModel.INDEX_INACTIVE) { return(GroundModel.INDEX_INACTIVE); } int result = app.model.groundModel.indexNextActive [prefabIndex] - 1; if (result < 0) { result = app.model.groundModel.pool [prefabIndex].Count - 1; } return(result); }
// menonaktifkan ground pertama yang aktif dari pool untuk prefab dengan ID prefabId public void DeactivateFromPool(GroundModel.ID prefabId) { int prefabIndex = GetPrefabIndex(prefabId); if (app.model.groundModel.indexFirstActive [prefabIndex] != GroundModel.INDEX_INACTIVE) { DeactivateGround(app.model.groundModel.pool [prefabIndex][app.model.groundModel.indexFirstActive [prefabIndex]]); app.model.groundModel.indexFirstActive [prefabIndex]++; if (app.model.groundModel.indexFirstActive [prefabIndex] >= app.model.groundModel.pool [prefabIndex].Count) { app.model.groundModel.indexFirstActive [prefabIndex] = 0; } if (app.model.groundModel.indexFirstActive [prefabIndex] == app.model.groundModel.indexNextActive [prefabIndex]) { app.model.groundModel.indexFirstActive [prefabIndex] = GroundModel.INDEX_INACTIVE; } } }
void Update() { // menggerakkan tanah for (int prefabIndex = 0; prefabIndex < app.model.groundModel.prefabs.Length; prefabIndex++) { int indexNow = app.model.groundModel.indexFirstActive [prefabIndex]; if (indexNow != GroundModel.INDEX_INACTIVE) { int indexBorder = app.model.groundModel.indexNextActive [prefabIndex]; float xTranslation = -app.model.groundModel.baseGroundSpeed * Time.deltaTime; do { app.model.groundModel.pool[prefabIndex][indexNow].GetComponent <Rigidbody2D>().transform.Translate( xTranslation, 0.0f, 0.0f ); indexNow++; if (indexNow >= app.model.groundModel.pool[prefabIndex].Count) { indexNow = 0; } } while (indexNow != indexBorder); } } // menyembunyikan tanah yang melewati batas kiri for (int prefabIndex = 0; prefabIndex < app.model.groundModel.prefabs.Length; prefabIndex++) { GroundModel.ID prefabId = app.model.groundModel.prefabIds [prefabIndex]; if (GetIndexFirstActive(prefabId) != GroundModel.INDEX_INACTIVE) { GameObject leftmostGround = app.model.groundModel.pool [prefabIndex] [GetIndexFirstActive(prefabId)]; while (leftmostGround.transform.position.x <= app.model.groundModel.xLeft) { DeactivateFromPool(prefabId); leftmostGround = app.model.groundModel.pool [prefabIndex] [GetIndexFirstActive(prefabId)]; } } } GameObject rightmostGround = app.model.groundModel.pool [GetPrefabIndex(GroundModel.ID.GROUND_MIDDLE)] [GetIndexLastActive(GroundModel.ID.GROUND_MIDDLE)]; // menempatkan tanah baru jika tanah paling kanan melewati batas kanan /* * if (app.controller.backgroundController.area == false) { * rightmostGround = app.model.groundModel.pool [GetPrefabIndex (GroundModel.ID.GROUND_MIDDLE)] [GetIndexLastActive (GroundModel.ID.GROUND_MIDDLE)]; * } else { * rightmostGround = app.model.groundModel.pool [GetPrefabIndex (GroundModel.ID.GROUND_LAVA)] [GetIndexLastActive (GroundModel.ID.GROUND_LAVA)]; * }*/ while ((rightmostGround.transform.position.x <= app.model.groundModel.xRight)) { //if (app.controller.backgroundController.area == false) { Debug.Log(app.controller.backgroundController.area); ActivateFromPool(GroundModel.ID.GROUND_MIDDLE); float xNext = rightmostGround.transform.position.x + app.model.groundModel.boundSize.x; rightmostGround = app.model.groundModel.pool [GetPrefabIndex(GroundModel.ID.GROUND_MIDDLE)] [GetIndexLastActive(GroundModel.ID.GROUND_MIDDLE)]; rightmostGround.transform.position = new Vector3( xNext, app.model.groundModel.yLast, rightmostGround.transform.position.z ); if (app.controller.backgroundController.area) { rightmostGround.GetComponent <SpriteRenderer> ().sprite = sprite2; } else { rightmostGround.GetComponent <SpriteRenderer> ().sprite = sprite1; } //} } }
// mendapatkan index tempat ground yang sedang aktif, diaktifkan paling awal, dan memiliki prefab id prefabId, pada pool yang sesuai public int GetIndexFirstActive(GroundModel.ID prefabId) { int prefabIndex = GetPrefabIndex(prefabId); return(app.model.groundModel.indexFirstActive [prefabIndex]); }
// mendapatkan index tempat prefab ber-id prefabId pada array of prefab public int GetPrefabIndex(GroundModel.ID prefabId) { return(app.model.groundModel.prefabIdToPrefabIndex [(int)prefabId]); }