Vector3 GetGridWall(Vector2 gridPosition, Vector2 direction, float height = 0) { Vector3 toReturn = GridF.TransformGridToPoint(gridPosition) + GridF.Direction2ToDirection3(direction * 0.5f * LabyrinthGenerator.instance.scale); toReturn.y = height; return(toReturn); }
void Start() { /*Vector3[] ring = CreateRing(); * * for(int i = 0; i < ring.Length; i++) * { * GameObject newGameObject = GameObject.CreatePrimitive(PrimitiveType.Cube); * newGameObject.transform.SetParent(transform); * newGameObject.transform.localPosition = ring[i]; * }*/ /*ring1 = mesh.AddArray(CreateRing(Vector3.zero)); * ring2 = mesh.AddArray(CreateRing(Vector3.forward)); * mesh.ConectRings(ring1, ring2);*/ GetComponent <MeshFilter>().sharedMesh = mesh.CreateMesh(); generatorInstance = LabyrinthGenerator.instance; scale = generatorInstance.scale; flower = Instantiate(flower); nextFlower = Instantiate(flower); positionInGrid = GridF.TransformPointToGrid(transform.position); GetSurroundings(); GetChains(); }
void Update() { //Always (before change) //When changes tile update information currentPositionInGrid = GridF.TransformPointToGrid(transform.position); if (currentPositionInGrid != prevPositionInGrid) { Point2 newPoint = new Point2(prevPositionInGrid, Point2.GetDirection(prevPositionInGrid, currentPositionInGrid)); if (controlPoints.Count > 1) { newPoint.RoundAngle(controlPoints[controlPoints.Count - 1].direction); } AddPoint(newPoint); ChangeTile(); } //Always Draw(); /*if (Input.GetKeyDown(KeyCode.Space)) * { * foreach(Point2 point in controlPoints) * { * GameObject newObject = GameObject.CreatePrimitive(PrimitiveType.Cube); * newObject.transform.position = GridF.TransformGridToPoint(point.position); * newObject.transform.rotation = Quaternion.Euler(0, point.angle, 0); * } * }*/ }
void Update() { float y = transform.position.y; Vector3 newPosition = GridF.TransformGridToPoint(GridF.TransformPointToGrid(transform.parent.transform.position)); newPosition.y = y; transform.position = newPosition; transform.rotation = rotation; }
void Update() { Compass(); PositionateFlowers(); if (positionInGrid != GridF.TransformPointToGrid(transform.position)) { positionInGrid = GridF.TransformPointToGrid(transform.position); GetSurroundings(); GetChains(); } mesh.Clear(); Draw(); Draw(0.7f, Mathf.PI / 2, 1.2f, 0.45f); }
void Draw(float radius = 0.8f, float sinOffset = 0, float sinOffsetMultiplicator = 1, float amplitude = 0.5f) { //float refScale = 0.1f; List <int[]> rings = new List <int[]>(); Vector3 gridOffset = GridF.TransformGridToPoint(positionInGrid) - transform.position; gridOffset.y = 0; foreach (List <Vector2> thread in walls) { foreach (Vector2 point in thread) { int pointX = (int)point.x; int pointY = (int)point.y; Vector2 transformOffset = ((point + Vector2.one) / 2 - Vector2.one * 1.5f); Vector3 offset = new Vector3(transformOffset.x, 0, transformOffset.y); float y = Mathf.Sin((offset.x + sinOffset * sinOffsetMultiplicator + positionInGrid.x) * 2) * amplitude + Mathf.Cos((offset.z - sinOffset * sinOffsetMultiplicator + positionInGrid.y) * 2) * amplitude; Vector3 position = offset * scale + gridOffset + Vector3.up * y; float distance = (transform.position - transform.TransformPoint(position)).magnitude; distance = Mathf.Clamp((scale - 0.15f * scale) - distance, 0, scale); rings.Add(mesh.AddArray(CreateRing(radius * distance, position, Quaternion.Euler(0, 45 * (pointsGrid[pointX, pointY] - 1), 0)))); //GameObject newObj = GameObject.CreatePrimitive(PrimitiveType.Capsule); //newObj.transform.position = transform.position + offset; //newObj.transform.rotation = Quaternion.Euler(0, 45 * (pointsGrid[pointX, pointY] - 1), 90); //newObj.transform.localScale = Vector3.one * refScale; //refScale += 0.1f; } for (int i = 1; i < rings.Count; i++) { mesh.ConectRings(rings[i], rings[i - 1]); } mesh.Cap(rings[0]); mesh.Cap(rings[rings.Count - 1], true); rings.Clear(); } mesh.UpdateMesh(); }
//Program --------------------------------------------------------------------- void Start() { GetComponent <MeshFilter>().sharedMesh = mesh.CreateMesh(); currentPositionInGrid = GridF.TransformPointToGrid(transform.position); prevPositionInGrid = currentPositionInGrid; nextWall = GetGridWall(currentPositionInGrid, enemy.direction); prevWall = transform.position; Point2 newPoint = new Point2(prevPositionInGrid, enemy.direction); for (int i = 0; i < maxControlPoints; i++) { AddPoint(newPoint); } }
void Start() { TextureToMap(); GetGroundFromMap(); GetWallsFromMap(); groundMeshObject = new MeshObject((grounds.Count * 4), grounds.Count * 2 * 3); wallMeshObject = new MeshObject(walls.Count * 4, walls.Count * 2 * 3); DrawGround(); DrawWalls(); DrawCeil(); CombineInstance[] combineMesh = new CombineInstance[3]; combineMesh[0].mesh = groundMeshObject.GenerateMesh(); combineMesh[1].mesh = wallMeshObject.GenerateMesh(); combineMesh[2].mesh = ceilMeshObject.GenerateMesh(); Mesh generatedMesh = new Mesh(); generatedMesh.CombineMeshes(combineMesh, false, false); GetComponent <MeshFilter>().sharedMesh = generatedMesh; GetComponent <MeshCollider>().sharedMesh = generatedMesh; player.transform.position = new Vector3(((origin.x * 2) + 1) * scale, height * scale / 2, ((origin.y * 2) + 1) * scale); float angle = Mathf.Atan2(originDirection.x, originDirection.y) * Mathf.Rad2Deg; player.transform.eulerAngles = new Vector3(0, angle, 0); goal.transform.position = new Vector3(((end.x * 2) + 1) * scale, 0, ((end.y * 2) + 1) * scale); //endDirection = new Vector2(1,1); angle = Mathf.Atan2(endDirection.x, endDirection.y) * Mathf.Rad2Deg; goal.transform.eulerAngles = new Vector3(0, angle, 0); if (Game.enemies) { GameObject enemy = Instantiate(enemyType); enemy.transform.position = GridF.TransformGridToPoint(end * 2 + Vector2.one + endDirection / 2); enemy.transform.eulerAngles = new Vector3(0, angle, 0); } }
void PositionateFlowers() { Vector3 position = transform.position; position.y = 0; float distance = (position - GridF.TransformGridToPoint(transform.parent.GetComponent <EnemyBase>().currentPositionInGrid)).magnitude; distance = (scale - Mathf.Clamp(distance, 0, scale)) / scale; //if (positionInGrid != GridF.TransformPointToGrid(transform.position)) flowerGrowing = true; if (transform.parent.GetComponent <EnemyBase>().currentPositionInGrid != transform.parent.GetComponent <EnemyBase>().prevPositionInGrid) { flowerGrowing = true; } else if (prevFlowerGrowingDistance > distance) { flowerGrowing = false; } prevFlowerGrowingDistance = distance; if (!flowerGrowing) { distance = unGrowRate.Evaluate(distance); } else { distance = growRate.Evaluate(distance); } flower.transform.position = GridF.TransformGridToPoint(transform.parent.GetComponent <EnemyBase>().currentPositionInGrid) /*+ Vector3.up * transform.position.y*/; flower.transform.localScale = Vector3.one * distance; distance = (position - GridF.TransformGridToPoint(transform.parent.GetComponent <EnemyBase>().currentPositionInGrid + transform.parent.GetComponent <EnemyBase>().direction)).magnitude; distance = (scale - Mathf.Clamp(distance, 0, scale)) / scale; distance = growRate.Evaluate(distance); nextFlower.transform.position = GridF.TransformGridToPoint(transform.parent.GetComponent <EnemyBase>().currentPositionInGrid + transform.parent.GetComponent <EnemyBase>().direction) /*+ Vector3.up * transform.position.y*/; nextFlower.transform.localScale = Vector3.one * distance; }
private void Awake() { grid = GetComponent <GridF>(); }
void GetChains() { walls.Clear(); Vector2 checkDirection, pointer, prevCheckDirection; for (int y = 0; y < pointsGrid.GetLength(1); y++) { for (int x = 0; x < pointsGrid.GetLength(0); x++) { if (((x == 0 || x == pointsGrid.GetLength(0) - 1) || (y == 0 || y == pointsGrid.GetLength(1) - 1)) && pointsGrid[x, y] != 0 && !CheckWallsList(new Vector2(x, y))) { List <Vector2> thread = new List <Vector2>(); checkDirection = Vector2.right; prevCheckDirection = Vector2.zero; pointer = new Vector2(x, y); bool keepGoing = true; while (keepGoing) { thread.Add(pointer); keepGoing = false; for (int i = 0; i < 4; i++) { Vector2 checkPosition = pointer + checkDirection; if ((InsideIndex((int)checkPosition.x, pointsGrid.GetLength(0)) && InsideIndex((int)checkPosition.y, pointsGrid.GetLength(1))) && pointsGrid[(int)checkPosition.x, (int)checkPosition.y] != 0 && !CheckWallsList(checkPosition)) { //Debug.Log("I'm here"); keepGoing = true; pointer = checkPosition; prevCheckDirection = -checkDirection; break; } else { //Debug.Log("I'm here rotating the vector you know that's what I do I wouldn't fail you, right?"); checkDirection = GridF.RotateVector(checkDirection); if (checkDirection == prevCheckDirection) { checkDirection = GridF.RotateVector(checkDirection); //rotate 2 times to avoid backwards movement } } } } walls.Add(thread); } } } //Debug.Log(walls.Count); //CORRECT CHAINS /*foreach (List<Vector2> thread in walls) * { * int prevValue = 0; * foreach (Vector2 point in thread) * { * int x, y; * x = (int)point.x; y = (int)point.y; * if (prevValue != 0) * { * int add = prevValue > 0 ? 0 : 1; //adjusting for the 0 * int difference = (prevValue + add) - pointsGrid[x, y]; * * if(Mathf.Abs(difference) > 1) * { * int newValue = prevValue + (int)Mathf.Sign(difference); * if (newValue == 0) newValue = -1; //adjusting for the 0 * pointsGrid[x, y] = newValue; * } * } * prevValue = pointsGrid[x,y]; * } * }*/ foreach (List <Vector2> thread in walls) { if (thread.Count > 1) { Vector2 prevDirection = thread[1] - thread[0]; pointsGrid[(int)thread[0].x, (int)thread[0].y] = (int)GetValueFromAngle(Vector2.Angle(Vector2.up, prevDirection) * Mathf.Sign(prevDirection.x)); for (int i = 1; i < thread.Count; i++) { int x, y; x = (int)thread[i].x; y = (int)thread[i].y; if (i != thread.Count - 1) { Vector2 currentDirection = thread[i + 1] - thread[i]; Vector2 angle = prevDirection + currentDirection; pointsGrid[x, y] = (int)GetValueFromAngle(Vector2.Angle(Vector2.up, angle) * Mathf.Sign(angle.x)); prevDirection = currentDirection; } else { pointsGrid[x, y] = (int)GetValueFromAngle(Vector2.Angle(Vector2.up, prevDirection) * Mathf.Sign(prevDirection.x)); } } } } }
void GetSurroundings() { int centerX = (int)positionInGrid.x; int centerY = (int)positionInGrid.y; //GENERATE GRID for (int y = 0; y < grid.GetLength(1); y++) { for (int x = 0; x < grid.GetLength(0); x++) { grid[x, y] = GridF.CheckTile(centerX + (x - 1), centerY + (y - 1)); } } //GENERATE POINTS GRID //Ignore halls for (int y = 0; y < pointsGrid.GetLength(1); y++) { for (int x = 0; x < pointsGrid.GetLength(0); x++) { if (x % 2 == 0 && y % 2 == 0) { pointsGrid[x, y] = 0; } else { pointsGrid[x, y] = 1; } } } //Get walls for (int y = 0; y < pointsGrid.GetLength(1); y++) { for (int x = 0; x < pointsGrid.GetLength(0); x++) { if (pointsGrid[x, y] != 0) { if (x % 2 == 0) { if (grid[x / 2, (y + 1) / 2] == grid[x / 2, (y + 1) / 2 - 1]) { pointsGrid[x, y] = 0; } else { pointsGrid[x, y] = 3; } } if (y % 2 == 0) { if (grid[(x + 1) / 2, y / 2] == grid[(x + 1) / 2 - 1, y / 2]) { pointsGrid[x, y] = 0; } else { pointsGrid[x, y] = 1; } } } } } //Get corners /*for(int y = 0; y < 2; y++) * { * for (int x = 0; x < 2; x++) * { * int cornerValue = 0; * bool[,] sample = new bool[2, 2]; * int occupiedTiles = 0; * * for(int sampleY = 0; sampleY < 2; sampleY++) * { * for (int sampleX = 0; sampleX < 2; sampleX++) * { * sample[sampleX, sampleY] = grid[x+sampleX, y+sampleY]; * if(!sample[sampleX, sampleY]) * { * occupiedTiles++; * } * } * } * * if (occupiedTiles == 3) * { * for (int sampleY = 0; sampleY < 2; sampleY++) * { * for (int sampleX = 0; sampleX < 2; sampleX++) * { * sample[sampleX, sampleY] = !sample[sampleX, sampleY]; * } * } * occupiedTiles=1; * } * * if (occupiedTiles == 1) * { * for (int sampleY = 0; sampleY < 2; sampleY++) * { * for (int sampleX = 0; sampleX < 2; sampleX++) * { * if(sample[sampleX, sampleY]) * { * if (sampleX % 2 != 0 || sampleY % 2 != 0) cornerValue = 4; * else cornerValue = 2; * } * } * } * } * * if (occupiedTiles == 2) * { * if (sample[0, 0] == sample[1, 0]) * { * cornerValue = 3; * } * else if (sample[0, 0] == sample[0, 1]) * { * cornerValue = 1; * } * else * { * if (sample[0, 0]) cornerValue = 2; * else cornerValue = 4; * } * } * pointsGrid[1 + x * 2, 1 + y * 2] = cornerValue; * } * }*///Who needs corners }
void Draw() { //Update head { Vector3 headPosition = head.position; headPosition.y = CalculateHeight(head.position); head.position = headPosition; } mesh.Clear(); if (controlPoints.Count > 0) { float addedDistance = 0; if (moved) { addedDistance = ((head.transform.position - prevWall).magnitude / betweenWallsDistance); if (turning && enemy.state != EnemySmooth.State.Turning) { turning = false; ChangeTile(); } } List <int[]> rings = new List <int[]>(); if (controlPoints.Count == maxControlPoints) { { Point2 point = controlPoints[0]; Vector3 realPointLocalPosition = (GridF.TransformGridToPoint(point.position) + GridF.Direction2ToDirection3(point.direction) * addedDistance * LabyrinthGenerator.instance.scale); realPointLocalPosition += CalculateHeight(realPointLocalPosition) * Vector3.up - transform.position; float width = widthCurve.Evaluate(0); rings.Add(mesh.AddArray(CreateRing(width, realPointLocalPosition, point.angle), 0)); if (addedDistance < 0.5f) { Point2 point2 = controlPoints[1]; float distanceToHead = 0.5f - addedDistance; realPointLocalPosition = (GridF.TransformGridToPoint(point.position) + CalculateHeight(GridF.TransformGridToPoint(point.position)) * Vector3.up) - transform.position; Vector3 realPointLocalPosition2 = (GridF.TransformGridToPoint(point2.position) + Vector3.up * CalculateHeight(GridF.TransformGridToPoint(point2.position))) - transform.position; width = widthCurve.Evaluate(distanceToHead / (controlPoints.Count - 0.5f)); rings.Add(mesh.AddArray(CreateRing(width, (realPointLocalPosition + realPointLocalPosition2) / 2, (point.angle + point2.angle) / 2), distanceToHead)); } } for (int i = 1; i < controlPoints.Count; i++) { Point2 point = controlPoints[i]; float distanceToHead = i - addedDistance; Vector3 realPointLocalPosition = (GridF.TransformGridToPoint(point.position) + CalculateHeight(GridF.TransformGridToPoint(point.position)) * Vector3.up) - transform.position; float width = widthCurve.Evaluate(distanceToHead / (controlPoints.Count - 0.5f)); rings.Add(mesh.AddArray(CreateRing(width, realPointLocalPosition, point.angle), distanceToHead)); //rings.Add(mesh.AddArray(CreateRing(1, realPointLocalPosition + (new Vector3(point.direction.x, 0, point.direction.y) * addedDistance), point.angle))); if (i == controlPoints.Count - 1) { distanceToHead = i + 0.5f - addedDistance; /*Quaternion headAngle = head.rotation * Quaternion.Euler(0,point.angle,0); * while (Mathf.Abs(headAngle - point.angle) > 360) * { * if (headAngle < point.angle) headAngle += 360; * else headAngle -= 360; * } * Debug.Log(headAngle.eulerAngles.y);*/ width = widthCurve.Evaluate(distanceToHead / (controlPoints.Count - 0.5f)); realPointLocalPosition = (realPointLocalPosition + transform.InverseTransformPoint(head.position)) / 2; rings.Add(mesh.AddArray(CreateRing(width, realPointLocalPosition, point.angle), distanceToHead)); distanceToHead -= 0.25f; width = widthCurve.Evaluate(distanceToHead / (controlPoints.Count - 0.5f)); rings.Add(mesh.AddArray(CreateRing(width, ((realPointLocalPosition + transform.InverseTransformPoint(head.position)) / 2), head.rotation), distanceToHead)); } else { distanceToHead = i + 0.5f - addedDistance; Point2 point2 = controlPoints[i + 1]; Vector3 realPointLocalPosition2 = (GridF.TransformGridToPoint(point2.position) + CalculateHeight(GridF.TransformGridToPoint(point2.position)) * Vector3.up) - transform.position; width = widthCurve.Evaluate(distanceToHead / (controlPoints.Count - 0.5f)); rings.Add(mesh.AddArray(CreateRing(width, (realPointLocalPosition + realPointLocalPosition2) / 2, point.angle), distanceToHead)); } } } //rings.Add(mesh.AddArray(CreateRing(widthCurve.Evaluate(1), transform.InverseTransformPoint(head.position), head.rotation), maxControlPoints - 0.5f)); for (int i = 1; i < rings.Count; i++) { mesh.ConectRings(rings[i], rings[i - 1]); } mesh.Cap(rings[0]); mesh.Cap(rings[rings.Count - 1], true); mesh.UpdateMesh(); } }