public void OnSectionSelect(ColorableSectionInstance colorableInstance) { if (Input.GetMouseButton(MouseButton.Left)) { IsFinishedSelecting = true; SelectedColorableSectionInstance = colorableInstance; } }
private void DoRaycast() { RaycastHit2D[] hits; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); hits = Physics2D.GetRayIntersectionAll(ray, 100.0f); Debug.Log(hits.Length); if (hits.Length > 0) { for (int i = 0; i < hits.Length; i++) { Collider2D collider = hits[i].collider; // First check that the collider has not been selected/painted yet ColorableSectionInstance colorableSection = collider.GetComponentInParent <ColorableSectionInstance>(); if (colorableSection == null) { Debug.Log("No colorable section."); continue; } if (!colorableSection.Colored) { SpriteRenderer spriteRenderer = collider.GetComponent <SpriteRenderer>(); if (spriteRenderer == null) { Debug.Log("No sprite renderer."); continue; } Texture2D currentTexture = spriteRenderer.sprite.texture; // Get current color Vector2 uv; uv.x = (hits[i].point.x - hits[i].collider.bounds.min.x) / hits[i].collider.bounds.size.x; uv.y = (hits[i].point.y - hits[i].collider.bounds.min.y) / hits[i].collider.bounds.size.y; uv.x *= currentTexture.width; uv.y *= currentTexture.height; Color currentColor = currentTexture.GetPixel((int)(uv.x), (int)(uv.y)); if (currentColor.r == 0 && currentColor.g == 0 && currentColor.b == 0) { OnSectionSelect(colorableSection); } //Debug.Log(uv); //Debug.Log(currentColor); } } } }
private void UpdateWhenPickingSection() { if (timer.IsNoTimeLeft) { sectionSelector.StopSelecting(); currentState = GameState.TimeIsUp; return; } if (sectionSelector.IsFinishedSelecting) { currentlySelectedSection = sectionSelector.SelectedColorableSectionInstance; currentState = GameState.PickingColor; } }
private int getHits() { int hits = 0; for (int i = 0; i < SectionHolders.Length; i++) { ColorableSectionInstance colorableSectionInstance = SectionHolders[i]; Color finalColor = colorableSectionInstance.FinalColor; Color selectedColor = colorableSectionInstance.SelectedColor; if (finalColor.r == selectedColor.r && finalColor.g == selectedColor.g && finalColor.b == selectedColor.b) { hits++; } else { // Maybe later } } return(hits); }
private void InitializeSections(ColorableInstance colorableInstance, GameObject colorableObject) { int depth = 10; Sprite[] sectionColorLayers = GetSectionColorLayers(colorableInstance.Colorable); GameObject[] layerObjects = new GameObject[sectionColorLayers.Length]; Colorable colorable = colorableInstance.Colorable; List <ColorableSectionInstance> sectionInstances = new List <ColorableSectionInstance>(); SectionSelector sectionSelector = null; for (int i = 0; i < sectionColorLayers.Length; i++) { if (sectionColorLayers[i] == null) { layerObjects[i] = null; continue; } GameObject gameObject = new GameObject(); gameObject.name = "Section: " + i; gameObject.AddComponent <ColorChanger>(); // Create the 2D box collider, add it to Collision Sprite Renderer /*if (i == 0) * { * sectionSelector = gameObject.AddComponent<SectionSelector>(); * }*/ // Create sprite renderer SpriteRenderer spriteRenderer = gameObject.AddComponent <SpriteRenderer>(); spriteRenderer.sprite = sectionColorLayers[i]; gameObject.transform.parent = colorableObject.transform; ColorableSectionInstance sectionInstance = gameObject.AddComponent <ColorableSectionInstance>(); sectionInstance.ColorableSection = colorableInstance.Colorable.Sections[i]; sectionInstances.Add(sectionInstance); // Collision object GameObject collisionGameObject = new GameObject(); collisionGameObject.name = "Collision: " + i; // Create sprite renderer SpriteRenderer collisionSpriteRenderer = collisionGameObject.AddComponent <SpriteRenderer>(); collisionSpriteRenderer.sprite = colorableInstance.Colorable.Sections[i].SelectionLayer; collisionGameObject.transform.parent = gameObject.transform; collisionSpriteRenderer.enabled = false; BoxCollider2D meshCollider = collisionGameObject.AddComponent <BoxCollider2D>(); // Change the depth Vector3 newPosition = gameObject.transform.localPosition; newPosition.z = depth; depth--; gameObject.transform.localPosition = newPosition; layerObjects[i] = gameObject; } colorableInstance.InitializeSectionInstances(sectionInstances.ToArray()); }