90 lines
2.8 KiB
C#
90 lines
2.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.U2D;
|
|
|
|
public class LevelGenerator : MonoBehaviour
|
|
{
|
|
public static LevelGenerator instance { get; private set; }
|
|
public SpriteShapeController spriteShapeController;
|
|
public int LevelCount = 1000;
|
|
public float groundLevel = -5.5f;
|
|
public float maxHeight = 5;
|
|
public Vector2 minDiff, maxDiff;
|
|
public Vector3[] points {get; private set;}
|
|
|
|
|
|
public GameObject flagPrefab;
|
|
public GameObject treePrefab;
|
|
|
|
|
|
public int GoalDistance= 10;
|
|
void Start()
|
|
{
|
|
spriteShapeController.spline.Clear();
|
|
|
|
points = new Vector3[LevelCount+1];
|
|
for(int i=0; i < LevelCount; i++){
|
|
if(i ==0){
|
|
points[i] = new Vector3(0,groundLevel);
|
|
spriteShapeController.spline.InsertPointAt(i, points[i]);
|
|
continue;
|
|
}
|
|
|
|
Vector3 addition = new Vector3(Random.Range(minDiff.x, maxDiff.x), Random.Range(minDiff.y, maxDiff.y));
|
|
float newX = points[i-1].x + addition.x;
|
|
float newY = points[i-1].y + addition.y;
|
|
while(newY < groundLevel+2){
|
|
newY += Mathf.Abs(addition.y);
|
|
}
|
|
while(newY > maxHeight){
|
|
newY -= Mathf.Abs(addition.y);
|
|
}
|
|
points[i] = new Vector3(newX, newY);
|
|
// spriteShapeController.spline.InsertPointAt(i, points[i]);
|
|
InsertNewPoint(points[i]);
|
|
|
|
|
|
if(Random.Range(0f,1f) > 0.5f){
|
|
Vector2 newTreePosition = points[i];
|
|
|
|
Vector3 diff = points[i]-points[i-1];
|
|
Vector2 newTreePosition2 = points[i-1] + (diff/2f);
|
|
Debug.Log($"{points[i-1]} + {points[i]} - {points[i-1]} / 0.5f | diff = {diff}");
|
|
GameObject newTree = Instantiate(treePrefab, newTreePosition2, Quaternion.identity);
|
|
newTree.transform.localScale = Vector3.one * Random.Range(0.75f, 1.1f);
|
|
}
|
|
|
|
|
|
if(i % GoalDistance == 0){
|
|
InsertNewPoint(points[i] + new Vector3(0, -1f));
|
|
InsertNewPoint(points[i] + new Vector3(1f, -1f));
|
|
InsertNewPoint(points[i] + new Vector3(1f, 0f));
|
|
|
|
Instantiate(flagPrefab, points[i], Quaternion.identity);
|
|
}
|
|
|
|
}
|
|
|
|
points[LevelCount] = new Vector3(points[LevelCount-1].x, groundLevel);
|
|
InsertNewPoint( points[LevelCount]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
void InsertNewPoint(Vector3 point){
|
|
int index= spriteShapeController.spline.GetPointCount();
|
|
spriteShapeController.spline.InsertPointAt(index, point);
|
|
spriteShapeController.spline.SetLeftTangent(index, Vector3.zero);
|
|
spriteShapeController.spline.SetTangentMode(index, ShapeTangentMode.Broken);
|
|
}
|
|
|
|
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
}
|