/* * Func: AddAction * Desc: Registers a function callback when an action (eg swipe) is performed on an object. * Args: * inpObj: GameObject: The object that we are registering the action for * inpType: inputType: The action type we are watching for (tap/swipe/hold) * enabled: bool: Whether or not the action is currently enabled * outFunc: Function: The function to run when the action is performed * outArgs: Arguments: The arguments to run with the function when an action is performed * ... outArgs can continue indefinitely, as many args as you want can be given... * Outs: * Int: -1 on Error, >0 is list index in registeredActions, the public list registry * Example Use(s): * * //Add action to grid for tapping to build a building, pass in touch pos to addBuilding func, it starts enabled * AddAction(this.gameobject, inputType.tap, true, this.addBuilding, touchEndPos) * * //Add action to building for when user presses and holds on it, it starts disabled * AddAction(this.GameObject, inputType.pressHold, false, this.showDetails) */ public int AddAction(GameObject inpObj, inputType inpType, bool enabled, System.Action outFunc, object[] outArgs = null) { LogAction(string.Format("Adding '{0}' action to '{1}' with func '{3}' and args '{4}' in '{2}' state...", inpType, inpObj.name, enabled, outFunc.Method, outArgs)); //Create action data for action actionData newActionData = new actionData(); //Return -1 (fail) if addActionData fails, as this validates the input data if (!(newActionData.addActionData(inpObj, inpType, enabled, outFunc, outArgs))) { LogAction("Error adding actionData to newActionData var."); return(-1); } ; //Register the action in our action list if (actionCount >= 4094) { print("AddAction: Error: Action List Full!"); } ; registeredActions[actionCount] = newActionData; actionCount++; //Return index in list to caller func return(actionCount - 1); }
public actionData SetCurrentAction(int teamnum, Soldier owner, Soldier tar, int actionnumber, int reactnum, int targetnum, Vector3 loc) { actionData tempAction = new actionData(); tempAction.actor = owner; tempAction.team = owner.team; tempAction.target = tar; tempAction.actionTime = 1.0f; tempAction.actionNumber = actionnumber; tempAction.reactType = reactnum; tempAction.targetType = targetnum; tempAction.loc = loc; return(tempAction); }
public void FireGun(actionData act, TurnManager turnManager) { // if (act.target == act.actor) { turnManager.CheckRange(5); turnManager.resultdisplay.text = "looking at self "; Debug.Log("dont shoot im you "); } //uiManager.UpdateScrollingText(act.actor.transform.name + " shoots at " + act.target.transform.name); // if (act.target.team != act.actor.team) // { act.actor.transform.LookAt(act.target.transform); Debug.Log("Firing gun "); // turnManager.actionRemaining--; turnManager.actionpointstext.text = turnManager.actionRemaining.ToString(); int tohit = turnManager.CalculateToHit(); int roll = Random.Range(1, 101); if (tohit > roll) { act.target.TakeDamage(act.damage + 1); // turnManager.lastActionPress = -1; GameObject clone = Instantiate(turnManager.bulletPrefab2, act.actor.transform.position + act.actor.transform.forward, transform.rotation) as GameObject; clone.GetComponent <Bullet>().holdvel = (act.target.transform.position - act.actor.transform.position); turnManager.resultdisplay.text = " HIT : " + act.target.transform.name + " :hp: " + act.target.currenthp.ToString(); uiManager.UpdateScrollingText(act.actor.transform.name + " : HIT : " + act.target.transform.name + " :hp: " + act.target.currenthp.ToString()); } else { Debug.Log("miss: Fire gun "); //check to see if miss destroys something Vector3 newdir = new Vector3(act.target.transform.position.x + (Random.Range(-0.3f, 1.0f)), act.target.transform.position.y + (Random.Range(-0.3f, 1.0f)), act.target.transform.position.z + (Random.Range(-0.3f, 1.0f))) - act.actor.transform.position; turnManager.resultdisplay.text = " ::MISSED:: " + act.target.transform.name; RaycastHit hit; GameObject clone = Instantiate(turnManager.bulletPrefab, act.actor.transform.position + act.actor.transform.forward, transform.rotation) as GameObject; uiManager.UpdateScrollingText(act.actor.transform.name + " ::MISSED:: " + act.target.transform.name); clone.GetComponent <Bullet>().holdvel = newdir; if (Physics.Raycast(turnManager.gun.transform.position, newdir, out hit, 15.0f)) { if (hit.transform.tag == "destructable") { // Destroy(hit.transform.gameObject); } } } // } // else { Debug.Log("sameteam"); uiManager.UpdateScrollingText(act.actor.transform.name + " doesnt shoot at " + act.target.transform.name); } }
public actionData SetCurrentAction(int teamnum, Soldier owner, Soldier tar, int actionnumber, int reactnum, int targetnum, Vector3 loc, int newdmg, int newrng, float waitTime) { actionData tempAction = new actionData(); tempAction.actor = owner; tempAction.team = owner.team; tempAction.target = tar; tempAction.actionTime = waitTime; tempAction.actionNumber = actionnumber; tempAction.reactType = reactnum; tempAction.targetType = targetnum; tempAction.loc = loc; tempAction.damage = newdmg; tempAction.range = newrng; return(tempAction); }
public void EndUseAbility(int ability) { actionsOnStack.Clear(); actionData tempAction = actionManager.ConfirmAction(ability, currentSoldier, lookTarget, this.GetComponent <TurnManager>()); actionsOnStack.Add(tempAction); if (actionManager.CheckActionPoints(lastActionPress, actionRemaining, currentSoldier, lookTarget, GetComponent <TurnManager>()) == false) { lastActionPress = -1; } // lastActionPress = -1; if (actionsOnStack.Count > 0) { CheckReactions(tempAction); } }
public float PerformAction(actionData act, TurnManager turnManager) { float actionTimer = 0.0f; switch (act.actionNumber) { case 0: // uiManager.UpdateScrollingText(" fire gun "); actionTimer = act.actionTime; FireGun(act, turnManager); break; case 1: Debug.Log("jump"); //SetCurrentAction(currentAction.team, currentAction.ownerpublic, 1, 1, 0); actionTimer = act.actionTime; act.actor.GetComponent <Rigidbody>().AddForce(Vector3.up * 510.0f * Time.deltaTime, ForceMode.Impulse); break; case 2: //regular move turnManager.IncrementMove(0); break; case 3: //overwatch act.actor.currentreactPoints += act.damage; act.actor.inOverwatch = true; act.actor.loadout.reactionActions.Add(1); turnManager.reactsOnStack.Add(ConfirmReaction(1, act.actor, turnManager)); break; default: break; } if (act.actor.inOverwatch == true && act.actor.currentreactPoints <= 0) { act.actor.inOverwatch = false; } return(actionTimer); }
public void CheckReactions(actionData act) { foreach (reaction el in reactsOnStack) { Debug.Log("check react"); //overwatch is temporary if (el.actor.inOverwatch == false && el.action == 1) { } else { //confirm the action matches the reaction event if (act.reactType == el.reacttype && Vector3.Distance(el.actor.transform.position, act.loc) < el.range && el.actor.currentreactPoints >= el.cost) { //confirm the target is the correct team if (((act.team == el.actor.team && el.teamtype == 0) || (act.team != el.actor.team && el.teamtype == 1)) && el.actor != act.actor) { Debug.Log(" confirmed react"); actionsOnStack.Add(actionManager.ConfirmAction(el.action, el.actor, act.target, this.GetComponent <TurnManager>())); el.actor.usedReaction = true; el.actor.currentreactPoints -= el.cost; if (el.actor.inOverwatch == true && el.action == 1) { el.actor.inOverwatch = false; } } } } } if (actionsOnStack.Count > 1) { cam.active = false; reactCam.active = true; } reacting = true; }
//Event called by inputManager, gives input with startPos and endPos in pixel coords, and the type of input given // 0 = Tap, 1 = Held Press, 2 = LeftSwipe, 3 = Right Swipe, 4 = UpSwipe, 5 = DownSwipe public void TouchEvent(Vector2 touchStartPos, Vector2 touchEndPos, int touchType) { //If no action is currently being performed, process this one if (!actionInProgress) { //Log touch event receipt //LogAction(string.Format ("actionManager: Touch Event received:(StartPos:{0},EndPos:{1},TypeVal:{2})). Checking for action to perform...",touchStartPos,touchEndPos,(inputType)touchType)); //Set action in progress actionInProgress = true; //Check through registered actions to see if we have one to perform for (int i = 0; i < (actionCount); i++) { //Get current action actionData actData = registeredActions [i]; //Skip disabled actions if (!(actData.enabled)) { continue; } //Skip when action doesnt match registered one if ((int)actData.inpType != touchType) { continue; } //Convert touch pos to world space Vector3 tpWs = new Vector3(); Camera cam = Camera.main; tpWs = cam.ScreenToWorldPoint(new Vector3(touchStartPos.x, touchStartPos.y, 0)); tpWs.z = 0; //Log position of touch //print ("tpWs: " + tpWs.ToString ()); //Log position of actionable obj //print ("objPos: " + actData.obj.transform.position); bool hasRenderer = (actData.obj.GetComponent <Renderer> () == null) ? false : true; bool hasSpriteRenderer = (actData.obj.GetComponent <SpriteRenderer> () == null) ? false : true; //bool hasRect = (actData.obj.GetComponent<Rect> () == null) ? false : true; //Result of this is always false, need new method? bool hasRectTrans = (actData.obj.GetComponent <RectTransform> () == null) ? false : true; //Log touched obj properties //print (string.Format ("hasRenderer: {0}\thasSpriteRenderer:{1}\thasRect:{2}\thasRectTrans:{3}", hasRenderer, hasSpriteRenderer, hasRect, hasRectTrans)); //If none of the above options then continue to check next action if (!hasRenderer && !hasSpriteRenderer && !hasRectTrans) { continue; } //If it has a regular renderer and touch is within its bounds if (hasRenderer) { if (actData.obj.GetComponent <Renderer> ().bounds.Contains(tpWs)) { //Log the action LogAction("Renderer: " + actData.obj.name + " " + (inputType)touchType + " action started."); //We got this far, invoke the function with the args! actData.func.Method.Invoke(actData.func.Target, actData.funcArgs); //Continue to next action to check continue; } else { continue; } } //If it has a sprite renderer and touch is within its bounds if (hasSpriteRenderer) { if (actData.obj.GetComponent <SpriteRenderer> ().bounds.Contains(tpWs)) { //Log the action LogAction("SpriteRenderer: " + actData.obj.name + " " + (inputType)touchType + " action started."); //We got this far, invoke the function with the args! actData.func.Method.Invoke(actData.func.Target, actData.funcArgs); //Continue to next action to check continue; } else { continue; } } //If it has a RectTrans and a Rect and touch is within its bounds if (hasRectTrans) { //Get world space position values for corners Vector3[] fourCornersArr = new Vector3[4]; actData.obj.GetComponent <RectTransform> ().GetWorldCorners(fourCornersArr); float xMin = fourCornersArr[0].x; float xMax = fourCornersArr[3].x; float yMin = fourCornersArr[0].y; float yMax = fourCornersArr[1].y; //print ("xMin: " + xMin + " " + "xMax: " + xMax + " " + "yMin: " + yMin + " " + "yMax: " + yMax); //Check if within X bounds if ((tpWs.x >= xMin) && (tpWs.x <= xMax)) { //Check if within y bounds if ((tpWs.y >= yMin) && (tpWs.y <= yMax)) { //Within bounds, log action & run the function associated with it! LogAction("Obj: " + actData.obj.name + " " + (inputType)touchType + " action started."); actData.func.Method.Invoke(actData.func.Target, actData.funcArgs); } } //Continue to check next action in list continue; } } //Reset action in progress so that next action can occur actionInProgress = false; } else { LogAction("Warning: Action ignored, action already running."); }; }