private void SetCurrentMatrix(Vector3 mousePos) { Vector2 result = Vector2.zero; bool xHit = false; bool yHit = false; for (int i = 0; i < AxisManager.XAxis.Count; i++) { if (AxisManager.GetXAxis(i) - 1.5f <= mousePos.x && mousePos.x <= AxisManager.GetXAxis(i) + 1.5f) { xHit = true; result.x = i; break; } } for (int j = 0; j < AxisManager.YAxis.Count; j++) { if (AxisManager.GetYAxis(j) - 2.9f <= mousePos.y && mousePos.y <= AxisManager.GetYAxis(j) + 0.1f) { yHit = true; result.y = j; break; } } if (xHit && yHit) { currentMatrix = result; positionSelected = true; } }
void UpdatePlanting() { Vector3 mousePos = gameCam.ScreenToWorldPoint(Input.mousePosition); if (positionSelected && Input.GetMouseButtonDown(0) && planting == true && plantingType != PlantType.NotDefined) { if (sun >= ManagerDefine.GetSunNeedByPlantType(plantingType)) { string prefabName = ""; switch (plantingType) { case PlantType.Peashooter: prefabName = "Peashooter"; break; case PlantType.Sunflower: prefabName = "SunFlower"; break; case PlantType.SnowPeashooter: prefabName = "SnowPeashooter"; break; case PlantType.WallNut: prefabName = "WallNut"; break; default: prefabName = "SunFlower"; break; } Instantiate(Resources.Load(prefabName), new Vector3(AxisManager.GetXAxis((int)currentMatrix.x), AxisManager.GetYAxis((int)currentMatrix.y), AxisManager.GetZAxis((int)currentMatrix.y)), Quaternion.identity); sun -= ManagerDefine.GetSunNeedByPlantType(plantingType); planting = false; plantingType = PlantType.NotDefined; positionSelected = false; CurrentPlantCard.CanBeSelected = false; Destroy(plantingSprite.gameObject); } } else if (Input.GetMouseButtonDown(1) && planting == true && plantingType != PlantType.NotDefined) { Destroy(plantingSprite.gameObject); planting = false; plantingType = PlantType.NotDefined; positionSelected = false; } else { if (planting == true && plantingType != PlantType.NotDefined) { // Moving the plant if (plantingSprite != null) { SetCurrentMatrix(mousePos); plantingSprite.transform.position = new Vector3(AxisManager.GetXAxis((int)currentMatrix.x), AxisManager.GetYAxis((int)currentMatrix.y) - 1.4f, AxisManager.GetZAxis((int)currentMatrix.y)); } else { plantingSprite = Instantiate(Resources.Load("PlantSprite", typeof(tk2dSprite)), new Vector3(mousePos.x, mousePos.y, 6.0f), Quaternion.identity) as tk2dSprite; plantingSprite.SetSprite(ManagerDefine.GetSpriteNameByPlantType(plantingType)); } } } }