public GameObject data = null; // data = CodeBlock public void OnDrop(PointerEventData eventData) // Function called when a draggable object is dropped in here { if (eventData.pointerDrag != null) { if (!beingUsed) { eventData.pointerDrag.GetComponent <RectTransform>().anchoredPosition = GetComponent <RectTransform>().anchoredPosition; // If the item dragged is not null, we get the RectTransform of the object that is being dragged and we make it equal to the RectTransform of the CodeSlot eventData.pointerDrag.GetComponent <DragDrop>().setOnSlot(true); GameObject slot = Instantiate(codeSlot, transform.position, Quaternion.identity); // Creating new CodeSlot slot.transform.SetParent(transform.parent); // Setting new CodeSlot as child of Canvas slot.GetComponent <RectTransform>().anchoredPosition += new Vector2(0, -100); // Update position of new CodeSlot slot.GetComponent <RectTransform>().localScale = new Vector3(1, 1, 1); slot.transform.SetSiblingIndex(1); // Moving new CodeSlot up in the hierarchy so that CodeBlocks can be seen in front of it // Setting node connections next = slot.GetComponent <CodeSlot>(); // Making reference to next slot next.previous = this; // Setting new CodeSlot's previous reference to this CodeSlot next.commandExecuter = commandExecuter; // Passing the commandExecuter to next CodeSlot data = eventData.pointerDrag.gameObject; // Getting data for the CodeBlock that is in this CodeSlot data.GetComponent <DragDrop>().codeSlot = this; // Setting data for the CodeBlock that is in this CodeSlot } } }
public IEnumerator ExecuteRoutine() { blockRaycast.blocksRaycasts = true; // When movement commands are being executed, we block the raycast so that the user can't alter the commands CodeSlot current = head; while (current.data && !gameOver) // While there are commands to run and the player hasn't lost, commands keep being executed { numberCommands++; // Track number of comands used current.data.GetComponent <DragDrop>().Run(); // Get command to be executed yield return(new WaitForSeconds(1.5f)); // Wait for 1.5 seconds to execute next command current = current.next; // make current command = next command } yield return(new WaitForSeconds(1f)); // wait for one second if (!gameOver) // if the player didnt reach the goal with the number of commands used { player.transform.position = player.startingPos; // reset characters position numberCommands = 0; // reset command count numberDeaths++; // increment number of deaths } blockRaycast.blocksRaycasts = false; // when all of the commands have been executed and all cases have been handled, stop blocking raycasts so the user can keep playing }
public void OnSelectCodeItem(CodeItem codeItem) { _selectedCodeItem = codeItem; _originalSlot = GetClosestSlot(_selectedCodeItem); _isSnapped = 0; _selectedCodeItem.transform.localScale = Vector3.one * _itemScaleDuringDrag; SoundManager.Instance.PlaySfx(SFX.Ui_MouseDown); }
public void OnBeginDrag(PointerEventData eventData) // Function called when we begin dragging object { canvasGroup.blocksRaycasts = false; // This line makes the raycast go through this object and land on the CodeSlot onSlot = false; dragged = true; rectTransform.position = Input.mousePosition; transform.SetSiblingIndex(-1); // Move the CodeBlock to the bottom of the hierarchy if (codeSlot != null) // Will remove CodeSlot.data if this code block has been assigned to a code slot { codeSlot.removeData(); codeSlot = null; } }
private bool GetIsTypeMatch(CodeItem codeItem, CodeSlot slot) { switch (slot.SlotType) { case SLOT_TYPE.Idea: return(true); case SLOT_TYPE.ConditionCode: return(codeItem.CodeType == CODE_TYPE.Condition); case SLOT_TYPE.MovementCode: return(codeItem.CodeType == CODE_TYPE.Movement); } return(false); }
private void OnDropItem(CodeSlot slot) { if (slot.InsertItem(_dragItem)) { _dragItem.OkDrop(); } bool complete = true; foreach (CodeSlot s in slots) { if (s.dropItem == null) { complete = false; break; } } uploadButton.interactable = complete; }
public void updateList(CodeSlot slot) // Called when a CodeBlock is removed or added { CodeSlot current = slot; while (current.next) { // Traverse the CodeSlots (Nodes) current = current.next; current.previous.data = current.data; // Previous CodeSlot CodeBlock = the current CodeSlot CodeBlock if (current.previous.data) // Check if the previous CodeSlot has data { current.previous.data.GetComponent <DragDrop>().codeSlot = current.previous; // Changing the CodeBlock CodeSlot to the previous CodeSlot current.previous.moveCodeBlock(); // Moving CodeBlock into position } } current.previous.next = null; // Making the previous CodeSlot point to null Destroy(current.gameObject); // Destroy last CodeSlot }
private void ArrangeCode(CodeItem codeItem, CodeSlot targetSlot) { if (codeItem.CodeSlot == targetSlot) { return; } switch (codeItem.CodeSlot.SlotType) { case SLOT_TYPE.Idea: _ideaPages.Remove(codeItem); break; case SLOT_TYPE.ConditionCode: _codingPages[codeItem.CodeSlot.Id].Condition = null; break; case SLOT_TYPE.MovementCode: _codingPages[codeItem.CodeSlot.Id].Movement = null; break; } switch (targetSlot.SlotType) { case SLOT_TYPE.Idea: _ideaPages.Add(codeItem); break; case SLOT_TYPE.ConditionCode: _codingPages[targetSlot.Id].Condition = codeItem; break; case SLOT_TYPE.MovementCode: _codingPages[targetSlot.Id].Movement = codeItem; break; } codeItem.CodeSlot.host = null; targetSlot.host = codeItem; codeItem.CodeSlot = targetSlot; }
public void CheckSlotSnap(CodeItem codeItem) { foreach (var slot in _codeSlots) { if (slot.host == null || slot.host == codeItem) { if (GetIsTypeMatch(codeItem, slot)) { var distance = GetDistanceToSlot(codeItem, slot); if (distance < _slotSnapDistance) { _selectedCodeItem.transform.position = slot.transform.position; _snappedSlot = slot; _isSnapped = _snapWaitCount; return; } } } } _isSnapped = Mathf.Max(_isSnapped - 1, 0); }
private void OnClearItem(CodeSlot slot) { slot.ClearItem(); uploadButton.interactable = false; }
void Start() { CodeSlot = AiEditor.Instance.GetClosestSlot(this); CodeSlot.host = this; }
public float GetDistanceToSlot(CodeItem codeItem, CodeSlot codeSlot) { return(Vector2.Scale(codeItem.RectTransform.anchoredPosition - (Vector2)codeSlot.transform.position, _codeSnapScale).magnitude); }