149 lines
4.0 KiB
C#
149 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using Newtonsoft.Json;
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
|
|
void Awake(){
|
|
instance = this;
|
|
if(!DataManager.isLogged){
|
|
Application.LoadLevel(0);
|
|
}
|
|
}
|
|
|
|
public static PlayerController instance;
|
|
public static Transform t => instance.transform ?? Camera.main.transform;
|
|
public static Vector3 position=> (instance == null) ? Camera.main.transform.position : instance.transform.position;
|
|
public float movingSpeed = 1;
|
|
public float speedIncremental = 0.01f;
|
|
public float verticalSpeed = 0.5f;
|
|
public float rotationRange = 15;
|
|
public bool invert= true;
|
|
|
|
public Text txtScore;
|
|
|
|
public GameObject ExplosionFX;
|
|
public AudioClip ExplosionSFX;
|
|
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
float serverTimer = 0;
|
|
void FixedUpdate()
|
|
{
|
|
transform.Translate(new Vector3(movingSpeed * (PowerupActive? PowerupSpeedMult : 1),0), Space.World);
|
|
|
|
input = Mathf.Clamp(Mathf.Lerp(input, dif / inputRange, inputSmoothness),-1,1);
|
|
|
|
|
|
transform.Translate(new Vector3(0,input * verticalSpeed), Space.World);
|
|
transform.localEulerAngles = new Vector3(0,0,input * rotationRange);
|
|
|
|
txtScore.text = (transform.position.x < 0) ? "" : transform.position.x.ToString("n0");
|
|
|
|
movingSpeed += speedIncremental * Time.deltaTime;
|
|
|
|
|
|
if(serverTimer < 10){
|
|
serverTimer+=Time.deltaTime;
|
|
}else{
|
|
serverTimer=0;
|
|
|
|
TalkWithServer();
|
|
}
|
|
|
|
playtime+=Time.deltaTime;
|
|
|
|
if(powerupTimer > 0){
|
|
powerupTimer-=Time.deltaTime;
|
|
if(powerupTimer <= 0){
|
|
OnPowerdown();
|
|
}
|
|
}
|
|
}
|
|
|
|
float playtime=0;
|
|
int score=0;
|
|
async void TalkWithServer(){
|
|
await DataManager.UpdateGame((int)playtime, (int)(transform.position.x-score), (int)transform.position.x);
|
|
playtime=0;
|
|
score=(int)transform.position.x;
|
|
|
|
}
|
|
|
|
|
|
public float input = 0;
|
|
public float inputSmoothness =0.1f;
|
|
public float inputRange = 200;
|
|
public float dif = 0;
|
|
float startedPos=0;
|
|
|
|
public void OnPointerDown(BaseEventData e){
|
|
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
|
|
|
startedPos = ped.position.y;
|
|
}
|
|
|
|
public void OnPointerDrag(BaseEventData e){
|
|
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
|
|
|
dif = (invert) ? ped.position.y-startedPos: startedPos - ped.position.y;
|
|
}
|
|
|
|
public void OnPointerUp(BaseEventData e){
|
|
dif = 0;
|
|
startedPos = 0;
|
|
}
|
|
public Animator nearMissAnim;
|
|
public static void NearMissFX(){
|
|
instance.nearMissAnim.gameObject.SetActive(true);
|
|
instance.nearMissAnim.CrossFade("txt_intro",0.01f);
|
|
AudioManager.PlayWhoosh();
|
|
}
|
|
public float PowerupLife = 30;
|
|
public float PowerupSpeedMult = 1.3f;
|
|
public bool PowerupActive => powerupTimer > 0;
|
|
public float powerupTimer = 0;
|
|
public void ActivatePowerup(){
|
|
powerupTimer = PowerupLife;
|
|
AudioManager.PlayPowerup();
|
|
}
|
|
public static int AdCounter=0;
|
|
public async void GameOver(){
|
|
// Application.LoadLevel(0);
|
|
TalkWithServer();
|
|
DataManager.AddItem("add_game.php");
|
|
Instantiate(ExplosionFX, transform.position, Quaternion.identity);
|
|
AudioManager.PlayExplosion();
|
|
|
|
Destroy(gameObject);
|
|
|
|
await Task.Delay(500);
|
|
|
|
GameOverUI.Activate();
|
|
|
|
AdCounter++;
|
|
if(AdCounter > 1 || DataManager.userData.faucet_id > 0){
|
|
AdCounter =0;
|
|
|
|
AdsManager.instance.ShowIntAd();
|
|
}
|
|
}
|
|
|
|
void OnPowerdown(){
|
|
AudioManager.PlayPowerdown();
|
|
}
|
|
|
|
public void SpawnExplosionFX(Vector3 pos){
|
|
Instantiate(ExplosionFX,pos, Quaternion.identity);
|
|
AudioManager.PlayExplosion();
|
|
}
|
|
}
|