/** * Loops through all the things the user could possibly be trying to click * and if the user is clicking an argument of this expression piece rather * than this expression piece itself, "forwards the click" i.e. calls the * OnClick() method of the argument. */ void IPointerClickHandler.OnPointerClick(PointerEventData eventData) { List <RaycastResult> results = new List <RaycastResult>(); EventSystem.current.RaycastAll(eventData, results); ExpressionPiece argumentClicked = null; foreach (RaycastResult r in results) { if (r.gameObject.GetComponent <ExpressionPiece>() != null && r.gameObject.GetComponent <ExpressionPiece>().id.Equals("_")) { argumentClicked = r.gameObject.GetComponent <ExpressionPiece>(); break; } } //if user wasn't clicking any empty arguments, call OnClick() for this ExpressionPiece; //otherwise, call OnClick() for the clicked empty arg if (argumentClicked == null) { if (this.gameController.selectedExpression != null && !this.gameController.selectedExpression.Equals(this)) { this.gameController.failure.Play(); } this.OnClick(); } else { argumentClicked.OnClick(); } }
public ExpressionPiece DeepCopy(bool isFirstCall) { GameObject exprPiece = Resources.Load("Piece") as GameObject; GameObject exprPieceInstance = Instantiate(exprPiece, new Vector2(0, 0), Quaternion.identity) as GameObject; exprPieceInstance.transform.position = this.transform.position; ExpressionPiece exprPieceScript = exprPieceInstance.GetComponent <ExpressionPiece>(); exprPieceScript.gameController = this.gameController; exprPieceScript.id = this.expression.ToString(); exprPieceScript.expression = this.expression; exprPieceScript.arguments = new ExpressionPiece[this.arguments.Length]; exprPieceScript.heightInUnits = this.heightInUnits; exprPieceScript.widthInUnits = this.widthInUnits; exprPieceScript.index = this.index; for (int i = 0; i < this.arguments.Length; i++) { if (this.arguments[i] != null) { exprPieceScript.arguments[i] = this.arguments[i].DeepCopy(false); exprPieceScript.arguments[i].SetParentExpressionPiece(exprPieceScript); } } return(exprPieceScript); }
/** * Called after an ExpressionPiece is created for the first time (all * arguments are empty) to set up the piece. I.e. called when an * ExpressionPiece is created by a Spawner, Controller or something that isn't OnDrop(). */ public void Initialize(Expression expr) { gameController = GameObject.Find("GameController").GetComponent <GameController>(); this.expression = expr; this.arguments = new ExpressionPiece[expr.GetNumArgs()]; this.gameObject.transform.position = GetStartingPosition(); GameObject tabletop = GameObject.Find("Workspace"); this.gameObject.transform.SetParent(this.transform.parent.transform); if (arguments.Length > 0) { this.heightInUnits = 2; this.widthInUnits = expr.GetNumArgs() + 1; } int counter = 0; int currentX = 1; float calculatedWidth = PIXELS_PER_UNIT * this.widthInUnits; float calculatedHeight = PIXELS_PER_UNIT * this.heightInUnits; // build empty arguments for (int i = 0; i < arguments.Length; i++) { // Expression argExpression = expr.GetArg(i); if (DRAW_OPEN_ARGUMENT_TYPE) { GameObject argumentPiece = Resources.Load("Piece") as GameObject; argumentPiece.name = "Argument"; GameObject argumentPieceInstance = Instantiate(argumentPiece, this.transform.position, Quaternion.identity) as GameObject; ExpressionPiece argumentPieceScript = argumentPieceInstance.GetComponent <ExpressionPiece>(); argumentPieceScript.gameController = gameController; argumentPieceScript.expression = new Word(expr.GetInputType(counter), "_"); argumentPieceScript.arguments = new ExpressionPiece[0]; //NOTE: arg positions are set relative to the center of their container expressions, by the center of the arg float overallPieceHeight = calculatedHeight; float overallPieceWidth = calculatedWidth; float overallPieceTopLeftX = this.transform.position.x - calculatedWidth / 2.0f; float overallPieceTopLeftY = this.transform.position.y + calculatedHeight / 2.0f; float argTopLeftX = overallPieceTopLeftX + PIXELS_PER_UNIT * currentX; float argTopLeftY = overallPieceTopLeftY - 1 * PIXELS_PER_UNIT; float argCenterX = argTopLeftX + (PIXELS_PER_UNIT * .5f) - BUFFER_IN_PIXELS; //.5f b/c center of arg w/ width & height 1 float argCenterY = argTopLeftY - (PIXELS_PER_UNIT * .5f) + BUFFER_IN_PIXELS; argumentPieceInstance.transform.SetParent(this.gameObject.transform); argumentPieceInstance.transform.position = new Vector3(argCenterX, argCenterY); argumentPieceScript.id = "_"; argumentPieceScript.index = counter; argumentPieceScript.SetParentExpressionPiece(this); this.arguments[i] = argumentPieceScript; counter++; } currentX++; } }
/** * Creates an new ExpressionPiece based on this ExpressionPieceSpawner */ public ExpressionPiece MakeNewExpressionPiece() { GameObject workspace = GameObject.Find("Workspace"); GameObject exprPiece = Resources.Load("Piece") as GameObject; GameObject exprPieceInstance = Instantiate(exprPiece, new Vector2(0, 0), Quaternion.identity) as GameObject; exprPieceInstance.transform.SetParent(workspace.transform); ExpressionPiece exprPieceScript = exprPieceInstance.GetComponent <ExpressionPiece>(); exprPieceScript.Initialize(expression); exprPieceScript.SetVisual(exprPieceScript.GenerateVisual()); return(exprPieceInstance.GetComponent <ExpressionPiece>()); }
/** * if the game is in Speaking Mode (kept track of by a bool in GameController, * and this NPC is clicked, say the expression to the NPC. */ private void OnMouseDown() { if (!EventSystem.current.IsPointerOverGameObject()) // make sure not clicking canvas { ExpressionPiece selectedExpr = controller.selectedExpression; if (selectedExpr == null) { // Debug.Log("No selected expression to say to this NPC"); } else { ReceiveExpression(Expression.PLAYER, selectedExpr.expression); Destroy(selectedExpr.gameObject); controller.HidePointer(); // scontroller.SetInSpeakingMode(false); } } }
// TODO HEIDI 1/27 fill this in so we can display the sentence an NPC is saying over their head! public void FromScratch(Expression expr, Vector3 position) { this.expression = expr; this.arguments = new ExpressionPiece[expr.GetNumArgs()]; this.gameObject.transform.position = position; GameObject tabletop = GameObject.Find("Workspace"); this.gameObject.transform.position = new Vector3(0, 0, 0); // this.gameObject.transform.SetParent = this.parent.transform; if (arguments.Length > 0) { this.heightInUnits = 2; } int currentXInUnits = 1; for (int i = 0; i < arguments.Length; i++) { Expression argExpression = expr.GetArg(i); if (argExpression == null) { this.widthInUnits++; currentXInUnits++; } else { GameObject exprPiece = Resources.Load("Piece") as GameObject; GameObject exprPieceInstance = Instantiate(exprPiece, new Vector2(0, 0), Quaternion.identity) as GameObject; ExpressionPiece exprPieceScript = exprPieceInstance.GetComponent <ExpressionPiece>(); // exprPieceScript.gameObject.parent = this; exprPieceScript.FromScratch(argExpression, new Vector3(currentXInUnits * PIXELS_PER_UNIT, PIXELS_PER_UNIT)); arguments[i] = exprPieceScript; currentXInUnits += arguments[i].widthInUnits; this.widthInUnits += exprPieceScript.widthInUnits; this.heightInUnits = (arguments[i].heightInUnits + 1 > this.heightInUnits) ? arguments[i].heightInUnits + 1 : this.heightInUnits; } } }
public IEnumerator ShowSpeechBubble(Expression expr) { GameObject exprPiece = Resources.Load("Piece") as GameObject; GameObject exprPieceInstance = Instantiate(exprPiece, new Vector2(0, 0), Quaternion.identity) as GameObject; exprPieceInstance.name = "LIONKING"; ExpressionPiece exprPieceScript = exprPieceInstance.GetComponent <ExpressionPiece>(); exprPieceScript.FromScratch(expr, new Vector3(0, 0, 0)); exprPieceScript.transform.SetParent(GameObject.Find("ResponseCanvas").transform); Camera cam; if (controller.is2D) { cam = GameObject.Find("Main Camera").GetComponent <Camera>(); exprPieceScript.transform.position = cam.WorldToScreenPoint(this.transform.position); exprPieceScript.transform.position = new Vector3(exprPieceScript.transform.position.x, exprPieceScript.transform.position.y + (exprPieceScript.heightInUnits * ExpressionPiece.PIXELS_PER_UNIT / 2) + 16); } else { cam = GameObject.Find("FirstPersonCharacter").GetComponent <Camera>(); exprPieceScript.transform.position = cam.WorldToScreenPoint(this.transform.position); exprPieceScript.transform.position = new Vector3(exprPieceScript.transform.position.x, exprPieceScript.transform.position.y + (exprPieceScript.heightInUnits * ExpressionPiece.PIXELS_PER_UNIT / 2)); } exprPieceScript.SetVisual(exprPieceScript.GenerateVisual()); Destroy(exprPieceInstance, 2.0f); yield return(new WaitForSeconds(2.0f)); }
void Update() { if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.F)) { if (currentUseObject != null) { currentUseObject.transform.parent = null; currentUseObject = null; } } if (Input.GetKeyDown(KeyCode.Alpha0)) { Debug.Log("0 pressed"); if (currentInteractObject != null) { GameObject thatInventory = GameObject.Find(currentInteractObject.name + "/Inventory"); GameObject playerInventory = GameObject.Find("FPSController/Inventory"); if (thatInventory != null && playerInventory.transform.childCount > 0) { Transform item = playerInventory.transform.GetChild(0); item.transform.SetParent(thatInventory.transform); item.transform.position = thatInventory.transform.position; } } } if (Input.GetKeyUp(KeyCode.Tab)) { if (selectedExpression != null) { if (!is2D) { selectedExpression.transform.SetParent(GameObject.Find("ScreenCanvas").transform); selectedExpression.transform.position = new Vector3(Screen.width / 2, Screen.height / 2); usableExpression = selectedExpression; // selectedExpression = null; } HidePointer(); selectedExpression = null; } canvas.SetActive(!canvas.activeInHierarchy); if (fpc) { FirstPersonController fpcScript = fpc.GetComponent <FirstPersonController>(); if (canvas.activeInHierarchy) { fpcScript.enabled = false; highClick.Play(); } else { fpcScript.enabled = true; Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; lowClick.Play(); } } } if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } }
/** * When the user tries to drag this ExpressionPieceSpawner, an ExpressionPiece * will get created based on the name of this ExpressionPieceSpawner. */ void IPointerClickHandler.OnPointerClick(PointerEventData eventData) { ExpressionPiece exprPiece = MakeNewExpressionPiece(); }
private GameObject GenerateVisual(bool isFirstLevel) { GameObject exprPiece = this.gameObject; RectTransform pieceRect = exprPiece.GetComponent <RectTransform>(); float calculatedWidth = PIXELS_PER_UNIT * this.widthInUnits; float calculatedHeight = PIXELS_PER_UNIT * this.heightInUnits; pieceRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, calculatedWidth); pieceRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, calculatedHeight); float pieceCenterX = 0; float pieceCenterY = 0; float pieceTopLeftX = pieceCenterX - calculatedWidth / 2; float pieceTopLeftY = pieceCenterY + calculatedHeight / 2; GameObject visualContainer = new GameObject(); visualContainer.name = "VisualContainer"; visualContainer.transform.SetParent(exprPiece.transform); visualContainer.layer = 0; RectTransform visContainerRectTransform = visualContainer.AddComponent <RectTransform>(); visContainerRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, calculatedWidth - BUFFER_IN_PIXELS); visContainerRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, calculatedHeight - BUFFER_IN_PIXELS); if (DRAW_SUBEXPRESSION_TYPE || this.expression.headString.Equals("_") || isFirstLevel) { Image bgImage = visualContainer.AddComponent <Image>(); bgImage.color = isFirstLevel ? this.expression.type.outputColor : this.expression.type.color + new Color(0.25f, 0.25f, 0.25f, 0f) - new Color(0, 0, 0, (1 - EXPRESSION_OPACITY)); Color borderColor = this.expression.type.color; GenerateBorder(borderColor, visualContainer, calculatedWidth, calculatedHeight, "North"); GenerateBorder(borderColor, visualContainer, calculatedWidth, calculatedHeight, "South"); GenerateBorder(borderColor, visualContainer, calculatedWidth, calculatedHeight, "West"); GenerateBorder(borderColor, visualContainer, calculatedWidth, calculatedHeight, "East"); } GameObject headObject = new GameObject(); headObject.name = "Head"; headObject.transform.SetParent(visualContainer.transform); Image headImage = headObject.AddComponent <Image>(); Expression expr = this.expression; // Sprite headSprite = Resources.Load<Sprite>("Symbols/" + expr.headString); Sprite headSprite = Resources.Load <Sprite>("English/" + expr.headString); if (headSprite == null) { headSprite = Resources.Load <Sprite>("PlaceholderSprites/" + expr.headString); } headImage.sprite = headSprite; headImage.transform.localScale *= .25f; headImage.transform.position = new Vector3(pieceTopLeftX + (.5f * PIXELS_PER_UNIT), pieceTopLeftY - (.5f * PIXELS_PER_UNIT)); visualContainer.transform.position = new Vector3(0, 0, 0); int numArgs = this.arguments.Length; int currentX = 1; //in units int currentY = 1; for (int i = 0; i < numArgs; i++) { ExpressionPiece arg = this.arguments[i]; if (arg == null) { currentX++; } else { currentX += arg.widthInUnits; float positionX = pieceTopLeftX + PIXELS_PER_UNIT * (currentX - ((.5f * arg.widthInUnits) + BUFFER_IN_UNITS)); float valToTopAlignArgs = (((this.heightInUnits - 1) - arg.heightInUnits)) * (PIXELS_PER_UNIT / 2); float positionY = PIXELS_PER_UNIT * ((-0.5f * currentY) + BUFFER_IN_UNITS) + valToTopAlignArgs; if (arg.id.Equals("_") && !isFirstLevel) { arg.expression = new Word(arg.expression.type, "blank"); } GameObject argVisual = arg.GenerateVisual(false); argVisual.transform.SetParent(visualContainer.transform); argVisual.transform.position = new Vector3(positionX, positionY); } } return(visualContainer); }
public void SetParentExpressionPiece(ExpressionPiece parent) { parentExpressionPiece = parent; }
/** * returns false if 2 expressions shouldn't combine with each other */ public bool CombineWith(ExpressionPiece inputExpression, int index) { Expression expr = null; //try to create new Expression try { expr = new Phrase(this.expression, inputExpression.expression, index); } catch (Exception e) { Debug.LogException(e); this.gameController.failure.Play(); return(false); } // generate new Piece GameObject exprPiece = Resources.Load("Piece") as GameObject; GameObject exprPieceInstance = Instantiate(exprPiece, GetSpawnPosition(), Quaternion.identity) as GameObject; ExpressionPiece exprPieceScript = exprPieceInstance.GetComponent <ExpressionPiece>(); exprPieceScript.expression = expr; exprPieceScript.arguments = new ExpressionPiece[expr.GetNumArgs()]; if (exprPieceScript.arguments.Length > 0) { exprPieceScript.heightInUnits = 2; } float originalPieceX = this.transform.position.x; float originalPieceY = this.transform.position.y; // exprPieceInstance.transform.position = new Vector3(originalPieceX, originalPieceY, 0); exprPieceScript.gameController = gameController; exprPieceScript.id = expr.ToString(); exprPieceScript.index = this.index; if (this.parentExpressionPiece != null) { exprPieceScript.SetParentExpressionPiece(this.parentExpressionPiece.DeepCopy()); // DeepCopy here bc 'this' will get destroyed } // int noArgInputWidth = 1; // int noArgInputHeight = 1; // for (int i = 0; i < inputExpression.arguments.Legth; i++) { // if (!inputExpression[i].id.Equals("_")) { // noArgInputWidth += inputExpression[i].widthInUnits; // noArgInputHeight = Max(noArgInputHeight, inputExpression.heightInUnits + 1); // } // } exprPieceScript.widthInUnits = 1; exprPieceScript.widthInUnits += inputExpression.widthInUnits; exprPieceScript.heightInUnits = Max(exprPieceScript.heightInUnits, inputExpression.heightInUnits + 1); //Add the arguments to the new piece int counter = -1; for (int i = 0; i < arguments.Length; i++) { if (this.arguments[i] == null) { counter++; exprPieceScript.widthInUnits++; } else { // this happens to every argument, whether blank or filled float originalArgLocalX = this.arguments[i].transform.localPosition.x; float originalArgLocalY = this.arguments[i].transform.localPosition.y; // place a copy of the old argument in the same position in the new expression. exprPieceScript.arguments[i] = this.arguments[i].DeepCopy(); exprPieceScript.arguments[i].transform.SetParent(exprPieceInstance.transform); exprPieceScript.arguments[i].gameObject.transform.localPosition = new Vector3(originalArgLocalX, originalArgLocalY, 0); // changing the width and height of the new expression exprPieceScript.widthInUnits += exprPieceScript.arguments[i].widthInUnits; exprPieceScript.heightInUnits = Max(exprPieceScript.heightInUnits, exprPieceScript.arguments[i].heightInUnits + 1); // if it's an empty argument slot, then move forward in the // argument index, and decrement the argument slot's index by 1 if // the argument has already been placed to preserve the new indexing. if (this.arguments[i].id.Equals("_")) { if (counter > index) { exprPieceScript.arguments[i].index--; } counter++; } } // place the input expression in the appropriate argument position. if (counter == index) { float originalArgLocalX = this.arguments[i].transform.localPosition.x; float originalArgLocalY = this.arguments[i].transform.localPosition.y; //need to destroy the empty arg that this piece is replacing Destroy(exprPieceScript.arguments[i].gameObject, 0.0f); exprPieceScript.arguments[i] = inputExpression.DeepCopy(); exprPieceScript.arguments[i].gameObject.transform.localPosition = new Vector3(originalArgLocalX, originalArgLocalY, 0); exprPieceScript.arguments[i].transform.SetParent(exprPieceInstance.transform); counter++; exprPieceScript.widthInUnits--; } } int xPositionInUnits = 1; // this is setting the parentexpressionpiece and parent of all of the new // expression's arguments as the new expression. This doesn't happen in // deepcopy because deepcopy makes a new copy of the expression to set. for (int i = 0; i < arguments.Length; i++) { float argLocalX = exprPieceScript.arguments[i].transform.localPosition.x; float argLocalY = exprPieceScript.arguments[i].transform.localPosition.y; float changeY = ((exprPieceScript.heightInUnits - 2) / 2f) * PIXELS_PER_UNIT; if (exprPieceScript.arguments[i].id.Equals("_")) { exprPieceScript.arguments[i].gameObject.transform.localPosition = new Vector3( ((-0.5f * exprPieceScript.widthInUnits) + xPositionInUnits + 0.5f - BUFFER_IN_UNITS) * PIXELS_PER_UNIT, argLocalY + changeY); } exprPieceScript.arguments[i].SetParentExpressionPiece(exprPieceScript); exprPieceInstance.transform.SetParent(this.transform.parent.transform); // Debug.Log(exprPieceScript.expression + "'s width is " + exprPieceScript.widthInUnits); // Debug.Log(exprPieceScript.expression + "[" + i + "]'s width is " + arguments[i].widthInUnits); xPositionInUnits += exprPieceScript.arguments[i].widthInUnits; } exprPieceInstance.transform.SetParent(this.transform.parent.transform); exprPieceScript.SetVisual(exprPieceScript.GenerateVisual()); int indexToOccupy = this.gameObject.transform.GetSiblingIndex(); // foreach (ExpressionPiece arg in this.arguments) { // Destroy(arg.gameObject, 0.0f); // } Destroy(this.gameObject, 0.0f); // the arguments to the input expression aren't being destroyed properly. They're just floating around Destroy(inputExpression.gameObject, 0.0f); exprPiece.transform.SetSiblingIndex(indexToOccupy); return(true); }