public void RegisterCharacter(GridCharacter c) { if (characters.Contains(c) == false) { characters.Add(c); } }
public void RegisterCharacter(GridCharacter character) { if (!characters.Contains(character)) { characters.Add(character); } }
public void UnRegisterCharacter(GridCharacter character) { if (characters.Contains(character)) { characters.Remove(character); } }
public Pathfinder(GridCharacter c, Node start, Node target, PathfindingComplete callback, GridManager gridManager) { this.gridManager = gridManager; character = c; startNode = start; endNode = target; completeCallback = callback; }
// Collision private void CharacterCollision(GridCharacter hitCharacter, GridCharacter byCharacter) { int hitIndex = GetCharacterIndex(hitCharacter); _players[hitIndex].GetComponent <PlayerOverlayView>().WasHit(); int byIndex = GetCharacterIndex(byCharacter); _players[byIndex].GetComponent <PlayerOverlayView>().DidHit(); }
// Pre push public override void PrePush(GridItem byItem, Direction inDirection) { base.PrePush(byItem, inDirection); if (data.isWinProp && byItem.GetType() == typeof(GridCharacter)) { GridCharacter c = (GridCharacter)byItem; GameManager.instance.WinStage(c.playerIndex); } }
// On awake, grab private void Awake() { // Grab Animator at start _animator = gameObject.GetComponent <Animator>(); CharacterStateChanged(GridCharacterState.Idle); // Grab character _character = gameObject.GetComponent <GridCharacter>(); if (_character != null) { _character.onStageChange += CharacterStateChanged; } }
void PathfinderCallback(List <Node> path, GridCharacter character) { isPathfinding = false; if (path == null) { return; } List <Node> pathActual = new List <Node>(); List <Vector3> reachablePositions = new List <Vector3>(); List <Vector3> unreachalePositions = new List <Vector3>(); reachablePositions.Add(character.currentNode.worldPosition); if (character.actionPoints > 0) { reachablePositions.Add(character.currentNode.worldPosition); } if (path.Count > character.actionPoints) { if (character.actionPoints == 0) { unreachalePositions.Add(character.currentNode.worldPosition); } else { unreachalePositions.Add(path[character.actionPoints - 1].worldPosition); } } for (int i = 0; i < path.Count; i++) { if (i <= character.actionPoints - 1) { pathActual.Add(path[i]); reachablePositions.Add(path[i].worldPosition); } else { unreachalePositions.Add(path[i].worldPosition); } } pathViz.positionCount = reachablePositions.Count; pathViz.SetPositions(reachablePositions.ToArray()); pathVizNotReachable.positionCount = unreachalePositions.Count; pathVizNotReachable.SetPositions(unreachalePositions.ToArray()); character.LoadPath(pathActual); }
public void RequestPathfind( GridCharacter character, Node start, Node target, Pathfinder.PathfindingComplete callback, GridManager gridManager ) { Pathfinder newJob = new Pathfinder(character, start, target, callback, gridManager); toDoJobs.Add(newJob); }
void PathfinderCallback(List <Node> pathfinderPath, GridCharacter c) { isPathfinding = false; if (pathfinderPath == null) { return; } List <Node> currentPath = new List <Node>(); List <Vector3> reachablePositions = new List <Vector3>(); List <Vector3> unreachablePositions = new List <Vector3>(); Vector3 offset = Vector3.up * .1f; if (c.actionPoints > 0) { reachablePositions.Add(c.currentNode.worldPosition + offset); } if (pathfinderPath.Count > c.actionPoints) { if (c.actionPoints == 0) { unreachablePositions.Add(c.currentNode.worldPosition + offset); } else { unreachablePositions.Add(pathfinderPath[c.actionPoints - 1].worldPosition + offset); } } for (int i = 0; i < pathfinderPath.Count; i++) { if (i <= c.actionPoints - 1) { currentPath.Add(pathfinderPath[i]); reachablePositions.Add(pathfinderPath[i].worldPosition + offset); } else { unreachablePositions.Add(pathfinderPath[i].worldPosition + offset); } } reachablePathViz.positionCount = currentPath.Count + 1; reachablePathViz.SetPositions(reachablePositions.ToArray()); unreachablePathViz.positionCount = unreachablePositions.Count; unreachablePathViz.SetPositions(unreachablePositions.ToArray()); c.SetCurrentPath(currentPath); }
public Pathfinder( GridCharacter character, Node startNode, Node targetNode, PathfindingComplete callback, GridManager gridManager ) { this.character = character; this.startNode = startNode; this.endNode = targetNode; this.completeCallback = callback; this.gridManager = gridManager; }
// Index private int GetCharacterIndex(GridCharacter character) { if (GridManager.instance.characters != null) { for (int c = 0; c < GridManager.instance.characters.Length; c++) { if (GridManager.instance.characters[c] == character) { return(c); } } } return(-1); }
// Update private void Update() { if (_players == null || _wins == null || GridManager.instance.characters == null) { return; } // Follow players & win props for (int i = 0; i < GridManager.instance.characters.Length; i++) { GridCharacter character = GridManager.instance.characters[i]; FollowTransform(character.transform, _players[i], playerOffset); } for (int i = 0; i < _wins.Length; i++) { int prefabIndex = _propLookup[i]; FollowTransform(GridManager.instance.props[prefabIndex].transform, _wins[i], winOffset); } }
public void PathfinderCall(GridCharacter character, Node targetNode) { if (!isPathfinding) { isPathfinding = true; Node start = character.currentNode; Node target = targetNode; if (start != null && target != null) { PathfinderMaster.singleton.RequestPathfind(character, start, target, PathfinderCallback, gridManager); } else { isPathfinding = false; } } }
private bool firstInit; // TODO: is this necessary public override void Execute(StateManager states, SessionManager sm, Turn turn) { GridCharacter gridCharacter = states.CurrentCharacter; if (!isInit) { if (gridCharacter == null || index > gridCharacter.currentPath.Count - 1) { states.SetStartingState(); return; } isInit = true; startNode = gridCharacter.currentNode; targetNode = gridCharacter.currentPath[index]; float t_ = t - 1; t_ = Mathf.Clamp01(t_); t = t_; float distance = Vector3.Distance(startNode.worldPosition, targetNode.worldPosition); speed = gridCharacter.Speed / distance; Vector3 direction = targetNode.worldPosition - startNode.worldPosition; targetRot = Quaternion.LookRotation(direction); startRot = gridCharacter.transform.rotation; if (!firstInit) { gridCharacter.isStateCurrentlyMoving = true; gridCharacter.PlayMovementAnimation(); firstInit = true; } } t += states.Delta * speed; rotT += states.Delta * gridCharacter.rotateSpeed; if (rotT > 1) { rotT = 1; } gridCharacter.transform.rotation = Quaternion.Slerp(startRot, targetRot, rotT); if (t > 1) { isInit = false; gridCharacter.currentNode.character = null; gridCharacter.currentNode = targetNode; gridCharacter.currentNode.character = gridCharacter; gridCharacter.actionPoints--; if (gridCharacter.owner.isLocalPlayer) { sm.gameVariables.UpdateActionPoints(gridCharacter.actionPoints); } index++; if (index > states.CurrentCharacter.currentPath.Count - 1) { // we moved onto our path... t = 1; index = 0; states.SetStartingState(); gridCharacter.PlayIdleAnimation(); gridCharacter.isStateCurrentlyMoving = false; firstInit = false; } } Vector3 tp = Vector3.Lerp(startNode.worldPosition, targetNode.worldPosition, t); gridCharacter.transform.position = tp; }
public override void Execute( StateManager stateManager, SessionManager sm, Turn turn ) { bool mouseClick = Input.GetMouseButtonDown(0); if (prevCharacter != null) { prevCharacter.OnDehighlight(stateManager.playerHolder); } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; Debug .DrawLine(ray.origin, ray.origin + ray.direction * 1000, Color.red); if (Physics.Raycast(ray, out hit, 1000, LayerMask.GetMask("MouseRaycast"))) { stateManager.currentNode = sm.gridManager.GetNode(hit.point); IDetectableByMouse detectable = hit.transform.GetComponent <IDetectableByMouse>(); if (detectable != null) { stateManager.currentNode = detectable.OnDetec(); } Node currentNode = stateManager.currentNode; if (currentNode != null) { if (currentNode.character != null) { if (currentNode.character.owner == stateManager.playerHolder) { currentNode.character.OnHighlight(stateManager.playerHolder); prevCharacter = currentNode.character; } else { } } if (stateManager.currentCharacter != null && currentNode.character == null) { if (mouseClick) { if (stateManager.currentCharacter) { if (stateManager.currentCharacter.currentPath != null || stateManager.currentCharacter.currentPath.Count > 0) { stateManager.SetState("moveOnPath"); } } } else { PathDetection(stateManager, sm, currentNode); } } else { if (mouseClick) { if (currentNode.character != null) { if (currentNode.character.owner == stateManager.playerHolder) { currentNode.character.OnSelect(stateManager.playerHolder); stateManager.prevNode = null; sm.ClearPath(stateManager); sm.gameVariables.actionPointsInt.value = currentNode.character.actionPoints; } } } } } } }
// Load stage private void Load(StageData stageData) { // Unload Unload(); // Set instance instance = this; // Get stage data data = stageData; // Set size width = tileSize * data.columns; height = tileSize * data.rows; Debug.Log("GRID MANAGER - LOAD STAGE: " + data.stageID); // Generate tiles tiles = new Dictionary <int, GridTile>(); foreach (TileData tileDatum in data.tiles) { // Ensure unique index if (tiles.ContainsKey(tileDatum.tileIndex)) { Debug.LogError("GRID MANAGER - CANNOT HAVE MULTIPLE TILES FOR INDEX: " + tileDatum.tileIndex); continue; } // Get tile prefab GridTile prefab = tileDatum.tilePrefab >= 0 && tileDatum.tilePrefab < tilePrefabs.Length ? tilePrefabs[tileDatum.tilePrefab] : null; if (prefab == null) { Debug.LogError("GRID MANAGER - NO TILE FOR PREFAB INDEX: " + tileDatum.tilePrefab); continue; } // Load tile instance GridTile inst = Pool.instance.Load(prefab.gameObject).GetComponent <GridTile>(); inst.movable = null; if (inst == null) { Debug.LogError("GRID MANAGER - CANNOT INSTANTIATE TILE PREFAB: " + prefab.gameObject.name); continue; } // Set position Transform tileTransform = inst.transform; tileTransform.SetParent(transform); tileTransform.localPosition = GetTilePosition(tileDatum.tileIndex); // Load data inst.LoadData(tileDatum); tiles[tileDatum.tileIndex] = inst; } // Generate props List <GameObject> winList = new List <GameObject>(); props = new GridProp[data.props.Length]; for (int p = 0; p < data.props.Length; p++) { // Prop datum PropData propDatum = data.props[p]; // Ensure tile exists if (!tiles.ContainsKey(propDatum.tileIndex)) { Debug.LogError("GRID MANAGER - NO TILE FOUND FOR INDEX: " + propDatum.tileIndex); continue; } // Get tile GridTile tile = tiles[propDatum.tileIndex]; if (tile.movable != null) { Debug.LogError("GRID MANAGER - CANNOT HAVE MULTIPLE PROPS ON THE SAME TILE FOR INDEX: " + propDatum.tileIndex); continue; } // Get prop prefab GridProp prefab = propDatum.propPrefab >= 0 && propDatum.propPrefab < propPrefabs.Length ? propPrefabs[propDatum.propPrefab] : null; if (prefab == null) { Debug.LogWarning("GRID MANAGER - NO PROP FOR PREFAB INDEX: " + propDatum.propPrefab); prefab = propPrefabs[0]; } // Load prop instance GridProp inst = Pool.instance.Load(prefab.gameObject).GetComponent <GridProp>(); if (inst == null) { Debug.LogError("GRID MANAGER - CANNOT INSTANTIATE PROP PREFAB: " + prefab.gameObject.name); continue; } // Set position Transform trans = inst.transform; trans.SetParent(transform); trans.localPosition = GetTilePosition(propDatum.tileIndex); Direction direction = propDatum.direction; if (direction == Direction.None) { int random = UnityEngine.Random.Range(0, 5) + 1; direction = (Direction)random; } trans.localRotation = GetRotation(direction); // Load data inst.LoadData(propDatum); props[p] = inst; // Add win effect if (propDatum.isWinProp) { GameObject winEffect = Pool.instance.Load(winPropEffect); winEffect.transform.SetParent(inst.transform); winEffect.transform.localPosition = new Vector3(0f, 1f, 0f); winEffect.transform.localRotation = Quaternion.identity; winEffect.transform.localScale = Vector3.one; winList.Add(winEffect); } } winEffects = winList.ToArray(); // Generate characters if (GameManager.instance.players != null && GameManager.instance.players.Count > 0) { if (GameManager.instance.players.Count > data.players.Length) { Debug.Log("STAGE: " + data.stageID + "\nPLAYERS: " + data.players.Length); } characters = new GridCharacter[GameManager.instance.players.Count]; for (int p = 0; p < GameManager.instance.players.Count; p++) { // Player GamePlayer player = GameManager.instance.players[p]; PlayerData playerData = data.players[p]; // Get character data int index = player.characterIndex; if (index < 0 || index >= GameManager.instance.gameData.characters.Length) { index = UnityEngine.Random.Range(0, GameManager.instance.gameData.characters.Length); } GameCharacter character = GameManager.instance.gameData.characters[index]; // Get prefab GridCharacter prefab = GetCharacterPrefab(character.characterID); // Get instance GridCharacter mover = Pool.instance.Load(prefab.gameObject).GetComponent <GridCharacter>(); mover.playerIndex = p; mover.Place(character.characterVariant, playerData.tileIndex, playerData.direction); // Set character characters[p] = mover; } } // Delegate if (onGridLoaded != null) { onGridLoaded(this); } // Set game intro if (GameManager.instance != null) { GameManager.instance.SetState(GameState.GameIntro); } }
public override void Execute(StateManager states, SessionManager sm, Turn t) { bool mouseClick = Input.GetMouseButtonDown(0); if (prevCharacter != null) { prevCharacter.OnDeHighlight(states.playerHolder); } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, MAX_DISTANCE)) { Node node = sm.gridManager.GetNode(hit.point); IDetectable detectable = hit.transform.GetComponent <IDetectable>(); if (detectable != null) { node = detectable.OnDetect(); } if (node != null) { if (node.character != null) { //you highlighted your own unit if (node.character.owner == states.playerHolder) { node.character.OnHighlight(states.playerHolder); prevCharacter = node.character; sm.ClearPath(states); sm.gameVariables.UpdateActionPoints(node.character.actionPoints); } else //you highlighted an enemy unit { } } if (states.CurrentCharacter != null && node.character == null) { if (mouseClick) { if (states.CurrentCharacter.currentPath != null || states.CurrentCharacter.currentPath.Count > 0) { states.SetState("moveOnPath"); } } else { PathDetection(states, sm, node); } } else //No character selected { if (mouseClick) { if (node.character != null) { if (node.character.owner == states.playerHolder) { node.character.OnSelect(states.playerHolder); states.prevNode = null; sm.ClearPath(states); sm.gameVariables.UpdateActionPoints(node.character.actionPoints); } } } } } } }
public override void Execute(StateManager stateManager, SessionManager sm, Turn turn) { GridCharacter gridCharacter = stateManager.currentCharacter; if (!isInit) { if (gridCharacter == null || index > gridCharacter.currentPath.Count - 1) { stateManager.SetStartingState(); return; } isInit = true; startNode = gridCharacter.currentNode; targetNode = gridCharacter.currentPath[index]; float timer = time - 1; timer = Mathf.Clamp01(timer); time = timer; float distance = Vector3.Distance(startNode.worldPosition, targetNode.worldPosition); speed = gridCharacter.moveSpeed / distance; Vector3 direction = targetNode.worldPosition - startNode.worldPosition; targetRot = Quaternion.LookRotation(direction); startRot = gridCharacter.transform.rotation; } time += Time.deltaTime * speed; rotTime += Time.deltaTime * rotSpeed; if (rotTime > 1) { rotTime = 1; } Quaternion tr = Quaternion.Lerp(startRot, targetRot, rotTime); gridCharacter.transform.rotation = tr; if (time > 1) { isInit = false; gridCharacter.currentNode.character = null; gridCharacter.currentNode = targetNode; targetNode.character = gridCharacter; gridCharacter.actionPoints--; if (gridCharacter.owner.isLocalPlayer) { sm.gameVariables.actionPointsInt.value = gridCharacter.actionPoints; } index++; if (index > gridCharacter.currentPath.Count - 1) { time = 1; index = 0; stateManager.SetStartingState(); } } Vector3 tp = Vector3.Lerp(startNode.worldPosition, targetNode.worldPosition, time); gridCharacter.transform.position = tp; }