// Delete the closest node to proximity of 1.0f on click void DeleteNode() { Event current = Event.current; WaypointNode dNode = null; if (!current.alt) { Ray mRay = HandleUtility.GUIPointToWorldRay(current.mousePosition); RaycastHit rHit; if (Physics.Raycast(mRay, out rHit, Mathf.Infinity)) { dNode = currentWaypoints.GetClosestNode(rHit.point, 1.0f); } if (dNode != null) { Handles.DrawWireDisc(dNode.Position, Vector3.up, 0.5f); } if (current.type == EventType.mouseDown && dNode != null) { foreach (WaypointLink p in dNode.NeighborNodes) { WaypointNode node = (WaypointNode)currentWaypoints.GetClosestNode(p.from); node.RemoveLink(p); node = (WaypointNode)currentWaypoints.GetClosestNode(p.to); node.RemoveLink(p); } currentWaypoints.WaypointList.Remove(dNode); dNode = null; } } }
WaypointNode GetNextWaypoint(int currentIndex) { WaypointNode nextNode = null; try { var waypoints = _waypoint.GetComponentsInChildren <WaypointNode>(); if (currentIndex < waypoints.Length) { nextNode = waypoints[currentIndex]; } } catch (UnityException ue) { nextNode = null; } if (nextNode != null) { try { Transform temp = nextNode.transform.FindChild("Stoplight"); if (temp != null) { stoplight = temp.gameObject; } else { stoplight = null; } } catch (UnityException ue) { stoplight = null; } } return(nextNode); }
private void MakeRailTo(WaypointNode src, WaypointNode dst, int segmentID) { Vector3 a = src.transform.localPosition; Vector3 b = dst.transform.localPosition; Vector3 dir = (b - a).normalized; a += dir * src.radius; b -= dir * dst.radius; float distance = Vector3.Distance(a, b); waypoints.Add(a); waypoints.Add(b); TrackSection tsp = Instantiate(trackSectionPrefab); tsp.SetEnds(src, dst); tsp.Init(); tsp.transform.SetParent(renderersHolder); tsp.generator = this; tsp.SetPositions(new List <Vector3> { a, b }.ToArray()); }
private void GenRail(WaypointNode curr) { grey.Add(curr); foreach (WaypointNode node in curr.Children) { if (!grey.Contains(node)) { GenRail(node); } if (!black.Contains(node)) { MakeRailTo(curr, node, grey.Count); } } foreach (KeyValuePair <WaypointNode, List <WaypointNode> > connections in curr.Connections) { foreach (WaypointNode node in connections.Value) { MakeCurveRail(curr, connections.Key, node, grey.Count); } } black.Add(curr); }
private void MakeCurveRail(WaypointNode curr, WaypointNode prev, WaypointNode next, int segmentID) { Vector3 a = curr.transform.localPosition + GetDirection(curr.transform, prev.transform) * curr.radius; Vector3 b = curr.transform.localPosition; Vector3 c = curr.transform.localPosition + GetDirection(curr.transform, next.transform) * curr.radius; //float distance = Vector3.Distance(a, b); float distance = (Mathf.PI / 2) * curr.radius; int midpoints = (int)(distance / step); float angleStep = step / (curr.radius * (Mathf.PI / 2)); List <Vector3> points = new List <Vector3>(); TrackSection tsp = Instantiate(trackSectionPrefab); tsp.SetEnds(prev, next); tsp.Init(); for (int i = 0; i < midpoints; i++) { Vector3 x = Vector3.Lerp(a, b, i * angleStep); Vector3 y = Vector3.Lerp(b, c, i * angleStep); points.Add(Vector3.Lerp(x, y, i * angleStep)); } tsp.transform.SetParent(curr.transform); tsp.generator = this; tsp.SetPositions(points.ToArray()); }
public void init() { pos = Map.grid.grid_to_world(Map.grid.world_to_grid(transform.position)); current_node = Map.grid.get_node(Map.grid.world_to_grid(pos)); controller = GetComponent <PlatformCollision>(); controller.init(); player_movement = GetComponent <PlayerMovement>(); player_movement.init(); Transform weapon_obj = transform.parent.FindChild("weapon"); if (weapon_obj == null) { Debug.LogError("'weapon' object cannot be found in player parent's children"); } weapon_inventory = weapon_obj.GetComponent <WeaponInventory>(); weapon_inventory.init(this); weapon_control = weapon_obj.GetComponent <PlayerWeaponControl>(); weapon_control.init(); grappling_hook = GetComponent <GrapplingHook>(); grappling_hook.init(); health = GetComponent <GenericHealth>(); health.init(this); }
public void get_next_path_node() { if (path != null && path.Count != 0 && current_path_index < path.Count) { next_node = path[current_path_index]; } }
void OnArrived(Creature owner) { if (_path == null || _path.nodes.Empty()) { return; } Cypher.Assert(_currentNode < _path.nodes.Count, $"WaypointMovementGenerator.OnArrived: tried to reference a node id ({_currentNode}) which is not included in path ({_path.id})"); WaypointNode waypoint = _path.nodes.ElementAt((int)_currentNode); if (waypoint.delay != 0) { owner.ClearUnitState(UnitState.RoamingMove); _nextMoveTime.Reset((int)waypoint.delay); } if (waypoint.eventId != 0 && RandomHelper.URand(0, 99) < waypoint.eventChance) { Log.outDebug(LogFilter.MapsScript, $"Creature movement start script {waypoint.eventId} at point {_currentNode} for {owner.GetGUID()}."); owner.ClearUnitState(UnitState.RoamingMove); owner.GetMap().ScriptsStart(ScriptsType.Waypoint, waypoint.eventId, owner, null); } // inform AI CreatureAI ai = owner.GetAI(); if (ai != null) { ai.MovementInform(MovementGeneratorType.Waypoint, (uint)_currentNode); ai.WaypointReached(waypoint.id, _path.id); } owner.UpdateCurrentWaypointInfo(waypoint.id, _path.id); }
public WaypointRoute(MovingObject movingObject, WaypointNode waypoint) { _waypoints = new Queue <WaypointNode>(); _waypoints.Enqueue(waypoint); Destination = waypoint; _movingObject = movingObject; }
void OnArrived(Creature creature) { if (path == null || path.nodes.Empty()) { return; } WaypointNode waypoint = path.nodes.LookupByIndex((int)currentNode); if (waypoint.delay != 0) { creature.ClearUnitState(UnitState.RoamingMove); Stop((int)waypoint.delay); } if (waypoint.eventId != 0 && RandomHelper.URand(0, 99) < waypoint.eventChance) { Log.outDebug(LogFilter.Unit, "Creature movement start script {0} at point {1} for {2}.", waypoint.eventId, currentNode, creature.GetGUID()); creature.ClearUnitState(UnitState.RoamingMove); creature.GetMap().ScriptsStart(ScriptsType.Waypoint, waypoint.eventId, creature, null); } // Inform script MovementInform(creature); creature.UpdateWaypointID(currentNode); creature.SetWalk(waypoint.moveType != WaypointMoveType.Run); }
public override void getPelletPositions(WaypointNode one, WaypointNode two, out List <Vector3> positions) { positions = new List <Vector3> (); int distance = (int)calculateDistance(one.transform.position, two.transform.position); Vector3 direction1 = (one.transform.position - transform.position).normalized; Vector3 direction2 = (two.transform.position - transform.position).normalized; Collider col = GetComponent <Collider> (); for (int i = 0; i <= distance; i++) { float ratio = (float)i / (float)distance; Vector3 direction = Vector3.Slerp(direction1, direction2, ratio); Ray ray = new Ray(transform.position, direction); // reverse ray ray.origin = ray.GetPoint(2 * planetRadius); ray.direction = -direction; RaycastHit hit; col.Raycast(ray, out hit, 2 * planetRadius); Vector3 position = hit.point; //print (position); position += direction * 0.25f; positions.Add(position); } }
public void DrawEdge(WaypointNode neighbor) { var drawColor = Color.cyan; drawColor.a = .5f; Gizmos.color = drawColor; // Offset slightly to one side, so that one-way edges are clearer Vector3 start = transform.position; Vector3 end = neighbor.transform.position; Vector3 dir = (end - start).normalized; Vector3 side = Vector3.Cross(transform.up, dir); var drawMagnitude = .5f; start += drawMagnitude * side; end += drawMagnitude * side; Gizmos.DrawLine(start, end); // Draw an arrow head part-way along the line too (slightly closer to the end) const float offsetFraction = .65f; var arrowStart = offsetFraction * end + (1.0f - offsetFraction) * start; var arrowSide1 = arrowStart - drawMagnitude * dir + drawMagnitude * side; var arrowSide2 = arrowStart - drawMagnitude * dir - drawMagnitude * side; Gizmos.DrawLine(arrowStart, arrowSide1); Gizmos.DrawLine(arrowStart, arrowSide2); }
protected void linkNodes(WaypointNode one, float uCoordA, float vCoordA, WaypointNode two, float uCoordB, float vCoordB) { float diffU = Mathf.Abs(uCoordA - uCoordB); float diffV = Mathf.Abs(vCoordA - vCoordB); if (diffU > diffV) { if (uCoordA > uCoordB) { one.setLeft(two); two.setRight(one); } else { one.setRight(two); two.setLeft(one); } } else { if (vCoordA > vCoordB) { one.setBack(two); two.setFront(one); } else { one.setFront(two); two.setBack(one); } } }
public void recalc_path() { if (next_node != null) { current_node = next_node; } Vector2 dest = Map.grid.world_to_grid(Entities.player.pos); List <WaypointNode> temp_path = Map.grid.find_path(Map.grid.find_neighbour_node(current_node), Map.grid.find_neighbour_node(Map.grid.get_node(dest.x, dest.y)), path_find_timeout_ms); if (temp_path.Count >= 2) { path = temp_path; } else if (path != null) { path.Clear(); } if (path != null && path.Count >= 2) { current_path_index = 1; get_next_path_node(); } }
//----------------------------------------------------------------------------------- // which directions are allowed now? //----------------------------------------------------------------------------------- protected List <Direction> allowedDirections() { // if two or more potential choices are an equal distance from the target, // the decision between them is made in the order of up > left > down (> right) List <Direction> directions = new List <Direction>(); Direction direction = walker.direction(); WaypointNode node = walker.getCurrentNode(); if (node.getFront() != null && !direction.isOpposite(Direction.Front)) { directions.Add(Direction.Front); } if (node.getLeft() != null && !direction.isOpposite(Direction.Left)) { directions.Add(Direction.Left); } if (node.getBack() != null && !direction.isOpposite(Direction.Back)) { directions.Add(Direction.Back); } if (node.getRight() != null && !direction.isOpposite(Direction.Right)) { directions.Add(Direction.Right); } return(directions); }
public List <WaypointNode> GetRouteTo(WaypointNode dest) { List <WaypointNode> route = new List <WaypointNode>(); int insertIndex = 0; WaypointNode begin = this; WaypointNode end = dest; while (begin.RecalculateLevel() > end.RecalculateLevel()) { begin = begin.PushTo(insertIndex, route); insertIndex++; } while (begin.RecalculateLevel() < end.RecalculateLevel()) { end = end.PushTo(insertIndex, route); } while (begin.RecalculateLevel() >= 0 && begin != end) { begin = begin.PushTo(insertIndex, route); insertIndex++; end = end.PushTo(insertIndex, route); } if (begin == null) { return(null); } else { begin.PushTo(insertIndex, route); return(route); } }
void FillPointMovementListForCreature() { var movePoints = Global.ScriptMgr.GetPointMoveList(me.GetEntry()); if (movePoints.Empty()) { return; } LastWP = movePoints.Last().uiPointId; foreach (var point in movePoints) { float x = point.fX; float y = point.fY; float z = point.fZ; GridDefines.NormalizeMapCoord(ref x); GridDefines.NormalizeMapCoord(ref y); WaypointNode wp = new WaypointNode(); wp.id = point.uiPointId; wp.x = x; wp.y = y; wp.z = z; wp.orientation = 0.0f; wp.moveType = m_bIsRunning ? WaypointMoveType.Run : WaypointMoveType.Walk; wp.delay = point.uiWaitTime; wp.eventId = 0; wp.eventChance = 100; _path.nodes.Add(wp); } }
public virtual void DoSwitch(WaypointNode swtch) { WaypointNode node = _connections[swtch][0]; _connections[swtch].RemoveAt(0); _connections[swtch].Add(node); }
public WaypointNode find_neighbour_node(WaypointNode start_node) { if (start_node.walkable) { return(start_node); } for (int n = 0; n < neighbours.Length; n += 2) { int x = (int)start_node.grid_pos.x + neighbours[n]; int y = (int)start_node.grid_pos.y + neighbours[n + 1]; if (x < 0 || x >= grid_width) { continue; } if (y < 0 || y >= grid_height) { continue; } WaypointNode node = get_node(x, y); if (node.walkable) { return(node); } } return(start_node); }
public override void OnInspectorGUI() { Handles.BeginGUI(); GUI.enabled = !_isActive; if (GUILayout.Button("start")) { _isActive = true; _start = _selected; } GUI.enabled = _isActive; if (GUILayout.Button("end")) { _isActive = false; _dest = _selected; _route = _start.GetRouteTo(_dest); } GUI.enabled = true; if (m_editMode) { if (GUILayout.Button("Disable Editing")) { EndEditMode(); } } else if (GUILayout.Button("Enable Editing")) { StartEditMode(); } Handles.EndGUI(); }
void FormationMove(Creature creature) { if (path == null || path.nodes.Empty()) { return; } bool transportPath = creature.GetTransport() != null; WaypointNode waypoint = path.nodes.LookupByIndex((int)currentNode); Position formationDest = new Position(waypoint.x, waypoint.y, waypoint.z, 0.0f); //! If creature is on transport, we assume waypoints set in DB are already transport offsets if (transportPath) { ITransport trans = creature.GetDirectTransport(); if (trans != null) { trans.CalculatePassengerPosition(ref formationDest.posX, ref formationDest.posY, ref formationDest.posZ, ref formationDest.Orientation); } } // Call for creature group update if (creature.GetFormation() != null && creature.GetFormation().getLeader() == creature) { creature.GetFormation().LeaderMoveTo(formationDest.posX, formationDest.posY, formationDest.posZ); } }
protected virtual Direction directionFrightened() { // ghosts use a pseudo-random number generator (PRNG) // to pick a way to turn at each intersection when frightened System.Random rnd = new System.Random(); Direction direction = (Direction)rnd.Next(4); if (validDirection(direction)) { return(direction); } // if a wall blocks the chosen direction, // the virus then attempts the remaining directions in this order: // up, left, down, and right, until a passable direction is found WaypointNode node = walker.getCurrentNode(); if (node.getFront() != null) { return(Direction.Front); } if (node.getLeft() != null) { return(Direction.Left); } if (node.getBack() != null) { return(Direction.Back); } if (node.getRight() != null) { return(Direction.Right); } return(Direction.None); }
public void init(WaypointNode cur, WaypointNode nex, Direction dir) { currentNode = cur; nextNode = nex; currentDirection = dir; _active = true; }
/// <summary> /// Computes the shorted path between the start and end nodes. /// Uses the Euclidean distance for edge costs. /// Note that this implementation is very inefficient - it uses Dijkstra's algorithm instead of A*, and does /// not have a proper priority queue. For the sizes of graphs that we're dealing with, this should be OK though. /// </summary> /// <param name="startNode"></param> /// <param name="endNode"></param> /// <returns>List of nodes for the path, or null if a path can't be found.</returns> public static List <WaypointNode> FindPath(WaypointNode startNode, WaypointNode endNode) { // TODO implement a priority queue. For now we have to linear search to find the lowest-cost node. // Distance from the startNode to each open node. Dictionary <WaypointNode, float> pathCost = new Dictionary <WaypointNode, float>(); // The parent of each node in the path, or null for the startNode Dictionary <WaypointNode, WaypointNode> parents = new Dictionary <WaypointNode, WaypointNode>(); // Nodes that have already been explored. HashSet <WaypointNode> closed = new HashSet <WaypointNode>(); parents[startNode] = null; pathCost[startNode] = 0.0f; while (!closed.Contains(endNode)) { if (pathCost.Count == 0) { // Unreachable return(null); } // "pop" the lowest cost node var currentNode = FindLowestValue(pathCost); var currentCost = pathCost[currentNode]; foreach (var neighbor in currentNode.Edges) { if (closed.Contains(neighbor)) { continue; } var costToNeighbor = Vector3.Distance(currentNode.transform.position, neighbor.transform.position); if (!pathCost.ContainsKey(neighbor) || currentCost + costToNeighbor < pathCost[neighbor]) { // Update cost and parent for the neighbor pathCost[neighbor] = currentCost + costToNeighbor; parents[neighbor] = currentNode; } } pathCost.Remove(currentNode); closed.Add(currentNode); } // Walk backwards from the goal List <WaypointNode> pathOut = new List <WaypointNode>(); var current = endNode; while (current != null) { pathOut.Add(current); current = parents[current]; } pathOut.Reverse(); return(pathOut); }
//------------------------------------------------------------------------ // start callbacks //------------------------------------------------------------------------ public virtual void startNavigation() { // set nodes currentNode = spawn; nextNode = currentNode; // move to spawn transform.position = spawn.transform.position; }
void Start() { ai_parent = GetComponent <AILogicFlyingEnemy>(); ai_parent.pos = Map.grid.grid_to_world(Map.grid.world_to_grid(transform.position)); ai_parent.pos.z = -10; current_node = Map.grid.get_node(Map.grid.world_to_grid(ai_parent.pos)); }
public WaypointRoute(MovingObject movingObject, List <WaypointNode> waypoints) { //Debug.Log(waypoints[0].gameObject.name); //Debug.Log(waypoints.Count); _waypoints = new Queue <WaypointNode>(waypoints); Destination = waypoints[waypoints.Count - 1]; //Debug.Log(Destination.gameObject.name); _movingObject = movingObject; }
public WaypointNode(int id, WaypointNode wpParent = null, List <WaypointNode> n = null, float f = 0, float g = 0, float h = 0) { position = transform.position; ID = id; parent = wpParent; neighbors = n; F = f; G = g; H = h; }
public void DeactivateWorker(BasicWorker worker, WaypointNode target) { worker.gameObject.SetActive(false); if (targets.IndexOf(target) != -1) { workersOnTarget[targets.IndexOf(target)]--; } waiting.Add(worker); workers.Remove(worker); }
public WaypointNode(Vector3 p, int id, WaypointNode wpParent = null, List <WaypointNode> n = null, float f = 0, float g = 0, float h = 0) { position = p; ID = id; parent = wpParent; neighbors = n; F = f; G = g; H = h; }
public WaypointNode(Vector3 p, int id, WaypointNode wpParent = null, List<WaypointNode> n = null, float f = 0, float g = 0, float h = 0) { position = p; ID = id; parent = wpParent; neighbors = n; F = f; G = g; H = h; }
public WaypointNode(int id, WaypointNode wpParent = null, List<WaypointNode> n = null, float f = 0, float g = 0, float h = 0) { position = transform.position; ID = id; parent = wpParent; neighbors = n; F = f; G = g; H = h; }
private bool NodesAlreadyConnected(WaypointNode n, WaypointNode a) { foreach (WaypointNode wpn in a.neighbors) { if (wpn == n) { return true; } } return false; }
public void check_arrived_next_node() { float dist = Mathf.Sqrt(Mathf.Pow(ai_parent.pos.x - next_node.world_pos.x, 2) + Mathf.Pow(ai_parent.pos.y - next_node.world_pos.y, 2)); if (dist <= 1) { try_recalc_path(); if (path != null && path.Count != 0 && current_path_index + 1 < path.Count) { ++current_path_index; next_node = path[current_path_index]; } } if (dist >= 4.0f) try_recalc_path(); }
public void recalc_path() { if (next_node != null) current_node = next_node; Vector2 dest = Map.grid.world_to_grid(Entities.player.pos); List<WaypointNode> temp_path = Map.grid.find_path(Map.grid.find_neighbour_node(current_node), Map.grid.find_neighbour_node(Map.grid.get_node(dest.x, dest.y)), path_find_timeout_ms); if (temp_path.Count >= 2) path = temp_path; else if (path != null) path.Clear(); if (path != null && path.Count >= 2) { current_path_index = 1; get_next_path_node(); } }
public void init() { pos = Map.grid.grid_to_world(Map.grid.world_to_grid(transform.position)); current_node = Map.grid.get_node(Map.grid.world_to_grid(pos)); controller = GetComponent<Controller2D>(); controller.init(); player_movement = GetComponent<PlayerMovement>(); player_movement.init(); Transform weapon_obj = transform.parent.FindChild("weapon_base"); if (weapon_obj == null) Debug.LogError("'weapon_base' object cannot be found in player parent's children"); inventory = weapon_obj.GetComponent<WeaponInventory>(); inventory.init(this); weapon_control = weapon_obj.GetComponent<PlayerWeaponControl>(); weapon_control.init(); grappling_hook = GetComponent<GrapplingHook>(); grappling_hook.init(); health = GetComponent<GenericHealth>(); health.init(this); }
public void SetWaypointNode(WaypointNode waypointNode) { if (waypointNode.destinationNodes.Length == 0) { nextNode = null; return; } if(currentNode!=null)currentNode.occupied = null; currentNode = waypointNode; distance = 0; int nodeIndex = Random.Range(0, waypointNode.destinationNodes.Length); for (int i=0; i<waypointNode.destinationNodes.Length; i++) { int index = (nodeIndex+i)%waypointNode.destinationNodes.Length; if (waypointNode.destinationNodes [index].occupied == null){ nodeIndex = index; break; } } nextNode = waypointNode.destinationNodes [nodeIndex]; if(nextNode!=null && nextNode.occupied == null)nextNode.occupied = gameObject; if(currentNode!=null)currentNode.occupied = gameObject; transform.position = currentNode.transform.position; virtualCamera.ApplyToTransform(transform); }
public void get_next_path_node() { if (path != null && path.Count != 0 && current_path_index < path.Count) next_node = path[current_path_index]; }
// Use this for initialization void Start() { enemiesAlive = 0; node = GetComponent<WaypointNode>(); //lastSpawnTime = Time.time; }
public NodeRecord(WaypointNode node) { this.node = node; costSoFar = 0; }
// Add node to the point in world. void AddNodes() { Event current = Event.current; if (!current.alt) { if (current.type == EventType.mouseDown && current.button == 0) { Ray mRay = HandleUtility.GUIPointToWorldRay(current.mousePosition); RaycastHit rHit; if (Physics.Raycast(mRay, out rHit, Mathf.Infinity)) { WaypointNode newNode = new WaypointNode(); newNode.Position = rHit.point; currentWaypoints.WaypointList.Add(newNode); Event.current.Use(); } } } }
void Update() { //an addition by lachlan to skip levels as we please if (Input.GetKeyDown(KeyCode.P)) { int p = Application.loadedLevel; Application.LoadLevel(p + 1); } if (Input.GetKeyDown(KeyCode.O)) { int o = Application.loadedLevel; Application.LoadLevel(o - 1); } grappling_hook.update(); if (grappling_hook.grapple_state == GrapplingHook.GrappleState.NONE) { update_scale(); player_movement.update(); } health.update(); weapon_control.update(); pos = transform.position; float world_bounds = 4.0f; Vector3 top_left = Camera.main.ScreenToWorldPoint(Vector3.zero); Vector3 bot_right = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height)); if (pos.x < top_left.x - world_bounds || pos.x > bot_right.x + world_bounds || pos.y < top_left.y - world_bounds || pos.y > bot_right.y + world_bounds) { //Lachlan changed this to restart current level not the first level //ie it used to restart the game on death (you should probably call void destroy here) Application.LoadLevel(Application.loadedLevel); } if (Input.GetKeyDown(KeyCode.Alpha1)) weapon_inventory.equip_weapon(weapon_inventory.weapons[0]); if (Input.GetKeyDown(KeyCode.Alpha2)) weapon_inventory.equip_weapon(weapon_inventory.weapons[1]); if (Input.GetKeyDown(KeyCode.Alpha3)) weapon_inventory.equip_weapon(weapon_inventory.weapons[2]); if (Input.GetKeyDown(KeyCode.Alpha4)) weapon_inventory.equip_weapon(weapon_inventory.weapons[3]); if (Input.GetKeyDown(KeyCode.Alpha5)) weapon_inventory.equip_weapon(weapon_inventory.weapons[4]); if (Input.GetKeyDown(KeyCode.Alpha6)) weapon_inventory.equip_weapon(weapon_inventory.weapons[5]); current_node = Map.grid.get_node(Map.grid.world_to_grid(pos)); }
void OnSceneGUI(SceneView view) { if (!Event.current.control && !Event.current.alt && Event.current.type == EventType.MouseUp && Event.current.button == 1) { Event e = Event.current; RaycastHit hit; Vector3 mousePos = new Vector3(e.mousePosition.x, -e.mousePosition.y + SceneView.lastActiveSceneView.camera.pixelHeight); Ray ray = SceneView.lastActiveSceneView.camera.ScreenPointToRay(mousePos); if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { if (waypointPrefab != null && canPlaceNewWaypoint && hit.transform.name != "Waypoint") { GameObject newWaypoint = (GameObject)PrefabUtility.InstantiatePrefab(waypointPrefab); newWaypoint.transform.position = hit.point; if (activeNode != null) { activeNode.isActive = false; } activeNode = newWaypoint.GetComponent<WaypointNode>(); activeNode.position = hit.point; activeNode.isActive = true; GameObject pFinder = GameObject.FindGameObjectWithTag("WPPathfinder"); newWaypoint.transform.parent = pFinder.transform; canPlaceNewWaypoint = false; EditorUtility.SetDirty(activeNode); } else if (hit.transform.name == "Waypoint") { if (activeNode != null) { activeNode.isActive = false; EditorUtility.SetDirty(activeNode); } activeNode = hit.transform.GetComponent<WaypointNode>(); activeNode.isActive = true; EditorUtility.SetDirty(activeNode); } } } else if (Event.current.control && !Event.current.alt && Event.current.type == EventType.MouseUp && Event.current.button == 1) { Event e = Event.current; RaycastHit hit; Vector3 mousePos = new Vector3(e.mousePosition.x, -e.mousePosition.y + SceneView.lastActiveSceneView.camera.pixelHeight); Ray ray = SceneView.lastActiveSceneView.camera.ScreenPointToRay(mousePos); if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { if (hit.transform.name == "Waypoint") { WaypointNode node = hit.transform.GetComponent<WaypointNode>(); if (node != activeNode && !NodesAlreadyConnected(node, activeNode)) { activeNode.neighbors.Add(node); node.neighbors.Add(activeNode); EditorUtility.SetDirty(activeNode); EditorUtility.SetDirty(node); } } } } else if (Event.current.alt && !Event.current.control &&Event.current.type == EventType.MouseUp && Event.current.button == 0) { Event e = Event.current; RaycastHit hit; Vector3 mousePos = new Vector3(e.mousePosition.x, -e.mousePosition.y + SceneView.lastActiveSceneView.camera.pixelHeight); Ray ray = SceneView.lastActiveSceneView.camera.ScreenPointToRay(mousePos); if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { if (hit.transform.name == "Waypoint") { WaypointNode node = hit.transform.GetComponent<WaypointNode>(); if (node != activeNode && NodesAlreadyConnected(node, activeNode)) { activeNode.neighbors.Remove(node); EditorUtility.SetDirty(activeNode); } } } } else if (Event.current.control && Event.current.alt && Event.current.type == EventType.MouseUp && Event.current.button == 1) { Event e = Event.current; RaycastHit hit; Vector3 mousePos = new Vector3(e.mousePosition.x, -e.mousePosition.y + SceneView.lastActiveSceneView.camera.pixelHeight); Ray ray = SceneView.lastActiveSceneView.camera.ScreenPointToRay(mousePos); if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { if (hit.transform.name == "Waypoint") { WaypointNode node = hit.transform.GetComponent<WaypointNode>(); GameObject[] wpList = GameObject.FindGameObjectsWithTag("Waypoint"); foreach (GameObject g in wpList) { if (g.GetComponent<WaypointNode>().neighbors.Contains(node)) { g.GetComponent<WaypointNode>().neighbors.Remove(node); } } GameObject.DestroyImmediate(hit.transform.gameObject); } } } else if (Event.current.alt && Event.current.control && Event.current.type == EventType.MouseUp && Event.current.button == 0) { Event e = Event.current; RaycastHit hit; Vector3 mousePos = new Vector3(e.mousePosition.x, -e.mousePosition.y + SceneView.lastActiveSceneView.camera.pixelHeight); Ray ray = SceneView.lastActiveSceneView.camera.ScreenPointToRay(mousePos); if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { if (hit.transform.name == "Waypoint") { WaypointNode node = hit.transform.GetComponent<WaypointNode>(); if (node != activeNode && NodesAlreadyConnected(node, activeNode) && NodesAlreadyConnected(activeNode, node)) { activeNode.neighbors.Remove(node); node.neighbors.Remove(activeNode); EditorUtility.SetDirty(activeNode); EditorUtility.SetDirty(node); } } } } canPlaceNewWaypoint = true; }
public Connection(double cost, WaypointNode fromNode, WaypointNode toNode) { this.cost = cost; this.fromNode = fromNode; this.toNode = toNode; }
void Start() { ai_parent = GetComponent<AILogicFlyingEnemy>(); ai_parent.pos = Map.grid.grid_to_world(Map.grid.world_to_grid(transform.position)); ai_parent.pos.z = -10; current_node = Map.grid.get_node(Map.grid.world_to_grid(ai_parent.pos)); }
public List<WaypointNode> find_path(WaypointNode start_node, WaypointNode end_node, float timeout_ms = 16) { return find_path((int)start_node.grid_pos.x, (int)start_node.grid_pos.y, (int)end_node.grid_pos.x, (int)end_node.grid_pos.y, timeout_ms); }
void Update() { recalc_path_timer += Time.deltaTime; current_node = Map.grid.get_node(Map.grid.world_to_grid(ai_parent.pos)); }
public WaypointNode find_neighbour_node(WaypointNode start_node) { if (start_node.walkable) return start_node; for (int n = 0; n < neighbours.Length; n += 2) { int x = (int)start_node.grid_pos.x + neighbours[n]; int y = (int)start_node.grid_pos.y + neighbours[n + 1]; if (x < 0 || x >= grid_width) continue; if (y < 0 || y >= grid_height) continue; WaypointNode node = get_node(x, y); if (node.walkable) return node; } return start_node; }
public float DistanceTo(WaypointNode node) { return new Vector2(transform.position.x - node.transform.position.x, transform.position.z - node.transform.position.z).magnitude; }
void Update() { //an addition by lachlan to skip levels as we please if (Input.GetKey(KeyCode.LeftShift)) { if (Input.GetKeyDown(KeyCode.P)) { Debug.Log("shift+p (prev level)"); int p = Application.loadedLevel; Map.switch_level(p + 1); } if (Input.GetKeyDown(KeyCode.O)) { Debug.Log("shift+o (next level)"); int o = Application.loadedLevel; Map.switch_level(o - 1); } if (Input.GetKeyDown(KeyCode.R)) { Debug.Log("shift+r (restart level)"); Map.reset_level(); } } if (Input.GetKeyDown(KeyCode.LeftShift)) { player_movement.moveSpeed = 10; } if (Input.GetKeyUp(KeyCode.LeftShift)) { player_movement.moveSpeed = 6; } grappling_hook.update(); if (grappling_hook.grapple_state == GrapplingHook.GrappleState.NONE) { player_movement.update(); } health.update(); weapon_control.update(); pos = transform.position; float world_bounds = 4.0f; Vector3 top_left = Camera.main.ScreenToWorldPoint(Vector3.zero); Vector3 bot_right = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height)); if (pos.x < top_left.x - world_bounds || pos.x > bot_right.x + world_bounds || pos.y < top_left.y - world_bounds || pos.y > bot_right.y + world_bounds) { Map.reset_level(); } int i; bool found = false; if (Input.GetKeyDown(KeyCode.Q)) { i = inventory.equipped_id - 1; while (true) { if (i < 0) i = inventory.weapons.Count - 1; if (inventory.weapons[i].active) { inventory.equip_weapon(inventory.weapons[i]); break; } if (i == inventory.equipped_id) break; --i; } } if (Input.GetKeyDown(KeyCode.E)) { i = inventory.equipped_id + 1; while (true) { if (i >= inventory.weapons.Count) i = 0; if (inventory.weapons[i].active) { inventory.equip_weapon(inventory.weapons[i]); break; } if (i == inventory.equipped_id) break; ++i; } } current_node = Map.grid.get_node(Map.grid.world_to_grid(pos)); }