game mechanics done, new ui
This commit is contained in:
@@ -1,17 +1,29 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
public static GameManager instance{get; private set;}
|
||||
void Awake(){
|
||||
instance=this;
|
||||
}
|
||||
public Rigidbody2D ball;
|
||||
public float holeCheckRadius =1;
|
||||
public Transform cam;
|
||||
public Vector3 camTargetPos;
|
||||
public float cameraSmoothness = 0.1f;
|
||||
public Vector3 cameraOffset ;
|
||||
public float inputSensitivity = 100f;
|
||||
public float BallFriction = 0.1f;
|
||||
public float BallFrictionStop = 0.1f;
|
||||
public float StopVelocity = 0.01f;
|
||||
public float SlowVelocity = 1f;
|
||||
|
||||
public float StopTime = 0.1f;
|
||||
|
||||
public float curVelocity;
|
||||
@@ -19,18 +31,60 @@ public class GameManager : MonoBehaviour
|
||||
public Transform ballProjection;
|
||||
public float ballProjectionScaleMin, ballProjectionScaleMax;
|
||||
|
||||
|
||||
[Header("Game")]
|
||||
public int MaxStrokes;
|
||||
[SerializeField]private int m_curStrokes;
|
||||
public int CurStrokes{get{return m_curStrokes;} set{ m_curStrokes=value; UpdateUI();}}
|
||||
private int m_curScore;
|
||||
public int Score{get {return m_curScore;} set{ m_curScore = value; UpdateUI(); }}
|
||||
public Text StrokesTxt;
|
||||
public Text ScoreTxt;
|
||||
public GameObject GameOverUI;
|
||||
public Animator HoleInOne;
|
||||
public int curHoleIndex;
|
||||
void Start()
|
||||
{
|
||||
camTargetPos = ball.transform.position;
|
||||
CurStrokes =1;
|
||||
lastPosition = ball.transform.position;
|
||||
ball.transform.position = LevelGenerator.holes[3].transform.position;
|
||||
Debug.Log("Moving ball to " + LevelGenerator.holes[3].transform.position);
|
||||
curHoleIndex = 2;
|
||||
}
|
||||
float stopCooldown = 0;
|
||||
|
||||
public bool isGrounded;
|
||||
|
||||
public static Vector3 lastPosition;
|
||||
|
||||
void Update(){
|
||||
float distToFirst = ball.transform.position.x - LevelGenerator.holes[curHoleIndex].transform.position.x;
|
||||
float distToLast = LevelGenerator.holes[curHoleIndex+1].transform.position.x - ball.transform.position.x;
|
||||
|
||||
if(distToFirst < -11 || distToLast < -11){
|
||||
//Out of bounds, return to safe
|
||||
ball.transform.position = lastPosition;
|
||||
ball.velocity=Vector2.zero;
|
||||
ball.simulated=false;
|
||||
}
|
||||
}
|
||||
void FixedUpdate(){
|
||||
Collider2D[] cols = Physics2D.OverlapCircleAll(ball.transform.position, holeCheckRadius);
|
||||
isGrounded=false;
|
||||
foreach(Collider2D col in cols){
|
||||
if(col.tag == "Ground"){
|
||||
isGrounded=true;
|
||||
}
|
||||
}
|
||||
|
||||
curVelocity = ball.velocity.magnitude;
|
||||
ball.velocity = Vector2.Lerp(ball.velocity, new Vector2(0, ball.velocity.y), BallFriction);
|
||||
if(isGrounded){ ball.velocity = Vector2.Lerp(ball.velocity, new Vector2(0, ball.velocity.y), ball.velocity.y > 0 ? BallFrictionStop : BallFrictionStop/10f);}
|
||||
if(Mathf.Abs(ball.velocity.magnitude) < StopVelocity){
|
||||
if(stopCooldown > StopTime){
|
||||
ball.simulated=false;
|
||||
CheckHole();
|
||||
lastPosition=ball.transform.position;
|
||||
}else{
|
||||
stopCooldown+=Time.deltaTime;
|
||||
}
|
||||
@@ -38,20 +92,57 @@ public class GameManager : MonoBehaviour
|
||||
stopCooldown=0;
|
||||
}
|
||||
}
|
||||
|
||||
void Update(){
|
||||
camTargetPos = ball.transform.position;
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
cam.position = Vector3.Lerp(cam.position, new Vector3(camTargetPos.x, cam.position.y,cam.position.z), cameraSmoothness);
|
||||
cam.position = Vector3.Lerp(cam.position, new Vector3(camTargetPos.x, cam.position.y,cam.position.z) + cameraOffset, cameraSmoothness);
|
||||
}
|
||||
|
||||
private static bool cancelledHole = false;
|
||||
public static void CancelHole(){
|
||||
cancelledHole = true;
|
||||
}
|
||||
|
||||
void CheckHole(){
|
||||
|
||||
Collider2D[] cols = Physics2D.OverlapCircleAll(ball.transform.position, holeCheckRadius);
|
||||
foreach(Collider2D col in cols){
|
||||
if(col.tag == "Hole"){
|
||||
Hole(col.transform.position);
|
||||
curHoleIndex++;
|
||||
camTargetPos = (Vector2)col.transform.position;
|
||||
Destroy(col.gameObject);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//No hole found
|
||||
if(CurStrokes <=0){
|
||||
GameOverUI.SetActive(true);
|
||||
}
|
||||
}
|
||||
public static async void Hole(Vector2 position){
|
||||
while(instance.ball.simulated){
|
||||
await Task.Delay(100);
|
||||
if(cancelledHole){return;}
|
||||
}
|
||||
|
||||
instance.ball.transform.position = position + new Vector2(0.6f, 0.75f);
|
||||
instance.Score++;
|
||||
|
||||
if(instance.CurStrokes >= instance.MaxStrokes-1){
|
||||
instance.HoleInOne.CrossFadeInFixedTime("Play",0.01f);
|
||||
instance.Score++;
|
||||
}
|
||||
instance.CurStrokes = instance.MaxStrokes;
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool dragging = false;
|
||||
Vector2 startPos;
|
||||
public void OnMouseDown(BaseEventData e){
|
||||
if(CurStrokes <= 0){return;}
|
||||
if(ball.simulated){return;}
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
|
||||
@@ -60,6 +151,7 @@ public class GameManager : MonoBehaviour
|
||||
}
|
||||
|
||||
public void OnMouseUp(BaseEventData e){
|
||||
if(CurStrokes <= 0){return;}
|
||||
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
if(dragging){
|
||||
@@ -69,18 +161,21 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
ball.simulated=true;
|
||||
ball.AddForce(-v * forceMultiplier);
|
||||
CurStrokes--;
|
||||
}
|
||||
dragging = false;
|
||||
ballProjection.position = Vector3.zero;
|
||||
}
|
||||
|
||||
public void OnMouseDrag(BaseEventData e){
|
||||
if(CurStrokes <= 0){return;}
|
||||
|
||||
if(ball.simulated){return;}
|
||||
|
||||
ballProjection.position = ball.position;
|
||||
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
||||
Vector2 v = ((ped.position-startPos)/inputSensitivity);
|
||||
Debug.Log(v.magnitude);
|
||||
// Debug.Log(v.magnitude);
|
||||
|
||||
if(v.magnitude > 1){v = v.normalized;}
|
||||
|
||||
@@ -91,4 +186,20 @@ public class GameManager : MonoBehaviour
|
||||
float scaleDiff = ballProjectionScaleMax - ballProjectionScaleMin;
|
||||
ballProjection.GetChild(0).localScale = new Vector3(ballProjection.GetChild(0).localScale.x,ballProjectionScaleMin + (scaleDiff*v.magnitude));
|
||||
}
|
||||
|
||||
|
||||
public void UpdateUI(){
|
||||
ScoreTxt.text = Score.ToString();
|
||||
StrokesTxt.text = CurStrokes.ToString();
|
||||
}
|
||||
|
||||
|
||||
void OnDrawGizmos(){
|
||||
Gizmos.DrawWireSphere(ball.transform.position, holeCheckRadius);
|
||||
}
|
||||
|
||||
public void Restrt(){
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
22
Assets/Scripts/Hole.cs
Normal file
22
Assets/Scripts/Hole.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Hole : MonoBehaviour
|
||||
{
|
||||
public void OnTriggerEnter2D(Collider2D other){
|
||||
if(other.tag == "Player"){
|
||||
Debug.Log(other.name + " Entered hole");
|
||||
|
||||
GameManager.Hole(transform.position);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnTriggerExit2D(Collider2D other){
|
||||
if(other.tag == "Player"){
|
||||
Debug.Log(other.name + " Exited hole");
|
||||
|
||||
GameManager.CancelHole();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Hole.cs.meta
Normal file
11
Assets/Scripts/Hole.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1fb697cb50af21e4888b6327fdbee705
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -6,23 +6,37 @@ using UnityEngine.U2D;
|
||||
public class LevelGenerator : MonoBehaviour
|
||||
{
|
||||
public static LevelGenerator instance { get; private set; }
|
||||
public SpriteShapeController spriteShapeController;
|
||||
public GameObject spriteShapeControllerPrefab;
|
||||
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 holePrefab;
|
||||
public GameObject flagPrefab;
|
||||
public GameObject treePrefab;
|
||||
|
||||
public static List<GameObject> holes;
|
||||
|
||||
|
||||
public int GoalDistance= 10;
|
||||
void Start()
|
||||
void Awake()
|
||||
{
|
||||
spriteShapeController.spline.Clear();
|
||||
instance=this;
|
||||
|
||||
holes = new List<GameObject>();
|
||||
|
||||
GenerateBlock(lastOffset);
|
||||
GenerateBlock(lastOffset);
|
||||
|
||||
|
||||
}
|
||||
|
||||
float lastOffset=0;
|
||||
void GenerateBlock(float offset){
|
||||
SpriteShapeController spriteShapeController = Instantiate(spriteShapeControllerPrefab, new Vector3(offset,0),Quaternion.identity).GetComponent<SpriteShapeController>();
|
||||
spriteShapeController.spline.Clear();
|
||||
points = new Vector3[LevelCount+1];
|
||||
for(int i=0; i < LevelCount; i++){
|
||||
if(i ==0){
|
||||
@@ -42,39 +56,41 @@ public class LevelGenerator : MonoBehaviour
|
||||
}
|
||||
points[i] = new Vector3(newX, newY);
|
||||
// spriteShapeController.spline.InsertPointAt(i, points[i]);
|
||||
InsertNewPoint(points[i]);
|
||||
InsertNewPoint(spriteShapeController, 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);
|
||||
Vector2 newTreePosition2 = points[i-1] + (diff/2f) + new Vector3(lastOffset,0);
|
||||
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);
|
||||
if(i % GoalDistance == 0){
|
||||
InsertNewPoint(spriteShapeController, points[i] + new Vector3(0, -1f));
|
||||
InsertNewPoint(spriteShapeController, points[i] + new Vector3(1f, -1f));
|
||||
InsertNewPoint(spriteShapeController, points[i] + new Vector3(1f, 0f));
|
||||
|
||||
Vector3 newHolePos = points[i] + new Vector3(lastOffset,0);
|
||||
|
||||
Instantiate(flagPrefab,newHolePos, Quaternion.identity);
|
||||
holes.Add(Instantiate(holePrefab,newHolePos, Quaternion.identity));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
points[LevelCount] = new Vector3(points[LevelCount-1].x, groundLevel);
|
||||
InsertNewPoint( points[LevelCount]);
|
||||
|
||||
|
||||
|
||||
points[LevelCount] = new Vector3(points[LevelCount-1].x+maxDiff.x, groundLevel);
|
||||
lastOffset = points[LevelCount].x - maxDiff.x;
|
||||
InsertNewPoint(spriteShapeController, points[LevelCount]);
|
||||
}
|
||||
|
||||
|
||||
void InsertNewPoint(Vector3 point){
|
||||
void InsertNewPoint(SpriteShapeController spriteShapeController,Vector3 point){
|
||||
int index= spriteShapeController.spline.GetPointCount();
|
||||
spriteShapeController.spline.InsertPointAt(index, point);
|
||||
spriteShapeController.spline.SetLeftTangent(index, Vector3.zero);
|
||||
|
||||
Reference in New Issue
Block a user