public static Vector3 Vector(this MovDir direction) { return(direction == MovDir.BACK ? Vector3.back : direction == MovDir.FORWARD ? Vector3.forward : direction == MovDir.LEFT ? Vector3.left : Vector3.right); }
public override void CharacterUpdate() { if ((transform.position - king.transform.position).magnitude < runRadius) { // The tax collector should raycast in all 4 directions and go in // whichever direction has the most space and is away from the king float[] distances = new float[4]; int indexOfGreatest = 0; for (int i = 0; i < 4; i++) { // if the ray direction is close to the king's direction //then continue to the next direction Vector3 kingPos = king.gameObject.transform.position; MovDir rayDir = (MovDir)i; MovDir kingDir; MovDir dontUseThis; Mover.WaypointDirection( this, kingPos, out kingDir, out dontUseThis); if (rayDir == kingDir) { distances[i] = 0; continue; } RaycastHit rayInfo; LayerMask defaultLayer = LayerMask.NameToLayer("Default"); Physics.Raycast( transform.position + rayStartingHeight, rayDir.Vector(), out rayInfo, 200, 1); distances[i] = rayInfo.distance; if (rayInfo.distance > distances[indexOfGreatest] || rayInfo.distance == 0) { indexOfGreatest = i; } } Mover.MoveCharacter( this, out charInTheWay, true, (MovDir)indexOfGreatest, Physics.DefaultRaycastLayers); } }
public override void CharacterUpdate() { currInputDelay -= 1; if (currInputDelay <= 0) { currInputDelay = 0; questionMark.SetActive(true); } if (currDelay <= 0) { currDelay = ActionDelay; //Vector3 waypointPos = objectiveManager.Waypoint; Character hitCharacter = null; MovDir backup = currDir; Mover.MoveCharacter(this, out charInTheWay, true, currDir, Physics.DefaultRaycastLayers); currDir = backup; /* * if (!Mover.MoveCharacterToWayPoint( * this, * out hitCharacter, * true, * waypointPos, * Physics.DefaultRaycastLayers)) * { * Mover.MoveCharacterToWayPoint( * this, * out hitCharacter, * true, * -waypointPos, * Physics.DefaultRaycastLayers); * } */ objectiveManager.AttemptToCompleteObjective(); } else { currDelay -= 1; } }
public static void WaypointDirection( Character character, Vector3 waypoint, out MovDir primaryDirection, out MovDir secondaryDirection) { Vector3 vector = waypoint - character.transform.position; bool forwardLeft = vector.z > vector.x; bool forwardRight = vector.z + vector.x > 0; bool forward = vector.z > 0; bool right = vector.x > 0; if (forwardLeft) { if (forwardRight) { primaryDirection = MovDir.FORWARD; secondaryDirection = right ? MovDir.RIGHT : MovDir.LEFT; } else // backLeft { primaryDirection = MovDir.LEFT; secondaryDirection = forward ? MovDir.FORWARD : MovDir.BACK; } } else // backRight { if (forwardRight) { primaryDirection = MovDir.RIGHT; secondaryDirection = forward ? MovDir.FORWARD : MovDir.BACK; } else // backLeft { primaryDirection = MovDir.BACK; secondaryDirection = right ? MovDir.RIGHT : MovDir.LEFT; } } }
public override void CharacterUpdate() { // Move in direction set last turn if (directionForNextTurn != MovDir.NONE) { Mover.MoveCharacter( this, out charInTheWay, false, directionForNextTurn, Physics.DefaultRaycastLayers); movementArrows[(int)directionForNextTurn].SetActive(false); } // Pick direction for next turn if (Random.value <= chanceToMove) { directionForNextTurn = (MovDir)Random.Range(0, 4); movementArrows[(int)directionForNextTurn].SetActive(true); } else { directionForNextTurn = MovDir.NONE; } }
public static bool MoveCharacter( Character sourceCharacter, out Character hitCharacter, bool pushHitCharacter, MovDir direction, LayerMask layerMask) { hitCharacter = null; // set sourceCharacter.currDir to direction or else characters // won't be able to push other characters sourceCharacter.currDir = direction; if (direction == MovDir.NONE) { return(false); } layerMask &= ~LayerMask.GetMask("Corpses"); //Do raycast Vector3 dir = direction.Vector(); Vector3 sourcePos = sourceCharacter.transform.position + sourceCharacter.rayStartingHeight; RaycastHit rayInfo; Physics.Linecast(sourcePos, sourcePos + dir, out rayInfo, layerMask); //Debug.DrawLine(sourcePos, sourcePos + dir, Color.red); //If we hit something, check if it blocks our path bool isBlocked = rayInfo.transform != null; if (isBlocked) { //Check if the blocking thing is a character hitCharacter = rayInfo.transform.gameObject.GetComponent <Character>(); if (hitCharacter == null) { return(false); } //If the function cant interact with objects then ignore the hit object and dont move, if it can, do an interact if (!pushHitCharacter) { return(false); } //If the character we interact with tells us that we shouldnt move, //we return false and we dont move //if it doesnt, then we move if (!hitCharacter.Interact(sourceCharacter)) { return(false); } } //If the way is clear, then move sourceCharacter and return true //The movements should ONLY be done in 1 unit increment //ie: I move right so dir = 1,0,0 sourceCharacter.prevModelPos = sourceCharacter.transform.position; sourceCharacter.transform.position += dir; sourceCharacter.MoveModelToPos(sourceCharacter.transform.position); sourceCharacter.currDir = MovDir.NONE; return(true); }