public PathNode(GameWorld.Terrain _weight, Vector3 _worldPos, int _X, int _Y) { weight = _weight; worldPosition = _worldPos; gridX = _X; gridY = _Y; }
private float getFOWBoundsDistance() { GameWorld.Terrain currentTerrain = gameWorld.getTerrainAtPoint(transform.position); float distance = boundEntity.fovDistance[currentTerrain]; if (gameWorld.isNight) { distance *= boundEntity.nightFOVModifier; } return(distance); }
private Vector3[] getFOWBoundsPolygon(bool isNight) { Vector2 currentPosition = transform.position; pointsFOWBOUNDS[0] = transform.InverseTransformPoint(currentPosition); pointsFOWBOUNDS[0].z = 0; Vector2 startAngleDir = new Vector2(0.01f, 0); int i = 1; GameWorld.Terrain currentTerrain = gameWorld.getTerrainAtPoint(currentPosition); if (currentTerrain == GameWorld.Terrain.Forest) { boundEntity.SetHidden(true); } else { boundEntity.SetHidden(false); } float distance = boundEntity.fovDistance[currentTerrain]; if (isNight) { distance *= boundEntity.nightFOVModifier; } for (float angle = 0; angle <= 360; angle += 4) { Vector2 angleDir = startAngleDir.Rotate(angle); //GameWorld.Terrain currentTerrain = gameWorld.getTerrainAtPoint(currentPosition + angleDir); Vector2 angleDirIncreased = new Vector2(distance * Mathf.Cos(Mathf.Deg2Rad * angle), distance * Mathf.Sin(Mathf.Deg2Rad * angle)); Vector2 endPosition = currentPosition + angleDirIncreased; //for (float dist = 0; dist < distance - 0.01f; dist = dist + terrainStep) /*{ * //Vector2 angleDirIncreased = angleDir + new Vector2(dist * Mathf.Cos(Mathf.Deg2Rad * angle), dist * Mathf.Sin( Mathf.Deg2Rad * angle)); * //endPosition = currentPosition + angleDirIncreased; * GameWorld.Terrain newTerrain = gameWorld.getTerrainAtPoint(endPosition); * if (newTerrain != currentTerrain) * { * //currentTerrain = newTerrain; * //distance = boundEntity.fovDistance[newTerrain]; * distance = boundEntity.fovDistance[currentTerrain]; * } * }*/ endPosition = transform.InverseTransformPoint(endPosition); pointsFOWBOUNDS[i] = endPosition; ++i; } return(pointsFOWBOUNDS); }
public void CreateGrid() { mesh = world.GetComponent <WorldMesh>(); generator = world.GetComponent <WorldGenerator>(); nodeDiameter = mesh.tileSize.x; nodeRadius = nodeDiameter / 2.0f; gridSizeX = generator.width; gridSizeY = generator.height; grid = new PathNode[gridSizeX, gridSizeY]; Vector3 worldBottomLeft = transform.position - Vector3.right * mesh.totalSize.x / 2 - Vector3.up * mesh.totalSize.y / 2; for (int x = 0; x < gridSizeX; ++x) { for (int y = 0; y < gridSizeY; ++y) { Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.up * (y * nodeDiameter + nodeRadius); GameWorld.Terrain terrain = world.getTerrainAtPoint(worldPoint); grid[x, y] = new PathNode(terrain, worldPoint, x, y); } } }
// Update is called once per frame void Update() { Vector2 currentPos = transform.position; PathNode node = grid.NodeFromWorldPoint(currentPos); if (node != currentTile) { node.entitiesCurrentlyOnTile.Add(gameObject); if (node.entitiesCurrentlyOnTile.Count > 1) { foreach (GameObject obj in node.entitiesCurrentlyOnTile) { if (!obj.GetComponent <Entity>().isPlayer) { node.battling = true; BattleState state = new BattleState(boundEntity, obj.GetComponent <Entity>(), gameManager, node); battleEntryScreen.Open(transform.position, boundEntity.soldierAmount, obj.GetComponent <Entity>().soldierAmount, state); gameManager.currentBattleState = state; gameManager.openMenuLockActions(transform.position); } } } if (currentTile != null) { currentTile.entitiesCurrentlyOnTile.Remove(gameObject); } currentTile = node; } if (gameManager.playerCheckpoint && gameManager.playerCheckpointUpdated) { line.GetComponent <CurveLineRenderer>().ClearVertices(); if (pathNodes != null) { pathNodes.Clear(); } Vector2 sameZ = gameManager.playerCheckpoint.transform.position; Vector2 sameZPlayer = transform.position; if (Vector2.Distance(sameZ, sameZPlayer) > distanceToEnablePathfinding) { pathNodes = grid.FindPath(sameZPlayer, sameZ, boundEntity); pathNodes[pathNodes.Count - 1] = sameZ; currentPathNodeIndex = 0; gameManager.playerCheckpointUpdated = false; } else if (!sameZ.FuzzyEquals(sameZPlayer, 0.01f)) { Vector3 direction = gameManager.playerCheckpoint.transform.position - transform.position; direction.z = 0; direction.Normalize(); GameWorld.Terrain currentTerrain = grid.NodeFromWorldPoint(transform.position).weight; float movementModifier = boundEntity.terrainModifiers[currentTerrain]; Vector3 newPosition = transform.position + direction * movementModifier * gameManager.timeMultiplier * Time.deltaTime; transform.position = newPosition; } else { GameObject.Destroy(gameManager.playerCheckpoint); } } else if (gameManager.playerCheckpoint && !gameManager.playerCheckpointUpdated) { Vector3 checkpointPosition = pathNodes[currentPathNodeIndex]; Vector2 sameZEnd = gameManager.playerCheckpoint.transform.position; Vector2 sameZCheckpoint = checkpointPosition; Vector2 sameZPlayer = transform.position; linePosition.Clear(); linePosition.Add(new Vector3(sameZPlayer.x, sameZPlayer.y, lineZ)); line.GetComponent <CurveLineRenderer>().ClearVertices(); for (int i = currentPathNodeIndex; i < pathNodes.Count; ++i) { linePosition.Add(new Vector3(pathNodes[i].x, pathNodes[i].y, lineZ)); } line.GetComponent <CurveLineRenderer>().SetVertices(linePosition); if (!sameZCheckpoint.FuzzyEquals(sameZPlayer, 0.025f)) { Vector2 direction2D = sameZCheckpoint - sameZPlayer; Vector3 direction = direction2D; direction.Normalize(); GameWorld.Terrain currentTerrain = grid.NodeFromWorldPoint(transform.position).weight; float movementModifier = boundEntity.terrainModifiers[currentTerrain] * boundEntity.pace * boundEntity.speed;; Vector3 newPosition = transform.position + direction * movementModifier * gameManager.timeMultiplier * Time.deltaTime; transform.position = newPosition; } else { transform.position = new Vector3(sameZCheckpoint.x, sameZCheckpoint.y, transform.position.z); if (currentPathNodeIndex + 1 <= pathNodes.Count - 1) { ++currentPathNodeIndex; } else { gameManager.playerCheckpointUpdated = true; } } } }
public PatrolStatus TickMovement(float dt) { Vector2 currentPos = transform.position; PathNode node = grid.NodeFromWorldPoint(currentPos); if (node != currentTile) { node.entitiesCurrentlyOnTile.Add(gameObject); if (node.entitiesCurrentlyOnTile.Count > 1) { foreach (GameObject obj in node.entitiesCurrentlyOnTile) { if (obj.GetComponent <Entity>().isPlayer&& !node.battling) { BattleState state = new BattleState(obj.GetComponent <Entity>(), boundEntity, gameManager, node); battleEntryScreen.Open(transform.position, boundEntity.soldierAmount, obj.GetComponent <Entity>().soldierAmount, state); gameManager.currentBattleState = state; gameManager.openMenuLockActions(transform.position); node.battling = true; } } } if (currentTile != null) { currentTile.entitiesCurrentlyOnTile.Remove(gameObject); } currentTile = node; } if (!usingPathfinding) { Vector2 sameZcheckpoint = currentCheckpoint; Vector2 sameZ = transform.position; if (Vector2.Distance(sameZ, sameZcheckpoint) > distanceToEnablePathfinding) { pathNodes = grid.FindPath(sameZ, sameZcheckpoint, boundEntity); pathNodes[pathNodes.Count - 1] = sameZcheckpoint; currentPathNodeIndex = 0; usingPathfinding = true; return(PatrolStatus.Ongoing); } else if (!sameZ.FuzzyEquals(sameZcheckpoint, 0.01f)) { Vector3 direction = sameZcheckpoint - sameZ; direction.z = 0; direction.Normalize(); GameWorld.Terrain currentTerrain = grid.NodeFromWorldPoint(transform.position).weight; if (currentTerrain == GameWorld.Terrain.Forest) { GetComponent <Entity>().SetHidden(true); } else { GetComponent <Entity>().SetHidden(false); } float movementModifier = boundEntity.terrainModifiers[currentTerrain] * boundEntity.pace * boundEntity.speed; Vector3 newPosition = transform.position + direction * movementModifier * gameManager.timeMultiplier * Time.deltaTime; transform.position = newPosition; return(PatrolStatus.Ongoing); } else { //REACHED THE DESTINATION return(PatrolStatus.Finished); } } else if (usingPathfinding) { Vector3 checkpointPosition = pathNodes[currentPathNodeIndex]; Vector2 sameZEnd = currentCheckpoint; Vector2 sameZCheckpoint = checkpointPosition; Vector2 sameZPlayer = transform.position; if (!sameZCheckpoint.FuzzyEquals(sameZPlayer, 0.025f)) { Vector2 direction2D = sameZCheckpoint - sameZPlayer; Vector3 direction = direction2D; direction.Normalize(); GameWorld.Terrain currentTerrain = grid.NodeFromWorldPoint(transform.position).weight; if (currentTerrain == GameWorld.Terrain.Forest) { GetComponent <Entity>().SetHidden(true); } else { GetComponent <Entity>().SetHidden(false); } float movementModifier = boundEntity.terrainModifiers[currentTerrain] * boundEntity.pace * boundEntity.speed; Vector3 newPosition = transform.position + direction * movementModifier * gameManager.timeMultiplier * Time.deltaTime; transform.position = newPosition; } else { transform.position = new Vector3(sameZCheckpoint.x, sameZCheckpoint.y, transform.position.z); if (currentPathNodeIndex + 1 <= pathNodes.Count - 1) { ++currentPathNodeIndex; } else { usingPathfinding = false; } } } return(PatrolStatus.Ongoing); }