public void Scan(Scannable scanned) { _cooldownCounter = _cooldown + Random.Range(-1.5f, 0.5f); Debug.Log(scanned.GetName()); _scannedGoods.Add(scanned); _beep = true; }
public void Deploy() //TODO: if interactable deploy should check puzzle ID { ActivateScanner(false); if (m_PreviewScanned) { //wont deploy if (!m_PreviewScanned.IsDeployable || m_timeLeftToDeploy > 0) { Destroy(m_PreviewScanned.gameObject); } //deploy else { m_PreviewScanned.OnDeploy(); m_PreviewScanned.gameObject.transform.SetParent(null); m_ChargeTimeEnd = Time.time + DeployChargeTime; if (((1 << m_PreviewScanned.gameObject.layer) & recalculateLayerMask) != 0) { var collider = m_PreviewScanned.gameObject.GetComponent <Collider2D>(); if (collider) { AstarPath.active.UpdateGraphs(collider.bounds); } } } m_PreviewScanned = null; } }
public override void Act(NPCAI npc, Scannable target) { NavMeshAgent agent = npc.GetComponent <NavMeshAgent>(); //Debug.Log("isStopped" + agent.isStopped); IsMoving = !agent.isStopped; }
public override void Act(NPCAI npc, Scannable target) { //Move towrds the target on the x and z axes, but not the y axis. Vector3 targetPosition = new Vector3(target.transform.position.x, npc.transform.position.y, target.transform.position.z); MoveNPCTowardsPosition(npc, targetPosition, movementSpeed, turnSpeed); }
private void OnTriggerExit2D(Collider2D collision) { if (m_previewKeyItem) { m_previewKeyItem.OnDeployed -= KeyDeployed; m_previewKeyItem = null; } }
private void OnTriggerEnter2D(Collider2D other) { var l = other.gameObject.GetComponent<Scannable>(); if(l) { m_l = l; m_l.OnDeployed += M_l_OnDeployed; } }
public override void Plan(NPCAI npc, Scannable target) { // Find the furtherest point you can get to for adjustmentDirections evenly distributed directions around a circle, if you try to travel //target distance in that direction. Vector3[] points = new Vector3[adjustmentDirections]; float angleSteps = 360f / adjustmentDirections; for (int i = 0; i < points.Length; i++) { //Raycast in each direction around the circle. Vector3 currentDirection = Quaternion.Euler(0, angleSteps * i, 0) * Vector3.forward; Ray ray = new Ray(npc.transform.position, currentDirection); RaycastHit[] hits = Physics.RaycastAll(ray, targetDistance); //Find closest hit for each direction. if (hits.Length <= 0) { //If there were no hits, the NPC can move target distance in the current direction. points[i] = ray.GetPoint(targetDistance); continue; } else { //If there were hits, find the closest. Vector3 closestHitPoint = hits[0].point; for (int j = 1; j < hits.Length; j++) { float distanceToClosestPoint = Vector3.Distance(closestHitPoint, npc.transform.position); float distanceToCurrentPoint = Vector3.Distance(hits[j].point, npc.transform.position); if (distanceToCurrentPoint < distanceToClosestPoint) { closestHitPoint = hits[j].point; } } points[i] = closestHitPoint; } } //Find furthest point from target - that is the target point. Vector3 furtherestPointFromTarget = points[0]; for (int i = 1; i < points.Length; i++) { float distanceBetweenFurthestPointAndTarget = Vector3.Distance(furtherestPointFromTarget, target.transform.position); float distanceBetweenCurrentPointAndTarget = Vector3.Distance(points[i], target.transform.position); if (distanceBetweenCurrentPointAndTarget > distanceBetweenFurthestPointAndTarget) { furtherestPointFromTarget = points[i]; } } targetPosition = furtherestPointFromTarget; }
private void OnTriggerExit2D(Collider2D other) { var scannable = other.GetComponent <Scannable>(); if (m_ObectToScanInRange && scannable == m_ObectToScanInRange) { m_ObectToScanInRange.OnScanStop(); m_ObectToScanInRange = null; } }
public override void Cease(NPCAI npc, Scannable target) { NavMeshAgent agent = npc.GetComponent <NavMeshAgent>(); if (agent == null) { throw new System.Exception(npc.name + " is trying to use the NavigateTowards behavour without having a NavMeshAgent"); } agent.ResetPath(); }
/// <summary> /// Sets the NavMeshAgent destination to the position of the target, if it exists. /// </summary> /// <param name="npc"></param> /// <param name="target"></param> private void NaviagteTowardsTarget(NPCAI npc, Scannable target) { NavMeshAgent agent = npc.GetComponent <NavMeshAgent>(); if (agent == null) { throw new System.Exception(npc.name + " is trying to use the NavigateTowards behavour without having a NavMeshAgent"); } agent.speed = moveSpeed; agent.SetDestination(target.transform.position); }
IEnumerator BeginReading(Scannable scannable, bool readOrWrite) { print("Begin reading..."); scanning = true; timeToScan = scannable.TimeToScan(readOrWrite); yield return(new WaitForSeconds(timeToScan)); print("Finished reading!"); scanning = false; finishedScan = true; trail.enabled = false; }
private void OnTriggerStay2D(Collider2D other) { if (!m_ObectToScanInRange) { var scannable = other.GetComponent <Scannable>(); if (scannable) { m_ObectToScanInRange = scannable; m_timeLeftToScan = ScanTime; } } }
public void StartPreview(Vector2 mouseLocation) { if (m_ScannedObject && !m_PreviewScanned && m_ChargeTimeEnd < Time.time) { var objToDeploy = Instantiate(m_ScannedObject.grantedObjectPrefab, GetPreviewPosition(mouseLocation), Quaternion.identity); m_PreviewScanned = objToDeploy.GetComponent <Scannable>(); m_PreviewScanned.transform.up = -transform.up; m_PreviewScanned.gameObject.transform.SetParent(gameObject.transform); m_PreviewScanned.OnPreviewStart(); m_timeLeftToDeploy = DeployTime; Debug.Log("startPreview"); } }
IEnumerator Scan(string programming, Color c, bool readOrWrite) { gunA.StopPlayback(); gunA.Play("GunCenter"); trail.enabled = true; trail.SetColors(c, c); while (true) { if (Input.GetAxis(programming) < 1) { StopCoroutine("BeginReading"); scanning = false; gunA.StopPlayback(); gunA.Play("GunReturn"); gunT.localRotation = Quaternion.identity; trail.enabled = false; finishedScan = false; break; } trail.SetPosition(0, laserT.position); if (!finishedScan && Physics.Raycast(camT.position, camT.forward, out ray, scanLength, layerMask)) { gunT.rotation = Quaternion.RotateTowards(gunT.rotation, Quaternion.LookRotation((ray.point - gunT.position).normalized), 5f); scannable = ray.transform.GetComponent <Scannable>(); if (scannable != null) { SetLaser(c, ray.point); if (!scanning) { StartCoroutine(BeginReading(scannable, readOrWrite)); } } else { SetLaser(missingColour, ray.point); } } else { StopCoroutine("BeginReading"); scanning = false; gunT.localRotation = Quaternion.RotateTowards(gunT.localRotation, Quaternion.identity, 5f); SetLaser(missingColour, laserT.position + laserT.up * scanLength); } yield return(null); } }
public override void Plan(NPCAI npc, Scannable target) { float distance = Vector3.Distance(npc.transform.position, target.transform.position); //if scary thing is within avoid set new path directly away from scary thing if (distance < avoidDistance) { Vector3 dirToScare = npc.transform.position - target.transform.position; agent = npc.GetComponent <NavMeshAgent>(); agent.speed = moveSpeed; agent.SetDestination(npc.transform.position + dirToScare); } }
private void OnTriggerExit2D(Collider2D other) { var l = other.gameObject.GetComponent<Scannable>(); if(l && m_l == l) { m_l.OnDeployed -= M_l_OnDeployed; m_l = null; return; } var player = other.gameObject.GetComponent<PlayerController>(); if(player && !m_l) { m_Collider.isTrigger = false; } }
protected override ICollection <Scannable> Scan(Transform originTranform) { List <Scannable> scannables = new List <Scannable>(); Collider[] colliders = Physics.OverlapBox(regionCentre, regionSize / 2); foreach (Collider c in colliders) { Scannable s = c.GetComponent <Scannable>(); if (s != null) { scannables.Add(s); } } return(scannables); }
protected override void TriggerEntered(Collider2D collision) { var keyItem = collision.GetComponent <KeyItem>(); if (TrySolve(keyItem)) { var scannable = collision.GetComponent <Scannable>(); if (scannable && scannable.IsPreview) { m_previewKeyItem = scannable; m_previewKeyItem.OnDeployed += KeyDeployed; } else { OnSolve(keyItem.gameObject); } } }
protected override ICollection <Scannable> Scan(Transform originTransform) { //Find all colliderable objects nearby. Collider[] objsNearby = Physics.OverlapSphere(originTransform.position, scanRadius); //Filter out the scannable objects and return them. List <Scannable> scannables = new List <Scannable>(); new List <Collider>(objsNearby).ForEach(obj => { Scannable scannable = obj.GetComponent <Scannable>(); if (scannable != null) { scannables.Add(scannable); } }); return(new List <Scannable>(scannables)); }
public override bool IsValidTarget(Scannable target, NPCAI npc) { //Find where the target and NPC are on the NavMesh. NavMeshHit npcPosition; NavMeshHit targetPosition; NavMesh.SamplePosition(npc.transform.position, out npcPosition, POINT_TOLERANCE, NavMesh.AllAreas); NavMesh.SamplePosition(target.transform.position, out targetPosition, POINT_TOLERANCE, NavMesh.AllAreas); if (targetPosition.position.Equals(new Vector3(Mathf.Infinity, Mathf.Infinity, Mathf.Infinity))) { return(false); } //Try to find a path between the two points and return whether is is successful. NavMeshPath path = new NavMeshPath(); NavMesh.CalculatePath(npcPosition.position, targetPosition.position, NavMesh.AllAreas, path); return(path.status == NavMeshPathStatus.PathComplete); }
public override Scannable SelectTarget(ICollection <Scannable> targets, NPCAI npc) { Scannable nearestScannable = null; foreach (Scannable target in targets) { //To begin with, set the nearest scannable to the first item in scannables. if (nearestScannable == null) { nearestScannable = target; continue; } float distanceToCurrent = Vector3.Distance(npc.transform.position, target.transform.position); float distanceToNearest = Vector3.Distance(npc.transform.position, nearestScannable.transform.position); if (distanceToCurrent < distanceToNearest) { nearestScannable = target; } } return(nearestScannable); }
public override void Adjust(NPCAI npc, Scannable target) { }
public override void Act(NPCAI npc, Scannable target) { Turn(target.transform.position, npc.transform, turnSpeed); }
/// <summary> /// Called when a target of another type is found - perform operations to cleanly stop current behaviour. /// </summary> /// <param name="npc"></param> /// <param name="target"></param> public abstract void Cease(NPCAI npc, Scannable target);
/// <summary> /// Describes a frame of the behaviour. /// </summary> /// <param name="instigator"></param> public abstract void Act(NPCAI npc, Scannable target);
/// <summary> /// Called when the target has first changed. /// </summary> /// <param name="npc"></param> /// <param name="target"></param> public abstract void Plan(NPCAI npc, Scannable target);
public override void Cease(NPCAI npc, Scannable target) { }
public abstract bool IsValidTarget(Scannable target, NPCAI npc);
public override void Plan(NPCAI npc, Scannable target) { }
private void Update() { //Check NPC's health (if it has any) and kill it if it reaches 0. if (stat != null && stat.currentHealth <= 0) { npcAnimator.Animation = "Die"; return; } if (stone) { return; } foreach (Reaction reaction in CurrentReactions) { //Use the reaction's scanner to search for objects of the relevant type, and skip reaction if none are found. List <Scannable> detectedObjects = reaction.scanner.ScanFor(reaction.objectType, transform); if (detectedObjects.Count <= 0) { continue; } Scannable targetScannable = reaction.targetSelectionCritera.FilterAndSelect(detectedObjects, this); //If the type of the target has changed, initialise a new behavior. if (!ReferenceEquals(currentReaction, reaction)) { //Cease performing current behaviour, if one was being performed. if (currentReaction != null) { currentReaction.reaction.Cease(this, targetScannable); } //Start new one. reaction.reaction.Plan(this, targetScannable); currentReaction = reaction; //Change the animation. if (npcAnimator != null) { npcAnimator.Animation = reaction.animation; } } //Otherwise, if information about the target has changed, adjust plan for new information. else if (!nearestScannablePosition.Equals(targetScannable.transform.position)) { reaction.reaction.Adjust(this, targetScannable); currentTarget = targetScannable; nearestScannablePosition = targetScannable.transform.position; } reaction.reaction.Act(this, targetScannable); SetIsMoving(reaction.reaction.IsMoving); return; //Only react to highest priority scannable. } //If the stopping condition for the behavior has been met, stop the behavior. if (currentReaction != null && currentReaction.reaction.ExtraStoppingCondition) { currentReaction.reaction.Cease(this, currentTarget); SetIsMoving(false); if (npcAnimator != null) { npcAnimator.Animation = "Idle"; } currentReaction = null; } }