almost ready

This commit is contained in:
2023-02-21 22:19:23 +05:30
parent dc15bcfd69
commit 53d53fa5c7
80 changed files with 3537 additions and 1342 deletions

View File

@@ -0,0 +1,158 @@
using System;
using System.Collections;
using System.Collections.Generic;
using GoogleMobileAds.Api;
using UnityEngine;
public class AdsManager : MonoBehaviour
{
// Start is called before the first frame update
public static AdsManager instance;
void Awake(){
if(instance!= null){Destroy(gameObject);}
instance = this;
}
const string intAdId = "ca-app-pub-3940256099942544/1033173712";
const string rewardedAdId = "ca-app-pub-3940256099942544/5224354917";
void Start()
{
DontDestroyOnLoad(gameObject);
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => { Debug.Log("admob Init status : " + initStatus.ToString());});
StartCoroutine(ReloadAds(true));
}
private InterstitialAd interstitialAd;
/// <summary>
/// Loads the interstitial ad.
/// </summary>
public void LoadInterstitialAd()
{
// Clean up the old ad before loading a new one.
if (interstitialAd != null)
{
interstitialAd.Destroy();
interstitialAd = null;
}
Debug.Log("Loading the interstitial ad.");
// create our request used to load the ad.
var adRequest = new AdRequest.Builder()
.AddKeyword("unity-admob-sample")
.Build();
// send the request to load the ad.
InterstitialAd.Load(intAdId, adRequest,
(InterstitialAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Interstitial ad loaded with response : "
+ ad.GetResponseInfo());
interstitialAd = ad;
});
}
public void ShowIntAd()
{
if (interstitialAd != null && interstitialAd.CanShowAd())
{
Debug.Log("Showing interstitial ad.");
interstitialAd.Show();
}
else
{
Debug.LogError("Interstitial ad is not ready yet.");
}
StartCoroutine(ReloadAds(false));
}
private RewardedAd rewardedAd;
/// <summary>
/// Loads the rewarded ad.
/// </summary>
public void LoadRewardedAd()
{
// Clean up the old ad before loading a new one.
if (rewardedAd != null)
{
rewardedAd.Destroy();
rewardedAd = null;
}
Debug.Log("Loading the rewarded ad.");
// create our request used to load the ad.
var adRequest = new AdRequest.Builder().Build();
// send the request to load the ad.
RewardedAd.Load(rewardedAdId, adRequest,
(RewardedAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("Rewarded ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Rewarded ad loaded with response : "
+ ad.GetResponseInfo());
rewardedAd = ad;
rewardedAd.OnAdPaid += OnRewardSuccess;
});
}
private void OnRewardSuccess(AdValue obj)
{
GameManager.AdWatched();
}
public void ShowRewardedAd()
{
const string rewardMsg =
"Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
if (rewardedAd != null && rewardedAd.CanShowAd())
{
rewardedAd.Show((Reward reward) =>
{
// TODO: Reward the user.
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
});
}
StartCoroutine(ReloadAds(true));
}
IEnumerator ReloadAds(bool rewarded){
yield return new WaitForSeconds(2);
LoadInterstitialAd();
if(rewarded){
LoadRewardedAd();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e46b7fd82b2948048adc56ec0c56c4f3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -5,8 +5,15 @@ using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance { get; private set;}
public static bool isMute {get; private set;}
void Awake(){
if(instance != null){Destroy(gameObject);return;}
instance =this;
if(PlayerPrefs.HasKey("mute")){
isMute = PlayerPrefs.GetInt("mute") == 1;
}
Refresh();
}
[SerializeField]private AudioSource sfxSource;
[SerializeField]private AudioSource dropSfxSource;
@@ -30,6 +37,21 @@ public class AudioManager : MonoBehaviour
instance.dropSfx(magnitude);
}
public static bool ToggleMute(){
isMute = !isMute;
Refresh();
PlayerPrefs.SetInt("mute", isMute ? 1 : 0);
PlayerPrefs.Save();
return isMute;
}
public static void Refresh(){
if(instance!=null){
instance.sfxSource.volume = isMute ? 0 : 1;
instance.dropSfxSource.volume = isMute ? 0 : 1;
}
}
void hitSfx(float magnitude){
AudioClip selectedClip;

View File

@@ -2,21 +2,31 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Google;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
public static class DataManager{
public const string API_ENDPOINT = "http://vps.playpoolstudios.com/faucet/golf/api/";
private const string key = "#2CuV1Bit^S!sW1ZcgRv8BhrO";
public static UserData userData{get; private set;}
public static bool isLogged{ get{return userData != null;}}
public static void Signout(){
GoogleSignIn.DefaultInstance.SignOut();
PlayerPrefs.DeleteAll();
PlayerPrefs.Save();
userData = null;
}
public static async void Login(string username,string password){
public static async Task<int> Login(string username,string password){
WWWForm form = new WWWForm();
form.AddField("username", username);
form.AddField("password", password);
form.AddField("key", "#2CuV1Bit^S!sW1ZcgRv8BhrO");
form.AddField("key", key);
@@ -35,28 +45,34 @@ public static class DataManager{
userData = JsonConvert.DeserializeObject<UserData>(request.downloadHandler.text);
Debug.Log("Success parsing userdata");
PlayerPrefs.SetString("username", username);
PlayerPrefs.SetString("password", password);
PlayerPrefs.Save();
}catch(Exception e){
Debug.Log("Error parsing userdata");
}
}else{
if(request.downloadHandler.text == "0"){
userData = new UserData(){username = username};
Debug.Log("Created local account");
}else{
MessageBox.ShowMessage("Error logging in, Server said\n" +request.downloadHandler.text);
return;
return 1;
}
}
}
LoadingScreen.LoadLevel("MainMenu");
return 0;
}
public static async void GoogleLogin(string username){
WWWForm form = new WWWForm();
form.AddField("username", username);
form.AddField("key", "#2CuV1Bit^S!sW1ZcgRv8BhrO");
form.AddField("key", key);
using (UnityWebRequest request = UnityWebRequest.Post(API_ENDPOINT + "google_login.php", form))
{
@@ -66,12 +82,22 @@ public static class DataManager{
await Task.Yield();
}
Debug.Log("glogin response: " +request.downloadHandler.text);
MessageBox.ShowMessage(request.downloadHandler.text);
if(request.downloadHandler.text.Contains("{")){
try{
userData = JsonConvert.DeserializeObject<UserData>(request.downloadHandler.text);
if(userData == null){
throw new NullReferenceException();
}
if(userData.username.Length < 3){
throw new IndexOutOfRangeException();
}
Debug.Log("Success parsing userdata");
PlayerPrefs.SetString("username", username);
PlayerPrefs.SetString("password", username);
PlayerPrefs.Save();
}catch(Exception e){
Debug.Log("Error parsing userdata");
}
@@ -88,10 +114,65 @@ public static class DataManager{
LoadingScreen.LoadLevel("MainMenu");
}
public static async void AddScores(int amount){
WWWForm form = new WWWForm();
Debug.Log(userData.ToString());
form.AddField("username", userData.username);
form.AddField("password", userData.password);
form.AddField("amount", amount);
form.AddField("key", key);
using (UnityWebRequest request = UnityWebRequest.Post(API_ENDPOINT + "add_scores.php", form))
{
var operation = request.SendWebRequest();
while (!operation.isDone)
{
await Task.Yield();
}
Debug.Log("add scores response: " +request.downloadHandler.text);
if(request.downloadHandler.text.Contains("{")){
try{
userData = JsonConvert.DeserializeObject<UserData>(request.downloadHandler.text);
if(userData == null){
throw new NullReferenceException();
}
if(userData.username.Length < 3){
throw new IndexOutOfRangeException();
}
Debug.Log("Success parsing userdata");
}catch(Exception e){
Debug.Log("Error parsing userdata");
}
}else{
MessageBox.ShowMessage("Error Updating scores, Server said\n" +request.downloadHandler.text);
}
}
LoadingScreen.LoadLevel("MainMenu");
}
}
[System.Serializable]
public class UserData{
public int id;
public string username;
public string password;
public int score;
public int TopScore;
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}

View File

@@ -40,11 +40,16 @@ public class GameManager : MonoBehaviour
public int Score{get {return m_curScore;} set{ m_curScore = value; UpdateUI(); }}
public Text StrokesTxt;
public Text ScoreTxt;
public Text gameOverBestScoreTxt, gameOverTotalScoreTxt;
public GameObject GameOverUI;
public GameObject PauseMenuUI;
public Animator HoleInOne;
public int curHoleIndex;
private static int adCounter = 0;
void Start()
{
strokesTxtDefaultSize = StrokesTxt.transform.localScale;
scoreTxtDefaultSize = ScoreTxt.transform.localScale;
camTargetPos = ball.transform.position;
CurStrokes =1;
lastPosition = ball.transform.position;
@@ -118,9 +123,25 @@ public class GameManager : MonoBehaviour
//No hole found
if(CurStrokes <=0){
GameOverUI.SetActive(true);
GameOver();
}
}
public void GameOver(){
if(GameOverUI.active){return;}
gameOverBestScoreTxt.text = DataManager.userData.TopScore.ToString();
gameOverTotalScoreTxt.text = DataManager.userData.score.ToString();
GameOverUI.SetActive(true);
adCounter++;
if(adCounter > 1){
adCounter=0;
AdsManager.instance.ShowIntAd();
}
}
public static async void Hole(Vector2 position){
while(instance.ball.simulated){
await Task.Delay(100);
@@ -196,17 +217,18 @@ public class GameManager : MonoBehaviour
int _tempScore;
int _tempStrokes;
Vector3 scoreTxtDefaultSize, strokesTxtDefaultSize;
public void UpdateUI(){
if(Score != _tempScore){
LeanTween.scale(ScoreTxt.gameObject, ScoreTxt.transform.localScale * 1.5f , 0.5f).setEasePunch();
_tempScore = Score;
ScoreTxt.transform.localScale = scoreTxtDefaultSize;
LeanTween.scale(ScoreTxt.gameObject, scoreTxtDefaultSize * 2f , 0.5f).setEasePunch();
}
if(CurStrokes != _tempStrokes){
_tempStrokes = CurStrokes;
LeanTween.scale(StrokesTxt.gameObject, StrokesTxt.transform.localScale * 1.5f, 0.5f).setEasePunch();
LeanTween.scale(StrokesTxt.gameObject, strokesTxtDefaultSize * 1.5f, 0.5f).setEasePunch();
}
ScoreTxt.text = Score.ToString();
StrokesTxt.text = CurStrokes.ToString();
@@ -217,12 +239,38 @@ public class GameManager : MonoBehaviour
Gizmos.DrawWireSphere(ball.transform.position, holeCheckRadius);
}
public void WatchAd(){
AdsManager.instance.ShowRewardedAd();
}
public static void AdWatched(){
try{
instance.GameOverUI.SetActive(false);
instance.CurStrokes= (int)((float)instance.MaxStrokes/2f);
}catch{
}
}
public void Restrt(){
LoadingScreen.LoadLevel(SceneManager.GetActiveScene().name);
DataManager.AddScores(Score);
}
public void MainMenu(){
LoadingScreen.LoadLevel("MainMenu");
DataManager.AddScores(Score);
}
public void PauseMenu(bool value){
if(value){
LeanTween.scale(PauseMenuUI, Vector3.one, 0.15f).setEaseInCirc();
}else{
LeanTween.scale(PauseMenuUI, Vector3.zero, 0.15f).setEaseOutCirc();
}
}
}

View File

@@ -27,14 +27,13 @@ public class LevelGenerator : MonoBehaviour
holes = new List<GameObject>();
GenerateBlock(lastOffset);
GenerateBlock(lastOffset);
GenerateBlock();
GenerateBlock();
}
float lastOffset=0;
void GenerateBlock(float offset){
void GenerateBlock(){
float offset = lastOffset;
SpriteShapeController spriteShapeController = Instantiate(spriteShapeControllerPrefab, new Vector3(offset,0),Quaternion.identity).GetComponent<SpriteShapeController>();
spriteShapeController.spline.Clear();
points = new Vector3[LevelCount+1];
@@ -100,6 +99,10 @@ public class LevelGenerator : MonoBehaviour
void Update()
{
if(GameManager.instance == null){return;}
if(GameManager.instance.ball.position.x > lastOffset - 100){
GenerateBlock();
}
}
}

View File

@@ -14,6 +14,8 @@ public class LoadingScreen : MonoBehaviour
if(instance != null){Destroy(gameObject);return;}
instance =this;
canvasGroup = GetComponent<CanvasGroup>();
Application.targetFrameRate = 60;
}
@@ -58,7 +60,11 @@ public class LoadingScreen : MonoBehaviour
yield return null;
}
Debug.Log("Loaded scene " + levelName);
yield return new WaitForSecondsRealtime(2f);
// yield return new WaitForSecondsRealtime(2f);
while(loadingProgress.fillAmount < 1){
SetProgress( loadingProgress.fillAmount+0.01f);
yield return new WaitForSeconds(0.1f);
}
canvasGroup.blocksRaycasts = false;

View File

@@ -33,6 +33,24 @@ public class Login : MonoBehaviour
RequestIdToken = true,
RequestEmail=true,
};
}
async void AutoLogin(){
Debug.Log("Start auto-login");
if(PlayerPrefs.HasKey("username")){
Debug.Log("Has saved credentials, Trying to login with them");
int loginResult = await DataManager.Login(PlayerPrefs.GetString("username"), PlayerPrefs.GetString("password"));
if(loginResult == 0){
LoadingScreen.LoadLevel("MainMenu");
}else{
Debug.Log("Failed auto-login");
}
}
}
void Start(){
AutoLogin();
}

View File

@@ -1,7 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using Google;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MainMenu : MonoBehaviour
{
@@ -11,28 +13,70 @@ public class MainMenu : MonoBehaviour
public LeanTweenType transitionEffect;
private Vector2 defaultCenter;
public Button copyBtn, muteBtn;
public Button signoutBtn;
public Sprite muteIcon, unmuteIcon;
public Text txtUserId;
void Awake(){
if(!DataManager.isLogged){
SceneManager.LoadScene(0);
return;
}
defaultCenter = MainMenuPanel.transform.position;
SettingsPanel.transform.position = MainMenuPanel.transform.position - new Vector3(3000,0);
SettingsPanel.SetActive(true);
muteBtn.onClick.AddListener(ToggleMute);
copyBtn.onClick.AddListener(CopyId);
signoutBtn.onClick.AddListener(SignOut);
}
public void Leave(){
Application.Quit();
}
void Start(){
Refresh();
// MessageBox.ShowMessage("Welcome to Infinite Golf 2D.\nThis is a slow paced 2d endless golf game, All the levels are proceduraly generated and you will be rewarded for putting the ball in every and each hole.\n\nGood Luck","Welcome");
}
void Refresh(){
txtUserId.text = DataManager.userData.id.ToString();
if(AudioManager.isMute){
muteBtn.transform.GetChild(0).GetComponent<Image>().sprite = muteIcon;
}else{
muteBtn.transform.GetChild(0).GetComponent<Image>().sprite = unmuteIcon;
}
}
public void SettingsPage(){
LeanTween.moveX(MainMenuPanel, 3000, transitionTime).setEase(transitionEffect);
LeanTween.moveX(MainMenuPanel, 10000, transitionTime).setEase(transitionEffect);
LeanTween.moveX(SettingsPanel, defaultCenter.x, transitionTime).setEase(transitionEffect);
}
public void MainPage(){
LeanTween.moveX(MainMenuPanel, defaultCenter.x, transitionTime).setEase(transitionEffect);
LeanTween.moveX(SettingsPanel, -3000, transitionTime).setEase(transitionEffect);
LeanTween.moveX(SettingsPanel, -10000, transitionTime).setEase(transitionEffect);
}
public void CopyId(){
GUIUtility.systemCopyBuffer = DataManager.userData.id.ToString();
copyBtn.transform.Find("lbl").GetComponent<Text>().text = "Copied";
}
public void ToggleMute(){
AudioManager.ToggleMute();
Refresh();
}
void SignOut(){
DataManager.Signout();
SceneManager.LoadScene(0);
}
}