public void RemoveElement(PuzzleElement element) { PuzzleContainer container = puzzle.GetElementContainer(element); ContainerFront containerFront = null; foreach (ContainerFront current in nodes) { if (current.container == container) { containerFront = current; } } foreach (ContainerFront current in edges) { if (current.container == container) { containerFront = current; } } if (containerFront == null) { return; } containerFront.SetElement(null); }
public void CheckIsGoal(PuzzleStateNode node) { bool anyPlayers = false; bool allPlayersInGoal = true; foreach (PuzzleItem item in items) { PuzzlePlayer player = item as PuzzlePlayer; if (player == null) { continue; } anyPlayers = true; PuzzleElement element = player.GetNode(this, node.state).element; if (element == null || !(element is PuzzleGoal)) { allPlayersInGoal = false; } } node.goal = false; if (anyPlayers && allPlayersInGoal) { node.goal = true; if (goalPathSteps > 0) { goalPathSteps = Math.Min(goalPathSteps, node.step); } else { goalPathSteps = node.step; } goalStates++; } }
public void ClearInfo() { if (m_Transform is Transform) { m_Transform = null; m_PuzzleElement = null; } }
public void SetElementValue(PuzzleState state, PuzzleElement element, object value) { int index = dynamicElements.IndexOf(element); if (index < 0) { return; } state.elementValues[index] = value; }
public object GetElementValue(PuzzleState state, PuzzleElement element) { int index = dynamicElements.IndexOf(element); if (index < 0) { return(null); } return(state.elementValues[index]); }
public bool ContainsKey(PuzzleElement key) { foreach (InteractionObject io in StoredItems) { if (io.m_PuzzleElement == key) { return(true); } } return(false); }
public void RemoveElement(PuzzleElement element) { // Remove receivers from this node. PuzzleNode node = GetElementContainer(element) as PuzzleNode; if (node != null) { for (int i = node.receiverElements.Count - 1; i >= 0; i--) { RemoveReceiver(node, node.receiverElements[i]); } } // Do callback first so it can perform proper lookup in puzzle removeElement(element); // Remove element from edges and nodes for (int i = 0; i < edges.Count; i++) { if (edges[i].element == element) { edges[i].element = null; } } for (int i = 0; i < nodes.Count; i++) { if (nodes[i].element == element) { nodes[i].element = null; } } PuzzleReceiverElement receiverElement = element as PuzzleReceiverElement; if (receiverElement != null) { for (int i = 0; i < nodes.Count; i++) { if (nodes[i].receiverElements.Contains(receiverElement)) { RemoveReceiver(nodes[i], receiverElement); } } } // Remove element if (element.dynamic) { dynamicElements.Remove(element); } else { staticElements.Remove(element); } }
public void AddElement(PuzzleElement element) { if (element.dynamic) { dynamicElements.Add(element); } else { staticElements.Add(element); } addElement(element); }
void EvaluateLaserTrap(PuzzleElement puzzleElement) { if (!puzzleElement.isActiveAndEnabled) { currentItem++; return; } else { currentItem--; } }
//---------------------------------------------------------------------------------------------------------------------------------------------------------- // Generate puzzle-pieces textures and order them in puzzleGrid Vector2 GeneratePuzzlePieces(int _cols, int _rows, Texture2D _subElement, int _elementBaseSize, Texture2D _image) { int top, left, bottom, right; // Calculate piece aspect-ratio accordingly to image size Vector2 elementSizeRatio = new Vector2(_image.width / (float)_cols / elementBaseSize, _image.height / (float)_rows / _elementBaseSize); // Prepare sub-element variants Color[] subElementPixels = _subElement.GetPixels(); Color[] topPixels = subElementPixels; Color[] leftPixels = TextureUtility.Rotate90(subElementPixels, _subElement.width, _subElement.height, false); // Generation for (int y = 0; y < _rows; y++) { for (int x = 0; x < _cols; x++) { EditorUtility.DisplayProgressBar("Step 1/4", "Generating textures...", (y * _cols + x) / (_rows * _cols + 0.1f)); // Calculate shape - which type/variant of sub-elements should be used for top/left/bottom/right parts of piece (accordingly to shapes of surrounding puzzle-pieces) // (0 - flat, 1-convex, 2-concave) top = y > 0 ? -puzzleGrid[((y - 1) * _cols + x)].bottom : 0; left = x > 0 ? -puzzleGrid[(y * _cols + x - 1)].right : 0; bottom = y < (_rows - 1) ? Random.Range(-1, 1) * 2 + 1 : 0; right = x < (_cols - 1) ? Random.Range(-1, 1) * 2 + 1 : 0; // Prepare element mask puzzleGrid[y * _cols + x] = new PuzzleElement( top, left, bottom, right, _elementBaseSize, _subElement, topPixels, leftPixels ); // Extract and mask image-piece to be used as puzzle-piece texture puzzleGrid[y * _cols + x].texture = ExtractFromImage(_image, puzzleGrid[y * _cols + x], x, y, _elementBaseSize, elementSizeRatio); // Set pivot to Left-Top corner of puzzle-piece base puzzleGrid[y * _cols + x].pivot = new Vector2( ((float)puzzleGrid[y * _cols + x].pixelOffset.x / puzzleGrid[y * _cols + x].texture.width * elementSizeRatio.x), (1.0f - (float)puzzleGrid[y * _cols + x].pixelOffset.y / puzzleGrid[y * _cols + x].texture.height * elementSizeRatio.y) ); } } return(elementSizeRatio); }
void OnGUI() { GUI.Label(new Rect(5, 5, 400, 25), "Score:" + score.ToString()); for (int x = 0; x < columns.Count; x++) { for (int y = 0; y < columns[x].Count; y++) { if (columns[x][y].texture) { columns[x][y].position = Vector2.Lerp(columns[x][y].position, new Vector2((Screen.width / 2 - (columns.Count * 64) / 2) + x * 64, (Screen.height / 2 - (columns[x].Count * 64) / 2) + y * 64), Time.deltaTime * 7); Rect elementRect = new Rect(columns[x][y].position.x, columns[x][y].position.y, 64, 64); if ((x == selectedColumn && (y == selectedRow - 1 || y == selectedRow + 1)) || ((x == selectedColumn - 1 || x == selectedColumn + 1) && y == selectedRow)) { if (GUI.Button(elementRect, columns[x][y].texture)) { //switching a puzzle elements PuzzleElement tempElement = columns[x][y]; columns[x][y] = columns[selectedColumn][selectedRow]; columns[selectedColumn][selectedRow] = tempElement; selectedColumn = -1; selectedRow = -1; StopCoroutine(DetectCombos()); StartCoroutine(DetectCombos()); } } else { if (elementRect.Contains(Event.current.mousePosition)) { GUI.enabled = false; if (Input.GetMouseButtonDown(0)) { selectedColumn = x; selectedRow = y; } } if (x == selectedColumn && y == selectedRow) { GUI.enabled = false; } GUI.Box(elementRect, columns[x][y].texture); } GUI.enabled = true; } } } }
public void AddStates(Puzzle puzzle, PuzzleStateNode parent, PuzzleNode nodeA, PuzzleNode nodeB, bool forward) { PuzzleState state = parent.state; PuzzleState next; PuzzleElement e = elementNotNull; PuzzleElement n = nodeB.elementNotNull; foreach (PuzzleItem player in puzzle.GetItemsInNode <PuzzlePlayer> (state, nodeA)) { // Walk if (e.CanWalk(puzzle, state, nodeA, nodeB, forward) && n.CanWalk(puzzle, state, nodeA, nodeB, forward)) { next = parent.CloneAndAddChild("Go here", nodeB).state; player.SetNode(puzzle, next, nodeB); } // Take or push ball List <PuzzleBall> balls = puzzle.GetItemsInNode <PuzzleBall> (state, nodeA); if (balls.Count > 0) { PuzzleBall ball = balls[0]; if (e.CanTakeBall(puzzle, state, nodeA, nodeB, forward) && n.CanTakeBall(puzzle, state, nodeA, nodeB, forward)) { next = parent.CloneAndAddChild("Bring ball here", nodeB).state; player.SetNode(puzzle, next, nodeB); ball.SetNode(puzzle, next, nodeB); } if (e.CanPushBall(puzzle, state, nodeA, nodeB, forward) && n.CanPushBall(puzzle, state, nodeA, nodeB, forward)) { next = parent.CloneAndAddChild("Push ball here", nodeB).state; ball.SetNode(puzzle, next, nodeB); } } // Hit toggle at other end if (e.CanSee(puzzle, state, nodeA, nodeB, forward) && n.CanSee(puzzle, state, nodeA, nodeB, forward)) { PuzzleToggle toggle = nodeB.element as PuzzleToggle; if (toggle != null) { next = parent.CloneAndAddChild("Shoot toggle", nodeB).state; nodeB.TriggerToggle(puzzle, next); } } } }
// Editing API public PuzzleContainer GetElementContainer(PuzzleElement element) { foreach (PuzzleNode node in nodes) { if (node.element == element) { return(node); } } foreach (PuzzleEdge edge in edges) { if (edge.element == element) { return(edge); } } return(null); }
private ElementData GetElementData() { PuzzleElement element = container.element; if (element == null) { return(null); } System.Type type = element.GetType(); foreach (var elem in elements) { if (elem.type == type) { return(elem); } } return(null); }
void EvaluatePressurePlate(PuzzleElement puzzleElement) { var pressurePlate = puzzleElement.GetRootGameObject().GetComponent <PressurePlate>(); if (pressurePlate.ToggledState) { currentItem++; return; } /*if (Vector3.Distance(pressurePlate.transform.position, transform.position) > 0.3f) * { * transform.LookAt(pressurePlate.transform.position); * transform.position = Vector3.Lerp(transform.position, pressurePlate.transform.position, 0.01f); * }*/ nav.SetDestination(pressurePlate.transform.position); }
//---------------------------------------------------------------------------------------------------------------------------------------------------------- // Extract and mask image-piece to be used as puzzle-piece texture Texture2D ExtractFromImage(Texture2D _image, PuzzleElement _puzzlElement, int _x, int _y, int _elementBaseSize, Vector2 _elementSizeRatio) { // Get proper piece of image Color[] pixels = _image.GetPixels( (int)((_x * _elementBaseSize - _puzzlElement.pixelOffset.x) * _elementSizeRatio.x), (int)(_image.height - (_y + 1) * _elementBaseSize * _elementSizeRatio.y - _puzzlElement.pixelOffset.height * _elementSizeRatio.y), (int)(_puzzlElement.maskWidth * _elementSizeRatio.x), (int)(_puzzlElement.maskHeight * _elementSizeRatio.y) ); Texture2D result = new Texture2D( (int)(_puzzlElement.maskWidth * _elementSizeRatio.x), (int)(_puzzlElement.maskHeight * _elementSizeRatio.y) ); // Apply mask result.wrapMode = TextureWrapMode.Clamp; _puzzlElement.ApplyMask(pixels, ref result); return(result); }
//---------------------------------------------------------------------------------------------------------------------------------------------------------- // Generates example puzzle-piece/ to be shown as preview in Editor window Texture2D GenerateExamplePiece(Texture2D _subElement, int _elementBaseSize) { PuzzleElement puzzlePiece; // Calculate randomly which sub-element variants should be used int top = Random.Range(-1, 1) * 2 + 1; int left = Random.Range(-1, 1) * 2 + 1; int bottom = Random.Range(-1, 1) * 2 + 1; int right = Random.Range(-1, 1) * 2 + 1; // Prepare sub-element variants Color[] subElementPixels = _subElement.GetPixels(); Color[] topPixels = subElementPixels; Color[] leftPixels = TextureUtility.Rotate90(subElementPixels, _subElement.width, _subElement.height, false); // Prepare element mask puzzlePiece = new PuzzleElement ( top, left, bottom, right, _elementBaseSize, _subElement, topPixels, leftPixels ); // Create resultTexture puzzlePiece.texture = new Texture2D(puzzlePiece.maskWidth, puzzlePiece.maskHeight); Color[] piecePreviewColor = new Color [puzzlePiece.maskWidth * puzzlePiece.maskHeight]; for (int i = 0; i < piecePreviewColor.Length; i++) { piecePreviewColor[i] = Color.black; } puzzlePiece.ApplyMask(piecePreviewColor, ref puzzlePiece.texture); return(puzzlePiece.texture); }
public void SetElement(PuzzleElement element) { PuzzleBoolElement boolElement = element as PuzzleBoolElement; if (boolElement != null) { boolElement.updateState -= UpdateState; boolElement.updateState += UpdateState; } if (element == null) { staticSprite.sprite = null; dynamicSprite.sprite = null; lineSprite.sprite = defaultLineImage; fillSprite.sprite = defaultFillImage; if (staticSprite.GetComponent <Collider>()) { staticSprite.GetComponent <Collider>().enabled = false; } } else { ElementData elem = GetElementData(); staticSprite.sprite = elem.imageStatic; bool state = false; if (boolElement != null) { state = boolElement.defaultOn; } SetState(state); lineSprite.sprite = (elem.imageLine != null ? elem.imageLine : defaultLineImage); fillSprite.sprite = (elem.imageFill != null? elem.imageFill : defaultFillImage); if (staticSprite.GetComponent <Collider>()) { staticSprite.GetComponent <Collider>().enabled = (boolElement != null); } } }
public override void Deserialize(SerializationTool tool, string str) { string[] tokens = str.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); int pos = 0; string[] coords = tokens[pos].Substring(4).Split(','); SetPosition(new Point(int.Parse(coords[0]), int.Parse(coords[1]))); pos++; while (pos < tokens.Length) { if (tokens[pos].StartsWith("notify:")) { string[] receiverNames = tokens[pos].Substring(7).Split(','); foreach (string name in receiverNames) { PuzzleElement receiver = tool.puzzle.dynamicElements.Find(e => e.GetName(tool) == name); tool.puzzle.AddReceiver(this, receiver as PuzzleReceiverElement); } } pos++; } }
private void generateSubElements(int amount, string parent) { PuzzleElement parentObject = findElement(elements, parent); if (parentObject == null) { Debug.LogError("The element " + parent + " was not found"); } if (amount >= 0 || parentObject.key == false) // We must end on a key. Therefore we will continue until we find it. { objectsToSpawn.Add(parentObject.NAME); if (parentObject.generate.Count > 0) { // Run the function recursively again, if we are out of elements, try to find a key element so we can end the recursion. generateSubElements(amount - 1, amount < 0 ? findKeyElement(parentObject.generate) : findNonKeyElement(parentObject.generate)); } else { if (!parentObject.key) { Debug.LogError(parent + " which is a non-key " + parentObject.key + "element has no sub elements"); } else { Debug.LogError("The recursion ended to early. Still missing " + amount + " iterations"); } } } else if (parentObject.key == true) { objectsToSpawn.Add(parentObject.NAME); // print ("Recursion ended"); } else { Debug.LogError("This state should never be possible"); } }
IEnumerator RestockEnumrator() { yield return(new WaitForSeconds(0.25f)); for (int x = 0; x < columns.Count; x++) { for (int y = 0; y < columns[x].Count; y++) { if (!columns[x][y].texture) { int randomElement = Random.Range(0, (elements.Length - 1) * 2); if (randomElement > elements.Length - 1) { randomElement -= elements.Length - 1; } PuzzleElement element = new PuzzleElement(); element.texture = elements[randomElement]; element.position = new Vector2((Screen.width / 2 - (totNumColumns * 64) / 2) + x * 64, (-Screen.height - (totNumRows * 64) / 2) + y * 64); columns[x][y] = element; } } } StartCoroutine(DetectCombos()); }
public void PopulateInfo(PuzzleElement startingInfo) { m_PuzzleElement = startingInfo; m_Transform = m_PuzzleElement.transform; }
public void PopulateInfo(Transform startingInfo) { m_Transform = startingInfo; m_PuzzleElement = m_Transform.GetComponent <PuzzleElement>(); }
void ContainerWindow(int id) { Rect position = GetInnerWindowRect(containerWindowRect); position.width = kLargeButtonSize; position.height = kLargeButtonSize; PuzzleNode node = selected as PuzzleNode; ElementData[] elements = node != null ? puzzleFrontPrefab.nodePrefab.elements : puzzleFrontPrefab.edgePrefab.elements; // Container options for (int i = 0; i < elements.Length; i++) { ElementData elementData = elements[i]; bool chosenElement = (selected.element != null && selected.element.GetType() == elementData.type); if (i % 2 == 0) { position.x = kBorder + kSpacing; } else { position.x += kLargeButtonSize + kSpacing; } if (ElementButton(position, elementData, chosenElement)) { if (!chosenElement) { if (selected.element != null) { puzzle.RemoveElement(selected.element); } PuzzleElement element = (PuzzleElement)Activator.CreateInstance(elementData.type); selected.element = element; puzzle.AddElement(element); } else { puzzle.RemoveElement(selected.element); } } if (i % 2 == 1 || i == elements.Length - 1) { position.y += kLargeButtonSize + kSpacing; } } position.y += 10; if (selected.element != null) { var boolElement = selected.element as PuzzleBoolElement; if (boolElement != null) { position.x = kBorder + kSpacing; position.width = GetInnerWindowRect(containerWindowRect).width; position.height = 24; Rect pos = new Rect(position.x, position.y, position.width, 20); boolElement.defaultOn = GUI.Toggle(pos, boolElement.defaultOn, "Enabled"); position.y += 24 + kSpacing + 10; } } // Item options if (node != null) { List <PuzzleItem> itemsInNode = puzzle.GetDefaultItemsInNode(node); ItemData[] itemDatas = puzzleFront.itemPrefab.items; foreach (var data in itemDatas) { position = new Rect(kBorder + kSpacing, position.y, 32, 48); DrawSprite(position, data.image); List <PuzzleItem> itemsOfType = itemsInNode.Where(e => e.GetType() == data.type).ToList(); position.x += position.width + kSpacing + 1; position.width = 33; GUI.Label(position, itemsOfType.Count.ToString()); Rect buttonRect = position; buttonRect.y += 8; buttonRect.height -= 16; buttonRect.x += buttonRect.width + kSpacing; GUI.enabled = (itemsOfType.Count > 0); if (GUI.Button(buttonRect, "-")) { puzzle.RemoveItem(itemsOfType[0]); } GUI.enabled = true; buttonRect.x += buttonRect.width + kSpacing; if (GUI.Button(buttonRect, "+")) { PuzzleItem item = (PuzzleItem)Activator.CreateInstance(data.type); item.defaultNode = node; puzzle.AddItem(item); } position.y += 48 - 8; } } }
/// <summary> /// Contain removed puzzle. /// </summary> public void ContainPuzzle(PuzzleElement element) { element.gameObject.SetActive(false); element.transform.position = puzzleContainer.transform.position; }
void EvaluateGoalDoor(PuzzleElement puzzleElement) { nav.SetDestination(puzzleElement.transform.position); }